Skip to content

Commit 91074f3

Browse files
authored
fix(jsii): Optional any represented as required (#237)
* fix(jsii): Optional `any` represented as required Optional parameters and properties typed `any` would be represented as required, despite the code unambiguously suggests the opposite. This is due to the fact that `any` implicitly covers `null` and `undefined`. This change fixes this by adding a specific provision for the question mark token in the declarations of those, and adds compliance test coverage for the same. Fixes #230 * Mark all `any` types as `optional`.
1 parent 578bf9c commit 91074f3

File tree

20 files changed

+215
-64
lines changed

20 files changed

+215
-64
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
},
7373
"name": "typeName",
7474
"returns": {
75+
"optional": true,
7576
"primitive": "any"
7677
}
7778
}
@@ -101,5 +102,5 @@
101102
}
102103
},
103104
"version": "0.7.5",
104-
"fingerprint": "Kzm7bNzxYO6frB7BwjqVBKhzTiCRwVG5aqgQrz7MOnE="
105+
"fingerprint": "kazFxdH9DydCwjHvToTbXzqqXE0CytJ5qNmGO1NlGAM="
105106
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ export class RuntimeTypeChecking {
244244
public methodWithDefaultedArguments(arg1: number = 2, arg2: string, arg3: Date = new Date()) {
245245
arg1; arg2; arg3;
246246
}
247+
248+
public methodWithOptionalAnyArgument(arg?: any) {
249+
arg;
250+
}
247251
}
248252

249253
export class OptionalConstructorArgument {
@@ -479,6 +483,7 @@ export interface DerivedStruct extends MyFirstStruct {
479483
bool: boolean
480484
anotherRequired: Date
481485
optionalArray?: string[]
486+
optionalAny?: any
482487
/**
483488
* This is optional.
484489
*/

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

+35-8
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
"type": {
378378
"collection": {
379379
"elementtype": {
380+
"optional": true,
380381
"primitive": "any"
381382
},
382383
"kind": "array"
@@ -388,18 +389,13 @@
388389
"type": {
389390
"collection": {
390391
"elementtype": {
392+
"optional": true,
391393
"primitive": "any"
392394
},
393395
"kind": "map"
394396
}
395397
}
396398
},
397-
{
398-
"name": "anyProperty",
399-
"type": {
400-
"primitive": "any"
401-
}
402-
},
403399
{
404400
"name": "arrayProperty",
405401
"type": {
@@ -521,6 +517,7 @@
521517
"type": {
522518
"collection": {
523519
"elementtype": {
520+
"optional": true,
524521
"primitive": "any"
525522
},
526523
"kind": "array"
@@ -532,15 +529,17 @@
532529
"type": {
533530
"collection": {
534531
"elementtype": {
532+
"optional": true,
535533
"primitive": "any"
536534
},
537535
"kind": "map"
538536
}
539537
}
540538
},
541539
{
542-
"name": "unknownProperty",
540+
"name": "anyProperty",
543541
"type": {
542+
"optional": true,
544543
"primitive": "any"
545544
}
546545
},
@@ -550,6 +549,13 @@
550549
"fqn": "jsii-calc.StringEnum",
551550
"optional": true
552551
}
552+
},
553+
{
554+
"name": "unknownProperty",
555+
"type": {
556+
"optional": true,
557+
"primitive": "any"
558+
}
553559
}
554560
]
555561
},
@@ -1152,6 +1158,14 @@
11521158
"optional": true
11531159
}
11541160
},
1161+
{
1162+
"abstract": true,
1163+
"name": "optionalAny",
1164+
"type": {
1165+
"optional": true,
1166+
"primitive": "any"
1167+
}
1168+
},
11551169
{
11561170
"abstract": true,
11571171
"name": "optionalArray",
@@ -2403,6 +2417,18 @@
24032417
}
24042418
]
24052419
},
2420+
{
2421+
"name": "methodWithOptionalAnyArgument",
2422+
"parameters": [
2423+
{
2424+
"name": "arg",
2425+
"type": {
2426+
"optional": true,
2427+
"primitive": "any"
2428+
}
2429+
}
2430+
]
2431+
},
24062432
{
24072433
"docs": {
24082434
"comment": "Used to verify verification of number of method arguments."
@@ -2854,6 +2880,7 @@
28542880
{
28552881
"name": "value",
28562882
"returns": {
2883+
"optional": true,
28572884
"primitive": "any"
28582885
}
28592886
}
@@ -3203,5 +3230,5 @@
32033230
}
32043231
},
32053232
"version": "0.7.5",
3206-
"fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8="
3233+
"fingerprint": "Zt3ElcP9k7ABYhwmP1xNZBni1sFj9iw0FZwWOe8n+L8="
32073234
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
},
7373
"name": "typeName",
7474
"returns": {
75+
"optional": true,
7576
"primitive": "any"
7677
}
7778
}
@@ -101,5 +102,5 @@
101102
}
102103
},
103104
"version": "0.7.5",
104-
"fingerprint": "Kzm7bNzxYO6frB7BwjqVBKhzTiCRwVG5aqgQrz7MOnE="
105+
"fingerprint": "kazFxdH9DydCwjHvToTbXzqqXE0CytJ5qNmGO1NlGAM="
105106
}

