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(python): correctly handle interface declarations on returned objects #980

Merged
merged 4 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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