Skip to content

Commit

Permalink
fix(jsii-dotnet-runtime): Proxy parameters should not throw exception.
Browse files Browse the repository at this point in the history
Proxy types when passed as parameters were throwing an exception in the dotnet runtime. Removing the check and throw did not have an
adverse effect on the runtime, so it was removed.

Compliance test added that passes an interface as a parameter.

Fixes aws#316
  • Loading branch information
costleya committed Nov 15, 2018
1 parent a55fe31 commit d324437
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 98 deletions.
276 changes: 205 additions & 71 deletions packages/jsii-calc/lib/compliance.ts

Large diffs are not rendered by default.

27 changes: 26 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,31 @@
}
]
},
"jsii-calc.GreetingAugmenter": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.GreetingAugmenter",
"initializer": {
"initializer": true
},
"kind": "class",
"methods": [
{
"name": "betterGreeting",
"parameters": [
{
"name": "friendly",
"type": {
"fqn": "@scope/jsii-calc-lib.IFriendly"
}
}
],
"returns": {
"primitive": "string"
}
}
],
"name": "GreetingAugmenter"
},
"jsii-calc.IFriendlier": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -3646,5 +3671,5 @@
}
},
"version": "0.7.10",
"fingerprint": "rxx0uqvwitnI8U0/tQLwhghDWxc85lS6Gghk4xDtIx0="
"fingerprint": "FD4+eWzYL86xxcqBEJh/D6O533Bbjwo/LHD4si6frmg="
}
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,18 @@ public void TestLiteralInterface()
Assert.Equal((double) 42, gen.Next());
}

[Fact(DisplayName = Prefix + nameof(TestInterfaceParameter))]
public void TestInterfaceParameter()
{
var obj = new JSObjectLiteralForInterface();
var friendly = obj.GiveMeFriendly();
Assert.Equal("I am literally friendly!", friendly.Hello());

var greetingAugmenter = new GreetingAugmenter();
var betterGreeting = greetingAugmenter.BetterGreeting(friendly);
Assert.Equal("I am literally friendly! Let me buy you a drink!", betterGreeting);
}