Diff for: packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon/JSII/Tests/CalculatorNamespace/BaseNamespace/Base.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected Base(DeputyProps props): base(props)
1919
}
2020

2121
/// <returns>the name of the class (to verify native type names are created for derived classes).</returns>
22-
[JsiiMethod("typeName", "{\"primitive\":\"any\"}", "[]")]
22+
[JsiiMethod("typeName", "{\"primitive\":\"any\",\"optional\":true}", "[]")]
2323
public virtual object TypeName()
2424
{
2525
return InvokeInstanceMethod<object>(new object[]{});

Diff for: packages/jsii-pacmak/test/expected.jsii-calc-base/java/src/main/java/software/amazon/jsii/tests/calculator/base/Base.java

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ protected Base(final software.amazon.jsii.JsiiObject.InitializationMode mode) {
1313
/**
1414
* @return the name of the class (to verify native type names are created for derived classes).
1515
*/
16+
@javax.annotation.Nullable
1617
public java.lang.Object typeName() {
1718
return this.jsiiCall("typeName", java.lang.Object.class);
1819
}

Diff for: packages/jsii-pacmak/test/expected.jsii-calc-base/sphinx/_scope_jsii-calc-base.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Base
158158
.. py:method:: typeName() -> any
159159
160160
:return: the name of the class (to verify native type names are created for derived classes).
161-
:rtype: any
161+
:rtype: any or undefined
162162

163163

164164
BaseProps (interface)

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

+35-8
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
"type": {
378378
"collection": {
379379
"elementtype": {
380+
"optional": true,
380381
"primitive": "any"
381382
},
382383
"kind": "array"
@@ -388,18 +389,13 @@
388389
"type": {
389390
"collection": {
390391
"elementtype": {
392+
"optional": true,
391393
"primitive": "any"
392394
},
393395
"kind": "map"
394396
}
395397
}
396398
},
397-
{
398-
"name": "anyProperty",
399-
"type": {
400-
"primitive": "any"
401-
}
402-
},
403399
{
404400
"name": "arrayProperty",
405401
"type": {
@@ -521,6 +517,7 @@
521517
"type": {
522518
"collection": {
523519
"elementtype": {
520+
"optional": true,
524521
"primitive": "any"
525522
},
526523
"kind": "array"
@@ -532,15 +529,17 @@
532529
"type": {
533530
"collection": {
534531
"elementtype": {
532+
"optional": true,
535533
"primitive": "any"
536534
},
537535
"kind": "map"
538536
}
539537
}
540538
},
541539
{
542-
"name": "unknownProperty",
540+
"name": "anyProperty",
543541
"type": {
542+
"optional": true,
544543
"primitive": "any"
545544
}
546545
},
@@ -550,6 +549,13 @@
550549
"fqn": "jsii-calc.StringEnum",
551550
"optional": true
552551
}
552+
},
553+
{
554+
"name": "unknownProperty",
555+
"type": {
556+
"optional": true,
557+
"primitive": "any"
558+
}
553559
}
554560
]
555561
},
@@ -1152,6 +1158,14 @@
11521158
"optional": true
11531159
}
11541160
},
1161+
{
1162+
"abstract": true,
1163+
"name": "optionalAny",
1164+
"type": {
1165+
"optional": true,
1166+
"primitive": "any"
1167+
}
1168+
},
11551169
{
11561170
"abstract": true,
11571171
"name": "optionalArray",
@@ -2403,6 +2417,18 @@
24032417
}
24042418
]
24052419
},
2420+
{
2421+
"name": "methodWithOptionalAnyArgument",
2422+
"parameters": [
2423+
{
2424+
"name": "arg",
2425+
"type": {
2426+
"optional": true,
2427+
"primitive": "any"
2428+
}
2429+
}
2430+
]
2431+
},
24062432
{
24072433
"docs": {
24082434
"comment": "Used to verify verification of number of method arguments."
@@ -2854,6 +2880,7 @@
28542880
{
28552881
"name": "value",
28562882
"returns": {
2883+
"optional": true,
28572884
"primitive": "any"
28582885
}
28592886
}
@@ -3203,5 +3230,5 @@
32033230
}
32043231
},
32053232
"version": "0.7.5",
3206-
"fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8="
3233+
"fingerprint": "Zt3ElcP9k7ABYhwmP1xNZBni1sFj9iw0FZwWOe8n+L8="
32073234
}

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,20 @@ public virtual double EnumPropertyValue
3131
get => GetInstanceProperty<double>();
3232
}
3333

