Skip to content

Commit

Permalink
anyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-mfg committed Oct 8, 2024
1 parent a4e0c10 commit e7dbea1
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,23 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
return ret;
}
{{/discriminator}}
{{#composedSchemas}}
{{#anyOf}}
// deserialize {{{.}}}
// deserialize {{{dataType}}}{{#isNullable}} (nullable){{/isNullable}}
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs({{{.}}}.class);
{{^isArray}}
{{^isMap}}
deserialized = tree.traverse(jp.getCodec()).readValueAs({{{dataType}}}.class);
{{/isMap}}
{{/isArray}}
{{#isArray}}
final TypeReference<{{{dataType}}}> ref = new TypeReference<{{{dataType}}}>(){};
deserialized = tree.traverse(jp.getCodec()).readValueAs(ref);
{{/isArray}}
{{#isMap}}
final TypeReference<{{{dataType}}}> ref = new TypeReference<{{{dataType}}}>(){};
deserialized = tree.traverse(jp.getCodec()).readValueAs(ref);
{{/isMap}}
{{classname}} ret = new {{classname}}();
ret.setActualInstance(deserialized);
return ret;
Expand All @@ -81,6 +94,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}

{{/anyOf}}
{{/composedSchemas}}
throw new IOException(String.format("Failed deserialization for {{classname}}: no match found"));
}

Expand Down Expand Up @@ -119,13 +133,17 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
return Objects.hash(getActualInstance(), isNullable(), getSchemaType(), additionalProperties);
}
{{/additionalPropertiesType}}
{{#composedSchemas}}
{{#anyOf}}
public {{classname}}({{{.}}} o) {
{{^vendorExtensions.x-duplicated-data-type}}
public {{classname}}({{{baseType}}} o) {
super("anyOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
setActualInstance(o);
}

{{/vendorExtensions.x-duplicated-data-type}}
{{/anyOf}}
{{/composedSchemas}}
static {
{{#anyOf}}
schemas.put("{{{.}}}", new GenericType<{{{.}}}>() {
Expand Down Expand Up @@ -165,13 +183,17 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}

{{/isNullable}}
{{#composedSchemas}}
{{#anyOf}}
if (JSON.isInstanceOf({{{.}}}.class, instance, new HashSet<>())) {
{{^vendorExtensions.x-duplicated-data-type}}
if (JSON.isInstanceOf({{{baseType}}}.class, instance, new HashSet<>())) {
super.setActualInstance(instance);
return;
}

{{/vendorExtensions.x-duplicated-data-type}}
{{/anyOf}}
{{/composedSchemas}}
throw new RuntimeException("Invalid instance type. Must be {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,23 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
return ret;
}
{{/discriminator}}
{{#composedSchemas}}
{{#anyOf}}
// deserialize {{{.}}}
// deserialize {{{dataType}}}{{#isNullable}} (nullable){{/isNullable}}
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs({{{.}}}.class);
{{^isArray}}
{{^isMap}}
deserialized = tree.traverse(jp.getCodec()).readValueAs({{{dataType}}}.class);
{{/isMap}}
{{/isArray}}
{{#isArray}}
final TypeReference<{{{dataType}}}> ref = new TypeReference<{{{dataType}}}>(){};
deserialized = tree.traverse(jp.getCodec()).readValueAs(ref);
{{/isArray}}
{{#isMap}}
final TypeReference<{{{dataType}}}> ref = new TypeReference<{{{dataType}}}>(){};
deserialized = tree.traverse(jp.getCodec()).readValueAs(ref);
{{/isMap}}
{{classname}} ret = new {{classname}}();
ret.setActualInstance(deserialized);
return ret;
Expand All @@ -81,6 +94,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}

{{/anyOf}}
{{/composedSchemas}}
throw new IOException(String.format("Failed deserialization for {{classname}}: no match found"));
}

Expand Down Expand Up @@ -119,13 +133,17 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
return Objects.hash(getActualInstance(), isNullable(), getSchemaType(), additionalProperties);
}
{{/additionalPropertiesType}}
{{#composedSchemas}}
{{#anyOf}}
public {{classname}}({{{.}}} o) {
{{^vendorExtensions.x-duplicated-data-type}}
public {{classname}}({{{baseType}}} o) {
super("anyOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
setActualInstance(o);
}

{{/vendorExtensions.x-duplicated-data-type}}
{{/anyOf}}
{{/composedSchemas}}
static {
{{#anyOf}}
schemas.put("{{{.}}}", new GenericType<{{{.}}}>() {
Expand Down Expand Up @@ -165,13 +183,17 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}

{{/isNullable}}
{{#composedSchemas}}
{{#anyOf}}
if (JSON.isInstanceOf({{{.}}}.class, instance, new HashSet<>())) {
{{^vendorExtensions.x-duplicated-data-type}}
if (JSON.isInstanceOf({{{baseType}}}.class, instance, new HashSet<>())) {
super.setActualInstance(instance);
return;
}

{{/vendorExtensions.x-duplicated-data-type}}
{{/anyOf}}
{{/composedSchemas}}
throw new RuntimeException("Invalid instance type. Must be {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public GmFruit deserialize(JsonParser jp, DeserializationContext ctxt) throws IO
JsonNode tree = jp.readValueAsTree();

Object deserialized = null;
// deserialize Apple
// deserialize Apple (nullable)
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs(Apple.class);
GmFruit ret = new GmFruit();
Expand Down Expand Up @@ -131,7 +131,7 @@ public GmFruit() {
}

public GmFruit(Apple o) {
super("anyOf", Boolean.FALSE);
super("anyOf", Boolean.TRUE);
setActualInstance(o);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public MammalAnyof deserialize(JsonParser jp, DeserializationContext ctxt) throw
ret.setActualInstance(deserialized);
return ret;
}
// deserialize Pig
// deserialize Whale
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs(Pig.class);
deserialized = tree.traverse(jp.getCodec()).readValueAs(Whale.class);
MammalAnyof ret = new MammalAnyof();
ret.setActualInstance(deserialized);
return ret;
Expand All @@ -117,9 +117,9 @@ public MammalAnyof deserialize(JsonParser jp, DeserializationContext ctxt) throw
log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e);
}

// deserialize Whale
// deserialize Zebra
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs(Whale.class);
deserialized = tree.traverse(jp.getCodec()).readValueAs(Zebra.class);
MammalAnyof ret = new MammalAnyof();
ret.setActualInstance(deserialized);
return ret;
Expand All @@ -128,9 +128,9 @@ public MammalAnyof deserialize(JsonParser jp, DeserializationContext ctxt) throw
log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e);
}

// deserialize Zebra
// deserialize Pig
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs(Zebra.class);
deserialized = tree.traverse(jp.getCodec()).readValueAs(Pig.class);
MammalAnyof ret = new MammalAnyof();
ret.setActualInstance(deserialized);
return ret;
Expand Down Expand Up @@ -207,17 +207,17 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(getActualInstance(), isNullable(), getSchemaType(), additionalProperties);
}
public MammalAnyof(Pig o) {
public MammalAnyof(Whale o) {
super("anyOf", Boolean.FALSE);
setActualInstance(o);
}

public MammalAnyof(Whale o) {
public MammalAnyof(Zebra o) {
super("anyOf", Boolean.FALSE);
setActualInstance(o);
}

public MammalAnyof(Zebra o) {
public MammalAnyof(Pig o) {
super("anyOf", Boolean.FALSE);
setActualInstance(o);
}
Expand Down Expand Up @@ -254,17 +254,17 @@ public Map<String, GenericType<?>> getSchemas() {
*/
@Override
public void setActualInstance(Object instance) {
if (JSON.isInstanceOf(Pig.class, instance, new HashSet<>())) {
if (JSON.isInstanceOf(Whale.class, instance, new HashSet<>())) {
super.setActualInstance(instance);
return;
}

if (JSON.isInstanceOf(Whale.class, instance, new HashSet<>())) {
if (JSON.isInstanceOf(Zebra.class, instance, new HashSet<>())) {
super.setActualInstance(instance);
return;
}

if (JSON.isInstanceOf(Zebra.class, instance, new HashSet<>())) {
if (JSON.isInstanceOf(Pig.class, instance, new HashSet<>())) {
super.setActualInstance(instance);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public GmFruit deserialize(JsonParser jp, DeserializationContext ctxt) throws IO
JsonNode tree = jp.readValueAsTree();

Object deserialized = null;
// deserialize Apple
// deserialize Apple (nullable)
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs(Apple.class);
GmFruit ret = new GmFruit();
Expand Down Expand Up @@ -129,7 +129,7 @@ public GmFruit() {
}

public GmFruit(Apple o) {
super("anyOf", Boolean.FALSE);
super("anyOf", Boolean.TRUE);
setActualInstance(o);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public MammalAnyof deserialize(JsonParser jp, DeserializationContext ctxt) throw
ret.setActualInstance(deserialized);
return ret;
}
// deserialize Pig
// deserialize Whale
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs(Pig.class);
deserialized = tree.traverse(jp.getCodec()).readValueAs(Whale.class);
MammalAnyof ret = new MammalAnyof();
ret.setActualInstance(deserialized);
return ret;
Expand All @@ -115,9 +115,9 @@ public MammalAnyof deserialize(JsonParser jp, DeserializationContext ctxt) throw
log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e);
}

// deserialize Whale
// deserialize Zebra
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs(Whale.class);
deserialized = tree.traverse(jp.getCodec()).readValueAs(Zebra.class);
MammalAnyof ret = new MammalAnyof();
ret.setActualInstance(deserialized);
return ret;
Expand All @@ -126,9 +126,9 @@ public MammalAnyof deserialize(JsonParser jp, DeserializationContext ctxt) throw
log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e);
}

// deserialize Zebra
// deserialize Pig
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs(Zebra.class);
deserialized = tree.traverse(jp.getCodec()).readValueAs(Pig.class);
MammalAnyof ret = new MammalAnyof();
ret.setActualInstance(deserialized);
return ret;
Expand Down Expand Up @@ -205,17 +205,17 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(getActualInstance(), isNullable(), getSchemaType(), additionalProperties);
}
public MammalAnyof(Pig o) {
public MammalAnyof(Whale o) {
super("anyOf", Boolean.FALSE);
setActualInstance(o);
}

public MammalAnyof(Whale o) {
public MammalAnyof(Zebra o) {
super("anyOf", Boolean.FALSE);
setActualInstance(o);
}

public MammalAnyof(Zebra o) {
public MammalAnyof(Pig o) {
super("anyOf", Boolean.FALSE);
setActualInstance(o);
}
Expand Down Expand Up @@ -252,17 +252,17 @@ public Map<String, GenericType<?>> getSchemas() {
*/
@Override
public void setActualInstance(Object instance) {
if (JSON.isInstanceOf(Pig.class, instance, new HashSet<>())) {
if (JSON.isInstanceOf(Whale.class, instance, new HashSet<>())) {
super.setActualInstance(instance);
return;
}

if (JSON.isInstanceOf(Whale.class, instance, new HashSet<>())) {
if (JSON.isInstanceOf(Zebra.class, instance, new HashSet<>())) {
super.setActualInstance(instance);
return;
}

if (JSON.isInstanceOf(Zebra.class, instance, new HashSet<>())) {
if (JSON.isInstanceOf(Pig.class, instance, new HashSet<>())) {
super.setActualInstance(instance);
return;
}
Expand Down

0 comments on commit e7dbea1

Please sign in to comment.