Skip to content

Commit 17403dc

Browse files
authored
fix(dotnet): Correctly generate "optional" markers (#466)
Method parameters were missing the "optional" marker due to the logic that was initially intended to prune only documentation from the JSON data attached to the method attribute.
1 parent c54f7d8 commit 17403dc

File tree

14 files changed

+49
-17
lines changed

14 files changed

+49
-17
lines changed

packages/jsii-dotnet-generator/src/Amazon.JSII.Generator.UnitTests/MethodExtensionsTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,38 @@ public void StripsDocs()
136136

137137
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
138138
}
139+
140+
[Fact(DisplayName = _Prefix + nameof(PreservesOptionality))]
141+
public void PreservesOptionality()
142+
{
143+
Method method = new Method
144+
(
145+
isProtected: false,
146+
isAbstract: false,
147+
name: "myMethod",
148+
parameters: new[]
149+
{
150+
new Parameter
151+
(
152+
name: "myParam1",
153+
type: new TypeReference("myParamTypeFqn1")
154+
),
155+
new Parameter
156+
(
157+
name: "myParam2",
158+
type: new TypeReference("myParamTypeFqn2"),
159+
isOptional: true
160+
)
161+
}
162+
);
163+
164+
SyntaxToken token = method.GetParametersJsonSyntaxToken();
165+
166+
string actual = token.ToString();
167+
string expected = @"""[{\""name\"":\""myParam1\"",\""type\"":{\""fqn\"":\""myParamTypeFqn1\""}},{\""name\"":\""myParam2\"",\""type\"":{\""fqn\"":\""myParamTypeFqn2\""},\""optional\"":true}]""";
168+
169+
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
170+
}
139171
}
140172
}
141173
}