34-
[JsiiProperty("anyArrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"any\"}}}")]
34+
[JsiiProperty("anyArrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"any\",\"optional\":true}}}")]
3535
public virtual object[] AnyArrayProperty
3636
{
3737
get => GetInstanceProperty<object[]>();
3838
set => SetInstanceProperty(value);
3939
}
4040

41-
[JsiiProperty("anyMapProperty", "{\"collection\":{\"kind\":\"map\",\"elementtype\":{\"primitive\":\"any\"}}}")]
41+
[JsiiProperty("anyMapProperty", "{\"collection\":{\"kind\":\"map\",\"elementtype\":{\"primitive\":\"any\",\"optional\":true}}}")]
4242
public virtual IDictionary<string, object> AnyMapProperty
4343
{
4444
get => GetInstanceProperty<IDictionary<string, object>>();
4545
set => SetInstanceProperty(value);
4646
}
4747

48-
[JsiiProperty("anyProperty", "{\"primitive\":\"any\"}")]
49-
public virtual object AnyProperty
50-
{
51-
get => GetInstanceProperty<object>();
52-
set => SetInstanceProperty(value);
53-
}
54-
5548
[JsiiProperty("arrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"string\"}}}")]
5649
public virtual string[] ArrayProperty
5750
{
@@ -129,22 +122,22 @@ public virtual object UnionProperty
129122
set => SetInstanceProperty(value);
130123
}
131124

132-
[JsiiProperty("unknownArrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"any\"}}}")]
125+
[JsiiProperty("unknownArrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"any\",\"optional\":true}}}")]
133126
public virtual object[] UnknownArrayProperty
134127
{
135128
get => GetInstanceProperty<object[]>();
136129
set => SetInstanceProperty(value);
137130
}
138131

139-
[JsiiProperty("unknownMapProperty", "{\"collection\":{\"kind\":\"map\",\"elementtype\":{\"primitive\":\"any\"}}}")]
132+
[JsiiProperty("unknownMapProperty", "{\"collection\":{\"kind\":\"map\",\"elementtype\":{\"primitive\":\"any\",\"optional\":true}}}")]
140133
public virtual IDictionary<string, object> UnknownMapProperty
141134
{
142135
get => GetInstanceProperty<IDictionary<string, object>>();
143136
set => SetInstanceProperty(value);
144137
}
145138

146-
[JsiiProperty("unknownProperty", "{\"primitive\":\"any\"}")]
147-
public virtual object UnknownProperty
139+
[JsiiProperty("anyProperty", "{\"primitive\":\"any\",\"optional\":true}")]
140+
public virtual object AnyProperty
148141
{
149142
get => GetInstanceProperty<object>();
150143
set => SetInstanceProperty(value);
@@ -157,6 +150,13 @@ public virtual StringEnum OptionalEnumValue
157150
set => SetInstanceProperty(value);
158151
}
159152

153+
[JsiiProperty("unknownProperty", "{\"primitive\":\"any\",\"optional\":true}")]
154+
public virtual object UnknownProperty
155+
{
156+
get => GetInstanceProperty<object>();
157+
set => SetInstanceProperty(value);
158+
}
159+
160160
[JsiiMethod("enumMethod", "{\"fqn\":\"jsii-calc.StringEnum\"}", "[{\"name\":\"value\",\"type\":{\"fqn\":\"jsii-calc.StringEnum\"}}]")]
161161
public virtual StringEnum EnumMethod(StringEnum value)
162162
{

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

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public IDictionary<string, Value_> AnotherOptional
3838
set;
3939
}
4040

41+
[JsiiProperty("optionalAny", "{\"primitive\":\"any\",\"optional\":true}", true)]
42+
public object OptionalAny
43+
{
44+
get;
45+
set;
46+
}
47+
4148
[JsiiProperty("optionalArray", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"string\"}},\"optional\":true}", true)]
4249
public string[] OptionalArray
4350
{

0 commit comments

Comments
 (0)