Skip to content

Commit

Permalink
fix(python): correctly handle interface declarations on returned obje…
Browse files Browse the repository at this point in the history
…cts (#980)

When the returned type had a known base type, the interfaces tagged on the object
reference tag. This addresses this problem.
  • Loading branch information
RomainMuller authored Nov 13, 2019
1 parent 948000d commit c2de100
Show file tree
Hide file tree
Showing 18 changed files with 608 additions and 22 deletions.
28 changes: 28 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2208,3 +2208,31 @@ export class RootStructValidator {

private constructor() { }
}

/**
* Returns a subclass of a known class which implements an interface.
*/
export interface IReturnJsii976 {
readonly foo: number;
}

export class BaseJsii976 { }

export class SomeTypeJsii976 {

static returnReturn(): IReturnJsii976 {
class Derived extends BaseJsii976 implements IReturnJsii976 {
public readonly foo = 333
}

return new Derived();
}

static returnAnonymous(): any {
class Derived implements IReturnJsii976 {
public readonly foo = 1337;
}

return new Derived();
}
}
95 changes: 94 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,20 @@
],
"name": "AugmentableClass"
},
"jsii-calc.BaseJsii976": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.BaseJsii976",
"initializer": {},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2219
},
"name": "BaseJsii976"
},
"jsii-calc.Bell": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -5140,6 +5154,37 @@
],
"name": "IRandomNumberGenerator"
},
"jsii-calc.IReturnJsii976": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental",
"summary": "Returns a subclass of a known class which implements an interface."
},
"fqn": "jsii-calc.IReturnJsii976",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2215
},
"name": "IReturnJsii976",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2216
},
"name": "foo",
"type": {
"primitive": "number"
}
}
]
},
"jsii-calc.IReturnsNumber": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -8654,6 +8699,54 @@
],
"name": "SingletonStringEnum"
},
"jsii-calc.SomeTypeJsii976": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.SomeTypeJsii976",
"initializer": {},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2221
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2231
},
"name": "returnAnonymous",
"returns": {
"type": {
"primitive": "any"
}
},
"static": true
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2223
},
"name": "returnReturn",
"returns": {
"type": {
"fqn": "jsii-calc.IReturnJsii976"
}
},
"static": true
}
],
"name": "SomeTypeJsii976"
},
"jsii-calc.StableClass": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -10913,5 +11006,5 @@
}
},
"version": "0.20.3",
"fingerprint": "1F+uskR3++T5mjRcWge9oG3H/jJvXm1C3IhR1AwsBTE="
"fingerprint": "umMeNAH41pX11GUAjHkw6RTyAwGCeTqRNJ2vPv5aeM0="
}
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,13 @@ public void VariadicCallbacksAreHandledCorrectly()
Assert.Equal(new double[]{2d, 3d, 4d}, invoker.AsArray(1, 2, 3));
}

[Fact(DisplayName = Prefix + nameof(ReturnSubclassThatImplementsInterface976))]
public void ReturnSubclassThatImplementsInterface976()
{
var obj = SomeTypeJsii976.ReturnReturn();
Assert.Equal(obj.Foo, 333);
}

private sealed class OverrideVariadicMethod : VariadicMethod
{
public override double[] AsArray(double first, params double[] others)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,12 @@ public void correctlyDeserializesStructUnions() {
assertTrue(StructUnionConsumer.isStructB(b1));
}

@Test
public void returnSubclassThatImplementsInterface976() {
IReturnJsii976 obj = SomeTypeJsii976.returnReturn();
assertEquals(obj.getFoo(), 333);
}

static class PartiallyInitializedThisConsumerImpl extends PartiallyInitializedThisConsumer {
@Override
public String consumePartiallyInitializedThis(final ConstructorPassesThisOut obj,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,20 @@
],
"name": "AugmentableClass"
},
"jsii-calc.BaseJsii976": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.BaseJsii976",
"initializer": {},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2219
},
"name": "BaseJsii976"
},
"jsii-calc.Bell": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -5140,6 +5154,37 @@
],
"name": "IRandomNumberGenerator"
},
"jsii-calc.IReturnJsii976": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental",
"summary": "Returns a subclass of a known class which implements an interface."
},
"fqn": "jsii-calc.IReturnJsii976",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2215
},
"name": "IReturnJsii976",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2216
},
"name": "foo",
"type": {
"primitive": "number"
}
}
]
},
"jsii-calc.IReturnsNumber": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -8654,6 +8699,54 @@
],
"name": "SingletonStringEnum"
},
"jsii-calc.SomeTypeJsii976": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.SomeTypeJsii976",
"initializer": {},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2221
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2231
},
"name": "returnAnonymous",
"returns": {
"type": {
"primitive": "any"
}
},
"static": true
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2223
},
"name": "returnReturn",
"returns": {
"type": {
"fqn": "jsii-calc.IReturnJsii976"
}
},
"static": true
}
],
"name": "SomeTypeJsii976"
},
"jsii-calc.StableClass": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -10913,5 +11006,5 @@
}
},
"version": "0.20.3",
"fingerprint": "1F+uskR3++T5mjRcWge9oG3H/jJvXm1C3IhR1AwsBTE="
"fingerprint": "umMeNAH41pX11GUAjHkw6RTyAwGCeTqRNJ2vPv5aeM0="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Amazon.JSII.Runtime.Deputy;

namespace Amazon.JSII.Tests.CalculatorNamespace
{
/// <remarks>
/// stability: Experimental
/// </remarks>
[JsiiClass(nativeType: typeof(Amazon.JSII.Tests.CalculatorNamespace.BaseJsii976), fullyQualifiedName: "jsii-calc.BaseJsii976")]
public class BaseJsii976 : DeputyBase
{
public BaseJsii976(): base(new DeputyProps(new object[]{}))
{
}

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

protected BaseJsii976(DeputyProps props): base(props)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Amazon.JSII.Runtime.Deputy;

namespace Amazon.JSII.Tests.CalculatorNamespace
{
/// <summary>Returns a subclass of a known class which implements an interface.</summary>
/// <remarks>
/// stability: Experimental
/// </remarks>
[JsiiInterface(nativeType: typeof(IReturnJsii976), fullyQualifiedName: "jsii-calc.IReturnJsii976")]
public interface IReturnJsii976
{
/// <remarks>
/// stability: Experimental
/// </remarks>
[JsiiProperty(name: "foo", typeJson: "{\"primitive\":\"number\"}")]
double Foo
{
get;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Amazon.JSII.Runtime.Deputy;

namespace Amazon.JSII.Tests.CalculatorNamespace
{
/// <summary>Returns a subclass of a known class which implements an interface.</summary>
/// <remarks>
/// stability: Experimental
/// </remarks>
[JsiiTypeProxy(nativeType: typeof(IReturnJsii976), fullyQualifiedName: "jsii-calc.IReturnJsii976")]
internal sealed class IReturnJsii976Proxy : DeputyBase, Amazon.JSII.Tests.CalculatorNamespace.IReturnJsii976
{
private IReturnJsii976Proxy(ByRefValue reference): base(reference)
{
}

/// <remarks>
/// stability: Experimental
/// </remarks>
[JsiiProperty(name: "foo", typeJson: "{\"primitive\":\"number\"}")]
public double Foo
{
get => GetInstanceProperty<double>();
}
}
}
Loading

0 comments on commit c2de100

Please sign in to comment.