Skip to content

[Java][jersey2] Make (de)serialization work for oneOf models #6323

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

Merged
merged 1 commit into from
May 25, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
package {{invokerPackage}}.model;

import {{invokerPackage}}.ApiException;
import java.util.Objects;
import java.lang.reflect.Type;
import java.util.Map;
import javax.ws.rs.core.GenericType;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Abstract class for oneOf,anyOf schemas defined in OpenAPI spec
*/
Expand Down Expand Up @@ -39,6 +42,7 @@ public abstract class AbstractOpenApiSchema {
*
* @return an instance of the actual schema/object
*/
@JsonValue
public Object getActualInstance() {return instance;}

/***
Expand All @@ -57,6 +61,46 @@ public abstract class AbstractOpenApiSchema {
return schemaType;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ").append(getClass()).append(" {\n");
sb.append(" instance: ").append(toIndentedString(instance)).append("\n");
sb.append(" isNullable: ").append(toIndentedString(isNullable)).append("\n");
sb.append(" schemaType: ").append(toIndentedString(schemaType)).append("\n");
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}

public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractOpenApiSchema a = (AbstractOpenApiSchema) o;
return Objects.equals(this.instance, a.instance) &&
Objects.equals(this.isNullable, a.isNullable) &&
Objects.equals(this.schemaType, a.schemaType);
}

@Override
public int hashCode() {
return Objects.hash(instance, isNullable, schemaType);
}

/***
* Is nullalble
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
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();

int match = 0;
Object deserialized = null;
{{#oneOf}}
try {
deserialized = tree.traverse(jp.getCodec()).readValueAs({{{.}}}.class);
match++;
} catch (Exception e) {
// deserialization failed, continue
}
{{/oneOf}}
if (match == 1) {
{{classname}} ret = new {{classname}}();
ret.setActualInstance(deserialized);
return ret;
}
throw new IOException(String.format("Failed deserialization for {{classname}}: %d classes match result, expected 1", match));
}
}

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

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

static {
{{#oneOf}}
schemas.put("{{{.}}}", new GenericType<{{{.}}}>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
package org.openapitools.client.model;

import org.openapitools.client.ApiException;
import java.util.Objects;
import java.lang.reflect.Type;
import java.util.Map;
import javax.ws.rs.core.GenericType;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Abstract class for oneOf,anyOf schemas defined in OpenAPI spec
*/
Expand Down Expand Up @@ -50,6 +53,7 @@ public AbstractOpenApiSchema(String schemaType, Boolean isNullable) {
*
* @return an instance of the actual schema/object
*/
@JsonValue
public Object getActualInstance() {return instance;}

/***
Expand All @@ -68,6 +72,46 @@ public String getSchemaType() {
return schemaType;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ").append(getClass()).append(" {\n");
sb.append(" instance: ").append(toIndentedString(instance)).append("\n");
sb.append(" isNullable: ").append(toIndentedString(isNullable)).append("\n");
sb.append(" schemaType: ").append(toIndentedString(schemaType)).append("\n");
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}

public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractOpenApiSchema a = (AbstractOpenApiSchema) o;
return Objects.equals(this.instance, a.instance) &&
Objects.equals(this.isNullable, a.isNullable) &&
Objects.equals(this.schemaType, a.schemaType);
}

@Override
public int hashCode() {
return Objects.hash(instance, isNullable, schemaType);
}

/***
* Is nullalble
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
package org.openapitools.client.model;

import org.openapitools.client.ApiException;
import java.util.Objects;
import java.lang.reflect.Type;
import java.util.Map;
import javax.ws.rs.core.GenericType;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Abstract class for oneOf,anyOf schemas defined in OpenAPI spec
*/
Expand Down Expand Up @@ -50,6 +53,7 @@ public AbstractOpenApiSchema(String schemaType, Boolean isNullable) {
*
* @return an instance of the actual schema/object
*/
@JsonValue
public Object getActualInstance() {return instance;}

/***
Expand All @@ -68,6 +72,46 @@ public String getSchemaType() {
return schemaType;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ").append(getClass()).append(" {\n");
sb.append(" instance: ").append(toIndentedString(instance)).append("\n");
sb.append(" isNullable: ").append(toIndentedString(isNullable)).append("\n");
sb.append(" schemaType: ").append(toIndentedString(schemaType)).append("\n");
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}

public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractOpenApiSchema a = (AbstractOpenApiSchema) o;
return Objects.equals(this.instance, a.instance) &&
Objects.equals(this.isNullable, a.isNullable) &&
Objects.equals(this.schemaType, a.schemaType);
}

@Override
public int hashCode() {
return Objects.hash(instance, isNullable, schemaType);
}

/***
* Is nullalble
*
Expand Down