Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(java): remove Jackson confusion with certain patterns #987

Merged
merged 2 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2266,3 +2266,18 @@ export class Demonstrate982 {

public constructor() { }
}

/**
* This tries to confuse Jackson by having overloaded property setters.
*
* @see https://github.com/aws/aws-cdk/issues/4080
*/
export class ConfusingToJackson {
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
public static makeInstance(): ConfusingToJackson {
return new ConfusingToJackson();
}

public unionProperty?: Array<IFriendly | AbstractClass> | IFriendly;

private constructor() { }
}
74 changes: 73 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,78 @@
}
]
},
"jsii-calc.ConfusingToJackson": {
"assembly": "jsii-calc",
"docs": {
"see": "https://github.com/aws/aws-cdk/issues/4080",
"stability": "experimental",
"summary": "This tries to confuse Jackson by having overloaded property setters."
},
"fqn": "jsii-calc.ConfusingToJackson",
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2275
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2276
},
"name": "makeInstance",
"returns": {
"type": {
"fqn": "jsii-calc.ConfusingToJackson"
}
},
"static": true
}
],
"name": "ConfusingToJackson",
"properties": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2280
},
"name": "unionProperty",
"optional": true,
"type": {
"union": {
"types": [
{
"fqn": "@scope/jsii-calc-lib.IFriendly"
},
{
"collection": {
"elementtype": {
"union": {
"types": [
{
"fqn": "@scope/jsii-calc-lib.IFriendly"
},
{
"fqn": "jsii-calc.AbstractClass"
}
]
}
},
"kind": "array"
}
}
]
}
}
}
]
},
"jsii-calc.ConstructorPassesThisOut": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -11128,5 +11200,5 @@
}
},
"version": "0.20.5",
"fingerprint": "/MRTbTnRC1UWxsPIrca+9Yo1IBKsEueT75P22pQoV1o="
"fingerprint": "kNXJGfAFmb/4Oq+2ojg9+ZxZhpMdb/y55go8OZWy/kE="
}
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ public void VariadicCallbacksAreHandledCorrectly()
}

[Fact(DisplayName = Prefix + nameof(ReturnSubclassThatImplementsInterface976))]
public void ReturnSubclassThatImplementsInterface976()
public void ReturnSubclassThatImplementsInterface976()
{
var obj = SomeTypeJsii976.ReturnReturn();
Assert.Equal(obj.Foo, 333);
Expand Down Expand Up @@ -1265,5 +1265,11 @@ public void StructsCanBeDowncastedToParentType()
Assert.NotNull(Demonstrate982.TakeThis());
Assert.NotNull(Demonstrate982.TakeThisToo());
}

[Fact(DisplayName = Prefix + nameof(CanObtainReferenceWithOverloadedSetters))]
public void CanObtainReferenceWithOverloadedSetters()
{
Assert.NotNull(ConfusingToJackson.MakeInstance());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1553,4 +1553,9 @@ public java.lang.Number next() {
return next;
}
}

@Test
public void canObtainReferenceWithOverloadedSetter() {
assertNotNull(ConfusingToJackson.makeInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,22 @@ public static <T> T treeToValue(final JsonNode tree, final Class<T> valueType) {
return null;
}
try {
final T result = INSTANCE.treeToValue(tree, valueType);
// If the needed type is a sub-class of JsiiObject, we'll be receiving it by-reference, so we can ask Jackson to
// de-serialize a JsiiObject instead of the actual type; and we'll still get the correct instance type. This
// avoids running into problems because of Jackson not liking the structure of a particular class (it will
// validate that before attempting any deserialization operation, and I don't know how to mute this behavior).
final Class<?> deserType = JsiiObject.class.isAssignableFrom(valueType)
? JsiiObject.class
: valueType;
final Object result = INSTANCE.treeToValue(tree, deserType);
if (result != null && valueType.isInterface() && result instanceof JsiiObject) {
// The result type does not implement the interface, returning the proxy instead!
if (!valueType.isAssignableFrom(result.getClass()) && valueType.isAnnotationPresent(Jsii.Proxy.class)) {
final Jsii.Proxy proxyAnnotation = valueType.getAnnotation(Jsii.Proxy.class);
return (T)((JsiiObject) result).asInterfaceProxy(proxyAnnotation.value());
}
}
return result;
return (T)result;
} catch (final JsonProcessingException jpe) {
throw new JsiiException(jpe);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,78 @@
}
]
},
"jsii-calc.ConfusingToJackson": {
"assembly": "jsii-calc",
"docs": {
"see": "https://github.com/aws/aws-cdk/issues/4080",
"stability": "experimental",
"summary": "This tries to confuse Jackson by having overloaded property setters."
},
"fqn": "jsii-calc.ConfusingToJackson",
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2275
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2276
},
"name": "makeInstance",
"returns": {
"type": {
"fqn": "jsii-calc.ConfusingToJackson"
}
},
"static": true
}
],
"name": "ConfusingToJackson",
"properties": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2280
},
"name": "unionProperty",
"optional": true,
"type": {
"union": {
"types": [
{
"fqn": "@scope/jsii-calc-lib.IFriendly"
},
{
"collection": {
"elementtype": {
"union": {
"types": [
{
"fqn": "@scope/jsii-calc-lib.IFriendly"
},
{
"fqn": "jsii-calc.AbstractClass"
}
]
}
},
"kind": "array"
}
}
]
}
}
}
]
},
"jsii-calc.ConstructorPassesThisOut": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -11128,5 +11200,5 @@
}
},
"version": "0.20.5",
"fingerprint": "/MRTbTnRC1UWxsPIrca+9Yo1IBKsEueT75P22pQoV1o="
"fingerprint": "kNXJGfAFmb/4Oq+2ojg9+ZxZhpMdb/y55go8OZWy/kE="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Amazon.JSII.Runtime.Deputy;

