Skip to content

Commit

Permalink
fix(kernel): make type serialization explicit and recursive
Browse files Browse the repository at this point in the history
Serialize and deserialize types according to their declared static type,
and add validation on the runtime types matching the declared types.

This is in contrast to previously, when we mostly only used the runtime
types to determine what to do, and harly any validation was done. The
runtime types used to be able to freely disagree with the declared
types, and we put a lot of burden on the JSII runtimes at the other
end of the wire.

Fix tests that used to exercise the API with invalid arguments.

Fixes aws/aws-cdk#1981.
  • Loading branch information
Rico Huijbers committed Mar 22, 2019
1 parent dd07831 commit 06cabfa
Show file tree
Hide file tree
Showing 6 changed files with 904 additions and 244 deletions.
42 changes: 40 additions & 2 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ export class AllTypes {
enumMethod(value: StringEnum) {
return value;
}


public anyOut(): any {
const ret = new Number(42);
Object.defineProperty(ret, 'tag', {
value: "you're it"
});
return ret;
}

public anyIn(inp: any) {
if (inp.tag !== "you're it") {
throw new Error('Not the same object that I gave you!');
}
}
}

//
Expand Down Expand Up @@ -1327,12 +1342,35 @@ export class InbetweenClass extends PublicClass {}
class PrivateClass extends InbetweenClass implements IPublicInterface {
public bye(): void {}
}

class HiddenClass implements IPublicInterface {
public bye(): void { }
}

class HiddenSubclass extends HiddenClass {
}

export class Constructors {
public static makeClass(): PublicClass {
return new PrivateClass();
return new PrivateClass(); // Wire type should be InbetweenClass
}
public static makeInterface(): IPublicInterface {
return new PrivateClass();
return new PrivateClass(); // Wire type should be InbetweenClass
}
public static makeInterfaces(): IPublicInterface[] {
return [new PrivateClass()]; // Wire type should be InbetweenClass[]
}

public static hiddenInterface(): IPublicInterface {
return new HiddenClass(); // Wire type should be IPublicInterface
}

public static hiddenInterfaces(): IPublicInterface[] {
return [new HiddenClass()]; // Wire type should be IPublicInterface[]
}

public static hiddenSubInterfaces(): IPublicInterface[] {
return [new HiddenSubclass()]; // Wire type should be IPublicInterface[]
}
}

Expand Down
10 changes: 9 additions & 1 deletion packages/jsii-kernel/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ export const TOKEN_DATE = '$jsii.date';
export const TOKEN_ENUM = '$jsii.enum';

export class ObjRef {
[token: string]: string; // token = TOKEN_REF
public [TOKEN_REF]: string;
}

export class WireDate {
public [TOKEN_DATE]: string;
}

export class WireEnum {
public [TOKEN_ENUM]: string;
}

export interface Override {
Expand Down
Loading

0 comments on commit 06cabfa

Please sign in to comment.