Skip to content

Commit

Permalink
java jersey2 enhance anyOf (#6420)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored May 26, 2020
1 parent 0572030 commit 707a88d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

{{>additionalModelTypeAnnotations}}{{>generatedAnnotation}}{{>xmlAnnotation}}
@JsonDeserialize(using={{classname}}.{{classname}}Deserializer.class)
public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-implements}}, {{{.}}}{{/vendorExtensions.x-implements}} {
public static class {{classname}}Deserializer extends StdDeserializer<{{classname}}> {
public {{classname}}Deserializer() {
this({{classname}}.class);
}

public {{classname}}Deserializer(Class<?> vc) {
super(vc);
}

@Override
public {{classname}} deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode tree = jp.readValueAsTree();
Object deserialized = null;
{{#anyOf}}
// deserialzie {{{.}}}
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs({{{.}}}.class);
{{classname}} ret = new {{classname}}();
ret.setActualInstance(deserialized);
return ret;
} catch (Exception e) {
// deserialization failed, continue
}

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

// store a list of schema names defined in anyOf
public final static Map<String, GenericType> schemas = new HashMap<String, GenericType>();
Expand All @@ -14,6 +52,13 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
super("anyOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
}

{{#anyOf}}
public {{classname}}({{{.}}} o) {
super("anyOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
setActualInstance(o);
}

{{/anyOf}}
static {
{{#anyOf}}
schemas.put("{{{.}}}", new GenericType<{{{.}}}>() {
Expand Down Expand Up @@ -42,6 +87,6 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}

{{/anyOf}}
throw new RuntimeException("Invalid instance type. Must be {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}");
throw new RuntimeException("Invalid instance type. Must be {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
int match = 0;
Object deserialized = null;
{{#oneOf}}
// deserialize {{{.}}}
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs({{{.}}}.class);
match++;
} catch (Exception e) {
// deserialization failed, continue
}

{{/oneOf}}
if (match == 1) {
{{classname}} ret = new {{classname}}();
Expand All @@ -59,8 +61,8 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
super("oneOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
setActualInstance(o);
}
{{/oneOf}}

{{/oneOf}}
static {
{{#oneOf}}
schemas.put("{{{.}}}", new GenericType<{{{.}}}>() {
Expand Down

0 comments on commit 707a88d

Please sign in to comment.