namespace Amazon.JSII.Tests.CalculatorNamespace
{
/// <summary>This tries to confuse Jackson by having overloaded property setters.</summary>
/// <remarks>
/// stability: Experimental
/// see:
/// https://github.com/aws/aws-cdk/issues/4080
/// </remarks>
[JsiiClass(nativeType: typeof(Amazon.JSII.Tests.CalculatorNamespace.ConfusingToJackson), fullyQualifiedName: "jsii-calc.ConfusingToJackson")]
public class ConfusingToJackson : DeputyBase
{
protected ConfusingToJackson(ByRefValue reference): base(reference)
{
}

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

/// <remarks>
/// stability: Experimental
/// </remarks>
[JsiiMethod(name: "makeInstance", returnsJson: "{\"type\":{\"fqn\":\"jsii-calc.ConfusingToJackson\"}}")]
public static Amazon.JSII.Tests.CalculatorNamespace.ConfusingToJackson MakeInstance()
{
return InvokeStaticMethod<Amazon.JSII.Tests.CalculatorNamespace.ConfusingToJackson>(typeof(Amazon.JSII.Tests.CalculatorNamespace.ConfusingToJackson), new System.Type[]{}, new object[]{});
}

/// <remarks>
/// stability: Experimental
/// </remarks>
[JsiiOptional]
[JsiiProperty(name: "unionProperty", typeJson: "{\"union\":{\"types\":[{\"fqn\":\"@scope/jsii-calc-lib.IFriendly\"},{\"collection\":{\"elementtype\":{\"union\":{\"types\":[{\"fqn\":\"@scope/jsii-calc-lib.IFriendly\"},{\"fqn\":\"jsii-calc.AbstractClass\"}]}},\"kind\":\"array\"}}]}}", isOptional: true)]
public virtual object UnionProperty
{
get => GetInstanceProperty<object>();
set => SetInstanceProperty(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected Class<?> resolveClass(final String fqn) throws ClassNotFoundException
case "jsii-calc.ClassWithJavaReservedWords": return software.amazon.jsii.tests.calculator.ClassWithJavaReservedWords.class;
case "jsii-calc.ClassWithMutableObjectLiteralProperty": return software.amazon.jsii.tests.calculator.ClassWithMutableObjectLiteralProperty.class;
case "jsii-calc.ClassWithPrivateConstructorAndAutomaticProperties": return software.amazon.jsii.tests.calculator.ClassWithPrivateConstructorAndAutomaticProperties.class;
case "jsii-calc.ConfusingToJackson": return software.amazon.jsii.tests.calculator.ConfusingToJackson.class;
case "jsii-calc.ConstructorPassesThisOut": return software.amazon.jsii.tests.calculator.ConstructorPassesThisOut.class;
case "jsii-calc.Constructors": return software.amazon.jsii.tests.calculator.Constructors.class;
case "jsii-calc.ConsumerCanRingBell": return software.amazon.jsii.tests.calculator.ConsumerCanRingBell.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package software.amazon.jsii.tests.calculator;

/**
* This tries to confuse Jackson by having overloaded property setters.
*
* EXPERIMENTAL
*
* @see https://github.com/aws/aws-cdk/issues/4080
*/
@javax.annotation.Generated(value = "jsii-pacmak")
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
@software.amazon.jsii.Jsii(module = software.amazon.jsii.tests.calculator.$Module.class, fqn = "jsii-calc.ConfusingToJackson")
public class ConfusingToJackson extends software.amazon.jsii.JsiiObject {

protected ConfusingToJackson(final software.amazon.jsii.JsiiObjectRef objRef) {
super(objRef);
}

protected ConfusingToJackson(final software.amazon.jsii.JsiiObject.InitializationMode initializationMode) {
super(initializationMode);
}

/**
* EXPERIMENTAL
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
public static software.amazon.jsii.tests.calculator.ConfusingToJackson makeInstance() {
return software.amazon.jsii.JsiiObject.jsiiStaticCall(software.amazon.jsii.tests.calculator.ConfusingToJackson.class, "makeInstance", software.amazon.jsii.tests.calculator.ConfusingToJackson.class);
}

/**
* EXPERIMENTAL
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
public java.lang.Object getUnionProperty() {
return this.jsiiGet("unionProperty", java.lang.Object.class);
}

/**
* EXPERIMENTAL
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
public void setUnionProperty(final software.amazon.jsii.tests.calculator.lib.IFriendly value) {
this.jsiiSet("unionProperty", value);
}

/**
* EXPERIMENTAL
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
public void setUnionProperty(final java.util.List<java.lang.Object> value) {
this.jsiiSet("unionProperty", value);
}
}
Loading