Skip to content

Commit 578bf9c

Browse files
authored
fix(jsii): Defaulted parameters were not rendered as optional (#234)
Parameters with a default value were not represented as optional in the assembly documents due to an oversight when re-writing `jsii`. There was also no coverage in the test corpus for this use-case, so I've added some. Fixes #233
1 parent b664805 commit 578bf9c

File tree

12 files changed

+551
-4
lines changed

12 files changed

+551
-4
lines changed

Diff for: packages/jsii-calc/lib/compliance.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,22 @@ export class RuntimeTypeChecking {
240240
public methodWithOptionalArguments(arg1: number, arg2: string, arg3?: Date) {
241241
arg1; arg2; arg3;
242242
}
243+
244+
public methodWithDefaultedArguments(arg1: number = 2, arg2: string, arg3: Date = new Date()) {
245+
arg1; arg2; arg3;
246+
}
247+
}
248+
249+
export class OptionalConstructorArgument {
250+
public constructor(public readonly arg1: number,
251+
public readonly arg2: string,
252+
public readonly arg3?: Date) {}
253+
}
254+
255+
export class DefaultedConstructorArgument {
256+
public constructor(public readonly arg1: number = 2,
257+
public readonly arg2: string,
258+
public readonly arg3: Date = new Date()) {}
243259
}
244260

245261
export namespace DerivedClassHasNoProperties {
@@ -865,4 +881,4 @@ export class AbstractClassReturner {
865881
abstractProperty: 'hello-abstract-property'
866882
}
867883
}
868-
}
884+
}

Diff for: packages/jsii-calc/test/assembly.jsii

+134-1
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,60 @@
10121012
}
10131013
]
10141014
},
1015+
"jsii-calc.DefaultedConstructorArgument": {
1016+
"assembly": "jsii-calc",
1017+
"fqn": "jsii-calc.DefaultedConstructorArgument",
1018+
"initializer": {
1019+
"initializer": true,
1020+
"parameters": [
1021+
{
1022+
"name": "arg1",
1023+
"type": {
1024+
"optional": true,
1025+
"primitive": "number"
1026+
}
1027+
},
1028+
{
1029+
"name": "arg2",
1030+
"type": {
1031+
"primitive": "string"
1032+
}
1033+
},
1034+
{
1035+
"name": "arg3",
1036+
"type": {
1037+
"optional": true,
1038+
"primitive": "date"
1039+
}
1040+
}
1041+
]
1042+
},
1043+
"kind": "class",
1044+
"name": "DefaultedConstructorArgument",
1045+
"properties": [
1046+
{
1047+
"immutable": true,
1048+
"name": "arg1",
1049+
"type": {
1050+
"primitive": "number"
1051+
}
1052+
},
1053+
{
1054+
"immutable": true,
1055+
"name": "arg2",
1056+
"type": {
1057+
"primitive": "string"
1058+
}
1059+
},
1060+
{
1061+
"immutable": true,
1062+
"name": "arg3",
1063+
"type": {
1064+
"primitive": "date"
1065+
}
1066+
}
1067+
]
1068+
},
10151069
"jsii-calc.DerivedClassHasNoProperties.Base": {
10161070
"assembly": "jsii-calc",
10171071
"fqn": "jsii-calc.DerivedClassHasNoProperties.Base",
@@ -2073,6 +2127,60 @@
20732127
],
20742128
"name": "ObjectRefsInCollections"
20752129
},
2130+
"jsii-calc.OptionalConstructorArgument": {
2131+
"assembly": "jsii-calc",
2132+
"fqn": "jsii-calc.OptionalConstructorArgument",
2133+
"initializer": {
2134+
"initializer": true,
2135+
"parameters": [
2136+
{
2137+
"name": "arg1",
2138+
"type": {
2139+
"primitive": "number"
2140+
}
2141+
},
2142+
{
2143+
"name": "arg2",
2144+
"type": {
2145+
"primitive": "string"
2146+
}
2147+
},
2148+
{
2149+
"name": "arg3",
2150+
"type": {
2151+
"optional": true,
2152+
"primitive": "date"
2153+
}
2154+
}
2155+
]
2156+
},
2157+
"kind": "class",
2158+
"name": "OptionalConstructorArgument",
2159+
"properties": [
2160+
{
2161+
"immutable": true,
2162+
"name": "arg1",
2163+
"type": {
2164+
"primitive": "number"
2165+
}
2166+
},
2167+
{
2168+
"immutable": true,
2169+
"name": "arg2",
2170+
"type": {
2171+
"primitive": "string"
2172+
}
2173+
},
2174+
{
2175+
"immutable": true,
2176+
"name": "arg3",
2177+
"type": {
2178+
"optional": true,
2179+
"primitive": "date"
2180+
}
2181+
}
2182+
]
2183+
},
20762184
"jsii-calc.OverrideReturnsObject": {
20772185
"assembly": "jsii-calc",
20782186
"fqn": "jsii-calc.OverrideReturnsObject",
@@ -2270,6 +2378,31 @@
22702378
},
22712379
"kind": "class",
22722380
"methods": [
2381+
{
2382+
"name": "methodWithDefaultedArguments",
2383+
"parameters": [
2384+
{
2385+
"name": "arg1",
2386+
"type": {
2387+
"optional": true,
2388+
"primitive": "number"
2389+
}
2390+
},
2391+
{
2392+
"name": "arg2",
2393+
"type": {
2394+
"primitive": "string"
2395+
}
2396+
},
2397+
{
2398+
"name": "arg3",
2399+
"type": {
2400+
"optional": true,
2401+
"primitive": "date"
2402+
}
2403+
}
2404+
]
2405+
},
22732406
{
22742407
"docs": {
22752408
"comment": "Used to verify verification of number of method arguments."
@@ -3070,5 +3203,5 @@
30703203
}
30713204
},
30723205
"version": "0.7.5",
3073-
"fingerprint": "XrmsNUcNdYiHEC6BRVT5XoeVmYQZzjYgiu6MyibgOwk="
3206+
"fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8="
30743207
}