[Fact(DisplayName = Prefix + nameof(Structs_StepBuilders), Skip = "There is no fluent API for C#")]
public void Structs_StepBuilders()
{
Expand Down Expand Up @@ -842,7 +854,7 @@ public void NullShouldBeTreatedAsUndefined()
obj.GiveMeUndefinedInsideAnObject(new NullShouldBeTreatedAsUndefinedData
{
ThisShouldBeUndefined = null,
ArrayWithThreeElementsAndUndefinedAsSecondArgument = new[] { "hello", null, "world" }
ArrayWithThreeElementsAndUndefinedAsSecondArgument = new[] {"hello", null, "world"}
});

// property
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Amazon.JSII.JsonModel.Spec;
using Amazon.JSII.Runtime.Deputy;
using Newtonsoft.Json.Linq;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Amazon.JSII.JsonModel.Spec;
using Amazon.JSII.Runtime.Deputy;
using Newtonsoft.Json.Linq;
using Type = System.Type;

namespace Amazon.JSII.Runtime.Services.Converters
{
Expand Down Expand Up @@ -33,9 +34,9 @@ protected override bool TryConvertClass(IReferenceMap referenceMap, object value
return true;
}

System.Type type = value.GetType();
Type type = value.GetType();

if (type.GetCustomAttribute<JsiiTypeProxyAttribute>() != null)
/*if (type.GetCustomAttribute<JsiiTypeProxyAttribute>() != null)
{
throw new ArgumentException
(
Expand All @@ -44,7 +45,7 @@ protected override bool TryConvertClass(IReferenceMap referenceMap, object value
"Instead, use a concrete type that implements the interface.",
nameof(value)
);
}
}*/

if (value is DeputyBase deputyValue)
{
Expand All @@ -56,15 +57,16 @@ protected override bool TryConvertClass(IReferenceMap referenceMap, object value
return false;
}

protected override bool TryConvertEnum(object value, bool isOptional, string fullyQualifiedName, out object result)
protected override bool TryConvertEnum(object value, bool isOptional, string fullyQualifiedName,
out object result)
{
if (value == null)
{
result = null;
return isOptional;
}

System.Type valueType = value.GetType();
Type valueType = value.GetType();
JsiiEnumAttribute attribute = value.GetType().GetCustomAttribute<JsiiEnumAttribute>();

if (attribute == null || attribute.FullyQualifiedName != fullyQualifiedName)
Expand Down Expand Up @@ -121,7 +123,7 @@ protected override bool TryConvertDate(object value, bool isOptional, out object

if (value.GetType().IsAssignableFrom(typeof(DateTime)))
{
result = new DateValue((DateTime)value);
result = new DateValue((DateTime) value);
return true;
}

Expand Down Expand Up @@ -175,15 +177,16 @@ protected override bool TryConvertString(object value, out object result)

if (value.GetType().IsAssignableFrom(typeof(string)))
{
result = (string)value;
result = (string) value;
return true;
}

result = null;
return false;
}

protected override bool TryConvertArray(IReferenceMap referenceMap, TypeReference elementType, object value, out object result)
protected override bool TryConvertArray(IReferenceMap referenceMap, TypeReference elementType, object value,
out object result)
{
if (value == null)
{
Expand All @@ -197,7 +200,7 @@ protected override bool TryConvertArray(IReferenceMap referenceMap, TypeReferenc
return false;
}

Array array = (Array)value;
Array array = (Array) value;

JArray resultArray = new JArray();
foreach (object element in array)
Expand All @@ -220,41 +223,45 @@ protected override bool TryConvertArray(IReferenceMap referenceMap, TypeReferenc
return true;
}

protected override bool TryConvertMap(IReferenceMap referenceMap, TypeReference elementType, object value, out object result)
protected override bool TryConvertMap(IReferenceMap referenceMap, TypeReference elementType, object value,
out object result)
{
if (value == null)
{
result = null;
return true;
}

System.Type valueType = value.GetType();
System.Type dictionaryInterface = valueType.GetInterfaces()
Type valueType = value.GetType();
Type dictionaryInterface = valueType.GetInterfaces()
.FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>));

if (dictionaryInterface == null || !dictionaryInterface.GetGenericArguments()[0].IsAssignableFrom(typeof(string)))
if (dictionaryInterface == null ||
!dictionaryInterface.GetGenericArguments()[0].IsAssignableFrom(typeof(string)))
{
result = null;
return false;
}

IEnumerable<string> keys = (IEnumerable<string>)valueType.GetProperty("Keys").GetValue(value);
IEnumerable<string> keys = (IEnumerable<string>) valueType.GetProperty("Keys").GetValue(value);
PropertyInfo indexer = ReflectionUtils.GetIndexer(valueType);

JObject resultObject = new JObject();
foreach (string key in keys)
{
object element = indexer.GetValue(value, new object[] { key });
object element = indexer.GetValue(value, new object[] {key});
if (!TryConvert(elementType, referenceMap, element, out object convertedElement))
{
result = null;
return false;
}

if (convertedElement != null && !(convertedElement is String) && !convertedElement.GetType().IsPrimitive)
if (convertedElement != null && !(convertedElement is String) &&
!convertedElement.GetType().IsPrimitive)
{
convertedElement = JObject.FromObject(convertedElement);
}

resultObject.Add(new JProperty(key, convertedElement));
}

Expand All @@ -269,7 +276,7 @@ protected override TypeReference InferType(IReferenceMap referenceMap, object va
return InferType(referenceMap, value.GetType());
}

TypeReference InferType(IReferenceMap referenceMap, System.Type type)
TypeReference InferType(IReferenceMap referenceMap, Type type)
{
type = type ?? throw new ArgumentNullException(nameof(type));

Expand Down Expand Up @@ -322,7 +329,7 @@ TypeReference InferType(IReferenceMap referenceMap, System.Type type)
);
}

System.Type dictionaryInterface = type.GetInterfaces()
Type dictionaryInterface = type.GetInterfaces()
.FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>));
if (dictionaryInterface != null)
{
Expand All @@ -331,7 +338,7 @@ TypeReference InferType(IReferenceMap referenceMap, System.Type type)
throw new ArgumentException("All dictionaries must have string keys", nameof(type));
}

System.Type elementType = dictionaryInterface.GetGenericArguments()[1];
Type elementType = dictionaryInterface.GetGenericArguments()[1];
return new TypeReference
(
collection: new CollectionTypeReference
Expand All @@ -345,4 +352,4 @@ TypeReference InferType(IReferenceMap referenceMap, System.Type type)
throw new ArgumentException($"Could not infer JSII type for .NET type '{type.Name}'", nameof(type));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import software.amazon.jsii.tests.calculator.DoNotOverridePrivates;
import software.amazon.jsii.tests.calculator.DoubleTrouble;
import software.amazon.jsii.tests.calculator.GiveMeStructs;
import software.amazon.jsii.tests.calculator.GreetingAugmenter;
import software.amazon.jsii.tests.calculator.IFriendlier;
import software.amazon.jsii.tests.calculator.IFriendlyRandomGenerator;
import software.amazon.jsii.tests.calculator.InterfaceWithProperties;
Expand Down Expand Up @@ -705,6 +706,17 @@ public void testLiteralInterface() {
assertEquals(42, gen.next());
}

@Test
public void testInterfaceParameter() {
JSObjectLiteralForInterface obj = new JSObjectLiteralForInterface();
IFriendly friendly = obj.giveMeFriendly();
assertEquals("I am literally friendly!", friendly.hello());

GreetingAugmenter greetingAugmenter = new GreetingAugmenter();
String betterGreeting = greetingAugmenter.betterGreeting(friendly);
assertEquals("I am literally friendly! Let me buy you a drink!", betterGreeting);
}

@Test
public void structs_stepBuilders() {
Instant someInstant = Instant.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,31 @@
}
]
},
"jsii-calc.GreetingAugmenter": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.GreetingAugmenter",
"initializer": {
"initializer": true
},
"kind": "class",
"methods": [
{
"name": "betterGreeting",
"parameters": [
{
"name": "friendly",
"type": {
"fqn": "@scope/jsii-calc-lib.IFriendly"
}
}
],
"returns": {
"primitive": "string"
}
}
],
"name": "GreetingAugmenter"
},
"jsii-calc.IFriendlier": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -3646,5 +3671,5 @@
}
},
"version": "0.7.10",
"fingerprint": "rxx0uqvwitnI8U0/tQLwhghDWxc85lS6Gghk4xDtIx0="
"fingerprint": "FD4+eWzYL86xxcqBEJh/D6O533Bbjwo/LHD4si6frmg="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Amazon.JSII.Runtime.Deputy;
using Amazon.JSII.Tests.CalculatorNamespace.LibNamespace;

