[refactor] Use getType in ModelUtils (#18577)

* use getType in model utils

* add tests

* update
This commit is contained in:
William Cheng
2024-05-07 18:05:22 +08:00
committed by GitHub
parent be94c22d08
commit 9929d35166
14 changed files with 816 additions and 58 deletions

View File

@@ -2492,10 +2492,10 @@ public class DefaultCodegen implements CodegenConfig {
private String getPrimitiveType(Schema schema) {
if (schema == null) {
throw new RuntimeException("schema cannot be null in getPrimitiveType");
} else if (typeMapping.containsKey(schema.getType() + "+" + schema.getFormat())) {
} else if (typeMapping.containsKey(ModelUtils.getType(schema) + "+" + schema.getFormat())) {
// allows custom type_format mapping.
// use {type}+{format}
return typeMapping.get(schema.getType() + "+" + schema.getFormat());
return typeMapping.get(ModelUtils.getType(schema) + "+" + schema.getFormat());
} else if (ModelUtils.isNullType(schema)) {
// The 'null' type is allowed in OAS 3.1 and above. It is not supported by OAS 3.0.x,
// though this tooling supports it.
@@ -2536,7 +2536,7 @@ public class DefaultCodegen implements CodegenConfig {
} else if (ModelUtils.isShortSchema(schema)) {// int32
return "integer";
} else {
return schema.getType(); // integer
return ModelUtils.getType(schema); // integer
}
} else if (ModelUtils.isMapSchema(schema)) {
return "map";
@@ -2567,11 +2567,11 @@ public class DefaultCodegen implements CodegenConfig {
return "object";
} else if (ModelUtils.isAnyType(schema)) {
return "AnyType";
} else if (StringUtils.isNotEmpty(schema.getType())) {
if (!schemaMapping.containsKey(schema.getType())) {
LOGGER.warn("Unknown type found in the schema: {}. To map it, please use the schema mapping option (e.g. --schema-mappings in CLI)", schema.getType());
} else if (StringUtils.isNotEmpty(ModelUtils.getType(schema))) {
if (!schemaMapping.containsKey(ModelUtils.getType(schema))) {
LOGGER.warn("Unknown type found in the schema: {}. To map it, please use the schema mapping option (e.g. --schema-mappings in CLI)", ModelUtils.getType(schema));
}
return schema.getType();
return ModelUtils.getType(schema);
}
// The 'type' attribute has not been set in the OAS schema, which means the value
// can be an arbitrary type, e.g. integer, string, object, array, number...
@@ -4019,10 +4019,10 @@ public class DefaultCodegen implements CodegenConfig {
property.name = toVarName(name);
property.baseName = name;
if (p.getType() == null) {
if (ModelUtils.getType(p) == null) {
property.openApiType = getSchemaType(p);
} else {
property.openApiType = p.getType();
property.openApiType = ModelUtils.getType(p);
}
property.nameInPascalCase = camelize(property.name);
property.nameInCamelCase = camelize(property.name, LOWERCASE_FIRST_LETTER);
@@ -7128,7 +7128,7 @@ public class DefaultCodegen implements CodegenConfig {
Schema original = null;
// check if it's allOf (only 1 sub schema) with or without default/nullable/etc set in the top level
if (ModelUtils.isAllOf(schema) && schema.getAllOf().size() == 1 &&
schema.getType() == null && schema.getTypes() == null) {
ModelUtils.getType(schema) == null) {
if (schema.getAllOf().get(0) instanceof Schema) {
original = schema;
schema = (Schema) schema.getAllOf().get(0);
@@ -7781,7 +7781,7 @@ public class DefaultCodegen implements CodegenConfig {
Schema original = null;
// check if it's allOf (only 1 sub schema) with or without default/nullable/etc set in the top level
if (ModelUtils.isAllOf(schema) && schema.getAllOf().size() == 1 &&
schema.getType() == null && schema.getTypes() == null) {
ModelUtils.getType(schema) == null) {
if (schema.getAllOf().get(0) instanceof Schema) {
original = schema;
schema = (Schema) schema.getAllOf().get(0);

View File

@@ -419,16 +419,7 @@ public class ModelUtils {
* @return true if the specified schema is an Object schema.
*/
public static boolean isTypeObjectSchema(Schema schema) {
if (schema instanceof JsonSchema) { // 3.1 spec
if (schema.getTypes() != null && schema.getTypes().size() == 1) {
return SchemaTypeUtil.OBJECT_TYPE.equals(schema.getTypes().iterator().next());
} else {
// null type or multiple types, e.g. [string, integer]
return false;
}
} else { // 3.0.x or 2.0 spec
return SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType());
}
return SchemaTypeUtil.OBJECT_TYPE.equals(getType(schema));
}
/**
@@ -460,9 +451,9 @@ public class ModelUtils {
return (schema instanceof ObjectSchema) ||
// must not be a map
(SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType()) && !(ModelUtils.isMapSchema(schema))) ||
(SchemaTypeUtil.OBJECT_TYPE.equals(getType(schema)) && !(ModelUtils.isMapSchema(schema))) ||
// must have at least one property
(schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
(getType(schema) == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
}
/**
@@ -594,17 +585,7 @@ public class ModelUtils {
if (schema == null) {
return false;
}
if (schema instanceof JsonSchema) { // 3.1 spec
if (schema.getTypes() != null && schema.getTypes().contains("array")) {
return true;
} else {
return false;
}
} else { // 3.0 spec
return (schema instanceof ArraySchema) || "array".equals(schema.getType());
}
return (schema instanceof ArraySchema) || "array".equals(getType(schema));
}
/**
@@ -636,89 +617,89 @@ public class ModelUtils {
}
public static boolean isStringSchema(Schema schema) {
return schema instanceof StringSchema || SchemaTypeUtil.STRING_TYPE.equals(schema.getType());
return schema instanceof StringSchema || SchemaTypeUtil.STRING_TYPE.equals(getType(schema));
}
public static boolean isIntegerSchema(Schema schema) {
return schema instanceof IntegerSchema || SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType());
return schema instanceof IntegerSchema || SchemaTypeUtil.INTEGER_TYPE.equals(getType(schema));
}
public static boolean isShortSchema(Schema schema) {
// format: short (int32)
return SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) // type: integer
return SchemaTypeUtil.INTEGER_TYPE.equals(getType(schema)) // type: integer
&& SchemaTypeUtil.INTEGER32_FORMAT.equals(schema.getFormat());
}
public static boolean isUnsignedIntegerSchema(Schema schema) {
return SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) && // type: integer
return SchemaTypeUtil.INTEGER_TYPE.equals(getType(schema)) && // type: integer
("int32".equals(schema.getFormat()) || schema.getFormat() == null) && // format: int32
(schema.getExtensions() != null && (Boolean) schema.getExtensions().getOrDefault("x-unsigned", Boolean.FALSE));
}
public static boolean isLongSchema(Schema schema) {
// format: long (int64)
return SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) // type: integer
return SchemaTypeUtil.INTEGER_TYPE.equals(getType(schema)) // type: integer
&& SchemaTypeUtil.INTEGER64_FORMAT.equals(schema.getFormat());
}
public static boolean isUnsignedLongSchema(Schema schema) {
return SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) && // type: integer
return SchemaTypeUtil.INTEGER_TYPE.equals(getType(schema)) && // type: integer
"int64".equals(schema.getFormat()) && // format: int64
(schema.getExtensions() != null && (Boolean) schema.getExtensions().getOrDefault("x-unsigned", Boolean.FALSE));
}
public static boolean isBooleanSchema(Schema schema) {
return schema instanceof BooleanSchema || SchemaTypeUtil.BOOLEAN_TYPE.equals(schema.getType());
return schema instanceof BooleanSchema || SchemaTypeUtil.BOOLEAN_TYPE.equals(getType(schema));
}
public static boolean isNumberSchema(Schema schema) {
return schema instanceof NumberSchema || SchemaTypeUtil.NUMBER_TYPE.equals(schema.getType());
return schema instanceof NumberSchema || SchemaTypeUtil.NUMBER_TYPE.equals(getType(schema));
}
public static boolean isFloatSchema(Schema schema) {
// format: float
return SchemaTypeUtil.NUMBER_TYPE.equals(schema.getType())
return SchemaTypeUtil.NUMBER_TYPE.equals(getType(schema))
&& SchemaTypeUtil.FLOAT_FORMAT.equals(schema.getFormat());
}
public static boolean isDoubleSchema(Schema schema) {
// format: double
return SchemaTypeUtil.NUMBER_TYPE.equals(schema.getType())
return SchemaTypeUtil.NUMBER_TYPE.equals(getType(schema))
&& SchemaTypeUtil.DOUBLE_FORMAT.equals(schema.getFormat());
}
public static boolean isDateSchema(Schema schema) {
return (schema instanceof DateSchema) ||
// format: date
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
(SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
&& SchemaTypeUtil.DATE_FORMAT.equals(schema.getFormat()));
}
public static boolean isDateTimeSchema(Schema schema) {
return (schema instanceof DateTimeSchema) ||
// format: date-time
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
(SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
&& SchemaTypeUtil.DATE_TIME_FORMAT.equals(schema.getFormat()));
}
public static boolean isPasswordSchema(Schema schema) {
return (schema instanceof PasswordSchema) ||
// double
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
(SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
&& SchemaTypeUtil.PASSWORD_FORMAT.equals(schema.getFormat()));
}
public static boolean isByteArraySchema(Schema schema) {
return (schema instanceof ByteArraySchema) ||
// format: byte
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
(SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
&& SchemaTypeUtil.BYTE_FORMAT.equals(schema.getFormat()));
}
public static boolean isBinarySchema(Schema schema) {
return (schema instanceof BinarySchema) ||
// format: binary
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
(SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
&& SchemaTypeUtil.BINARY_FORMAT.equals(schema.getFormat()));
}
@@ -731,26 +712,26 @@ public class ModelUtils {
public static boolean isUUIDSchema(Schema schema) {
return (schema instanceof UUIDSchema) ||
// format: uuid
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
(SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
&& SchemaTypeUtil.UUID_FORMAT.equals(schema.getFormat()));
}
public static boolean isURISchema(Schema schema) {
// format: uri
return SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
return SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
&& URI_FORMAT.equals(schema.getFormat());
}
public static boolean isEmailSchema(Schema schema) {
return (schema instanceof EmailSchema) ||
// format: email
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
(SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
&& SchemaTypeUtil.EMAIL_FORMAT.equals(schema.getFormat()));
}
public static boolean isDecimalSchema(Schema schema) {
// format: number
return SchemaTypeUtil.STRING_TYPE.equals(schema.getType()) // type: string
return SchemaTypeUtil.STRING_TYPE.equals(getType(schema)) // type: string
&& "number".equals(schema.getFormat());
}
@@ -898,7 +879,7 @@ public class ModelUtils {
}
// has at least one property
if ("object".equals(schema.getType())) {
if ("object".equals(getType(schema))) {
// no properties
if ((schema.getProperties() == null || schema.getProperties().isEmpty())) {
Schema addlProps = ModelUtils.getAdditionalProperties(schema);
@@ -1782,7 +1763,7 @@ public class ModelUtils {
* @return true if the schema is the 'null' type
*/
public static boolean isNullType(Schema schema) {
return "null".equals(schema.getType());
return "null".equals(getType(schema));
}
/**
@@ -1798,7 +1779,7 @@ public class ModelUtils {
// TODO remove the ref check here, or pass in the spec version
// openapi 3.1.0 specs allow ref to be adjacent to any keyword
// openapi 3.0.3 and earlier do not allow adjacent keywords to refs
return (schema.get$ref() == null && schema.getType() == null);
return (schema.get$ref() == null && getType(schema) == null);
}
public static void syncValidationProperties(Schema schema, IJsonSchemaValidationProperties target) {
@@ -1930,7 +1911,7 @@ public class ModelUtils {
private static void logWarnMessagesForIneffectiveValidations(Set<String> setValidations, Schema schema, Set<String> effectiveValidations) {
setValidations.removeAll(effectiveValidations);
setValidations.stream().forEach(validation -> {
LOGGER.warn("Validation '" + validation + "' has no effect on schema '" + schema.getType() +"'. Ignoring!");
LOGGER.warn("Validation '" + validation + "' has no effect on schema '" + getType(schema) +"'. Ignoring!");
});
}
@@ -2139,7 +2120,11 @@ public class ModelUtils {
}
if (schema instanceof JsonSchema) {
return String.valueOf(schema.getTypes().iterator().next());
if (schema.getTypes() != null) {
return String.valueOf(schema.getTypes().iterator().next());
} else {
return null;
}
} else {
return schema.getType();
}

View File

@@ -969,3 +969,15 @@ components:
- Code 1
- Code 2
- Code 3
SimpleModelWithArrayProperty:
type: object
required:
- arrayOfStrings
properties:
arrayOfStrings:
type: array
items:
type: string
AllOfSimpleModel:
allOf:
- $ref: '#/components/schemas/SimpleModelWithArrayProperty'

View File

@@ -5,6 +5,7 @@ README.md
api/openapi.yaml
build.gradle
build.sbt
docs/AllOfSimpleModel.md
docs/Animal.md
docs/AnyTypeTest.md
docs/ArrayOfSameRef.md
@@ -21,6 +22,7 @@ docs/OneOfStringOrInt.md
docs/Order.md
docs/Pet.md
docs/PetApi.md
docs/SimpleModelWithArrayProperty.md
docs/StoreApi.md
docs/StringOrInt.md
docs/Tag.md
@@ -61,6 +63,7 @@ src/main/java/org/openapitools/client/auth/OAuthFlow.java
src/main/java/org/openapitools/client/auth/OAuthOkHttpClient.java
src/main/java/org/openapitools/client/auth/RetryingOAuth.java
src/main/java/org/openapitools/client/model/AbstractOpenApiSchema.java
src/main/java/org/openapitools/client/model/AllOfSimpleModel.java
src/main/java/org/openapitools/client/model/Animal.java
src/main/java/org/openapitools/client/model/AnyTypeTest.java
src/main/java/org/openapitools/client/model/ArrayOfSameRef.java
@@ -75,6 +78,7 @@ src/main/java/org/openapitools/client/model/ModelApiResponse.java
src/main/java/org/openapitools/client/model/OneOfStringOrInt.java
src/main/java/org/openapitools/client/model/Order.java
src/main/java/org/openapitools/client/model/Pet.java
src/main/java/org/openapitools/client/model/SimpleModelWithArrayProperty.java
src/main/java/org/openapitools/client/model/StringOrInt.java
src/main/java/org/openapitools/client/model/Tag.java
src/main/java/org/openapitools/client/model/User.java

View File

@@ -144,6 +144,7 @@ Class | Method | HTTP request | Description
## Documentation for Models
- [AllOfSimpleModel](docs/AllOfSimpleModel.md)
- [Animal](docs/Animal.md)
- [AnyTypeTest](docs/AnyTypeTest.md)
- [ArrayOfSameRef](docs/ArrayOfSameRef.md)
@@ -158,6 +159,7 @@ Class | Method | HTTP request | Description
- [OneOfStringOrInt](docs/OneOfStringOrInt.md)
- [Order](docs/Order.md)
- [Pet](docs/Pet.md)
- [SimpleModelWithArrayProperty](docs/SimpleModelWithArrayProperty.md)
- [StringOrInt](docs/StringOrInt.md)
- [Tag](docs/Tag.md)
- [User](docs/User.md)

View File

@@ -1063,6 +1063,17 @@ components:
- Code 2
- Code 3
type: string
SimpleModelWithArrayProperty:
properties:
arrayOfStrings:
items:
type: string
type: array
required:
- arrayOfStrings
AllOfSimpleModel:
allOf:
- $ref: '#/components/schemas/SimpleModelWithArrayProperty'
updatePetWithForm_request:
properties:
name:

View File

@@ -0,0 +1,13 @@
# AllOfSimpleModel
## Properties
| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**arrayOfStrings** | **List&lt;String&gt;** | | |

View File

@@ -0,0 +1,13 @@
# SimpleModelWithArrayProperty
## Properties
| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**arrayOfStrings** | **List&lt;String&gt;** | | |

View File

@@ -129,6 +129,7 @@ public class JSON {
gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter);
gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter);
gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter);
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AllOfSimpleModel.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());
@@ -141,6 +142,7 @@ public class JSON {
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.OneOfStringOrInt.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Order.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Pet.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.SimpleModelWithArrayProperty.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.StringOrInt.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Tag.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.User.CustomTypeAdapterFactory());

View File

@@ -0,0 +1,308 @@
/*
* 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 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;
/**
* AllOfSimpleModel
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.6.0-SNAPSHOT")
public class AllOfSimpleModel {
public static final String SERIALIZED_NAME_ARRAY_OF_STRINGS = "arrayOfStrings";
@SerializedName(SERIALIZED_NAME_ARRAY_OF_STRINGS)
private List<String> arrayOfStrings = new ArrayList<>();
public AllOfSimpleModel() {
}
public AllOfSimpleModel arrayOfStrings(List<String> arrayOfStrings) {
this.arrayOfStrings = arrayOfStrings;
return this;
}
public AllOfSimpleModel addArrayOfStringsItem(String arrayOfStringsItem) {
if (this.arrayOfStrings == null) {
this.arrayOfStrings = new ArrayList<>();
}
this.arrayOfStrings.add(arrayOfStringsItem);
return this;
}
/**
* Get arrayOfStrings
* @return arrayOfStrings
**/
@javax.annotation.Nonnull
public List<String> getArrayOfStrings() {
return arrayOfStrings;
}
public void setArrayOfStrings(List<String> arrayOfStrings) {
this.arrayOfStrings = arrayOfStrings;
}
/**
* 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<String, Object> 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 AllOfSimpleModel instance itself
*/
public AllOfSimpleModel putAdditionalProperty(String key, Object value) {
if (this.additionalProperties == null) {
this.additionalProperties = new HashMap<String, Object>();
}
this.additionalProperties.put(key, value);
return this;
}
/**
* Return the additional (undeclared) property.
*
* @return a map of objects
*/
public Map<String, Object> 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;
}
AllOfSimpleModel allOfSimpleModel = (AllOfSimpleModel) o;
return Objects.equals(this.arrayOfStrings, allOfSimpleModel.arrayOfStrings)&&
Objects.equals(this.additionalProperties, allOfSimpleModel.additionalProperties);
}
@Override
public int hashCode() {
return Objects.hash(arrayOfStrings, additionalProperties);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class AllOfSimpleModel {\n");
sb.append(" arrayOfStrings: ").append(toIndentedString(arrayOfStrings)).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<String> openapiFields;
public static HashSet<String> openapiRequiredFields;
static {
// a set of all properties/fields (JSON key names)
openapiFields = new HashSet<String>();
openapiFields.add("arrayOfStrings");
// a set of required properties/fields (JSON key names)
openapiRequiredFields = new HashSet<String>();
openapiRequiredFields.add("arrayOfStrings");
}
/**
* 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 AllOfSimpleModel
*/
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
if (jsonElement == null) {
if (!AllOfSimpleModel.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null
throw new IllegalArgumentException(String.format("The required field(s) %s in AllOfSimpleModel is not found in the empty JSON string", AllOfSimpleModel.openapiRequiredFields.toString()));
}
}
// check to make sure all required properties/fields are present in the JSON string
for (String requiredField : AllOfSimpleModel.openapiRequiredFields) {
if (jsonElement.getAsJsonObject().get(requiredField) == null) {
throw new IllegalArgumentException(String.format("The required field `%s` is not found in the JSON string: %s", requiredField, jsonElement.toString()));
}
}
JsonObject jsonObj = jsonElement.getAsJsonObject();
// ensure the required json array is present
if (jsonObj.get("arrayOfStrings") == null) {
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
} else if (!jsonObj.get("arrayOfStrings").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `arrayOfStrings` to be an array in the JSON string but got `%s`", jsonObj.get("arrayOfStrings").toString()));
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!AllOfSimpleModel.class.isAssignableFrom(type.getRawType())) {
return null; // this class only serializes 'AllOfSimpleModel' and its subtypes
}
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
final TypeAdapter<AllOfSimpleModel> thisAdapter
= gson.getDelegateAdapter(this, TypeToken.get(AllOfSimpleModel.class));
return (TypeAdapter<T>) new TypeAdapter<AllOfSimpleModel>() {
@Override
public void write(JsonWriter out, AllOfSimpleModel value) throws IOException {
JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject();
obj.remove("additionalProperties");
// serialize additional properties
if (value.getAdditionalProperties() != null) {
for (Map.Entry<String, Object> 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 AllOfSimpleModel read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
validateJsonElement(jsonElement);
JsonObject jsonObj = jsonElement.getAsJsonObject();
// store additional fields in the deserialized instance
AllOfSimpleModel instance = thisAdapter.fromJsonTree(jsonObj);
for (Map.Entry<String, JsonElement> 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 AllOfSimpleModel given an JSON string
*
* @param jsonString JSON string
* @return An instance of AllOfSimpleModel
* @throws IOException if the JSON string is invalid with respect to AllOfSimpleModel
*/
public static AllOfSimpleModel fromJson(String jsonString) throws IOException {
return JSON.getGson().fromJson(jsonString, AllOfSimpleModel.class);
}
/**
* Convert an instance of AllOfSimpleModel to an JSON string
*
* @return JSON string
*/
public String toJson() {
return JSON.getGson().toJson(this);
}
}

View File

@@ -0,0 +1,308 @@
/*
* 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 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;
/**
* SimpleModelWithArrayProperty
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.6.0-SNAPSHOT")
public class SimpleModelWithArrayProperty {
public static final String SERIALIZED_NAME_ARRAY_OF_STRINGS = "arrayOfStrings";
@SerializedName(SERIALIZED_NAME_ARRAY_OF_STRINGS)
private List<String> arrayOfStrings = new ArrayList<>();
public SimpleModelWithArrayProperty() {
}
public SimpleModelWithArrayProperty arrayOfStrings(List<String> arrayOfStrings) {
this.arrayOfStrings = arrayOfStrings;
return this;
}
public SimpleModelWithArrayProperty addArrayOfStringsItem(String arrayOfStringsItem) {
if (this.arrayOfStrings == null) {
this.arrayOfStrings = new ArrayList<>();
}
this.arrayOfStrings.add(arrayOfStringsItem);
return this;
}
/**
* Get arrayOfStrings
* @return arrayOfStrings
**/
@javax.annotation.Nonnull
public List<String> getArrayOfStrings() {
return arrayOfStrings;
}
public void setArrayOfStrings(List<String> arrayOfStrings) {
this.arrayOfStrings = arrayOfStrings;
}
/**
* 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<String, Object> 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 SimpleModelWithArrayProperty instance itself
*/
public SimpleModelWithArrayProperty putAdditionalProperty(String key, Object value) {
if (this.additionalProperties == null) {
this.additionalProperties = new HashMap<String, Object>();
}
this.additionalProperties.put(key, value);
return this;
}
/**
* Return the additional (undeclared) property.
*
* @return a map of objects
*/
public Map<String, Object> 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;
}
SimpleModelWithArrayProperty simpleModelWithArrayProperty = (SimpleModelWithArrayProperty) o;
return Objects.equals(this.arrayOfStrings, simpleModelWithArrayProperty.arrayOfStrings)&&
Objects.equals(this.additionalProperties, simpleModelWithArrayProperty.additionalProperties);
}
@Override
public int hashCode() {
return Objects.hash(arrayOfStrings, additionalProperties);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class SimpleModelWithArrayProperty {\n");
sb.append(" arrayOfStrings: ").append(toIndentedString(arrayOfStrings)).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<String> openapiFields;
public static HashSet<String> openapiRequiredFields;
static {
// a set of all properties/fields (JSON key names)
openapiFields = new HashSet<String>();
openapiFields.add("arrayOfStrings");
// a set of required properties/fields (JSON key names)
openapiRequiredFields = new HashSet<String>();
openapiRequiredFields.add("arrayOfStrings");
}
/**
* 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 SimpleModelWithArrayProperty
*/
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
if (jsonElement == null) {
if (!SimpleModelWithArrayProperty.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null
throw new IllegalArgumentException(String.format("The required field(s) %s in SimpleModelWithArrayProperty is not found in the empty JSON string", SimpleModelWithArrayProperty.openapiRequiredFields.toString()));
}
}
// check to make sure all required properties/fields are present in the JSON string
for (String requiredField : SimpleModelWithArrayProperty.openapiRequiredFields) {
if (jsonElement.getAsJsonObject().get(requiredField) == null) {
throw new IllegalArgumentException(String.format("The required field `%s` is not found in the JSON string: %s", requiredField, jsonElement.toString()));
}
}
JsonObject jsonObj = jsonElement.getAsJsonObject();
// ensure the required json array is present
if (jsonObj.get("arrayOfStrings") == null) {
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
} else if (!jsonObj.get("arrayOfStrings").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `arrayOfStrings` to be an array in the JSON string but got `%s`", jsonObj.get("arrayOfStrings").toString()));
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!SimpleModelWithArrayProperty.class.isAssignableFrom(type.getRawType())) {
return null; // this class only serializes 'SimpleModelWithArrayProperty' and its subtypes
}
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
final TypeAdapter<SimpleModelWithArrayProperty> thisAdapter
= gson.getDelegateAdapter(this, TypeToken.get(SimpleModelWithArrayProperty.class));
return (TypeAdapter<T>) new TypeAdapter<SimpleModelWithArrayProperty>() {
@Override
public void write(JsonWriter out, SimpleModelWithArrayProperty value) throws IOException {
JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject();
obj.remove("additionalProperties");
// serialize additional properties
if (value.getAdditionalProperties() != null) {
for (Map.Entry<String, Object> 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 SimpleModelWithArrayProperty read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
validateJsonElement(jsonElement);
JsonObject jsonObj = jsonElement.getAsJsonObject();
// store additional fields in the deserialized instance
SimpleModelWithArrayProperty instance = thisAdapter.fromJsonTree(jsonObj);
for (Map.Entry<String, JsonElement> 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 SimpleModelWithArrayProperty given an JSON string
*
* @param jsonString JSON string
* @return An instance of SimpleModelWithArrayProperty
* @throws IOException if the JSON string is invalid with respect to SimpleModelWithArrayProperty
*/
public static SimpleModelWithArrayProperty fromJson(String jsonString) throws IOException {
return JSON.getGson().fromJson(jsonString, SimpleModelWithArrayProperty.class);
}
/**
* Convert an instance of SimpleModelWithArrayProperty to an JSON string
*
* @return JSON string
*/
public String toJson() {
return JSON.getGson().toJson(this);
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* Model tests for AllOfSimpleModel
*/
public class AllOfSimpleModelTest {
private final AllOfSimpleModel model = new AllOfSimpleModel();
/**
* Model tests for AllOfSimpleModel
*/
@Test
public void testAllOfSimpleModel() {
// TODO: test AllOfSimpleModel
}
/**
* Test the property 'arrayOfStrings'
*/
@Test
public void arrayOfStringsTest() {
// TODO: test arrayOfStrings
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* Model tests for SimpleModelWithArrayProperty
*/
public class SimpleModelWithArrayPropertyTest {
private final SimpleModelWithArrayProperty model = new SimpleModelWithArrayProperty();
/**
* Model tests for SimpleModelWithArrayProperty
*/
@Test
public void testSimpleModelWithArrayProperty() {
// TODO: test SimpleModelWithArrayProperty
}
/**
* Test the property 'arrayOfStrings'
*/
@Test
public void arrayOfStringsTest() {
// TODO: test arrayOfStrings
}
}