Skip to content

Commit

Permalink
fix(dotnet): Correctly generate "optional" markers (#466)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RomainMuller authored Apr 17, 2019
1 parent c54f7d8 commit 17403dc
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,38 @@ public void StripsDocs()

Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = _Prefix + nameof(PreservesOptionality))]
public void PreservesOptionality()
{
Method method = new Method
(
isProtected: false,
isAbstract: false,
name: "myMethod",
parameters: new[]
{
new Parameter
(
name: "myParam1",
type: new TypeReference("myParamTypeFqn1")
),
new Parameter
(
name: "myParam2",
type: new TypeReference("myParamTypeFqn2"),
isOptional: true
)
}
);

SyntaxToken token = method.GetParametersJsonSyntaxToken();

string actual = token.ToString();
string expected = @"""[{\""name\"":\""myParam1\"",\""type\"":{\""fqn\"":\""myParamTypeFqn1\""}},{\""name\"":\""myParam2\"",\""type\"":{\""fqn\"":\""myParamTypeFqn2\""},\""optional\"":true}]""";

Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static SyntaxToken GetParametersJsonSyntaxToken(this Callable callable)
{
// Strip docs before serializing.
Parameter[] parameters = (callable?.Parameters ?? Enumerable.Empty<Parameter>())
.Select(p => new Parameter(p.Name, p.Type))
.Select(p => new Parameter(name: p.Name, type: p.Type, isOptional: p.IsOptional, isVariadic: p.IsVariadic))
.ToArray();

return SF.Literal(JsonConvert.SerializeObject(parameters, SerializerSettings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Amazon.JSII.Tests.CalculatorNamespace
{
/// <summary>A calculator which maintains a current value and allows adding operations.</summary>
[JsiiClass(nativeType: typeof(Calculator), fullyQualifiedName: "jsii-calc.Calculator", parametersJson: "[{\"name\":\"props\",\"type\":{\"fqn\":\"jsii-calc.CalculatorProps\"}}]")]
[JsiiClass(nativeType: typeof(Calculator), fullyQualifiedName: "jsii-calc.Calculator", parametersJson: "[{\"name\":\"props\",\"type\":{\"fqn\":\"jsii-calc.CalculatorProps\"},\"optional\":true}]")]
public class Calculator : CompositeOperation_
{
public Calculator(ICalculatorProps props): base(new DeputyProps(new object[]{props}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Amazon.JSII.Tests.CalculatorNamespace
{
[JsiiClass(nativeType: typeof(DefaultedConstructorArgument), fullyQualifiedName: "jsii-calc.DefaultedConstructorArgument", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\"}}]")]
[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}]")]
public class DefaultedConstructorArgument : DeputyBase
{
public DefaultedConstructorArgument(double? arg1, string arg2, DateTime? arg3): base(new DeputyProps(new object[]{arg1, arg2, arg3}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected DoNotRecognizeAnyAsOptional(DeputyProps props): base(props)
{
}

[JsiiMethod(name: "method", parametersJson: "[{\"name\":\"_requiredAny\",\"type\":{\"primitive\":\"any\"}},{\"name\":\"_optionalAny\",\"type\":{\"primitive\":\"any\"}},{\"name\":\"_optionalString\",\"type\":{\"primitive\":\"string\"}}]")]
[JsiiMethod(name: "method", parametersJson: "[{\"name\":\"_requiredAny\",\"type\":{\"primitive\":\"any\"}},{\"name\":\"_optionalAny\",\"type\":{\"primitive\":\"any\"},\"optional\":true},{\"name\":\"_optionalString\",\"type\":{\"primitive\":\"string\"},\"optional\":true}]")]
public virtual void Method(object _requiredAny, object _optionalAny, string _optionalString)
{
InvokeInstanceVoidMethod(new object[]{_requiredAny, _optionalAny, _optionalString});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected DocumentedClass(DeputyProps props): base(props)
/// This will print out a friendly greeting intended for
/// the indicated person.
/// </remarks>
[JsiiMethod(name: "greet", returnsJson: "{\"type\":{\"primitive\":\"number\"}}", parametersJson: "[{\"name\":\"greetee\",\"type\":{\"fqn\":\"jsii-calc.Greetee\"}}]")]
[JsiiMethod(name: "greet", returnsJson: "{\"type\":{\"primitive\":\"number\"}}", parametersJson: "[{\"name\":\"greetee\",\"type\":{\"fqn\":\"jsii-calc.Greetee\"},\"optional\":true}]")]
public virtual double Greet(IGreetee greetee)
{
return InvokeInstanceMethod<double>(new object[]{greetee});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected DontComplainAboutVariadicAfterOptional(DeputyProps props): base(props)
{
}

[JsiiMethod(name: "optionalAndVariadic", returnsJson: "{\"type\":{\"primitive\":\"string\"}}", parametersJson: "[{\"name\":\"optional\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"things\",\"type\":{\"primitive\":\"string\"}}]")]
[JsiiMethod(name: "optionalAndVariadic", returnsJson: "{\"type\":{\"primitive\":\"string\"}}", parametersJson: "[{\"name\":\"optional\",\"type\":{\"primitive\":\"string\"},\"optional\":true},{\"name\":\"things\",\"variadic\":true,\"type\":{\"primitive\":\"string\"}}]")]
public virtual string OptionalAndVariadic(string optional, string things)
{
return InvokeInstanceMethod<string>(new object[]{optional, things});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Amazon.JSII.Tests.CalculatorNamespace
[JsiiInterface(nativeType: typeof(IIInterfaceWithOptionalMethodArguments), fullyQualifiedName: "jsii-calc.IInterfaceWithOptionalMethodArguments")]
public interface IIInterfaceWithOptionalMethodArguments
{
[JsiiMethod(name: "hello", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"number\"}}]")]
[JsiiMethod(name: "hello", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"number\"},\"optional\":true}]")]
void Hello(string arg1, double? arg2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private IInterfaceWithOptionalMethodArgumentsProxy(ByRefValue reference): base(r
{
}

[JsiiMethod(name: "hello", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"number\"}}]")]
[JsiiMethod(name: "hello", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"number\"},\"optional\":true}]")]
public void Hello(string arg1, double? arg2)
{
InvokeInstanceVoidMethod(new object[]{arg1, arg2});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Amazon.JSII.Tests.CalculatorNamespace
{
/// <summary>jsii#282, aws-cdk#157: null should be treated as "undefined".</summary>
[JsiiClass(nativeType: typeof(NullShouldBeTreatedAsUndefined), fullyQualifiedName: "jsii-calc.NullShouldBeTreatedAsUndefined", parametersJson: "[{\"name\":\"_param1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"optional\",\"type\":{\"primitive\":\"any\"}}]")]
[JsiiClass(nativeType: typeof(NullShouldBeTreatedAsUndefined), fullyQualifiedName: "jsii-calc.NullShouldBeTreatedAsUndefined", parametersJson: "[{\"name\":\"_param1\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"optional\",\"type\":{\"primitive\":\"any\"},\"optional\":true}]")]
public class NullShouldBeTreatedAsUndefined : DeputyBase
{
public NullShouldBeTreatedAsUndefined(string _param1, object optional): base(new DeputyProps(new object[]{_param1, optional}))
Expand All @@ -25,7 +25,7 @@ public virtual string ChangeMeToUndefined
set => SetInstanceProperty(value);
}

[JsiiMethod(name: "giveMeUndefined", parametersJson: "[{\"name\":\"value\",\"type\":{\"primitive\":\"any\"}}]")]
[JsiiMethod(name: "giveMeUndefined", parametersJson: "[{\"name\":\"value\",\"type\":{\"primitive\":\"any\"},\"optional\":true}]")]
public virtual void GiveMeUndefined(object value)
{
InvokeInstanceVoidMethod(new object[]{value});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Amazon.JSII.Tests.CalculatorNamespace
{
[JsiiClass(nativeType: typeof(OptionalConstructorArgument), fullyQualifiedName: "jsii-calc.OptionalConstructorArgument", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\"}}]")]
[JsiiClass(nativeType: typeof(OptionalConstructorArgument), fullyQualifiedName: "jsii-calc.OptionalConstructorArgument", parametersJson: "[{\"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}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Amazon.JSII.Tests.CalculatorNamespace
{
[JsiiClass(nativeType: typeof(OptionalStructConsumer), fullyQualifiedName: "jsii-calc.OptionalStructConsumer", parametersJson: "[{\"name\":\"optionalStruct\",\"type\":{\"fqn\":\"jsii-calc.OptionalStruct\"}}]")]
[JsiiClass(nativeType: typeof(OptionalStructConsumer), fullyQualifiedName: "jsii-calc.OptionalStructConsumer", parametersJson: "[{\"name\":\"optionalStruct\",\"type\":{\"fqn\":\"jsii-calc.OptionalStruct\"},\"optional\":true}]")]
public class OptionalStructConsumer : DeputyBase
{
public OptionalStructConsumer(IOptionalStruct optionalStruct): base(new DeputyProps(new object[]{optionalStruct}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ protected RuntimeTypeChecking(DeputyProps props): base(props)
{
}

[JsiiMethod(name: "methodWithDefaultedArguments", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\"}}]")]
[JsiiMethod(name: "methodWithDefaultedArguments", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"},\"optional\":true},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"},\"optional\":true},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\"},\"optional\":true}]")]
public virtual void MethodWithDefaultedArguments(double? arg1, string arg2, DateTime? arg3)
{
InvokeInstanceVoidMethod(new object[]{arg1, arg2, arg3});
}

[JsiiMethod(name: "methodWithOptionalAnyArgument", parametersJson: "[{\"name\":\"arg\",\"type\":{\"primitive\":\"any\"}}]")]
[JsiiMethod(name: "methodWithOptionalAnyArgument", parametersJson: "[{\"name\":\"arg\",\"type\":{\"primitive\":\"any\"},\"optional\":true}]")]
public virtual void MethodWithOptionalAnyArgument(object arg)
{
InvokeInstanceVoidMethod(new object[]{arg});
}

/// <summary>Used to verify verification of number of method arguments.</summary>
[JsiiMethod(name: "methodWithOptionalArguments", parametersJson: "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\"}}]")]
[JsiiMethod(name: "methodWithOptionalArguments", parametersJson: "[{\"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)
{
InvokeInstanceVoidMethod(new object[]{arg1, arg2, arg3});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Amazon.JSII.Tests.CalculatorNamespace
{
[JsiiClass(nativeType: typeof(VariadicMethod), fullyQualifiedName: "jsii-calc.VariadicMethod", parametersJson: "[{\"name\":\"prefix\",\"type\":{\"primitive\":\"number\"}}]")]
[JsiiClass(nativeType: typeof(VariadicMethod), fullyQualifiedName: "jsii-calc.VariadicMethod", parametersJson: "[{\"name\":\"prefix\",\"variadic\":true,\"type\":{\"primitive\":\"number\"}}]")]
public class VariadicMethod : DeputyBase
{
public VariadicMethod(double prefix): base(new DeputyProps(new object[]{prefix}))
Expand All @@ -19,7 +19,7 @@ protected VariadicMethod(DeputyProps props): base(props)

/// <param name = "first">the first element of the array to be returned (after the `prefix` provided at construction time).</param>
/// <param name = "others">other elements to be included in the array.</param>
[JsiiMethod(name: "asArray", returnsJson: "{\"type\":{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"number\"}}}}", parametersJson: "[{\"name\":\"first\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"others\",\"type\":{\"primitive\":\"number\"}}]")]
[JsiiMethod(name: "asArray", returnsJson: "{\"type\":{\"collection\":{\"kind\":\"array\",\"elementtype\":{\"primitive\":\"number\"}}}}", parametersJson: "[{\"name\":\"first\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"others\",\"variadic\":true,\"type\":{\"primitive\":\"number\"}}]")]
public virtual double[] AsArray(double first, double others)
{
return InvokeInstanceMethod<double[]>(new object[]{first, others});
Expand Down

0 comments on commit 17403dc

Please sign in to comment.