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(kernel): Correct null handling in JSON types #523

Merged
merged 3 commits into from
Jun 7, 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
4 changes: 3 additions & 1 deletion packages/jsii-kernel/lib/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ export const SERIALIZERS: {[k: string]: Serializer} = {
// Just whatever. Dates will automatically serialize themselves to strings.
return value;
},
deserialize(value) {
deserialize(value, optionalValue) {
// /!\ Top-level "null" will turn to underfined, but any null nested in the value is valid JSON, so it'll stay!
if (nullAndOk(value, optionalValue)) { return undefined; }
return value;
},
},
Expand Down
2 changes: 2 additions & 0 deletions packages/jsii-pacmak/lib/targets/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,9 @@ class JavaGenerator extends Generator {
this.code.line(`com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();`);

for (const prop of props) {
if (prop.nullable) { this.code.openBlock(`if (this.get${prop.propName}() != null)`); }
this.code.line(`obj.set(\"${prop.spec.name}\", om.valueToTree(this.get${prop.propName}()));`);
if (prop.nullable) { this.code.closeBlock(); }
}

this.code.line(`return obj;`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public java.util.List<java.lang.String> getFirstOptional() {
com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
obj.set("anumber", om.valueToTree(this.getAnumber()));
obj.set("astring", om.valueToTree(this.getAstring()));
obj.set("firstOptional", om.valueToTree(this.getFirstOptional()));
if (this.getFirstOptional() != null) {
obj.set("firstOptional", om.valueToTree(this.getFirstOptional()));
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,15 @@ public java.lang.Boolean getOptional3() {
public com.fasterxml.jackson.databind.JsonNode $jsii$toJson() {
com.fasterxml.jackson.databind.ObjectMapper om = software.amazon.jsii.JsiiObjectMapper.INSTANCE;
com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
obj.set("optional1", om.valueToTree(this.getOptional1()));
obj.set("optional2", om.valueToTree(this.getOptional2()));
obj.set("optional3", om.valueToTree(this.getOptional3()));
if (this.getOptional1() != null) {
obj.set("optional1", om.valueToTree(this.getOptional1()));
}
if (this.getOptional2() != null) {
obj.set("optional2", om.valueToTree(this.getOptional2()));
}
if (this.getOptional3() != null) {
obj.set("optional3", om.valueToTree(this.getOptional3()));
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ public java.lang.Number getMaximumValue() {
public com.fasterxml.jackson.databind.JsonNode $jsii$toJson() {
com.fasterxml.jackson.databind.ObjectMapper om = software.amazon.jsii.JsiiObjectMapper.INSTANCE;
com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
obj.set("initialValue", om.valueToTree(this.getInitialValue()));
obj.set("maximumValue", om.valueToTree(this.getMaximumValue()));
if (this.getInitialValue() != null) {
obj.set("initialValue", om.valueToTree(this.getInitialValue()));
}
if (this.getMaximumValue() != null) {
obj.set("maximumValue", om.valueToTree(this.getMaximumValue()));
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,20 @@ public java.util.List<java.lang.String> getFirstOptional() {
obj.set("anotherRequired", om.valueToTree(this.getAnotherRequired()));
obj.set("bool", om.valueToTree(this.getBool()));
obj.set("nonPrimitive", om.valueToTree(this.getNonPrimitive()));
obj.set("anotherOptional", om.valueToTree(this.getAnotherOptional()));
obj.set("optionalAny", om.valueToTree(this.getOptionalAny()));
obj.set("optionalArray", om.valueToTree(this.getOptionalArray()));
if (this.getAnotherOptional() != null) {
obj.set("anotherOptional", om.valueToTree(this.getAnotherOptional()));
}
if (this.getOptionalAny() != null) {
obj.set("optionalAny", om.valueToTree(this.getOptionalAny()));
}
if (this.getOptionalArray() != null) {
obj.set("optionalArray", om.valueToTree(this.getOptionalArray()));
}
obj.set("anumber", om.valueToTree(this.getAnumber()));
obj.set("astring", om.valueToTree(this.getAstring()));
obj.set("firstOptional", om.valueToTree(this.getFirstOptional()));
if (this.getFirstOptional() != null) {
obj.set("firstOptional", om.valueToTree(this.getFirstOptional()));
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ public java.lang.String getOption2() {
public com.fasterxml.jackson.databind.JsonNode $jsii$toJson() {
com.fasterxml.jackson.databind.ObjectMapper om = software.amazon.jsii.JsiiObjectMapper.INSTANCE;
com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
obj.set("option1", om.valueToTree(this.getOption1()));
obj.set("option2", om.valueToTree(this.getOption2()));
if (this.getOption1() != null) {
obj.set("option1", om.valueToTree(this.getOption1()));
}
if (this.getOption2() != null) {
obj.set("option2", om.valueToTree(this.getOption2()));
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public java.lang.String getName() {
public com.fasterxml.jackson.databind.JsonNode $jsii$toJson() {
com.fasterxml.jackson.databind.ObjectMapper om = software.amazon.jsii.JsiiObjectMapper.INSTANCE;
com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
obj.set("name", om.valueToTree(this.getName()));
if (this.getName() != null) {
obj.set("name", om.valueToTree(this.getName()));
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,21 @@ public java.lang.Boolean getPublicTasks() {
public com.fasterxml.jackson.databind.JsonNode $jsii$toJson() {
com.fasterxml.jackson.databind.ObjectMapper om = software.amazon.jsii.JsiiObjectMapper.INSTANCE;
com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
obj.set("containerPort", om.valueToTree(this.getContainerPort()));
obj.set("cpu", om.valueToTree(this.getCpu()));
obj.set("memoryMiB", om.valueToTree(this.getMemoryMiB()));
obj.set("publicLoadBalancer", om.valueToTree(this.getPublicLoadBalancer()));
obj.set("publicTasks", om.valueToTree(this.getPublicTasks()));
if (this.getContainerPort() != null) {
obj.set("containerPort", om.valueToTree(this.getContainerPort()));
}
if (this.getCpu() != null) {
obj.set("cpu", om.valueToTree(this.getCpu()));
}
if (this.getMemoryMiB() != null) {
obj.set("memoryMiB", om.valueToTree(this.getMemoryMiB()));
}
if (this.getPublicLoadBalancer() != null) {
obj.set("publicLoadBalancer", om.valueToTree(this.getPublicLoadBalancer()));
}
if (this.getPublicTasks() != null) {
obj.set("publicTasks", om.valueToTree(this.getPublicTasks()));
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public java.lang.Object getThisShouldBeUndefined() {
com.fasterxml.jackson.databind.ObjectMapper om = software.amazon.jsii.JsiiObjectMapper.INSTANCE;
com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
obj.set("arrayWithThreeElementsAndUndefinedAsSecondArgument", om.valueToTree(this.getArrayWithThreeElementsAndUndefinedAsSecondArgument()));
obj.set("thisShouldBeUndefined", om.valueToTree(this.getThisShouldBeUndefined()));
if (this.getThisShouldBeUndefined() != null) {
obj.set("thisShouldBeUndefined", om.valueToTree(this.getThisShouldBeUndefined()));
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public java.lang.String getField() {
public com.fasterxml.jackson.databind.JsonNode $jsii$toJson() {
com.fasterxml.jackson.databind.ObjectMapper om = software.amazon.jsii.JsiiObjectMapper.INSTANCE;
com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
obj.set("field", om.valueToTree(this.getField()));
if (this.getField() != null) {
obj.set("field", om.valueToTree(this.getField()));
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public java.lang.Object getFoo() {
com.fasterxml.jackson.databind.ObjectMapper om = software.amazon.jsii.JsiiObjectMapper.INSTANCE;
com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
obj.set("bar", om.valueToTree(this.getBar()));
obj.set("foo", om.valueToTree(this.getFoo()));
if (this.getFoo() != null) {
obj.set("foo", om.valueToTree(this.getFoo()));
}
return obj;
}

Expand Down