From 91074f3a89bb64980fc16f6c0603f414b05891be Mon Sep 17 00:00:00 2001 From: Romain Marcadier-Muller Date: Wed, 19 Sep 2018 14:52:13 +0200 Subject: [PATCH] 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`. --- packages/jsii-calc-base/test/assembly.jsii | 3 +- packages/jsii-calc/lib/compliance.ts | 5 +++ packages/jsii-calc/test/assembly.jsii | 43 +++++++++++++++---- .../.jsii | 3 +- .../CalculatorNamespace/BaseNamespace/Base.cs | 2 +- .../jsii/tests/calculator/base/Base.java | 1 + .../sphinx/_scope_jsii-calc-base.rst | 2 +- .../.jsii | 43 +++++++++++++++---- .../Tests/CalculatorNamespace/AllTypes.cs | 26 +++++------ .../CalculatorNamespace/DerivedStruct.cs | 7 +++ .../CalculatorNamespace/DerivedStructProxy.cs | 7 +++ .../CalculatorNamespace/IDerivedStruct.cs | 7 +++ .../RuntimeTypeChecking.cs | 6 +++ .../UseBundledDependency.cs | 2 +- .../jsii/tests/calculator/AllTypes.java | 26 +++++------ .../jsii/tests/calculator/DerivedStruct.java | 36 ++++++++++++++++ .../tests/calculator/RuntimeTypeChecking.java | 8 ++++ .../calculator/UseBundledDependency.java | 1 + .../expected.jsii-calc/sphinx/jsii-calc.rst | 35 +++++++++------ packages/jsii/lib/assembler.ts | 16 ++++--- 20 files changed, 215 insertions(+), 64 deletions(-) diff --git a/packages/jsii-calc-base/test/assembly.jsii b/packages/jsii-calc-base/test/assembly.jsii index e3781d4f3f..b0a58aa10c 100644 --- a/packages/jsii-calc-base/test/assembly.jsii +++ b/packages/jsii-calc-base/test/assembly.jsii @@ -72,6 +72,7 @@ }, "name": "typeName", "returns": { + "optional": true, "primitive": "any" } } @@ -101,5 +102,5 @@ } }, "version": "0.7.5", - "fingerprint": "Kzm7bNzxYO6frB7BwjqVBKhzTiCRwVG5aqgQrz7MOnE=" + "fingerprint": "kazFxdH9DydCwjHvToTbXzqqXE0CytJ5qNmGO1NlGAM=" } diff --git a/packages/jsii-calc/lib/compliance.ts b/packages/jsii-calc/lib/compliance.ts index 112921793d..09990ed8af 100644 --- a/packages/jsii-calc/lib/compliance.ts +++ b/packages/jsii-calc/lib/compliance.ts @@ -244,6 +244,10 @@ export class RuntimeTypeChecking { public methodWithDefaultedArguments(arg1: number = 2, arg2: string, arg3: Date = new Date()) { arg1; arg2; arg3; } + + public methodWithOptionalAnyArgument(arg?: any) { + arg; + } } export class OptionalConstructorArgument { @@ -479,6 +483,7 @@ export interface DerivedStruct extends MyFirstStruct { bool: boolean anotherRequired: Date optionalArray?: string[] + optionalAny?: any /** * This is optional. */ diff --git a/packages/jsii-calc/test/assembly.jsii b/packages/jsii-calc/test/assembly.jsii index a2c2a938a0..f424f819c2 100644 --- a/packages/jsii-calc/test/assembly.jsii +++ b/packages/jsii-calc/test/assembly.jsii @@ -377,6 +377,7 @@ "type": { "collection": { "elementtype": { + "optional": true, "primitive": "any" }, "kind": "array" @@ -388,18 +389,13 @@ "type": { "collection": { "elementtype": { + "optional": true, "primitive": "any" }, "kind": "map" } } }, - { - "name": "anyProperty", - "type": { - "primitive": "any" - } - }, { "name": "arrayProperty", "type": { @@ -521,6 +517,7 @@ "type": { "collection": { "elementtype": { + "optional": true, "primitive": "any" }, "kind": "array" @@ -532,6 +529,7 @@ "type": { "collection": { "elementtype": { + "optional": true, "primitive": "any" }, "kind": "map" @@ -539,8 +537,9 @@ } }, { - "name": "unknownProperty", + "name": "anyProperty", "type": { + "optional": true, "primitive": "any" } }, @@ -550,6 +549,13 @@ "fqn": "jsii-calc.StringEnum", "optional": true } + }, + { + "name": "unknownProperty", + "type": { + "optional": true, + "primitive": "any" + } } ] }, @@ -1152,6 +1158,14 @@ "optional": true } }, + { + "abstract": true, + "name": "optionalAny", + "type": { + "optional": true, + "primitive": "any" + } + }, { "abstract": true, "name": "optionalArray", @@ -2403,6 +2417,18 @@ } ] }, + { + "name": "methodWithOptionalAnyArgument", + "parameters": [ + { + "name": "arg", + "type": { + "optional": true, + "primitive": "any" + } + } + ] + }, { "docs": { "comment": "Used to verify verification of number of method arguments." @@ -2854,6 +2880,7 @@ { "name": "value", "returns": { + "optional": true, "primitive": "any" } } @@ -3203,5 +3230,5 @@ } }, "version": "0.7.5", - "fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8=" + "fingerprint": "Zt3ElcP9k7ABYhwmP1xNZBni1sFj9iw0FZwWOe8n+L8=" } diff --git a/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/.jsii b/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/.jsii index e3781d4f3f..b0a58aa10c 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/.jsii +++ b/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/.jsii @@ -72,6 +72,7 @@ }, "name": "typeName", "returns": { + "optional": true, "primitive": "any" } } @@ -101,5 +102,5 @@ } }, "version": "0.7.5", - "fingerprint": "Kzm7bNzxYO6frB7BwjqVBKhzTiCRwVG5aqgQrz7MOnE=" + "fingerprint": "kazFxdH9DydCwjHvToTbXzqqXE0CytJ5qNmGO1NlGAM=" } diff --git a/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon/JSII/Tests/CalculatorNamespace/BaseNamespace/Base.cs b/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon/JSII/Tests/CalculatorNamespace/BaseNamespace/Base.cs index c38472e6b3..83224daeac 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon/JSII/Tests/CalculatorNamespace/BaseNamespace/Base.cs +++ b/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon/JSII/Tests/CalculatorNamespace/BaseNamespace/Base.cs @@ -19,7 +19,7 @@ protected Base(DeputyProps props): base(props) } /// the name of the class (to verify native type names are created for derived classes). - [JsiiMethod("typeName", "{\"primitive\":\"any\"}", "[]")] + [JsiiMethod("typeName", "{\"primitive\":\"any\",\"optional\":true}", "[]")] public virtual object TypeName() { return InvokeInstanceMethod(new object[]{}); diff --git a/packages/jsii-pacmak/test/expected.jsii-calc-base/java/src/main/java/software/amazon/jsii/tests/calculator/base/Base.java b/packages/jsii-pacmak/test/expected.jsii-calc-base/java/src/main/java/software/amazon/jsii/tests/calculator/base/Base.java index 7a64b8e3d7..2b1a3402f8 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc-base/java/src/main/java/software/amazon/jsii/tests/calculator/base/Base.java +++ b/packages/jsii-pacmak/test/expected.jsii-calc-base/java/src/main/java/software/amazon/jsii/tests/calculator/base/Base.java @@ -13,6 +13,7 @@ protected Base(final software.amazon.jsii.JsiiObject.InitializationMode mode) { /** * @return the name of the class (to verify native type names are created for derived classes). */ + @javax.annotation.Nullable public java.lang.Object typeName() { return this.jsiiCall("typeName", java.lang.Object.class); } diff --git a/packages/jsii-pacmak/test/expected.jsii-calc-base/sphinx/_scope_jsii-calc-base.rst b/packages/jsii-pacmak/test/expected.jsii-calc-base/sphinx/_scope_jsii-calc-base.rst index a5777e4015..de6f7e52fa 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc-base/sphinx/_scope_jsii-calc-base.rst +++ b/packages/jsii-pacmak/test/expected.jsii-calc-base/sphinx/_scope_jsii-calc-base.rst @@ -158,7 +158,7 @@ Base .. py:method:: typeName() -> any :return: the name of the class (to verify native type names are created for derived classes). - :rtype: any + :rtype: any or undefined BaseProps (interface) diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii index a2c2a938a0..f424f819c2 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii @@ -377,6 +377,7 @@ "type": { "collection": { "elementtype": { + "optional": true, "primitive": "any" }, "kind": "array" @@ -388,18 +389,13 @@ "type": { "collection": { "elementtype": { + "optional": true, "primitive": "any" }, "kind": "map" } } }, - { - "name": "anyProperty", - "type": { - "primitive": "any" - } - }, { "name": "arrayProperty", "type": { @@ -521,6 +517,7 @@ "type": { "collection": { "elementtype": { + "optional": true, "primitive": "any" }, "kind": "array" @@ -532,6 +529,7 @@ "type": { "collection": { "elementtype": { + "optional": true, "primitive": "any" }, "kind": "map" @@ -539,8 +537,9 @@ } }, { - "name": "unknownProperty", + "name": "anyProperty", "type": { + "optional": true, "primitive": "any" } }, @@ -550,6 +549,13 @@ "fqn": "jsii-calc.StringEnum", "optional": true } + }, + { + "name": "unknownProperty", + "type": { + "optional": true, + "primitive": "any" + } } ] }, @@ -1152,6 +1158,14 @@ "optional": true } }, + { + "abstract": true, + "name": "optionalAny", + "type": { + "optional": true, + "primitive": "any" + } + }, { "abstract": true, "name": "optionalArray", @@ -2403,6 +2417,18 @@ } ] }, + { + "name": "methodWithOptionalAnyArgument", + "parameters": [ + { + "name": "arg", + "type": { + "optional": true, + "primitive": "any" + } + } + ] + }, { "docs": { "comment": "Used to verify verification of number of method arguments." @@ -2854,6 +2880,7 @@ { "name": "value", "returns": { + "optional": true, "primitive": "any" } } @@ -3203,5 +3230,5 @@ } }, "version": "0.7.5", - "fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8=" + "fingerprint": "Zt3ElcP9k7ABYhwmP1xNZBni1sFj9iw0FZwWOe8n+L8=" } diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/AllTypes.cs b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/AllTypes.cs index 46e7cbbac0..b4a4896b31 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/AllTypes.cs +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/AllTypes.cs @@ -31,27 +31,20 @@ public virtual double EnumPropertyValue get => GetInstanceProperty(); } - [JsiiProperty("anyArrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"any\"}}}")] + [JsiiProperty("anyArrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"any\",\"optional\":true}}}")] public virtual object[] AnyArrayProperty { get => GetInstanceProperty(); set => SetInstanceProperty(value); } - [JsiiProperty("anyMapProperty", "{\"collection\":{\"kind\":\"map\",\"elementtype\":{\"primitive\":\"any\"}}}")] + [JsiiProperty("anyMapProperty", "{\"collection\":{\"kind\":\"map\",\"elementtype\":{\"primitive\":\"any\",\"optional\":true}}}")] public virtual IDictionary AnyMapProperty { get => GetInstanceProperty>(); set => SetInstanceProperty(value); } - [JsiiProperty("anyProperty", "{\"primitive\":\"any\"}")] - public virtual object AnyProperty - { - get => GetInstanceProperty(); - set => SetInstanceProperty(value); - } - [JsiiProperty("arrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"string\"}}}")] public virtual string[] ArrayProperty { @@ -129,22 +122,22 @@ public virtual object UnionProperty set => SetInstanceProperty(value); } - [JsiiProperty("unknownArrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"any\"}}}")] + [JsiiProperty("unknownArrayProperty", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"any\",\"optional\":true}}}")] public virtual object[] UnknownArrayProperty { get => GetInstanceProperty(); set => SetInstanceProperty(value); } - [JsiiProperty("unknownMapProperty", "{\"collection\":{\"kind\":\"map\",\"elementtype\":{\"primitive\":\"any\"}}}")] + [JsiiProperty("unknownMapProperty", "{\"collection\":{\"kind\":\"map\",\"elementtype\":{\"primitive\":\"any\",\"optional\":true}}}")] public virtual IDictionary UnknownMapProperty { get => GetInstanceProperty>(); set => SetInstanceProperty(value); } - [JsiiProperty("unknownProperty", "{\"primitive\":\"any\"}")] - public virtual object UnknownProperty + [JsiiProperty("anyProperty", "{\"primitive\":\"any\",\"optional\":true}")] + public virtual object AnyProperty { get => GetInstanceProperty(); set => SetInstanceProperty(value); @@ -157,6 +150,13 @@ public virtual StringEnum OptionalEnumValue set => SetInstanceProperty(value); } + [JsiiProperty("unknownProperty", "{\"primitive\":\"any\",\"optional\":true}")] + public virtual object UnknownProperty + { + get => GetInstanceProperty(); + set => SetInstanceProperty(value); + } + [JsiiMethod("enumMethod", "{\"fqn\":\"jsii-calc.StringEnum\"}", "[{\"name\":\"value\",\"type\":{\"fqn\":\"jsii-calc.StringEnum\"}}]")] public virtual StringEnum EnumMethod(StringEnum value) { diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DerivedStruct.cs b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DerivedStruct.cs index 5a999f1a32..7795e279d1 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DerivedStruct.cs +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DerivedStruct.cs @@ -38,6 +38,13 @@ public IDictionary AnotherOptional set; } + [JsiiProperty("optionalAny", "{\"primitive\":\"any\",\"optional\":true}", true)] + public object OptionalAny + { + get; + set; + } + [JsiiProperty("optionalArray", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"string\"}},\"optional\":true}", true)] public string[] OptionalArray { diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DerivedStructProxy.cs b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DerivedStructProxy.cs index 372ea6267f..59adc06210 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DerivedStructProxy.cs +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DerivedStructProxy.cs @@ -43,6 +43,13 @@ public virtual IDictionary AnotherOptional set => SetInstanceProperty(value); } + [JsiiProperty("optionalAny", "{\"primitive\":\"any\",\"optional\":true}")] + public virtual object OptionalAny + { + get => GetInstanceProperty(); + set => SetInstanceProperty(value); + } + [JsiiProperty("optionalArray", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"string\"}},\"optional\":true}")] public virtual string[] OptionalArray { diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/IDerivedStruct.cs b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/IDerivedStruct.cs index 58a290f26b..59503cab6f 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/IDerivedStruct.cs +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/IDerivedStruct.cs @@ -39,6 +39,13 @@ IDictionary AnotherOptional set; } + [JsiiProperty("optionalAny", "{\"primitive\":\"any\",\"optional\":true}")] + object OptionalAny + { + get; + set; + } + [JsiiProperty("optionalArray", "{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"string\"}},\"optional\":true}")] string[] OptionalArray { diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/RuntimeTypeChecking.cs b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/RuntimeTypeChecking.cs index 4f6a82c414..eb99aa7e3b 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/RuntimeTypeChecking.cs +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/RuntimeTypeChecking.cs @@ -24,6 +24,12 @@ public virtual void MethodWithDefaultedArguments(double? arg1, string arg2, Date InvokeInstanceVoidMethod(new object[]{arg1, arg2, arg3}); } + [JsiiMethod("methodWithOptionalAnyArgument", null, "[{\"name\":\"arg\",\"type\":{\"primitive\":\"any\",\"optional\":true}}]")] + public virtual void MethodWithOptionalAnyArgument(object arg) + { + InvokeInstanceVoidMethod(new object[]{arg}); + } + /// Used to verify verification of number of method arguments. [JsiiMethod("methodWithOptionalArguments", null, "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\",\"optional\":true}}]")] public virtual void MethodWithOptionalArguments(double arg1, string arg2, DateTime? arg3) diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/UseBundledDependency.cs b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/UseBundledDependency.cs index 7470df65fe..f438961582 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/UseBundledDependency.cs +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/UseBundledDependency.cs @@ -17,7 +17,7 @@ protected UseBundledDependency(DeputyProps props): base(props) { } - [JsiiMethod("value", "{\"primitive\":\"any\"}", "[]")] + [JsiiMethod("value", "{\"primitive\":\"any\",\"optional\":true}", "[]")] public virtual object Value() { return InvokeInstanceMethod(new object[]{}); diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/AllTypes.java b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/AllTypes.java index f5efd39b12..b57eb209d9 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/AllTypes.java +++ b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/AllTypes.java @@ -39,14 +39,6 @@ public void setAnyMapProperty(final java.util.Map getArrayProperty() { return this.jsiiGet("arrayProperty", java.util.List.class); } @@ -159,12 +151,13 @@ public void setUnknownMapProperty(final java.util.Map value); + java.lang.Object getOptionalAny(); + void setOptionalAny(final java.lang.Object value); java.util.List getOptionalArray(); void setOptionalArray(final java.util.List value); @@ -45,6 +47,8 @@ final class Builder { @javax.annotation.Nullable private java.util.Map _anotherOptional; @javax.annotation.Nullable + private java.lang.Object _optionalAny; + @javax.annotation.Nullable private java.util.List _optionalArray; private java.lang.Number _anumber; private java.lang.String _astring; @@ -87,6 +91,15 @@ public Builder withAnotherOptional(@javax.annotation.Nullable final java.util.Ma this._anotherOptional = value; return this; } + /** + * Sets the value of OptionalAny + * @param value the value to be set + * @return {@code this} + */ + public Builder withOptionalAny(@javax.annotation.Nullable final java.lang.Object value) { + this._optionalAny = value; + return this; + } /** * Sets the value of OptionalArray * @param value the value to be set @@ -137,6 +150,8 @@ public DerivedStruct build() { @javax.annotation.Nullable private java.util.Map $anotherOptional = _anotherOptional; @javax.annotation.Nullable + private java.lang.Object $optionalAny = _optionalAny; + @javax.annotation.Nullable private java.util.List $optionalArray = _optionalArray; private java.lang.Number $anumber = java.util.Objects.requireNonNull(_anumber, "anumber is required"); private java.lang.String $astring = java.util.Objects.requireNonNull(_astring, "astring is required"); @@ -183,6 +198,16 @@ public void setAnotherOptional(@javax.annotation.Nullable final java.util.Map getOptionalArray() { return this.$optionalArray; @@ -288,6 +313,17 @@ public void setAnotherOptional(@javax.annotation.Nullable final java.util.Map getOptionalArray() { diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/RuntimeTypeChecking.java b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/RuntimeTypeChecking.java index b48aeca4a9..2f1e8cea29 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/RuntimeTypeChecking.java +++ b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/RuntimeTypeChecking.java @@ -19,6 +19,14 @@ public void methodWithDefaultedArguments(@javax.annotation.Nullable final java.l this.jsiiCall("methodWithDefaultedArguments", Void.class, java.util.stream.Stream.concat(java.util.stream.Stream.of(arg1), java.util.stream.Stream.of(java.util.Objects.requireNonNull(arg2, "arg2 is required"))).toArray()); } + public void methodWithOptionalAnyArgument(@javax.annotation.Nullable final java.lang.Object arg) { + this.jsiiCall("methodWithOptionalAnyArgument", Void.class, java.util.stream.Stream.of(arg).toArray()); + } + + public void methodWithOptionalAnyArgument() { + this.jsiiCall("methodWithOptionalAnyArgument", Void.class); + } + /** * Used to verify verification of number of method arguments. */ diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/UseBundledDependency.java b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/UseBundledDependency.java index 8339a2ea3d..98ca53d5ae 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/UseBundledDependency.java +++ b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/UseBundledDependency.java @@ -11,6 +11,7 @@ public UseBundledDependency() { software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this); } + @javax.annotation.Nullable public java.lang.Object value() { return this.jsiiCall("value", java.lang.Object.class); } diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/sphinx/jsii-calc.rst b/packages/jsii-pacmak/test/expected.jsii-calc/sphinx/jsii-calc.rst index 97a8d61620..3318a85d4e 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/sphinx/jsii-calc.rst +++ b/packages/jsii-pacmak/test/expected.jsii-calc/sphinx/jsii-calc.rst @@ -348,17 +348,12 @@ AllTypes .. py:attribute:: anyArrayProperty - :type: any[] + :type: (any or undefined)[] .. py:attribute:: anyMapProperty - :type: string => any - - - .. py:attribute:: anyProperty - - :type: any + :type: string => (any or undefined) .. py:attribute:: arrayProperty @@ -418,17 +413,17 @@ AllTypes .. py:attribute:: unknownArrayProperty - :type: any[] + :type: (any or undefined)[] .. py:attribute:: unknownMapProperty - :type: string => any + :type: string => (any or undefined) - .. py:attribute:: unknownProperty + .. py:attribute:: anyProperty - :type: any + :type: any or undefined .. py:attribute:: optionalEnumValue @@ -436,6 +431,11 @@ AllTypes :type: :py:class:`~jsii-calc.StringEnum`\ or undefined + .. py:attribute:: unknownProperty + + :type: any or undefined + + AllTypesEnum (enum) ^^^^^^^^^^^^^^^^^^^ @@ -1009,6 +1009,11 @@ DerivedStruct (interface) :type: string => :py:class:`@scope/jsii-calc-lib.Value`\ or undefined *(abstract)* + .. py:attribute:: optionalAny + + :type: any or undefined *(abstract)* + + .. py:attribute:: optionalArray :type: string[] or undefined *(abstract)* @@ -2525,6 +2530,12 @@ RuntimeTypeChecking :type arg3: date or undefined + .. py:method:: methodWithOptionalAnyArgument([arg]) + + :param arg: + :type arg: any or undefined + + .. py:method:: methodWithOptionalArguments(arg1, arg2, [arg3]) Used to verify verification of number of method arguments. @@ -2966,7 +2977,7 @@ UseBundledDependency .. py:method:: value() -> any - :rtype: any + :rtype: any or undefined UseCalcBase diff --git a/packages/jsii/lib/assembler.ts b/packages/jsii/lib/assembler.ts index 1511018f80..74f8feb620 100644 --- a/packages/jsii/lib/assembler.ts +++ b/packages/jsii/lib/assembler.ts @@ -604,6 +604,10 @@ export class Assembler implements Emitter { property.immutable = (ts.getCombinedModifierFlags(signature) & ts.ModifierFlags.Readonly) !== 0; } + if (signature.questionToken) { + property.type.optional = true; + } + if (property.static && property.immutable && ts.isPropertyDeclaration(signature) && signature.initializer) { property.const = true; } @@ -627,7 +631,7 @@ export class Assembler implements Emitter { if (parameter.variadic) { parameter.type = (parameter.type as spec.CollectionTypeReference).collection.elementtype; } - if (paramDeclaration.initializer != null) { + if (paramDeclaration.initializer || paramDeclaration.questionToken) { parameter.type.optional = true; } this._visitDocumentation(paramSymbol, parameter); @@ -650,7 +654,7 @@ export class Assembler implements Emitter { if (!type.symbol) { this._diagnostic(declaration, ts.DiagnosticCategory.Error, `Non-primitive types must have a symbol`); - return { primitive: spec.PrimitiveType.Any }; + return { primitive: spec.PrimitiveType.Any, optional: true }; } if (type.symbol.name === 'Array') { @@ -667,7 +671,7 @@ export class Assembler implements Emitter { this._diagnostic(declaration, ts.DiagnosticCategory.Error, `Un-specified promise type (need to specify as Promise)`); - return { primitive: spec.PrimitiveType.Any, promise: true }; + return { primitive: spec.PrimitiveType.Any, optional: true, promise: true }; } else { return { ...await this._typeReference(typeRef.typeArguments[0], declaration), @@ -689,7 +693,7 @@ export class Assembler implements Emitter { this._diagnostic(declaration, ts.DiagnosticCategory.Error, `Array references must have exactly one type argument (found ${count})`); - elementtype = { primitive: spec.PrimitiveType.Any }; + elementtype = { primitive: spec.PrimitiveType.Any, optional: true }; } return { @@ -709,7 +713,7 @@ export class Assembler implements Emitter { this._diagnostic(declaration, ts.DiagnosticCategory.Error, `Only string index maps are supported`); - elementtype = { primitive: spec.PrimitiveType.Any }; + elementtype = { primitive: spec.PrimitiveType.Any, optional: true }; } return { collection: { @@ -727,7 +731,7 @@ export class Assembler implements Emitter { } // tslint:disable-next-line:no-bitwise if (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) { - return { primitive: spec.PrimitiveType.Any }; + return { primitive: spec.PrimitiveType.Any, optional: true }; } } else { switch (type.symbol.name) {