namespace Amazon.JSII.Tests.CalculatorNamespace
{
[JsiiClass(typeof(GreetingAugmenter), "jsii-calc.GreetingAugmenter", "[]")]
public class GreetingAugmenter : DeputyBase
{
public GreetingAugmenter(): base(new DeputyProps(new object[]{}))
{
}

protected GreetingAugmenter(ByRefValue reference): base(reference)
{
}

protected GreetingAugmenter(DeputyProps props): base(props)
{
}

[JsiiMethod("betterGreeting", "{\"primitive\":\"string\"}", "[{\"name\":\"friendly\",\"type\":{\"fqn\":\"@scope/jsii-calc-lib.IFriendly\"}}]")]
public virtual string BetterGreeting(IIFriendly friendly)
{
return InvokeInstanceMethod<string>(new object[]{friendly});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected Class<?> resolveClass(final String fqn) throws ClassNotFoundException
case "jsii-calc.DontComplainAboutVariadicAfterOptional": return software.amazon.jsii.tests.calculator.DontComplainAboutVariadicAfterOptional.class;
case "jsii-calc.DoubleTrouble": return software.amazon.jsii.tests.calculator.DoubleTrouble.class;
case "jsii-calc.GiveMeStructs": return software.amazon.jsii.tests.calculator.GiveMeStructs.class;
case "jsii-calc.GreetingAugmenter": return software.amazon.jsii.tests.calculator.GreetingAugmenter.class;
case "jsii-calc.IFriendlier": return software.amazon.jsii.tests.calculator.IFriendlier.class;
case "jsii-calc.IFriendlyRandomGenerator": return software.amazon.jsii.tests.calculator.IFriendlyRandomGenerator.class;
case "jsii-calc.IInterfaceThatShouldNotBeADataType": return software.amazon.jsii.tests.calculator.IInterfaceThatShouldNotBeADataType.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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.GreetingAugmenter")
public class GreetingAugmenter extends software.amazon.jsii.JsiiObject {
protected GreetingAugmenter(final software.amazon.jsii.JsiiObject.InitializationMode mode) {
super(mode);
}
public GreetingAugmenter() {
super(software.amazon.jsii.JsiiObject.InitializationMode.Jsii);
software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this);
}

public java.lang.String betterGreeting(final software.amazon.jsii.tests.calculator.lib.IFriendly friendly) {
return this.jsiiCall("betterGreeting", java.lang.String.class, java.util.stream.Stream.of(java.util.Objects.requireNonNull(friendly, "friendly is required")).toArray());
}
}
Loading

0 comments on commit d324437

Please sign in to comment.