packages/jsii-dotnet-generator/src/Amazon.JSII.Generator/CallableExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static SyntaxToken GetParametersJsonSyntaxToken(this Callable callable)
5151
{
5252
// Strip docs before serializing.
5353
Parameter[] parameters = (callable?.Parameters ?? Enumerable.Empty<Parameter>())
54-
.Select(p => new Parameter(p.Name, p.Type))
54+
.Select(p => new Parameter(name: p.Name, type: p.Type, isOptional: p.IsOptional, isVariadic: p.IsVariadic))
5555
.ToArray();
5656

5757
return SF.Literal(JsonConvert.SerializeObject(parameters, SerializerSettings));

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/Calculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Amazon.JSII.Tests.CalculatorNamespace
77
{
88
/// <summary>A calculator which maintains a current value and allows adding operations.</summary>
9-
[JsiiClass(nativeType: typeof(Calculator), fullyQualifiedName: "jsii-calc.Calculator", parametersJson: "[{\"name\":\"props\",\"type\":{\"fqn\":\"jsii-calc.CalculatorProps\"}}]")]
9+
[JsiiClass(nativeType: typeof(Calculator), fullyQualifiedName: "jsii-calc.Calculator", parametersJson: "[{\"name\":\"props\",\"type\":{\"fqn\":\"jsii-calc.CalculatorProps\"},\"optional\":true}]")]
1010
public class Calculator : CompositeOperation_
1111
{
1212
public Calculator(ICalculatorProps props): base(new DeputyProps(new object[]{props}))

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DefaultedConstructorArgument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Amazon.JSII.Tests.CalculatorNamespace
55
{
6-
[JsiiClass(nativeType: typeof(DefaultedConstructorArgument), fullyQualifiedName: "jsii-calc.DefaultedConstructorArgument", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\"}}]")]
6+
[JsiiClass(nativeType: typeof(DefaultedConstructorArgument), fullyQualifiedName: "jsii-calc.DefaultedConstructorArgument", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"},\"optional\":true},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"},\"optional\":true},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\"},\"optional\":true}]")]
77
public class DefaultedConstructorArgument : DeputyBase
88
{
99
public DefaultedConstructorArgument(double? arg1, string arg2, DateTime? arg3): base(new DeputyProps(new object[]{arg1, arg2, arg3}))

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DoNotRecognizeAnyAsOptional.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected DoNotRecognizeAnyAsOptional(DeputyProps props): base(props)
1818
{
1919
}
2020

21-
[JsiiMethod(name: "method", parametersJson: "[{\"name\":\"_requiredAny\",\"type\":{\"primitive\":\"any\"}},{\"name\":\"_optionalAny\",\"type\":{\"primitive\":\"any\"}},{\"name\":\"_optionalString\",\"type\":{\"primitive\":\"string\"}}]")]
21+
[JsiiMethod(name: "method", parametersJson: "[{\"name\":\"_requiredAny\",\"type\":{\"primitive\":\"any\"}},{\"name\":\"_optionalAny\",\"type\":{\"primitive\":\"any\"},\"optional\":true},{\"name\":\"_optionalString\",\"type\":{\"primitive\":\"string\"},\"optional\":true}]")]
2222
public virtual void Method(object _requiredAny, object _optionalAny, string _optionalString)
2323
{
2424
InvokeInstanceVoidMethod(new object[]{_requiredAny, _optionalAny, _optionalString});

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DocumentedClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected DocumentedClass(DeputyProps props): base(props)
3232
/// This will print out a friendly greeting intended for
3333
/// the indicated person.
3434
/// </remarks>
35-
[JsiiMethod(name: "greet", returnsJson: "{\"type\":{\"primitive\":\"number\"}}", parametersJson: "[{\"name\":\"greetee\",\"type\":{\"fqn\":\"jsii-calc.Greetee\"}}]")]
35+
[JsiiMethod(name: "greet", returnsJson: "{\"type\":{\"primitive\":\"number\"}}", parametersJson: "[{\"name\":\"greetee\",\"type\":{\"fqn\":\"jsii-calc.Greetee\"},\"optional\":true}]")]
3636
public virtual double Greet(IGreetee greetee)
3737
{
3838
return InvokeInstanceMethod<double>(new object[]{greetee});

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/DontComplainAboutVariadicAfterOptional.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected DontComplainAboutVariadicAfterOptional(DeputyProps props): base(props)
1717
{
1818
}
1919

20-
[JsiiMethod(name: "optionalAndVariadic", returnsJson: "{\"type\":{\"primitive\":\"string\"}}", parametersJson: "[{\"name\":\"optional\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"things\",\"type\":{\"primitive\":\"string\"}}]")]
20+
[JsiiMethod(name: "optionalAndVariadic", returnsJson: "{\"type\":{\"primitive\":\"string\"}}", parametersJson: "[{\"name\":\"optional\",\"type\":{\"primitive\":\"string\"},\"optional\":true},{\"name\":\"things\",\"variadic\":true,\"type\":{\"primitive\":\"string\"}}]")]
2121
public virtual string OptionalAndVariadic(string optional, string things)
2222
{
2323
return InvokeInstanceMethod<string>(new object[]{optional, things});

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/IIInterfaceWithOptionalMethodArguments.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Amazon.JSII.Tests.CalculatorNamespace
66
[JsiiInterface(nativeType: typeof(IIInterfaceWithOptionalMethodArguments), fullyQualifiedName: "jsii-calc.IInterfaceWithOptionalMethodArguments")]
77
public interface IIInterfaceWithOptionalMethodArguments
88
{
9-
[JsiiMethod(name: "hello", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"number\"}}]")]
9+
[JsiiMethod(name: "hello", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"number\"},\"optional\":true}]")]
1010
void Hello(string arg1, double? arg2);
1111
}
1212
}

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/IInterfaceWithOptionalMethodArgumentsProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private IInterfaceWithOptionalMethodArgumentsProxy(ByRefValue reference): base(r
1010
{
1111
}
1212

13-
[JsiiMethod(name: "hello", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"number\"}}]")]
13+
[JsiiMethod(name: "hello", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"number\"},\"optional\":true}]")]
1414
public void Hello(string arg1, double? arg2)
1515
{
1616
InvokeInstanceVoidMethod(new object[]{arg1, arg2});

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/NullShouldBeTreatedAsUndefined.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Amazon.JSII.Tests.CalculatorNamespace
44
{
55
/// <summary>jsii#282, aws-cdk#157: null should be treated as "undefined".</summary>
6-
[JsiiClass(nativeType: typeof(NullShouldBeTreatedAsUndefined), fullyQualifiedName: "jsii-calc.NullShouldBeTreatedAsUndefined", parametersJson: "[{\"name\":\"_param1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"optional\",\"type\":{\"primitive\":\"any\"}}]")]
6+
[JsiiClass(nativeType: typeof(NullShouldBeTreatedAsUndefined), fullyQualifiedName: "jsii-calc.NullShouldBeTreatedAsUndefined", parametersJson: "[{\"name\":\"_param1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"optional\",\"type\":{\"primitive\":\"any\"},\"optional\":true}]")]
77
public class NullShouldBeTreatedAsUndefined : DeputyBase
88
{
99
public NullShouldBeTreatedAsUndefined(string _param1, object optional): base(new DeputyProps(new object[]{_param1, optional}))
@@ -25,7 +25,7 @@ public virtual string ChangeMeToUndefined
2525
set => SetInstanceProperty(value);
2626
}
2727

28-
[JsiiMethod(name: "giveMeUndefined", parametersJson: "[{\"name\":\"value\",\"type\":{\"primitive\":\"any\"}}]")]
28+
[JsiiMethod(name: "giveMeUndefined", parametersJson: "[{\"name\":\"value\",\"type\":{\"primitive\":\"any\"},\"optional\":true}]")]
2929
public virtual void GiveMeUndefined(object value)
3030
{
3131
InvokeInstanceVoidMethod(new object[]{value});

0 commit comments

Comments
 (0)