All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Java.libraries.okhttp-gson.oneof_model.mustache Maven / Gradle / Ivy

There is a newer version: 7.7.0
Show newest version


import java.io.IOException;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonParseException;

import {{invokerPackage}}.JSON;

{{>additionalModelTypeAnnotations}}{{>generatedAnnotation}}{{>xmlAnnotation}}
public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-implements}} implements {{{.}}}{{^-last}}, {{/-last}}{{/vendorExtensions.x-implements}} {
    private static final Logger log = Logger.getLogger({{classname}}.class.getName());

    public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
        @SuppressWarnings("unchecked")
        @Override
        public  TypeAdapter create(Gson gson, TypeToken type) {
            if (!{{classname}}.class.isAssignableFrom(type.getRawType())) {
                return null; // this class only serializes '{{classname}}' and its subtypes
            }
            final TypeAdapter elementAdapter = gson.getAdapter(JsonElement.class);
            {{#composedSchemas}}
            {{#oneOf}}
            {{^isArray}}
            {{^vendorExtensions.x-duplicated-data-type}}
            final TypeAdapter<{{{dataType}}}> adapter{{{dataType}}} = gson.getDelegateAdapter(this, TypeToken.get({{{dataType}}}.class));
            {{/vendorExtensions.x-duplicated-data-type}}
            {{/isArray}}
            {{#isArray}}

            final Type typeInstance = new TypeToken>(){}.getType();
            final TypeAdapter<{{{dataType}}}> adapter{{complexType}}List = (TypeAdapter>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
            {{/isArray}}
            {{/oneOf}}
            {{/composedSchemas}}

            return (TypeAdapter) new TypeAdapter<{{classname}}>() {
                @Override
                public void write(JsonWriter out, {{classname}} value) throws IOException {
                    if (value == null || value.getActualInstance() == null) {
                        elementAdapter.write(out, null);
                        return;
                    }

                    {{#composedSchemas}}
                    {{#oneOf}}
                    {{^vendorExtensions.x-duplicated-data-type}}
                    // check if the actual instance is of the type `{{{dataType}}}`
                    if (value.getActualInstance() instanceof {{#isArray}}List{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
                    {{#isPrimitiveType}}
                      JsonPrimitive primitive = adapter{{{dataType}}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonPrimitive();
                      elementAdapter.write(out, primitive);
                      return;
                    {{/isPrimitiveType}}
                    {{#isArray}}
                      List list = (List) value.getActualInstance();
        	            if(list.get(0) instanceof {{complexType}}) {
        		            JsonArray array = adapter{{{complexType}}}List.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonArray();
                        elementAdapter.write(out, array);
                        return;
        	            }
                    {{/isArray}}
                    {{^isArray}}
                    {{^isPrimitiveType}}
                      JsonElement element = adapter{{{dataType}}}.toJsonTree(({{{dataType}}})value.getActualInstance());
                      elementAdapter.write(out, element);
                      return;
                    {{/isPrimitiveType}}
                    {{/isArray}}
                    }
                    {{/vendorExtensions.x-duplicated-data-type}}
                    {{/oneOf}}
                    {{/composedSchemas}}
                    throw new IOException("Failed to serialize as the type doesn't match oneOf schemas: {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}");
                }

                @Override
                public {{classname}} read(JsonReader in) throws IOException {
                    Object deserialized = null;
                    JsonElement jsonElement = elementAdapter.read(in);

                    {{#useOneOfDiscriminatorLookup}}
                    {{#discriminator}}
                    JsonObject jsonObject = jsonElement.getAsJsonObject();

                    // use discriminator value for faster oneOf lookup
                    {{classname}} new{{classname}} = new {{classname}}();
                    if (jsonObject.get("{{{propertyBaseName}}}") == null) {
                        log.log(Level.WARNING, "Failed to lookup discriminator value for {{classname}} as `{{{propertyBaseName}}}` was not found in the payload or the payload is empty.");
                    } else  {
                        // look up the discriminator value in the field `{{{propertyBaseName}}}`
                        switch (jsonObject.get("{{{propertyBaseName}}}").getAsString()) {
                        {{#mappedModels}}
                            case "{{{mappingName}}}":
                                deserialized = adapter{{modelName}}.fromJsonTree(jsonObject);
                                new{{classname}}.setActualInstance(deserialized);
                                return new{{classname}};
                        {{/mappedModels}}
                            default:
                                log.log(Level.WARNING, String.format("Failed to lookup discriminator value `%s` for {{classname}}. Possible values:{{#mappedModels}} {{{mappingName}}}{{/mappedModels}}", jsonObject.get("{{{propertyBaseName}}}").getAsString()));
                        }
                    }

                    {{/discriminator}}
                    {{/useOneOfDiscriminatorLookup}}
                    int match = 0;
                    ArrayList errorMessages = new ArrayList<>();
                    TypeAdapter actualAdapter = elementAdapter;

                    {{#composedSchemas}}
                    {{#oneOf}}
                    {{^vendorExtensions.x-duplicated-data-type}}
                    {{^hasVars}}
                    // deserialize {{{dataType}}}
                    try {
                      // validate the JSON object to see if any exception is thrown
                    {{#isNumber}}
                      if(!jsonElement.getAsJsonPrimitive().isNumber()) {
                        throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
                      }
                      actualAdapter = adapter{{{dataType}}};
                    {{/isNumber}}
                    {{^isNumber}}
                    {{#isPrimitiveType}}
                      if(!jsonElement.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
                        throw new IllegalArgumentException(String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
                      }
                      actualAdapter = adapter{{{dataType}}};
                    {{/isPrimitiveType}}
                    {{^isPrimitiveType}}
                    {{^isArray}}
                      {{{dataType}}}.validateJsonElement(jsonElement);
                      actualAdapter = adapter{{{dataType}}};
                    {{/isArray}}
                    {{/isPrimitiveType}}
                    {{/isNumber}}
                    {{#isArray}}
                      if (!jsonElement.isJsonArray()) {
                        throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
                      }

                      JsonArray array = jsonElement.getAsJsonArray();
                      // validate array items
                      for(JsonElement element : array) {
                      {{#items}}
                      {{#isNumber}}
                        if(!jsonElement.getAsJsonPrimitive().isNumber()) {
                          throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
                        }
                        actualAdapter = adapter{{{dataType}}};
                      {{/isNumber}}
                      {{^isNumber}}
                      {{#isPrimitiveType}}
                      if(!element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
                        throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
                      }
                      {{/isPrimitiveType}}
                      {{^isPrimitiveType}}
                        {{{dataType}}}.validateJsonElement(element);
                      {{/isPrimitiveType}}
                      {{/isNumber}}
                      {{/items}}
                      }
                      actualAdapter = adapter{{{complexType}}}List;
                    {{/isArray}}
                      match++;
                      log.log(Level.FINER, "Input data matches schema '{{{dataType}}}'");
                    } catch (Exception e) {
                      // deserialization failed, continue
                      errorMessages.add(String.format("Deserialization for {{{dataType}}} failed with `%s`.", e.getMessage()));
                      log.log(Level.FINER, "Input data does not match schema '{{{dataType}}}'", e);
                    }
                    {{/hasVars}}
                    {{#hasVars}}
                    // deserialize {{{.}}}
                    try {
                        // validate the JSON object to see if any exception is thrown
                        {{.}}.validateJsonElement(jsonElement);
                        actualAdapter = adapter{{.}};
                        match++;
                        log.log(Level.FINER, "Input data matches schema '{{{.}}}'");
                    } catch (Exception e) {
                        // deserialization failed, continue
                        errorMessages.add(String.format("Deserialization for {{{.}}} failed with `%s`.", e.getMessage()));
                        log.log(Level.FINER, "Input data does not match schema '{{{.}}}'", e);
                    }
                    {{/hasVars}}
                    {{/vendorExtensions.x-duplicated-data-type}}
                    {{/oneOf}}
                    {{/composedSchemas}}

                    if (match == 1) {
                        {{classname}} ret = new {{classname}}();
                        ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement));
                        return ret;
                    }

                    throw new IOException(String.format("Failed deserialization for {{classname}}: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()));
                }
            }.nullSafe();
        }
    }

    // store a list of schema names defined in oneOf
    public static final Map> schemas = new HashMap>();

    public {{classname}}() {
        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 {
        {{#composedSchemas}}
        {{#oneOf}}
        {{^vendorExtensions.x-duplicated-data-type}}
        schemas.put("{{{dataType}}}", {{{baseType}}}.class);
        {{/vendorExtensions.x-duplicated-data-type}}
        {{/oneOf}}
        {{/composedSchemas}}
    }

    @Override
    public Map> getSchemas() {
        return {{classname}}.schemas;
    }

    /**
     * Set the instance that matches the oneOf child schema, check
     * the instance parameter is valid against the oneOf child schemas:
     * {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}
     *
     * It could be an instance of the 'oneOf' schemas.
     */
    @Override
    public void setActualInstance(Object instance) {
        {{#isNullable}}
        if (instance == null) {
           super.setActualInstance(instance);
           return;
        }

        {{/isNullable}}
        {{#composedSchemas}}
        {{#oneOf}}
        {{^vendorExtensions.x-duplicated-data-type}}
        if (instance instanceof {{#isArray}}List{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
        {{#isArray}}
            List list = (List) instance;
        	if(list.get(0) instanceof {{complexType}}) {
        		super.setActualInstance(instance);
        		return;
        	}
            {{/isArray}}
            {{^isArray}}
            super.setActualInstance(instance);
            return;
        {{/isArray}}
        }

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

    /**
     * Get the actual instance, which can be the following:
     * {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}
     *
     * @return The actual instance ({{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}})
     */
    @Override
    public Object getActualInstance() {
        return super.getActualInstance();
    }

    {{#composedSchemas}}
    {{#oneOf}}
    {{^vendorExtensions.x-duplicated-data-type}}
    /**
     * Get the actual instance of `{{{dataType}}}`. If the actual instance is not `{{{dataType}}}`,
     * the ClassCastException will be thrown.
     *
     * @return The actual instance of `{{{dataType}}}`
     * @throws ClassCastException if the instance is not `{{{dataType}}}`
     */
    public {{{dataType}}} get{{#isArray}}{{complexType}}List{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}() throws ClassCastException {
        return ({{{dataType}}})super.getActualInstance();
    }
    {{/vendorExtensions.x-duplicated-data-type}}
    {{/oneOf}}
    {{/composedSchemas}}

 /**
  * Validates the JSON Element and throws an exception if issues found
  *
  * @param jsonElement JSON Element
  * @throws IOException if the JSON Element is invalid with respect to {{classname}}
  */
  public static void validateJsonElement(JsonElement jsonElement) throws IOException {
    // validate oneOf schemas one by one
    int validCount = 0;
    ArrayList errorMessages = new ArrayList<>();
    {{#composedSchemas}}
    {{#oneOf}}
    {{^vendorExtensions.x-duplicated-data-type}}
    // validate the json string with {{{dataType}}}
    try {
    {{^hasVars}}
    {{#isNumber}}
      if(!jsonElement.getAsJsonPrimitive().isNumber()) {
        throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
      }
    {{/isNumber}}
    {{^isNumber}}
    {{#isPrimitiveType}}
      if(!jsonElement.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
        throw new IllegalArgumentException(String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
      }
    {{/isPrimitiveType}}
    {{^isPrimitiveType}}
    {{^isArray}}
      {{{dataType}}}.validateJsonElement(jsonElement);
    {{/isArray}}
    {{/isPrimitiveType}}
    {{/isNumber}}
    {{#isArray}}
      if (!jsonElement.isJsonArray()) {
          throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
      }
      JsonArray array = jsonElement.getAsJsonArray();
      // validate array items
      for(JsonElement element : array) {
        {{#items}}
        {{#isNumber}}
          if(!jsonElement.getAsJsonPrimitive().isNumber()) {
            throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
          }
        {{/isNumber}}
        {{^isNumber}}
        {{#isPrimitiveType}}
        if(!element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
          throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
        }
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}
        {{{dataType}}}.validateJsonElement(element);
        {{/isPrimitiveType}}
        {{/isNumber}}
        {{/items}}
      }
    {{/isArray}}
    {{/hasVars}}
    {{#hasVars}}
      {{{.}}}.validateJsonElement(jsonElement);
      validCount++;
    {{/hasVars}}
      validCount++;
    } catch (Exception e) {
      errorMessages.add(String.format("Deserialization for {{{dataType}}} failed with `%s`.", e.getMessage()));
      // continue to the next one
    }
    {{/vendorExtensions.x-duplicated-data-type}}
    {{/oneOf}}
    {{/composedSchemas}}
    if (validCount != 1) {
      throw new IOException(String.format("The JSON string is invalid for {{classname}} with oneOf schemas: {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. %d class(es) match the result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", validCount, errorMessages, jsonElement.toString()));
    }
  }

 /**
  * Create an instance of {{classname}} given an JSON string
  *
  * @param jsonString JSON string
  * @return An instance of {{classname}}
  * @throws IOException if the JSON string is invalid with respect to {{classname}}
  */
  public static {{{classname}}} fromJson(String jsonString) throws IOException {
    return JSON.getGson().fromJson(jsonString, {{{classname}}}.class);
  }

 /**
  * Convert an instance of {{classname}} to an JSON string
  *
  * @return JSON string
  */
  public String toJson() {
    return JSON.getGson().toJson(this);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy