diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java index d120b4d7b26..c68929726bd 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java @@ -499,7 +499,7 @@ public class OpenAPINormalizer { } if (StringUtils.isNotEmpty(schema.get$ref())) { - // not need to process $ref + // no need to process $ref return schema; } @@ -594,7 +594,7 @@ public class OpenAPINormalizer { } for (Map.Entry propertiesEntry : properties.entrySet()) { Schema property = propertiesEntry.getValue(); - Schema newProperty = normalizeSchema(property, visitedSchemas); + Schema newProperty = normalizeSchema(property, new HashSet<>()); propertiesEntry.setValue(newProperty); } } diff --git a/modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml b/modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml index e38732656f3..ebdaa239ea9 100644 --- a/modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml @@ -914,3 +914,33 @@ components: description: A list of "good output" node IDs in the prompt. items: type: string + circular_reference_1: + type: object + properties: + prop1: + $ref: '#/components/schemas/circular_reference_2' + circular_reference_2: + type: object + properties: + prop1: + $ref: '#/components/schemas/circular_reference_3' + circular_reference_3: + type: object + properties: + prop1: + $ref: '#/components/schemas/circular_reference_1' + array_of_same_ref: + type: object + properties: + arrayFooOne: + type: array + items: + $ref: '#/components/schemas/Tag' + arrayFooTwo: + type: array + items: + $ref: '#/components/schemas/Tag' + arrayFooThree: + type: array + items: + $ref: '#/components/schemas/Tag' diff --git a/samples/client/petstore/java/okhttp-gson-3.1/.openapi-generator/FILES b/samples/client/petstore/java/okhttp-gson-3.1/.openapi-generator/FILES index 5ba46e594fb..47d0d7f85f7 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/.openapi-generator/FILES +++ b/samples/client/petstore/java/okhttp-gson-3.1/.openapi-generator/FILES @@ -8,8 +8,12 @@ build.sbt docs/Animal.md docs/AnyOfArray.md docs/AnyTypeTest.md +docs/ArrayOfSameRef.md docs/Cat.md docs/Category.md +docs/CircularReference1.md +docs/CircularReference2.md +docs/CircularReference3.md docs/Dog.md docs/FakeApi.md docs/ModelApiResponse.md @@ -60,8 +64,12 @@ src/main/java/org/openapitools/client/model/AbstractOpenApiSchema.java src/main/java/org/openapitools/client/model/Animal.java src/main/java/org/openapitools/client/model/AnyOfArray.java src/main/java/org/openapitools/client/model/AnyTypeTest.java +src/main/java/org/openapitools/client/model/ArrayOfSameRef.java src/main/java/org/openapitools/client/model/Cat.java src/main/java/org/openapitools/client/model/Category.java +src/main/java/org/openapitools/client/model/CircularReference1.java +src/main/java/org/openapitools/client/model/CircularReference2.java +src/main/java/org/openapitools/client/model/CircularReference3.java src/main/java/org/openapitools/client/model/Dog.java src/main/java/org/openapitools/client/model/ModelApiResponse.java src/main/java/org/openapitools/client/model/OneOfStringOrInt.java diff --git a/samples/client/petstore/java/okhttp-gson-3.1/README.md b/samples/client/petstore/java/okhttp-gson-3.1/README.md index 81af2d7a47f..05de45aa90e 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/README.md +++ b/samples/client/petstore/java/okhttp-gson-3.1/README.md @@ -146,8 +146,12 @@ Class | Method | HTTP request | Description - [Animal](docs/Animal.md) - [AnyOfArray](docs/AnyOfArray.md) - [AnyTypeTest](docs/AnyTypeTest.md) + - [ArrayOfSameRef](docs/ArrayOfSameRef.md) - [Cat](docs/Cat.md) - [Category](docs/Category.md) + - [CircularReference1](docs/CircularReference1.md) + - [CircularReference2](docs/CircularReference2.md) + - [CircularReference3](docs/CircularReference3.md) - [Dog](docs/Dog.md) - [ModelApiResponse](docs/ModelApiResponse.md) - [OneOfStringOrInt](docs/OneOfStringOrInt.md) diff --git a/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml b/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml index 4d4b96f4761..6d512e1d644 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml +++ b/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml @@ -1015,6 +1015,32 @@ components: maxItems: 5 minItems: 3 type: array + circular_reference_1: + properties: + prop1: + $ref: '#/components/schemas/circular_reference_2' + circular_reference_2: + properties: + prop1: + $ref: '#/components/schemas/circular_reference_3' + circular_reference_3: + properties: + prop1: + $ref: '#/components/schemas/circular_reference_1' + array_of_same_ref: + properties: + arrayFooOne: + items: + $ref: '#/components/schemas/Tag' + type: array + arrayFooTwo: + items: + $ref: '#/components/schemas/Tag' + type: array + arrayFooThree: + items: + $ref: '#/components/schemas/Tag' + type: array updatePetWithForm_request: properties: name: diff --git a/samples/client/petstore/java/okhttp-gson-3.1/docs/ArrayOfSameRef.md b/samples/client/petstore/java/okhttp-gson-3.1/docs/ArrayOfSameRef.md new file mode 100644 index 00000000000..d4be6b19854 --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/docs/ArrayOfSameRef.md @@ -0,0 +1,15 @@ + + +# ArrayOfSameRef + + +## Properties + +| Name | Type | Description | Notes | +|------------ | ------------- | ------------- | -------------| +|**arrayFooOne** | [**List<Tag>**](Tag.md) | | [optional] | +|**arrayFooTwo** | [**List<Tag>**](Tag.md) | | [optional] | +|**arrayFooThree** | [**List<Tag>**](Tag.md) | | [optional] | + + + diff --git a/samples/client/petstore/java/okhttp-gson-3.1/docs/CircularReference1.md b/samples/client/petstore/java/okhttp-gson-3.1/docs/CircularReference1.md new file mode 100644 index 00000000000..57d2e5f35df --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/docs/CircularReference1.md @@ -0,0 +1,13 @@ + + +# CircularReference1 + + +## Properties + +| Name | Type | Description | Notes | +|------------ | ------------- | ------------- | -------------| +|**prop1** | [**CircularReference2**](CircularReference2.md) | | [optional] | + + + diff --git a/samples/client/petstore/java/okhttp-gson-3.1/docs/CircularReference2.md b/samples/client/petstore/java/okhttp-gson-3.1/docs/CircularReference2.md new file mode 100644 index 00000000000..08b2cb49b29 --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/docs/CircularReference2.md @@ -0,0 +1,13 @@ + + +# CircularReference2 + + +## Properties + +| Name | Type | Description | Notes | +|------------ | ------------- | ------------- | -------------| +|**prop1** | [**CircularReference3**](CircularReference3.md) | | [optional] | + + + diff --git a/samples/client/petstore/java/okhttp-gson-3.1/docs/CircularReference3.md b/samples/client/petstore/java/okhttp-gson-3.1/docs/CircularReference3.md new file mode 100644 index 00000000000..d34ffab11bc --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/docs/CircularReference3.md @@ -0,0 +1,13 @@ + + +# CircularReference3 + + +## Properties + +| Name | Type | Description | Notes | +|------------ | ------------- | ------------- | -------------| +|**prop1** | [**CircularReference1**](CircularReference1.md) | | [optional] | + + + diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/JSON.java index 1e8826d32e8..93f068247a0 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/JSON.java @@ -131,8 +131,12 @@ public class JSON { gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AnyOfArray.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AnyTypeTest.CustomTypeAdapterFactory()); + gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfSameRef.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Cat.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Category.CustomTypeAdapterFactory()); + gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.CircularReference1.CustomTypeAdapterFactory()); + gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.CircularReference2.CustomTypeAdapterFactory()); + gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.CircularReference3.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Dog.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelApiResponse.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.OneOfStringOrInt.CustomTypeAdapterFactory()); diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/ArrayOfSameRef.java b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/ArrayOfSameRef.java new file mode 100644 index 00000000000..4a53b78479a --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/ArrayOfSameRef.java @@ -0,0 +1,405 @@ +/* + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.model; + +import java.util.Objects; +import com.google.gson.TypeAdapter; +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 java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.openapitools.client.model.Tag; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.reflect.TypeToken; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.openapitools.client.JSON; + +/** + * ArrayOfSameRef + */ +@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT") +public class ArrayOfSameRef { + public static final String SERIALIZED_NAME_ARRAY_FOO_ONE = "arrayFooOne"; + @SerializedName(SERIALIZED_NAME_ARRAY_FOO_ONE) + private List arrayFooOne = new ArrayList<>(); + + public static final String SERIALIZED_NAME_ARRAY_FOO_TWO = "arrayFooTwo"; + @SerializedName(SERIALIZED_NAME_ARRAY_FOO_TWO) + private List arrayFooTwo = new ArrayList<>(); + + public static final String SERIALIZED_NAME_ARRAY_FOO_THREE = "arrayFooThree"; + @SerializedName(SERIALIZED_NAME_ARRAY_FOO_THREE) + private List arrayFooThree = new ArrayList<>(); + + public ArrayOfSameRef() { + } + + public ArrayOfSameRef arrayFooOne(List arrayFooOne) { + this.arrayFooOne = arrayFooOne; + return this; + } + + public ArrayOfSameRef addArrayFooOneItem(Tag arrayFooOneItem) { + if (this.arrayFooOne == null) { + this.arrayFooOne = new ArrayList<>(); + } + this.arrayFooOne.add(arrayFooOneItem); + return this; + } + + /** + * Get arrayFooOne + * @return arrayFooOne + **/ + @javax.annotation.Nullable + public List getArrayFooOne() { + return arrayFooOne; + } + + public void setArrayFooOne(List arrayFooOne) { + this.arrayFooOne = arrayFooOne; + } + + + public ArrayOfSameRef arrayFooTwo(List arrayFooTwo) { + this.arrayFooTwo = arrayFooTwo; + return this; + } + + public ArrayOfSameRef addArrayFooTwoItem(Tag arrayFooTwoItem) { + if (this.arrayFooTwo == null) { + this.arrayFooTwo = new ArrayList<>(); + } + this.arrayFooTwo.add(arrayFooTwoItem); + return this; + } + + /** + * Get arrayFooTwo + * @return arrayFooTwo + **/ + @javax.annotation.Nullable + public List getArrayFooTwo() { + return arrayFooTwo; + } + + public void setArrayFooTwo(List arrayFooTwo) { + this.arrayFooTwo = arrayFooTwo; + } + + + public ArrayOfSameRef arrayFooThree(List arrayFooThree) { + this.arrayFooThree = arrayFooThree; + return this; + } + + public ArrayOfSameRef addArrayFooThreeItem(Tag arrayFooThreeItem) { + if (this.arrayFooThree == null) { + this.arrayFooThree = new ArrayList<>(); + } + this.arrayFooThree.add(arrayFooThreeItem); + return this; + } + + /** + * Get arrayFooThree + * @return arrayFooThree + **/ + @javax.annotation.Nullable + public List getArrayFooThree() { + return arrayFooThree; + } + + public void setArrayFooThree(List arrayFooThree) { + this.arrayFooThree = arrayFooThree; + } + + /** + * A container for additional, undeclared properties. + * This is a holder for any undeclared properties as specified with + * the 'additionalProperties' keyword in the OAS document. + */ + private Map additionalProperties; + + /** + * Set the additional (undeclared) property with the specified name and value. + * If the property does not already exist, create it otherwise replace it. + * + * @param key name of the property + * @param value value of the property + * @return the ArrayOfSameRef instance itself + */ + public ArrayOfSameRef putAdditionalProperty(String key, Object value) { + if (this.additionalProperties == null) { + this.additionalProperties = new HashMap(); + } + this.additionalProperties.put(key, value); + return this; + } + + /** + * Return the additional (undeclared) property. + * + * @return a map of objects + */ + public Map getAdditionalProperties() { + return additionalProperties; + } + + /** + * Return the additional (undeclared) property with the specified name. + * + * @param key name of the property + * @return an object + */ + public Object getAdditionalProperty(String key) { + if (this.additionalProperties == null) { + return null; + } + return this.additionalProperties.get(key); + } + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ArrayOfSameRef arrayOfSameRef = (ArrayOfSameRef) o; + return Objects.equals(this.arrayFooOne, arrayOfSameRef.arrayFooOne) && + Objects.equals(this.arrayFooTwo, arrayOfSameRef.arrayFooTwo) && + Objects.equals(this.arrayFooThree, arrayOfSameRef.arrayFooThree)&& + Objects.equals(this.additionalProperties, arrayOfSameRef.additionalProperties); + } + + @Override + public int hashCode() { + return Objects.hash(arrayFooOne, arrayFooTwo, arrayFooThree, additionalProperties); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class ArrayOfSameRef {\n"); + sb.append(" arrayFooOne: ").append(toIndentedString(arrayFooOne)).append("\n"); + sb.append(" arrayFooTwo: ").append(toIndentedString(arrayFooTwo)).append("\n"); + sb.append(" arrayFooThree: ").append(toIndentedString(arrayFooThree)).append("\n"); + sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).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(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } + + + public static HashSet openapiFields; + public static HashSet openapiRequiredFields; + + static { + // a set of all properties/fields (JSON key names) + openapiFields = new HashSet(); + openapiFields.add("arrayFooOne"); + openapiFields.add("arrayFooTwo"); + openapiFields.add("arrayFooThree"); + + // a set of required properties/fields (JSON key names) + openapiRequiredFields = new HashSet(); + } + + /** + * 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 ArrayOfSameRef + */ + public static void validateJsonElement(JsonElement jsonElement) throws IOException { + if (jsonElement == null) { + if (!ArrayOfSameRef.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null + throw new IllegalArgumentException(String.format("The required field(s) %s in ArrayOfSameRef is not found in the empty JSON string", ArrayOfSameRef.openapiRequiredFields.toString())); + } + } + JsonObject jsonObj = jsonElement.getAsJsonObject(); + if (jsonObj.get("arrayFooOne") != null && !jsonObj.get("arrayFooOne").isJsonNull()) { + JsonArray jsonArrayarrayFooOne = jsonObj.getAsJsonArray("arrayFooOne"); + if (jsonArrayarrayFooOne != null) { + // ensure the json data is an array + if (!jsonObj.get("arrayFooOne").isJsonArray()) { + throw new IllegalArgumentException(String.format("Expected the field `arrayFooOne` to be an array in the JSON string but got `%s`", jsonObj.get("arrayFooOne").toString())); + } + + // validate the optional field `arrayFooOne` (array) + for (int i = 0; i < jsonArrayarrayFooOne.size(); i++) { + Tag.validateJsonElement(jsonArrayarrayFooOne.get(i)); + }; + } + } + if (jsonObj.get("arrayFooTwo") != null && !jsonObj.get("arrayFooTwo").isJsonNull()) { + JsonArray jsonArrayarrayFooTwo = jsonObj.getAsJsonArray("arrayFooTwo"); + if (jsonArrayarrayFooTwo != null) { + // ensure the json data is an array + if (!jsonObj.get("arrayFooTwo").isJsonArray()) { + throw new IllegalArgumentException(String.format("Expected the field `arrayFooTwo` to be an array in the JSON string but got `%s`", jsonObj.get("arrayFooTwo").toString())); + } + + // validate the optional field `arrayFooTwo` (array) + for (int i = 0; i < jsonArrayarrayFooTwo.size(); i++) { + Tag.validateJsonElement(jsonArrayarrayFooTwo.get(i)); + }; + } + } + if (jsonObj.get("arrayFooThree") != null && !jsonObj.get("arrayFooThree").isJsonNull()) { + JsonArray jsonArrayarrayFooThree = jsonObj.getAsJsonArray("arrayFooThree"); + if (jsonArrayarrayFooThree != null) { + // ensure the json data is an array + if (!jsonObj.get("arrayFooThree").isJsonArray()) { + throw new IllegalArgumentException(String.format("Expected the field `arrayFooThree` to be an array in the JSON string but got `%s`", jsonObj.get("arrayFooThree").toString())); + } + + // validate the optional field `arrayFooThree` (array) + for (int i = 0; i < jsonArrayarrayFooThree.size(); i++) { + Tag.validateJsonElement(jsonArrayarrayFooThree.get(i)); + }; + } + } + } + + public static class CustomTypeAdapterFactory implements TypeAdapterFactory { + @SuppressWarnings("unchecked") + @Override + public TypeAdapter create(Gson gson, TypeToken type) { + if (!ArrayOfSameRef.class.isAssignableFrom(type.getRawType())) { + return null; // this class only serializes 'ArrayOfSameRef' and its subtypes + } + final TypeAdapter elementAdapter = gson.getAdapter(JsonElement.class); + final TypeAdapter thisAdapter + = gson.getDelegateAdapter(this, TypeToken.get(ArrayOfSameRef.class)); + + return (TypeAdapter) new TypeAdapter() { + @Override + public void write(JsonWriter out, ArrayOfSameRef value) throws IOException { + JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject(); + obj.remove("additionalProperties"); + // serialize additional properties + if (value.getAdditionalProperties() != null) { + for (Map.Entry entry : value.getAdditionalProperties().entrySet()) { + if (entry.getValue() instanceof String) + obj.addProperty(entry.getKey(), (String) entry.getValue()); + else if (entry.getValue() instanceof Number) + obj.addProperty(entry.getKey(), (Number) entry.getValue()); + else if (entry.getValue() instanceof Boolean) + obj.addProperty(entry.getKey(), (Boolean) entry.getValue()); + else if (entry.getValue() instanceof Character) + obj.addProperty(entry.getKey(), (Character) entry.getValue()); + else { + JsonElement jsonElement = gson.toJsonTree(entry.getValue()); + if (jsonElement.isJsonArray()) { + obj.add(entry.getKey(), jsonElement.getAsJsonArray()); + } else { + obj.add(entry.getKey(), jsonElement.getAsJsonObject()); + } + } + } + } + elementAdapter.write(out, obj); + } + + @Override + public ArrayOfSameRef read(JsonReader in) throws IOException { + JsonElement jsonElement = elementAdapter.read(in); + validateJsonElement(jsonElement); + JsonObject jsonObj = jsonElement.getAsJsonObject(); + // store additional fields in the deserialized instance + ArrayOfSameRef instance = thisAdapter.fromJsonTree(jsonObj); + for (Map.Entry entry : jsonObj.entrySet()) { + if (!openapiFields.contains(entry.getKey())) { + if (entry.getValue().isJsonPrimitive()) { // primitive type + if (entry.getValue().getAsJsonPrimitive().isString()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsString()); + else if (entry.getValue().getAsJsonPrimitive().isNumber()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsNumber()); + else if (entry.getValue().getAsJsonPrimitive().isBoolean()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsBoolean()); + else + throw new IllegalArgumentException(String.format("The field `%s` has unknown primitive type. Value: %s", entry.getKey(), entry.getValue().toString())); + } else if (entry.getValue().isJsonArray()) { + instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), List.class)); + } else { // JSON object + instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), HashMap.class)); + } + } + } + return instance; + } + + }.nullSafe(); + } + } + + /** + * Create an instance of ArrayOfSameRef given an JSON string + * + * @param jsonString JSON string + * @return An instance of ArrayOfSameRef + * @throws IOException if the JSON string is invalid with respect to ArrayOfSameRef + */ + public static ArrayOfSameRef fromJson(String jsonString) throws IOException { + return JSON.getGson().fromJson(jsonString, ArrayOfSameRef.class); + } + + /** + * Convert an instance of ArrayOfSameRef to an JSON string + * + * @return JSON string + */ + public String toJson() { + return JSON.getGson().toJson(this); + } +} + diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/CircularReference1.java b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/CircularReference1.java new file mode 100644 index 00000000000..bcd71981989 --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/CircularReference1.java @@ -0,0 +1,289 @@ +/* + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.model; + +import java.util.Objects; +import com.google.gson.TypeAdapter; +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 java.io.IOException; +import java.util.Arrays; +import org.openapitools.client.model.CircularReference2; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.reflect.TypeToken; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.openapitools.client.JSON; + +/** + * CircularReference1 + */ +@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT") +public class CircularReference1 { + public static final String SERIALIZED_NAME_PROP1 = "prop1"; + @SerializedName(SERIALIZED_NAME_PROP1) + private CircularReference2 prop1; + + public CircularReference1() { + } + + public CircularReference1 prop1(CircularReference2 prop1) { + this.prop1 = prop1; + return this; + } + + /** + * Get prop1 + * @return prop1 + **/ + @javax.annotation.Nullable + public CircularReference2 getProp1() { + return prop1; + } + + public void setProp1(CircularReference2 prop1) { + this.prop1 = prop1; + } + + /** + * A container for additional, undeclared properties. + * This is a holder for any undeclared properties as specified with + * the 'additionalProperties' keyword in the OAS document. + */ + private Map additionalProperties; + + /** + * Set the additional (undeclared) property with the specified name and value. + * If the property does not already exist, create it otherwise replace it. + * + * @param key name of the property + * @param value value of the property + * @return the CircularReference1 instance itself + */ + public CircularReference1 putAdditionalProperty(String key, Object value) { + if (this.additionalProperties == null) { + this.additionalProperties = new HashMap(); + } + this.additionalProperties.put(key, value); + return this; + } + + /** + * Return the additional (undeclared) property. + * + * @return a map of objects + */ + public Map getAdditionalProperties() { + return additionalProperties; + } + + /** + * Return the additional (undeclared) property with the specified name. + * + * @param key name of the property + * @return an object + */ + public Object getAdditionalProperty(String key) { + if (this.additionalProperties == null) { + return null; + } + return this.additionalProperties.get(key); + } + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + CircularReference1 circularReference1 = (CircularReference1) o; + return Objects.equals(this.prop1, circularReference1.prop1)&& + Objects.equals(this.additionalProperties, circularReference1.additionalProperties); + } + + @Override + public int hashCode() { + return Objects.hash(prop1, additionalProperties); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class CircularReference1 {\n"); + sb.append(" prop1: ").append(toIndentedString(prop1)).append("\n"); + sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).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(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } + + + public static HashSet openapiFields; + public static HashSet openapiRequiredFields; + + static { + // a set of all properties/fields (JSON key names) + openapiFields = new HashSet(); + openapiFields.add("prop1"); + + // a set of required properties/fields (JSON key names) + openapiRequiredFields = new HashSet(); + } + + /** + * 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 CircularReference1 + */ + public static void validateJsonElement(JsonElement jsonElement) throws IOException { + if (jsonElement == null) { + if (!CircularReference1.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null + throw new IllegalArgumentException(String.format("The required field(s) %s in CircularReference1 is not found in the empty JSON string", CircularReference1.openapiRequiredFields.toString())); + } + } + JsonObject jsonObj = jsonElement.getAsJsonObject(); + // validate the optional field `prop1` + if (jsonObj.get("prop1") != null && !jsonObj.get("prop1").isJsonNull()) { + CircularReference2.validateJsonElement(jsonObj.get("prop1")); + } + } + + public static class CustomTypeAdapterFactory implements TypeAdapterFactory { + @SuppressWarnings("unchecked") + @Override + public TypeAdapter create(Gson gson, TypeToken type) { + if (!CircularReference1.class.isAssignableFrom(type.getRawType())) { + return null; // this class only serializes 'CircularReference1' and its subtypes + } + final TypeAdapter elementAdapter = gson.getAdapter(JsonElement.class); + final TypeAdapter thisAdapter + = gson.getDelegateAdapter(this, TypeToken.get(CircularReference1.class)); + + return (TypeAdapter) new TypeAdapter() { + @Override + public void write(JsonWriter out, CircularReference1 value) throws IOException { + JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject(); + obj.remove("additionalProperties"); + // serialize additional properties + if (value.getAdditionalProperties() != null) { + for (Map.Entry entry : value.getAdditionalProperties().entrySet()) { + if (entry.getValue() instanceof String) + obj.addProperty(entry.getKey(), (String) entry.getValue()); + else if (entry.getValue() instanceof Number) + obj.addProperty(entry.getKey(), (Number) entry.getValue()); + else if (entry.getValue() instanceof Boolean) + obj.addProperty(entry.getKey(), (Boolean) entry.getValue()); + else if (entry.getValue() instanceof Character) + obj.addProperty(entry.getKey(), (Character) entry.getValue()); + else { + JsonElement jsonElement = gson.toJsonTree(entry.getValue()); + if (jsonElement.isJsonArray()) { + obj.add(entry.getKey(), jsonElement.getAsJsonArray()); + } else { + obj.add(entry.getKey(), jsonElement.getAsJsonObject()); + } + } + } + } + elementAdapter.write(out, obj); + } + + @Override + public CircularReference1 read(JsonReader in) throws IOException { + JsonElement jsonElement = elementAdapter.read(in); + validateJsonElement(jsonElement); + JsonObject jsonObj = jsonElement.getAsJsonObject(); + // store additional fields in the deserialized instance + CircularReference1 instance = thisAdapter.fromJsonTree(jsonObj); + for (Map.Entry entry : jsonObj.entrySet()) { + if (!openapiFields.contains(entry.getKey())) { + if (entry.getValue().isJsonPrimitive()) { // primitive type + if (entry.getValue().getAsJsonPrimitive().isString()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsString()); + else if (entry.getValue().getAsJsonPrimitive().isNumber()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsNumber()); + else if (entry.getValue().getAsJsonPrimitive().isBoolean()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsBoolean()); + else + throw new IllegalArgumentException(String.format("The field `%s` has unknown primitive type. Value: %s", entry.getKey(), entry.getValue().toString())); + } else if (entry.getValue().isJsonArray()) { + instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), List.class)); + } else { // JSON object + instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), HashMap.class)); + } + } + } + return instance; + } + + }.nullSafe(); + } + } + + /** + * Create an instance of CircularReference1 given an JSON string + * + * @param jsonString JSON string + * @return An instance of CircularReference1 + * @throws IOException if the JSON string is invalid with respect to CircularReference1 + */ + public static CircularReference1 fromJson(String jsonString) throws IOException { + return JSON.getGson().fromJson(jsonString, CircularReference1.class); + } + + /** + * Convert an instance of CircularReference1 to an JSON string + * + * @return JSON string + */ + public String toJson() { + return JSON.getGson().toJson(this); + } +} + diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/CircularReference2.java b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/CircularReference2.java new file mode 100644 index 00000000000..239d41bf65c --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/CircularReference2.java @@ -0,0 +1,289 @@ +/* + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.model; + +import java.util.Objects; +import com.google.gson.TypeAdapter; +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 java.io.IOException; +import java.util.Arrays; +import org.openapitools.client.model.CircularReference3; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.reflect.TypeToken; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.openapitools.client.JSON; + +/** + * CircularReference2 + */ +@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT") +public class CircularReference2 { + public static final String SERIALIZED_NAME_PROP1 = "prop1"; + @SerializedName(SERIALIZED_NAME_PROP1) + private CircularReference3 prop1; + + public CircularReference2() { + } + + public CircularReference2 prop1(CircularReference3 prop1) { + this.prop1 = prop1; + return this; + } + + /** + * Get prop1 + * @return prop1 + **/ + @javax.annotation.Nullable + public CircularReference3 getProp1() { + return prop1; + } + + public void setProp1(CircularReference3 prop1) { + this.prop1 = prop1; + } + + /** + * A container for additional, undeclared properties. + * This is a holder for any undeclared properties as specified with + * the 'additionalProperties' keyword in the OAS document. + */ + private Map additionalProperties; + + /** + * Set the additional (undeclared) property with the specified name and value. + * If the property does not already exist, create it otherwise replace it. + * + * @param key name of the property + * @param value value of the property + * @return the CircularReference2 instance itself + */ + public CircularReference2 putAdditionalProperty(String key, Object value) { + if (this.additionalProperties == null) { + this.additionalProperties = new HashMap(); + } + this.additionalProperties.put(key, value); + return this; + } + + /** + * Return the additional (undeclared) property. + * + * @return a map of objects + */ + public Map getAdditionalProperties() { + return additionalProperties; + } + + /** + * Return the additional (undeclared) property with the specified name. + * + * @param key name of the property + * @return an object + */ + public Object getAdditionalProperty(String key) { + if (this.additionalProperties == null) { + return null; + } + return this.additionalProperties.get(key); + } + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + CircularReference2 circularReference2 = (CircularReference2) o; + return Objects.equals(this.prop1, circularReference2.prop1)&& + Objects.equals(this.additionalProperties, circularReference2.additionalProperties); + } + + @Override + public int hashCode() { + return Objects.hash(prop1, additionalProperties); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class CircularReference2 {\n"); + sb.append(" prop1: ").append(toIndentedString(prop1)).append("\n"); + sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).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(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } + + + public static HashSet openapiFields; + public static HashSet openapiRequiredFields; + + static { + // a set of all properties/fields (JSON key names) + openapiFields = new HashSet(); + openapiFields.add("prop1"); + + // a set of required properties/fields (JSON key names) + openapiRequiredFields = new HashSet(); + } + + /** + * 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 CircularReference2 + */ + public static void validateJsonElement(JsonElement jsonElement) throws IOException { + if (jsonElement == null) { + if (!CircularReference2.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null + throw new IllegalArgumentException(String.format("The required field(s) %s in CircularReference2 is not found in the empty JSON string", CircularReference2.openapiRequiredFields.toString())); + } + } + JsonObject jsonObj = jsonElement.getAsJsonObject(); + // validate the optional field `prop1` + if (jsonObj.get("prop1") != null && !jsonObj.get("prop1").isJsonNull()) { + CircularReference3.validateJsonElement(jsonObj.get("prop1")); + } + } + + public static class CustomTypeAdapterFactory implements TypeAdapterFactory { + @SuppressWarnings("unchecked") + @Override + public TypeAdapter create(Gson gson, TypeToken type) { + if (!CircularReference2.class.isAssignableFrom(type.getRawType())) { + return null; // this class only serializes 'CircularReference2' and its subtypes + } + final TypeAdapter elementAdapter = gson.getAdapter(JsonElement.class); + final TypeAdapter thisAdapter + = gson.getDelegateAdapter(this, TypeToken.get(CircularReference2.class)); + + return (TypeAdapter) new TypeAdapter() { + @Override + public void write(JsonWriter out, CircularReference2 value) throws IOException { + JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject(); + obj.remove("additionalProperties"); + // serialize additional properties + if (value.getAdditionalProperties() != null) { + for (Map.Entry entry : value.getAdditionalProperties().entrySet()) { + if (entry.getValue() instanceof String) + obj.addProperty(entry.getKey(), (String) entry.getValue()); + else if (entry.getValue() instanceof Number) + obj.addProperty(entry.getKey(), (Number) entry.getValue()); + else if (entry.getValue() instanceof Boolean) + obj.addProperty(entry.getKey(), (Boolean) entry.getValue()); + else if (entry.getValue() instanceof Character) + obj.addProperty(entry.getKey(), (Character) entry.getValue()); + else { + JsonElement jsonElement = gson.toJsonTree(entry.getValue()); + if (jsonElement.isJsonArray()) { + obj.add(entry.getKey(), jsonElement.getAsJsonArray()); + } else { + obj.add(entry.getKey(), jsonElement.getAsJsonObject()); + } + } + } + } + elementAdapter.write(out, obj); + } + + @Override + public CircularReference2 read(JsonReader in) throws IOException { + JsonElement jsonElement = elementAdapter.read(in); + validateJsonElement(jsonElement); + JsonObject jsonObj = jsonElement.getAsJsonObject(); + // store additional fields in the deserialized instance + CircularReference2 instance = thisAdapter.fromJsonTree(jsonObj); + for (Map.Entry entry : jsonObj.entrySet()) { + if (!openapiFields.contains(entry.getKey())) { + if (entry.getValue().isJsonPrimitive()) { // primitive type + if (entry.getValue().getAsJsonPrimitive().isString()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsString()); + else if (entry.getValue().getAsJsonPrimitive().isNumber()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsNumber()); + else if (entry.getValue().getAsJsonPrimitive().isBoolean()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsBoolean()); + else + throw new IllegalArgumentException(String.format("The field `%s` has unknown primitive type. Value: %s", entry.getKey(), entry.getValue().toString())); + } else if (entry.getValue().isJsonArray()) { + instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), List.class)); + } else { // JSON object + instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), HashMap.class)); + } + } + } + return instance; + } + + }.nullSafe(); + } + } + + /** + * Create an instance of CircularReference2 given an JSON string + * + * @param jsonString JSON string + * @return An instance of CircularReference2 + * @throws IOException if the JSON string is invalid with respect to CircularReference2 + */ + public static CircularReference2 fromJson(String jsonString) throws IOException { + return JSON.getGson().fromJson(jsonString, CircularReference2.class); + } + + /** + * Convert an instance of CircularReference2 to an JSON string + * + * @return JSON string + */ + public String toJson() { + return JSON.getGson().toJson(this); + } +} + diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/CircularReference3.java b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/CircularReference3.java new file mode 100644 index 00000000000..421f06f64ef --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/CircularReference3.java @@ -0,0 +1,289 @@ +/* + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.model; + +import java.util.Objects; +import com.google.gson.TypeAdapter; +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 java.io.IOException; +import java.util.Arrays; +import org.openapitools.client.model.CircularReference1; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.reflect.TypeToken; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.openapitools.client.JSON; + +/** + * CircularReference3 + */ +@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT") +public class CircularReference3 { + public static final String SERIALIZED_NAME_PROP1 = "prop1"; + @SerializedName(SERIALIZED_NAME_PROP1) + private CircularReference1 prop1; + + public CircularReference3() { + } + + public CircularReference3 prop1(CircularReference1 prop1) { + this.prop1 = prop1; + return this; + } + + /** + * Get prop1 + * @return prop1 + **/ + @javax.annotation.Nullable + public CircularReference1 getProp1() { + return prop1; + } + + public void setProp1(CircularReference1 prop1) { + this.prop1 = prop1; + } + + /** + * A container for additional, undeclared properties. + * This is a holder for any undeclared properties as specified with + * the 'additionalProperties' keyword in the OAS document. + */ + private Map additionalProperties; + + /** + * Set the additional (undeclared) property with the specified name and value. + * If the property does not already exist, create it otherwise replace it. + * + * @param key name of the property + * @param value value of the property + * @return the CircularReference3 instance itself + */ + public CircularReference3 putAdditionalProperty(String key, Object value) { + if (this.additionalProperties == null) { + this.additionalProperties = new HashMap(); + } + this.additionalProperties.put(key, value); + return this; + } + + /** + * Return the additional (undeclared) property. + * + * @return a map of objects + */ + public Map getAdditionalProperties() { + return additionalProperties; + } + + /** + * Return the additional (undeclared) property with the specified name. + * + * @param key name of the property + * @return an object + */ + public Object getAdditionalProperty(String key) { + if (this.additionalProperties == null) { + return null; + } + return this.additionalProperties.get(key); + } + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + CircularReference3 circularReference3 = (CircularReference3) o; + return Objects.equals(this.prop1, circularReference3.prop1)&& + Objects.equals(this.additionalProperties, circularReference3.additionalProperties); + } + + @Override + public int hashCode() { + return Objects.hash(prop1, additionalProperties); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class CircularReference3 {\n"); + sb.append(" prop1: ").append(toIndentedString(prop1)).append("\n"); + sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).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(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } + + + public static HashSet openapiFields; + public static HashSet openapiRequiredFields; + + static { + // a set of all properties/fields (JSON key names) + openapiFields = new HashSet(); + openapiFields.add("prop1"); + + // a set of required properties/fields (JSON key names) + openapiRequiredFields = new HashSet(); + } + + /** + * 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 CircularReference3 + */ + public static void validateJsonElement(JsonElement jsonElement) throws IOException { + if (jsonElement == null) { + if (!CircularReference3.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null + throw new IllegalArgumentException(String.format("The required field(s) %s in CircularReference3 is not found in the empty JSON string", CircularReference3.openapiRequiredFields.toString())); + } + } + JsonObject jsonObj = jsonElement.getAsJsonObject(); + // validate the optional field `prop1` + if (jsonObj.get("prop1") != null && !jsonObj.get("prop1").isJsonNull()) { + CircularReference1.validateJsonElement(jsonObj.get("prop1")); + } + } + + public static class CustomTypeAdapterFactory implements TypeAdapterFactory { + @SuppressWarnings("unchecked") + @Override + public TypeAdapter create(Gson gson, TypeToken type) { + if (!CircularReference3.class.isAssignableFrom(type.getRawType())) { + return null; // this class only serializes 'CircularReference3' and its subtypes + } + final TypeAdapter elementAdapter = gson.getAdapter(JsonElement.class); + final TypeAdapter thisAdapter + = gson.getDelegateAdapter(this, TypeToken.get(CircularReference3.class)); + + return (TypeAdapter) new TypeAdapter() { + @Override + public void write(JsonWriter out, CircularReference3 value) throws IOException { + JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject(); + obj.remove("additionalProperties"); + // serialize additional properties + if (value.getAdditionalProperties() != null) { + for (Map.Entry entry : value.getAdditionalProperties().entrySet()) { + if (entry.getValue() instanceof String) + obj.addProperty(entry.getKey(), (String) entry.getValue()); + else if (entry.getValue() instanceof Number) + obj.addProperty(entry.getKey(), (Number) entry.getValue()); + else if (entry.getValue() instanceof Boolean) + obj.addProperty(entry.getKey(), (Boolean) entry.getValue()); + else if (entry.getValue() instanceof Character) + obj.addProperty(entry.getKey(), (Character) entry.getValue()); + else { + JsonElement jsonElement = gson.toJsonTree(entry.getValue()); + if (jsonElement.isJsonArray()) { + obj.add(entry.getKey(), jsonElement.getAsJsonArray()); + } else { + obj.add(entry.getKey(), jsonElement.getAsJsonObject()); + } + } + } + } + elementAdapter.write(out, obj); + } + + @Override + public CircularReference3 read(JsonReader in) throws IOException { + JsonElement jsonElement = elementAdapter.read(in); + validateJsonElement(jsonElement); + JsonObject jsonObj = jsonElement.getAsJsonObject(); + // store additional fields in the deserialized instance + CircularReference3 instance = thisAdapter.fromJsonTree(jsonObj); + for (Map.Entry entry : jsonObj.entrySet()) { + if (!openapiFields.contains(entry.getKey())) { + if (entry.getValue().isJsonPrimitive()) { // primitive type + if (entry.getValue().getAsJsonPrimitive().isString()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsString()); + else if (entry.getValue().getAsJsonPrimitive().isNumber()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsNumber()); + else if (entry.getValue().getAsJsonPrimitive().isBoolean()) + instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsBoolean()); + else + throw new IllegalArgumentException(String.format("The field `%s` has unknown primitive type. Value: %s", entry.getKey(), entry.getValue().toString())); + } else if (entry.getValue().isJsonArray()) { + instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), List.class)); + } else { // JSON object + instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), HashMap.class)); + } + } + } + return instance; + } + + }.nullSafe(); + } + } + + /** + * Create an instance of CircularReference3 given an JSON string + * + * @param jsonString JSON string + * @return An instance of CircularReference3 + * @throws IOException if the JSON string is invalid with respect to CircularReference3 + */ + public static CircularReference3 fromJson(String jsonString) throws IOException { + return JSON.getGson().fromJson(jsonString, CircularReference3.class); + } + + /** + * Convert an instance of CircularReference3 to an JSON string + * + * @return JSON string + */ + public String toJson() { + return JSON.getGson().toJson(this); + } +} + diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/ArrayOfSameRefTest.java b/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/ArrayOfSameRefTest.java new file mode 100644 index 00000000000..0a2e22f4c71 --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/ArrayOfSameRefTest.java @@ -0,0 +1,67 @@ +/* + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.model; + +import com.google.gson.TypeAdapter; +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 java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.openapitools.client.model.Tag; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * Model tests for ArrayOfSameRef + */ +public class ArrayOfSameRefTest { + private final ArrayOfSameRef model = new ArrayOfSameRef(); + + /** + * Model tests for ArrayOfSameRef + */ + @Test + public void testArrayOfSameRef() { + // TODO: test ArrayOfSameRef + } + + /** + * Test the property 'arrayFooOne' + */ + @Test + public void arrayFooOneTest() { + // TODO: test arrayFooOne + } + + /** + * Test the property 'arrayFooTwo' + */ + @Test + public void arrayFooTwoTest() { + // TODO: test arrayFooTwo + } + + /** + * Test the property 'arrayFooThree' + */ + @Test + public void arrayFooThreeTest() { + // TODO: test arrayFooThree + } + +} diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/CircularReference1Test.java b/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/CircularReference1Test.java new file mode 100644 index 00000000000..ed5b5a50747 --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/CircularReference1Test.java @@ -0,0 +1,49 @@ +/* + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.model; + +import com.google.gson.TypeAdapter; +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 java.io.IOException; +import java.util.Arrays; +import org.openapitools.client.model.CircularReference2; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * Model tests for CircularReference1 + */ +public class CircularReference1Test { + private final CircularReference1 model = new CircularReference1(); + + /** + * Model tests for CircularReference1 + */ + @Test + public void testCircularReference1() { + // TODO: test CircularReference1 + } + + /** + * Test the property 'prop1' + */ + @Test + public void prop1Test() { + // TODO: test prop1 + } + +} diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/CircularReference2Test.java b/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/CircularReference2Test.java new file mode 100644 index 00000000000..47a42fa1230 --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/CircularReference2Test.java @@ -0,0 +1,49 @@ +/* + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.model; + +import com.google.gson.TypeAdapter; +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 java.io.IOException; +import java.util.Arrays; +import org.openapitools.client.model.CircularReference3; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * Model tests for CircularReference2 + */ +public class CircularReference2Test { + private final CircularReference2 model = new CircularReference2(); + + /** + * Model tests for CircularReference2 + */ + @Test + public void testCircularReference2() { + // TODO: test CircularReference2 + } + + /** + * Test the property 'prop1' + */ + @Test + public void prop1Test() { + // TODO: test prop1 + } + +} diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/CircularReference3Test.java b/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/CircularReference3Test.java new file mode 100644 index 00000000000..72059095445 --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/test/java/org/openapitools/client/model/CircularReference3Test.java @@ -0,0 +1,49 @@ +/* + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.model; + +import com.google.gson.TypeAdapter; +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 java.io.IOException; +import java.util.Arrays; +import org.openapitools.client.model.CircularReference1; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * Model tests for CircularReference3 + */ +public class CircularReference3Test { + private final CircularReference3 model = new CircularReference3(); + + /** + * Model tests for CircularReference3 + */ + @Test + public void testCircularReference3() { + // TODO: test CircularReference3 + } + + /** + * Test the property 'prop1' + */ + @Test + public void prop1Test() { + // TODO: test prop1 + } + +} diff --git a/w b/w new file mode 100644 index 00000000000..eb9dd279b14 --- /dev/null +++ b/w @@ -0,0 +1,51 @@ +openapi: 3.1.0 +info: + title: "" + version: "" + +paths: + /user/getInfo: + get: + operationId: getUserInfo + responses: + "200": + content: + application/json: + schema: + $ref: '#/components/schemas/Bar' + description: | + OK + security: + - Session: [] + x-accepts: application/json + +components: + schemas: + Foo: + type: object + required: + - arrayOfStrings + properties: + arrayOfStrings: + type: array + items: + type: string + + Bar: + allOf: + - $ref: '#/components/schemas/Foo' + Hello: + type: object + properties: + arrayFooOne: + type: array + items: + $ref: '#/components/schemas/Foo' + arrayFooTwo: + type: array + items: + $ref: '#/components/schemas/Foo' + arrayFooThree: + type: array + items: + $ref: '#/components/schemas/Foo'