Diff for: packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii

+134-1
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,60 @@
10121012
}
10131013
]
10141014
},
1015+
"jsii-calc.DefaultedConstructorArgument": {
1016+
"assembly": "jsii-calc",
1017+
"fqn": "jsii-calc.DefaultedConstructorArgument",
1018+
"initializer": {
1019+
"initializer": true,
1020+
"parameters": [
1021+
{
1022+
"name": "arg1",
1023+
"type": {
1024+
"optional": true,
1025+
"primitive": "number"
1026+
}
1027+
},
1028+
{
1029+
"name": "arg2",
1030+
"type": {
1031+
"primitive": "string"
1032+
}
1033+
},
1034+
{
1035+
"name": "arg3",
1036+
"type": {
1037+
"optional": true,
1038+
"primitive": "date"
1039+
}
1040+
}
1041+
]
1042+
},
1043+
"kind": "class",
1044+
"name": "DefaultedConstructorArgument",
1045+
"properties": [
1046+
{
1047+
"immutable": true,
1048+
"name": "arg1",
1049+
"type": {
1050+
"primitive": "number"
1051+
}
1052+
},
1053+
{
1054+
"immutable": true,
1055+
"name": "arg2",
1056+
"type": {
1057+
"primitive": "string"
1058+
}
1059+
},
1060+
{
1061+
"immutable": true,
1062+
"name": "arg3",
1063+
"type": {
1064+
"primitive": "date"
1065+
}
1066+
}
1067+
]
1068+
},
10151069
"jsii-calc.DerivedClassHasNoProperties.Base": {
10161070
"assembly": "jsii-calc",
10171071
"fqn": "jsii-calc.DerivedClassHasNoProperties.Base",
@@ -2073,6 +2127,60 @@
20732127
],
20742128
"name": "ObjectRefsInCollections"
20752129
},
2130+
"jsii-calc.OptionalConstructorArgument": {
2131+
"assembly": "jsii-calc",
2132+
"fqn": "jsii-calc.OptionalConstructorArgument",
2133+
"initializer": {
2134+
"initializer": true,
2135+
"parameters": [
2136+
{
2137+
"name": "arg1",
2138+
"type": {
2139+
"primitive": "number"
2140+
}
2141+
},
2142+
{
2143+
"name": "arg2",
2144+
"type": {
2145+
"primitive": "string"
2146+
}
2147+
},
2148+
{
2149+
"name": "arg3",
2150+
"type": {
2151+
"optional": true,
2152+
"primitive": "date"
2153+
}
2154+
}
2155+
]
2156+
},
2157+
"kind": "class",
2158+
"name": "OptionalConstructorArgument",
2159+
"properties": [
2160+
{
2161+
"immutable": true,
2162+
"name": "arg1",
2163+
"type": {
2164+
"primitive": "number"
2165+
}
2166+
},
2167+
{
2168+
"immutable": true,
2169+
"name": "arg2",
2170+
"type": {
2171+
"primitive": "string"
2172+
}
2173+
},
2174+
{
2175+
"immutable": true,
2176+
"name": "arg3",
2177+
"type": {
2178+
"optional": true,
2179+
"primitive": "date"
2180+
}
2181+
}
2182+
]
2183+
},
20762184
"jsii-calc.OverrideReturnsObject": {
20772185
"assembly": "jsii-calc",
20782186
"fqn": "jsii-calc.OverrideReturnsObject",
@@ -2270,6 +2378,31 @@
22702378
},
22712379
"kind": "class",
22722380
"methods": [
2381+
{
2382+
"name": "methodWithDefaultedArguments",
2383+
"parameters": [
2384+
{
2385+
"name": "arg1",
2386+
"type": {
2387+
"optional": true,
2388+
"primitive": "number"
2389+
}
2390+
},
2391+
{
2392+
"name": "arg2",
2393+
"type": {
2394+
"primitive": "string"
2395+
}
2396+
},
2397+
{
2398+
"name": "arg3",
2399+
"type": {
2400+
"optional": true,
2401+
"primitive": "date"
2402+
}
2403+
}
2404+
]
2405+
},
22732406
{
22742407
"docs": {
22752408
"comment": "Used to verify verification of number of method arguments."
@@ -3070,5 +3203,5 @@
30703203
}
30713204
},
30723205
"version": "0.7.5",
3073-
"fingerprint": "XrmsNUcNdYiHEC6BRVT5XoeVmYQZzjYgiu6MyibgOwk="
3206+
"fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8="
30743207
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Amazon.JSII.Runtime.Deputy;
2+
using System;
3+
4+
namespace Amazon.JSII.Tests.CalculatorNamespace
5+
{
6+
[JsiiClass(typeof(DefaultedConstructorArgument), "jsii-calc.DefaultedConstructorArgument", "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\",\"optional\":true}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\",\"optional\":true}}]")]
7+
public class DefaultedConstructorArgument : DeputyBase
8+
{
9+
public DefaultedConstructorArgument(double? arg1, string arg2, DateTime? arg3): base(new DeputyProps(new object[]{arg1, arg2, arg3}))
10+
{
11+
}
12+
13+
protected DefaultedConstructorArgument(ByRefValue reference): base(reference)
14+
{
15+
}
16+
17+
protected DefaultedConstructorArgument(DeputyProps props): base(props)
18+
{
19+
}
20+
21+
[JsiiProperty("arg1", "{\"primitive\":\"number\"}")]
22+
public virtual double Arg1
23+
{
24+
get => GetInstanceProperty<double>();
25+
}
26+
27+
[JsiiProperty("arg2", "{\"primitive\":\"string\"}")]
28+
public virtual string Arg2
29+
{
30+
get => GetInstanceProperty<string>();
31+
}
32+
33+
[JsiiProperty("arg3", "{\"primitive\":\"date\"}")]
34+
public virtual DateTime Arg3
35+
{
36+
get => GetInstanceProperty<DateTime>();
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)