From 578bf9caf4414daa337c8673f2751fac4a860d51 Mon Sep 17 00:00:00 2001 From: Romain Marcadier-Muller Date: Mon, 17 Sep 2018 18:55:15 +0200 Subject: [PATCH] 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 --- packages/jsii-calc/lib/compliance.ts | 18 ++- packages/jsii-calc/test/assembly.jsii | 135 +++++++++++++++++- .../.jsii | 135 +++++++++++++++++- .../DefaultedConstructorArgument.cs | 39 +++++ .../OptionalConstructorArgument.cs | 39 +++++ .../RuntimeTypeChecking.cs | 6 + .../amazon/jsii/tests/calculator/$Module.java | 2 + .../DefaultedConstructorArgument.java | 29 ++++ .../OptionalConstructorArgument.java | 30 ++++ .../tests/calculator/RuntimeTypeChecking.java | 8 ++ .../expected.jsii-calc/sphinx/jsii-calc.rst | 108 ++++++++++++++ packages/jsii/lib/assembler.ts | 6 +- 12 files changed, 551 insertions(+), 4 deletions(-) create mode 100644 packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DefaultedConstructorArgument.cs create mode 100644 packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/OptionalConstructorArgument.cs create mode 100644 packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/DefaultedConstructorArgument.java create mode 100644 packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/OptionalConstructorArgument.java diff --git a/packages/jsii-calc/lib/compliance.ts b/packages/jsii-calc/lib/compliance.ts index 4d62821557..112921793d 100644 --- a/packages/jsii-calc/lib/compliance.ts +++ b/packages/jsii-calc/lib/compliance.ts @@ -240,6 +240,22 @@ export class RuntimeTypeChecking { public methodWithOptionalArguments(arg1: number, arg2: string, arg3?: Date) { arg1; arg2; arg3; } + + public methodWithDefaultedArguments(arg1: number = 2, arg2: string, arg3: Date = new Date()) { + arg1; arg2; arg3; + } +} + +export class OptionalConstructorArgument { + public constructor(public readonly arg1: number, + public readonly arg2: string, + public readonly arg3?: Date) {} +} + +export class DefaultedConstructorArgument { + public constructor(public readonly arg1: number = 2, + public readonly arg2: string, + public readonly arg3: Date = new Date()) {} } export namespace DerivedClassHasNoProperties { @@ -865,4 +881,4 @@ export class AbstractClassReturner { abstractProperty: 'hello-abstract-property' } } -} \ No newline at end of file +} diff --git a/packages/jsii-calc/test/assembly.jsii b/packages/jsii-calc/test/assembly.jsii index fd17e047fa..a2c2a938a0 100644 --- a/packages/jsii-calc/test/assembly.jsii +++ b/packages/jsii-calc/test/assembly.jsii @@ -1012,6 +1012,60 @@ } ] }, + "jsii-calc.DefaultedConstructorArgument": { + "assembly": "jsii-calc", + "fqn": "jsii-calc.DefaultedConstructorArgument", + "initializer": { + "initializer": true, + "parameters": [ + { + "name": "arg1", + "type": { + "optional": true, + "primitive": "number" + } + }, + { + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "name": "arg3", + "type": { + "optional": true, + "primitive": "date" + } + } + ] + }, + "kind": "class", + "name": "DefaultedConstructorArgument", + "properties": [ + { + "immutable": true, + "name": "arg1", + "type": { + "primitive": "number" + } + }, + { + "immutable": true, + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "immutable": true, + "name": "arg3", + "type": { + "primitive": "date" + } + } + ] + }, "jsii-calc.DerivedClassHasNoProperties.Base": { "assembly": "jsii-calc", "fqn": "jsii-calc.DerivedClassHasNoProperties.Base", @@ -2073,6 +2127,60 @@ ], "name": "ObjectRefsInCollections" }, + "jsii-calc.OptionalConstructorArgument": { + "assembly": "jsii-calc", + "fqn": "jsii-calc.OptionalConstructorArgument", + "initializer": { + "initializer": true, + "parameters": [ + { + "name": "arg1", + "type": { + "primitive": "number" + } + }, + { + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "name": "arg3", + "type": { + "optional": true, + "primitive": "date" + } + } + ] + }, + "kind": "class", + "name": "OptionalConstructorArgument", + "properties": [ + { + "immutable": true, + "name": "arg1", + "type": { + "primitive": "number" + } + }, + { + "immutable": true, + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "immutable": true, + "name": "arg3", + "type": { + "optional": true, + "primitive": "date" + } + } + ] + }, "jsii-calc.OverrideReturnsObject": { "assembly": "jsii-calc", "fqn": "jsii-calc.OverrideReturnsObject", @@ -2270,6 +2378,31 @@ }, "kind": "class", "methods": [ + { + "name": "methodWithDefaultedArguments", + "parameters": [ + { + "name": "arg1", + "type": { + "optional": true, + "primitive": "number" + } + }, + { + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "name": "arg3", + "type": { + "optional": true, + "primitive": "date" + } + } + ] + }, { "docs": { "comment": "Used to verify verification of number of method arguments." @@ -3070,5 +3203,5 @@ } }, "version": "0.7.5", - "fingerprint": "XrmsNUcNdYiHEC6BRVT5XoeVmYQZzjYgiu6MyibgOwk=" + "fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8=" } 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 fd17e047fa..a2c2a938a0 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 @@ -1012,6 +1012,60 @@ } ] }, + "jsii-calc.DefaultedConstructorArgument": { + "assembly": "jsii-calc", + "fqn": "jsii-calc.DefaultedConstructorArgument", + "initializer": { + "initializer": true, + "parameters": [ + { + "name": "arg1", + "type": { + "optional": true, + "primitive": "number" + } + }, + { + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "name": "arg3", + "type": { + "optional": true, + "primitive": "date" + } + } + ] + }, + "kind": "class", + "name": "DefaultedConstructorArgument", + "properties": [ + { + "immutable": true, + "name": "arg1", + "type": { + "primitive": "number" + } + }, + { + "immutable": true, + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "immutable": true, + "name": "arg3", + "type": { + "primitive": "date" + } + } + ] + }, "jsii-calc.DerivedClassHasNoProperties.Base": { "assembly": "jsii-calc", "fqn": "jsii-calc.DerivedClassHasNoProperties.Base", @@ -2073,6 +2127,60 @@ ], "name": "ObjectRefsInCollections" }, + "jsii-calc.OptionalConstructorArgument": { + "assembly": "jsii-calc", + "fqn": "jsii-calc.OptionalConstructorArgument", + "initializer": { + "initializer": true, + "parameters": [ + { + "name": "arg1", + "type": { + "primitive": "number" + } + }, + { + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "name": "arg3", + "type": { + "optional": true, + "primitive": "date" + } + } + ] + }, + "kind": "class", + "name": "OptionalConstructorArgument", + "properties": [ + { + "immutable": true, + "name": "arg1", + "type": { + "primitive": "number" + } + }, + { + "immutable": true, + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "immutable": true, + "name": "arg3", + "type": { + "optional": true, + "primitive": "date" + } + } + ] + }, "jsii-calc.OverrideReturnsObject": { "assembly": "jsii-calc", "fqn": "jsii-calc.OverrideReturnsObject", @@ -2270,6 +2378,31 @@ }, "kind": "class", "methods": [ + { + "name": "methodWithDefaultedArguments", + "parameters": [ + { + "name": "arg1", + "type": { + "optional": true, + "primitive": "number" + } + }, + { + "name": "arg2", + "type": { + "primitive": "string" + } + }, + { + "name": "arg3", + "type": { + "optional": true, + "primitive": "date" + } + } + ] + }, { "docs": { "comment": "Used to verify verification of number of method arguments." @@ -3070,5 +3203,5 @@ } }, "version": "0.7.5", - "fingerprint": "XrmsNUcNdYiHEC6BRVT5XoeVmYQZzjYgiu6MyibgOwk=" + "fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8=" } diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DefaultedConstructorArgument.cs b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DefaultedConstructorArgument.cs new file mode 100644 index 0000000000..4ddd807d62 --- /dev/null +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DefaultedConstructorArgument.cs @@ -0,0 +1,39 @@ +using Amazon.JSII.Runtime.Deputy; +using System; + +namespace Amazon.JSII.Tests.CalculatorNamespace +{ + [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}}]")] + public class DefaultedConstructorArgument : DeputyBase + { + public DefaultedConstructorArgument(double? arg1, string arg2, DateTime? arg3): base(new DeputyProps(new object[]{arg1, arg2, arg3})) + { + } + + protected DefaultedConstructorArgument(ByRefValue reference): base(reference) + { + } + + protected DefaultedConstructorArgument(DeputyProps props): base(props) + { + } + + [JsiiProperty("arg1", "{\"primitive\":\"number\"}")] + public virtual double Arg1 + { + get => GetInstanceProperty(); + } + + [JsiiProperty("arg2", "{\"primitive\":\"string\"}")] + public virtual string Arg2 + { + get => GetInstanceProperty(); + } + + [JsiiProperty("arg3", "{\"primitive\":\"date\"}")] + public virtual DateTime Arg3 + { + get => GetInstanceProperty(); + } + } +} \ No newline at end of file diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/OptionalConstructorArgument.cs b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/OptionalConstructorArgument.cs new file mode 100644 index 0000000000..8ff423ebd2 --- /dev/null +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/OptionalConstructorArgument.cs @@ -0,0 +1,39 @@ +using Amazon.JSII.Runtime.Deputy; +using System; + +namespace Amazon.JSII.Tests.CalculatorNamespace +{ + [JsiiClass(typeof(OptionalConstructorArgument), "jsii-calc.OptionalConstructorArgument", "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\",\"optional\":true}}]")] + public class OptionalConstructorArgument : DeputyBase + { + public OptionalConstructorArgument(double arg1, string arg2, DateTime? arg3): base(new DeputyProps(new object[]{arg1, arg2, arg3})) + { + } + + protected OptionalConstructorArgument(ByRefValue reference): base(reference) + { + } + + protected OptionalConstructorArgument(DeputyProps props): base(props) + { + } + + [JsiiProperty("arg1", "{\"primitive\":\"number\"}")] + public virtual double Arg1 + { + get => GetInstanceProperty(); + } + + [JsiiProperty("arg2", "{\"primitive\":\"string\"}")] + public virtual string Arg2 + { + get => GetInstanceProperty(); + } + + [JsiiProperty("arg3", "{\"primitive\":\"date\",\"optional\":true}")] + public virtual DateTime? Arg3 + { + get => GetInstanceProperty(); + } + } +} \ No newline at end of file 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 c3da746ef9..4f6a82c414 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 @@ -18,6 +18,12 @@ protected RuntimeTypeChecking(DeputyProps props): base(props) { } + [JsiiMethod("methodWithDefaultedArguments", null, "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\",\"optional\":true}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\",\"optional\":true}}]")] + public virtual void MethodWithDefaultedArguments(double? arg1, string arg2, DateTime? arg3) + { + InvokeInstanceVoidMethod(new object[]{arg1, arg2, arg3}); + } + /// 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/java/src/main/java/software/amazon/jsii/tests/calculator/$Module.java b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/$Module.java index c8e09cac93..b9a7a469ca 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/$Module.java +++ b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/$Module.java @@ -29,6 +29,7 @@ protected Class resolveClass(final String fqn) throws ClassNotFoundException case "jsii-calc.BinaryOperation": return software.amazon.jsii.tests.calculator.BinaryOperation.class; case "jsii-calc.Calculator": return software.amazon.jsii.tests.calculator.Calculator.class; case "jsii-calc.CalculatorProps": return software.amazon.jsii.tests.calculator.CalculatorProps.class; + case "jsii-calc.DefaultedConstructorArgument": return software.amazon.jsii.tests.calculator.DefaultedConstructorArgument.class; case "jsii-calc.DerivedClassHasNoProperties.Base": return software.amazon.jsii.tests.calculator.DerivedClassHasNoProperties.Base.class; case "jsii-calc.DerivedClassHasNoProperties.Derived": return software.amazon.jsii.tests.calculator.DerivedClassHasNoProperties.Derived.class; case "jsii-calc.DerivedStruct": return software.amazon.jsii.tests.calculator.DerivedStruct.class; @@ -54,6 +55,7 @@ protected Class resolveClass(final String fqn) throws ClassNotFoundException case "jsii-calc.NodeStandardLibrary": return software.amazon.jsii.tests.calculator.NodeStandardLibrary.class; case "jsii-calc.NumberGenerator": return software.amazon.jsii.tests.calculator.NumberGenerator.class; case "jsii-calc.ObjectRefsInCollections": return software.amazon.jsii.tests.calculator.ObjectRefsInCollections.class; + case "jsii-calc.OptionalConstructorArgument": return software.amazon.jsii.tests.calculator.OptionalConstructorArgument.class; case "jsii-calc.OverrideReturnsObject": return software.amazon.jsii.tests.calculator.OverrideReturnsObject.class; case "jsii-calc.Polymorphism": return software.amazon.jsii.tests.calculator.Polymorphism.class; case "jsii-calc.Power": return software.amazon.jsii.tests.calculator.Power.class; diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/DefaultedConstructorArgument.java b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/DefaultedConstructorArgument.java new file mode 100644 index 0000000000..dcd9be75a2 --- /dev/null +++ b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/DefaultedConstructorArgument.java @@ -0,0 +1,29 @@ +package software.amazon.jsii.tests.calculator; + +@javax.annotation.Generated(value = "jsii-pacmak") +@software.amazon.jsii.Jsii(module = software.amazon.jsii.tests.calculator.$Module.class, fqn = "jsii-calc.DefaultedConstructorArgument") +public class DefaultedConstructorArgument extends software.amazon.jsii.JsiiObject { + protected DefaultedConstructorArgument(final software.amazon.jsii.JsiiObject.InitializationMode mode) { + super(mode); + } + public DefaultedConstructorArgument(@javax.annotation.Nullable final java.lang.Number arg1, final java.lang.String arg2, @javax.annotation.Nullable final java.time.Instant arg3) { + super(software.amazon.jsii.JsiiObject.InitializationMode.Jsii); + software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this, java.util.stream.Stream.concat(java.util.stream.Stream.concat(java.util.stream.Stream.of(arg1), java.util.stream.Stream.of(java.util.Objects.requireNonNull(arg2, "arg2 is required"))), java.util.stream.Stream.of(arg3)).toArray()); + } + public DefaultedConstructorArgument(@javax.annotation.Nullable final java.lang.Number arg1, final java.lang.String arg2) { + super(software.amazon.jsii.JsiiObject.InitializationMode.Jsii); + software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this, 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 java.lang.Number getArg1() { + return this.jsiiGet("arg1", java.lang.Number.class); + } + + public java.lang.String getArg2() { + return this.jsiiGet("arg2", java.lang.String.class); + } + + public java.time.Instant getArg3() { + return this.jsiiGet("arg3", java.time.Instant.class); + } +} diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/OptionalConstructorArgument.java b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/OptionalConstructorArgument.java new file mode 100644 index 0000000000..be783ac487 --- /dev/null +++ b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/OptionalConstructorArgument.java @@ -0,0 +1,30 @@ +package software.amazon.jsii.tests.calculator; + +@javax.annotation.Generated(value = "jsii-pacmak") +@software.amazon.jsii.Jsii(module = software.amazon.jsii.tests.calculator.$Module.class, fqn = "jsii-calc.OptionalConstructorArgument") +public class OptionalConstructorArgument extends software.amazon.jsii.JsiiObject { + protected OptionalConstructorArgument(final software.amazon.jsii.JsiiObject.InitializationMode mode) { + super(mode); + } + public OptionalConstructorArgument(final java.lang.Number arg1, final java.lang.String arg2, @javax.annotation.Nullable final java.time.Instant arg3) { + super(software.amazon.jsii.JsiiObject.InitializationMode.Jsii); + software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this, java.util.stream.Stream.concat(java.util.stream.Stream.concat(java.util.stream.Stream.of(java.util.Objects.requireNonNull(arg1, "arg1 is required")), java.util.stream.Stream.of(java.util.Objects.requireNonNull(arg2, "arg2 is required"))), java.util.stream.Stream.of(arg3)).toArray()); + } + public OptionalConstructorArgument(final java.lang.Number arg1, final java.lang.String arg2) { + super(software.amazon.jsii.JsiiObject.InitializationMode.Jsii); + software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this, java.util.stream.Stream.concat(java.util.stream.Stream.of(java.util.Objects.requireNonNull(arg1, "arg1 is required")), java.util.stream.Stream.of(java.util.Objects.requireNonNull(arg2, "arg2 is required"))).toArray()); + } + + public java.lang.Number getArg1() { + return this.jsiiGet("arg1", java.lang.Number.class); + } + + public java.lang.String getArg2() { + return this.jsiiGet("arg2", java.lang.String.class); + } + + @javax.annotation.Nullable + public java.time.Instant getArg3() { + return this.jsiiGet("arg3", java.time.Instant.class); + } +} 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 55c7e39c86..b48aeca4a9 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 @@ -11,6 +11,14 @@ public RuntimeTypeChecking() { software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this); } + public void methodWithDefaultedArguments(@javax.annotation.Nullable final java.lang.Number arg1, final java.lang.String arg2, @javax.annotation.Nullable final java.time.Instant arg3) { + this.jsiiCall("methodWithDefaultedArguments", Void.class, java.util.stream.Stream.concat(java.util.stream.Stream.concat(java.util.stream.Stream.of(arg1), java.util.stream.Stream.of(java.util.Objects.requireNonNull(arg2, "arg2 is required"))), java.util.stream.Stream.of(arg3)).toArray()); + } + + public void methodWithDefaultedArguments(@javax.annotation.Nullable final java.lang.Number arg1, final java.lang.String arg2) { + 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()); + } + /** * Used to verify verification of number of method arguments. */ 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 291eaeebfa..97a8d61620 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 @@ -831,6 +831,55 @@ CalculatorProps (interface) :type: number or undefined *(abstract)* +DefaultedConstructorArgument +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. py:class:: DefaultedConstructorArgument([arg1, arg2, [arg3]]) + + **Language-specific names:** + + .. tabs:: + + .. code-tab:: c# + + using Amazon.JSII.Tests.CalculatorNamespace; + + .. code-tab:: java + + import software.amazon.jsii.tests.calculator.DefaultedConstructorArgument; + + .. code-tab:: javascript + + const { DefaultedConstructorArgument } = require('jsii-calc'); + + .. code-tab:: typescript + + import { DefaultedConstructorArgument } from 'jsii-calc'; + + + + :param arg1: + :type arg1: number or undefined + :param arg2: + :type arg2: string + :param arg3: + :type arg3: date or undefined + + .. py:attribute:: arg1 + + :type: number *(readonly)* + + + .. py:attribute:: arg2 + + :type: string *(readonly)* + + + .. py:attribute:: arg3 + + :type: date *(readonly)* + + DerivedClassHasNoProperties ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -2172,6 +2221,55 @@ ObjectRefsInCollections :rtype: number +OptionalConstructorArgument +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. py:class:: OptionalConstructorArgument(arg1, arg2, [arg3]) + + **Language-specific names:** + + .. tabs:: + + .. code-tab:: c# + + using Amazon.JSII.Tests.CalculatorNamespace; + + .. code-tab:: java + + import software.amazon.jsii.tests.calculator.OptionalConstructorArgument; + + .. code-tab:: javascript + + const { OptionalConstructorArgument } = require('jsii-calc'); + + .. code-tab:: typescript + + import { OptionalConstructorArgument } from 'jsii-calc'; + + + + :param arg1: + :type arg1: number + :param arg2: + :type arg2: string + :param arg3: + :type arg3: date or undefined + + .. py:attribute:: arg1 + + :type: number *(readonly)* + + + .. py:attribute:: arg2 + + :type: string *(readonly)* + + + .. py:attribute:: arg3 + + :type: date or undefined *(readonly)* + + OverrideReturnsObject ^^^^^^^^^^^^^^^^^^^^^ @@ -2417,6 +2515,16 @@ RuntimeTypeChecking + .. py:method:: methodWithDefaultedArguments([arg1, arg2, [arg3]]) + + :param arg1: + :type arg1: number or undefined + :param arg2: + :type arg2: string + :param arg3: + :type arg3: date or undefined + + .. py:method:: methodWithOptionalArguments(arg1, arg2, [arg3]) Used to verify verification of number of method arguments. diff --git a/packages/jsii/lib/assembler.ts b/packages/jsii/lib/assembler.ts index e130fe0db6..1511018f80 100644 --- a/packages/jsii/lib/assembler.ts +++ b/packages/jsii/lib/assembler.ts @@ -618,14 +618,18 @@ export class Assembler implements Emitter { if (LOG.isTraceEnabled()) { LOG.trace(`Processing parameter: ${colors.cyan(paramSymbol.name)}`); } + const paramDeclaration = paramSymbol.valueDeclaration as ts.ParameterDeclaration; const parameter: spec.Parameter = { name: paramSymbol.name, type: await this._typeReference(this._typeChecker.getTypeAtLocation(paramSymbol.valueDeclaration), paramSymbol.valueDeclaration), - variadic: !!(paramSymbol.valueDeclaration as ts.ParameterDeclaration).dotDotDotToken + variadic: !!paramDeclaration.dotDotDotToken }; if (parameter.variadic) { parameter.type = (parameter.type as spec.CollectionTypeReference).collection.elementtype; } + if (paramDeclaration.initializer != null) { + parameter.type.optional = true; + } this._visitDocumentation(paramSymbol, parameter); return parameter; }