[java][okhttp-gson] Fix oneof, anyof for array type (#18324)

* fix oneof, anyof for array type in java okhttp-gson

* fix oneof

* fix add tests

* clean up comments

* update

* add new files
This commit is contained in:
William Cheng 2024-04-08 22:20:23 +08:00 committed by GitHub
parent b2faf39ac7
commit 83b45fd1e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
37 changed files with 2958 additions and 1767 deletions

View File

@ -10,7 +10,7 @@
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/ClientTest.java"
sha256: db505f7801fef62c13a08a8e9ca1fc4c5c947ab46b46f12943139d353feacf17
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java"
sha256: c479b587cf0d51fa550eb81d33b277081807b87dc28649027d1164224c25ad0a
sha256: 8210bdaf06aae8dc46521515a8a0ef10e48c980fadd3efd95313e6c806f409c2
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/api/PetApiTest.java"
sha256: 0d64cdc11809a7b5b952ccdad2bd91bd0045b3894d6fabf3e368fa0be12b8217
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/PetTest.java"

View File

@ -54,8 +54,8 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/isArray}}
{{#isArray}}
final Type typeInstance = new TypeToken<List<{{complexType}}>>(){}.getType();
final TypeAdapter<{{{dataType}}}> adapter{{complexType}}List = (TypeAdapter<List<{{complexType}}>>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
final Type typeInstance = new TypeToken<{{{dataType}}}>(){}.getType();
final TypeAdapter<{{{dataType}}}> adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = (TypeAdapter<{{{dataType}}}>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
{{/isArray}}
{{/anyOf}}
{{/composedSchemas}}
@ -74,18 +74,20 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
// check if the actual instance is of the type `{{{dataType}}}`
if (value.getActualInstance() instanceof {{#isArray}}List<?>{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
{{#isPrimitiveType}}
JsonPrimitive primitive = adapter{{{dataType}}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonPrimitive();
JsonPrimitive primitive = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonPrimitive();
elementAdapter.write(out, primitive);
return;
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isArray}}
List<?> list = (List<?>) value.getActualInstance();
if(list.get(0) instanceof {{complexType}}) {
JsonArray array = adapter{{{complexType}}}List.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonArray();
if (list.get(0) instanceof {{{items.dataType}}}) {
JsonArray array = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonArray();
elementAdapter.write(out, array);
return;
}
{{/isArray}}
{{/isPrimitiveType}}
{{^isArray}}
{{^isPrimitiveType}}
JsonElement element = adapter{{{dataType}}}.toJsonTree(({{{dataType}}})value.getActualInstance());
@ -97,7 +99,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/vendorExtensions.x-duplicated-data-type}}
{{/anyOf}}
{{/composedSchemas}}
throw new IOException("Failed to serialize as the type doesn't match anyOf schemae: {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}");
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}");
}
@Override
@ -115,26 +117,28 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
// deserialize {{{dataType}}}
try {
// validate the JSON object to see if any exception is thrown
{{^isArray}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
actualAdapter = adapter{{{dataType}}};
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isNumber}}
{{^isNumber}}
{{#isPrimitiveType}}
if (!jsonElement.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
actualAdapter = adapter{{{dataType}}};
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{^isArray}}
{{{dataType}}}.validateJsonElement(jsonElement);
actualAdapter = adapter{{{dataType}}};
{{/isArray}}
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(jsonElement);
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isPrimitiveType}}
{{/isNumber}}
{{/isArray}}
{{#isArray}}
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
@ -148,7 +152,6 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
actualAdapter = adapter{{{dataType}}};
{{/isNumber}}
{{^isNumber}}
{{#isPrimitiveType}}
@ -156,13 +159,15 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(element);
{{/isPrimitiveType}}
{{/isNumber}}
{{/items}}
}
actualAdapter = adapter{{{complexType}}}List;
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isArray}}
{{classname}} ret = new {{classname}}();
ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement));
@ -182,7 +187,6 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{classname}} ret = new {{classname}}();
ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement));
return ret;
log.log(Level.FINER, "Input data matches schema '{{{.}}}'");
} catch (Exception e) {
// deserialization failed, continue
errorMessages.add(String.format("Deserialization for {{{.}}} failed with `%s`.", e.getMessage()));
@ -250,7 +254,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
if (instance instanceof {{#isArray}}List<?>{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
{{#isArray}}
List<?> list = (List<?>) instance;
if(list.get(0) instanceof {{complexType}}) {
if (list.get(0) instanceof {{{items.dataType}}}) {
super.setActualInstance(instance);
return;
}
@ -273,6 +277,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
*
* @return The actual instance ({{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}})
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();
@ -288,7 +293,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
* @return The actual instance of `{{{dataType}}}`
* @throws ClassCastException if the instance is not `{{{dataType}}}`
*/
public {{{dataType}}} get{{#isArray}}{{complexType}}List{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}() throws ClassCastException {
public {{{dataType}}} get{{#isArray}}{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}() throws ClassCastException {
return ({{{dataType}}})super.getActualInstance();
}
{{/vendorExtensions.x-duplicated-data-type}}
@ -310,6 +315,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
// validate the json string with {{{dataType}}}
try {
{{^hasVars}}
{{^isArray}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
@ -321,12 +327,13 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
throw new IllegalArgumentException(String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{^isArray}}
{{{dataType}}}.validateJsonElement(jsonElement);
{{/isArray}}
{{/isPrimitiveType}}
{{/isNumber}}
{{/isArray}}
{{#isArray}}
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
@ -346,6 +353,8 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(element);
{{/isPrimitiveType}}
@ -367,7 +376,6 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/anyOf}}
{{/composedSchemas}}
throw new IOException(String.format("The JSON string is invalid for {{classname}} with anyOf schemas: {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s", errorMessages, jsonElement.toString()));
}
/**

View File

@ -54,8 +54,8 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/isArray}}
{{#isArray}}
final Type typeInstance = new TypeToken<List<{{complexType}}>>(){}.getType();
final TypeAdapter<{{{dataType}}}> adapter{{complexType}}List = (TypeAdapter<List<{{complexType}}>>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
final Type typeInstance = new TypeToken<{{{dataType}}}>(){}.getType();
final TypeAdapter<{{{dataType}}}> adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = (TypeAdapter<{{{dataType}}}>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
{{/isArray}}
{{/oneOf}}
{{/composedSchemas}}
@ -74,18 +74,20 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
// check if the actual instance is of the type `{{{dataType}}}`
if (value.getActualInstance() instanceof {{#isArray}}List<?>{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
{{#isPrimitiveType}}
JsonPrimitive primitive = adapter{{{dataType}}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonPrimitive();
JsonPrimitive primitive = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonPrimitive();
elementAdapter.write(out, primitive);
return;
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isArray}}
List<?> list = (List<?>) value.getActualInstance();
if(list.get(0) instanceof {{complexType}}) {
JsonArray array = adapter{{{complexType}}}List.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonArray();
if (list.get(0) instanceof {{{items.dataType}}}) {
JsonArray array = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonArray();
elementAdapter.write(out, array);
return;
}
{{/isArray}}
{{/isPrimitiveType}}
{{^isArray}}
{{^isPrimitiveType}}
JsonElement element = adapter{{{dataType}}}.toJsonTree(({{{dataType}}})value.getActualInstance());
@ -140,26 +142,28 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
// deserialize {{{dataType}}}
try {
// validate the JSON object to see if any exception is thrown
{{^isArray}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
actualAdapter = adapter{{{dataType}}};
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isNumber}}
{{^isNumber}}
{{#isPrimitiveType}}
if (!jsonElement.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
actualAdapter = adapter{{{dataType}}};
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{^isArray}}
{{{dataType}}}.validateJsonElement(jsonElement);
actualAdapter = adapter{{{dataType}}};
{{/isArray}}
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(jsonElement);
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isPrimitiveType}}
{{/isNumber}}
{{/isArray}}
{{#isArray}}
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
@ -173,7 +177,6 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
actualAdapter = adapter{{{dataType}}};
{{/isNumber}}
{{^isNumber}}
{{#isPrimitiveType}}
@ -181,13 +184,15 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(element);
{{/isPrimitiveType}}
{{/isNumber}}
{{/items}}
}
actualAdapter = adapter{{{complexType}}}List;
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isArray}}
match++;
log.log(Level.FINER, "Input data matches schema '{{{dataType}}}'");
@ -278,7 +283,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
if (instance instanceof {{#isArray}}List<?>{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
{{#isArray}}
List<?> list = (List<?>) instance;
if(list.get(0) instanceof {{complexType}}) {
if (list.get(0) instanceof {{{items.dataType}}}) {
super.setActualInstance(instance);
return;
}
@ -301,6 +306,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
*
* @return The actual instance ({{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}})
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();
@ -316,7 +322,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
* @return The actual instance of `{{{dataType}}}`
* @throws ClassCastException if the instance is not `{{{dataType}}}`
*/
public {{{dataType}}} get{{#isArray}}{{complexType}}List{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}() throws ClassCastException {
public {{{dataType}}} get{{#isArray}}{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}() throws ClassCastException {
return ({{{dataType}}})super.getActualInstance();
}
{{/vendorExtensions.x-duplicated-data-type}}
@ -339,6 +345,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
// validate the json string with {{{dataType}}}
try {
{{^hasVars}}
{{^isArray}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
@ -350,12 +357,13 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
throw new IllegalArgumentException(String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{^isArray}}
{{{dataType}}}.validateJsonElement(jsonElement);
{{/isArray}}
{{/isPrimitiveType}}
{{/isNumber}}
{{/isArray}}
{{#isArray}}
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
@ -375,6 +383,8 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(element);
{{/isPrimitiveType}}

View File

@ -2675,3 +2675,22 @@ components:
$ref: '#/components/schemas/Pet/properties/status'
xml:
name: Pet
ArrayOneOf:
oneOf:
- type: integer
- type: array
items:
type: string
ArrayAnyOf:
anyOf:
- type: integer
- type: array
items:
type: string
ModelWithOneOfAnyOfProperties:
type: object
properties:
oneof_prop:
$ref: '#/components/schemas/ArrayOneOf'
anyof_prop:
$ref: '#/components/schemas/ArrayAnyOf'

View File

@ -161,6 +161,7 @@ public class MyExamplePostRequest extends AbstractOpenApiSchema {
*
* @return The actual instance (String)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -194,6 +194,7 @@ public class SimpleOneOf extends AbstractOpenApiSchema implements Serializable {
*
* @return The actual instance (Integer, String)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -193,6 +193,7 @@ public class OneOfStringOrInt extends AbstractOpenApiSchema {
*
* @return The actual instance (Integer, String)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -85,7 +85,7 @@ public class StringOrInt extends AbstractOpenApiSchema {
elementAdapter.write(out, primitive);
return;
}
throw new IOException("Failed to serialize as the type doesn't match anyOf schemae: Integer, String");
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: Integer, String");
}
@Override
@ -188,6 +188,7 @@ public class StringOrInt extends AbstractOpenApiSchema {
*
* @return The actual instance (Integer, String)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();
@ -244,7 +245,6 @@ public class StringOrInt extends AbstractOpenApiSchema {
// continue to the next one
}
throw new IOException(String.format("The JSON string is invalid for StringOrInt with anyOf schemas: Integer, String. no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s", errorMessages, jsonElement.toString()));
}
/**

View File

@ -15,11 +15,13 @@ docs/Animal.md
docs/AnotherFakeApi.md
docs/Apple.md
docs/AppleReq.md
docs/ArrayAnyOf.md
docs/ArrayDefault.md
docs/ArrayOfArrayOfNumberOnly.md
docs/ArrayOfInlineAllOf.md
docs/ArrayOfInlineAllOfArrayAllofDogPropertyInner.md
docs/ArrayOfNumberOnly.md
docs/ArrayOneOf.md
docs/ArrayTest.md
docs/Banana.md
docs/BananaReq.md
@ -61,6 +63,7 @@ docs/ModelApiResponse.md
docs/ModelFile.md
docs/ModelList.md
docs/ModelReturn.md
docs/ModelWithOneOfAnyOfProperties.md
docs/Name.md
docs/NewPet.md
docs/NewPetCategoryInlineAllof.md
@ -154,11 +157,13 @@ src/main/java/org/openapitools/client/model/AllOfModelArrayAnyOfAllOfLinkListCol
src/main/java/org/openapitools/client/model/Animal.java
src/main/java/org/openapitools/client/model/Apple.java
src/main/java/org/openapitools/client/model/AppleReq.java
src/main/java/org/openapitools/client/model/ArrayAnyOf.java
src/main/java/org/openapitools/client/model/ArrayDefault.java
src/main/java/org/openapitools/client/model/ArrayOfArrayOfNumberOnly.java
src/main/java/org/openapitools/client/model/ArrayOfInlineAllOf.java
src/main/java/org/openapitools/client/model/ArrayOfInlineAllOfArrayAllofDogPropertyInner.java
src/main/java/org/openapitools/client/model/ArrayOfNumberOnly.java
src/main/java/org/openapitools/client/model/ArrayOneOf.java
src/main/java/org/openapitools/client/model/ArrayTest.java
src/main/java/org/openapitools/client/model/Banana.java
src/main/java/org/openapitools/client/model/BananaReq.java
@ -197,6 +202,7 @@ src/main/java/org/openapitools/client/model/ModelApiResponse.java
src/main/java/org/openapitools/client/model/ModelFile.java
src/main/java/org/openapitools/client/model/ModelList.java
src/main/java/org/openapitools/client/model/ModelReturn.java
src/main/java/org/openapitools/client/model/ModelWithOneOfAnyOfProperties.java
src/main/java/org/openapitools/client/model/Name.java
src/main/java/org/openapitools/client/model/NewPet.java
src/main/java/org/openapitools/client/model/NewPetCategoryInlineAllof.java

View File

@ -175,11 +175,13 @@ Class | Method | HTTP request | Description
- [Animal](docs/Animal.md)
- [Apple](docs/Apple.md)
- [AppleReq](docs/AppleReq.md)
- [ArrayAnyOf](docs/ArrayAnyOf.md)
- [ArrayDefault](docs/ArrayDefault.md)
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
- [ArrayOfInlineAllOf](docs/ArrayOfInlineAllOf.md)
- [ArrayOfInlineAllOfArrayAllofDogPropertyInner](docs/ArrayOfInlineAllOfArrayAllofDogPropertyInner.md)
- [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
- [ArrayOneOf](docs/ArrayOneOf.md)
- [ArrayTest](docs/ArrayTest.md)
- [Banana](docs/Banana.md)
- [BananaReq](docs/BananaReq.md)
@ -218,6 +220,7 @@ Class | Method | HTTP request | Description
- [ModelFile](docs/ModelFile.md)
- [ModelList](docs/ModelList.md)
- [ModelReturn](docs/ModelReturn.md)
- [ModelWithOneOfAnyOfProperties](docs/ModelWithOneOfAnyOfProperties.md)
- [Name](docs/Name.md)
- [NewPet](docs/NewPet.md)
- [NewPetCategoryInlineAllof](docs/NewPetCategoryInlineAllof.md)

View File

@ -2711,6 +2711,25 @@ components:
type: object
xml:
name: Pet
ArrayOneOf:
oneOf:
- type: integer
- items:
type: string
type: array
ArrayAnyOf:
anyOf:
- type: integer
- items:
type: string
type: array
ModelWithOneOfAnyOfProperties:
properties:
oneof_prop:
$ref: '#/components/schemas/ArrayOneOf'
anyof_prop:
$ref: '#/components/schemas/ArrayAnyOf'
type: object
_foo_get_default_response:
example:
string:

View File

@ -0,0 +1,12 @@
# ArrayAnyOf
## Properties
| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|

View File

@ -0,0 +1,12 @@
# ArrayOneOf
## Properties
| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|

View File

@ -0,0 +1,14 @@
# ModelWithOneOfAnyOfProperties
## Properties
| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**oneofProp** | [**ArrayOneOf**](ArrayOneOf.md) | | [optional] |
|**anyofProp** | [**ArrayAnyOf**](ArrayAnyOf.md) | | [optional] |

View File

@ -244,11 +244,13 @@ public class JSON {
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AllOfModelArrayAnyOfAllOfLinkListColumn1Value.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Apple.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AppleReq.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayAnyOf.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayDefault.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfArrayOfNumberOnly.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfInlineAllOf.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfInlineAllOfArrayAllofDogPropertyInner.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfNumberOnly.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOneOf.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayTest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Banana.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.BananaReq.CustomTypeAdapterFactory());
@ -285,6 +287,7 @@ public class JSON {
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelFile.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelList.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelReturn.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelWithOneOfAnyOfProperties.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Name.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.NewPet.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.NewPetCategoryInlineAllof.CustomTypeAdapterFactory());

View File

@ -203,6 +203,7 @@ public class AllOfModelArrayAnyOfAllOfAttributesC extends AbstractOpenApiSchema
*
* @return The actual instance (Order, Pet)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -95,7 +95,7 @@ public class AllOfModelArrayAnyOfAllOfLinkListColumn1Value extends AbstractOpenA
elementAdapter.write(out, element);
return;
}
throw new IOException("Failed to serialize as the type doesn't match anyOf schemae: Tag, User");
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: Tag, User");
}
@Override
@ -194,6 +194,7 @@ public class AllOfModelArrayAnyOfAllOfLinkListColumn1Value extends AbstractOpenA
*
* @return The actual instance (Tag, User)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();
@ -246,7 +247,6 @@ public class AllOfModelArrayAnyOfAllOfLinkListColumn1Value extends AbstractOpenA
// continue to the next one
}
throw new IOException(String.format("The JSON string is invalid for AllOfModelArrayAnyOfAllOfLinkListColumn1Value with anyOf schemas: Tag, User. no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s", errorMessages, jsonElement.toString()));
}
/**

View File

@ -0,0 +1,291 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* 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 java.util.List;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonParseException;
import org.openapitools.client.JSON;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT")
public class ArrayAnyOf extends AbstractOpenApiSchema {
private static final Logger log = Logger.getLogger(ArrayAnyOf.class.getName());
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!ArrayAnyOf.class.isAssignableFrom(type.getRawType())) {
return null; // this class only serializes 'ArrayAnyOf' and its subtypes
}
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
final TypeAdapter<Integer> adapterInteger = gson.getDelegateAdapter(this, TypeToken.get(Integer.class));
final Type typeInstance = new TypeToken<List<String>>(){}.getType();
final TypeAdapter<List<String>> adapterListString = (TypeAdapter<List<String>>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
return (TypeAdapter<T>) new TypeAdapter<ArrayAnyOf>() {
@Override
public void write(JsonWriter out, ArrayAnyOf value) throws IOException {
if (value == null || value.getActualInstance() == null) {
elementAdapter.write(out, null);
return;
}
// check if the actual instance is of the type `Integer`
if (value.getActualInstance() instanceof Integer) {
JsonPrimitive primitive = adapterInteger.toJsonTree((Integer)value.getActualInstance()).getAsJsonPrimitive();
elementAdapter.write(out, primitive);
return;
}
// check if the actual instance is of the type `List<String>`
if (value.getActualInstance() instanceof List<?>) {
JsonPrimitive primitive = adapterListString.toJsonTree((List<String>)value.getActualInstance()).getAsJsonPrimitive();
elementAdapter.write(out, primitive);
return;
}
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: Integer, List<String>");
}
@Override
public ArrayAnyOf read(JsonReader in) throws IOException {
Object deserialized = null;
JsonElement jsonElement = elementAdapter.read(in);
ArrayList<String> errorMessages = new ArrayList<>();
TypeAdapter actualAdapter = elementAdapter;
// deserialize Integer
try {
// validate the JSON object to see if any exception is thrown
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
actualAdapter = adapterInteger;
ArrayAnyOf ret = new ArrayAnyOf();
ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement));
return ret;
} catch (Exception e) {
// deserialization failed, continue
errorMessages.add(String.format("Deserialization for Integer failed with `%s`.", e.getMessage()));
log.log(Level.FINER, "Input data does not match schema 'Integer'", e);
}
// deserialize List<String>
try {
// validate the JSON object to see if any exception is thrown
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
}
JsonArray array = jsonElement.getAsJsonArray();
// validate array items
for(JsonElement element : array) {
if (!element.getAsJsonPrimitive().isString()) {
throw new IllegalArgumentException(String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString()));
}
}
actualAdapter = adapterListString;
ArrayAnyOf ret = new ArrayAnyOf();
ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement));
return ret;
} catch (Exception e) {
// deserialization failed, continue
errorMessages.add(String.format("Deserialization for List<String> failed with `%s`.", e.getMessage()));
log.log(Level.FINER, "Input data does not match schema 'List<String>'", e);
}
throw new IOException(String.format("Failed deserialization for ArrayAnyOf: no class matches result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s", errorMessages, jsonElement.toString()));
}
}.nullSafe();
}
}
// store a list of schema names defined in anyOf
public static final Map<String, Class<?>> schemas = new HashMap<String, Class<?>>();
public ArrayAnyOf() {
super("anyOf", Boolean.FALSE);
}
public ArrayAnyOf(Integer o) {
super("anyOf", Boolean.FALSE);
setActualInstance(o);
}
public ArrayAnyOf(List<String> o) {
super("anyOf", Boolean.FALSE);
setActualInstance(o);
}
static {
schemas.put("Integer", Integer.class);
schemas.put("List<String>", List.class);
}
@Override
public Map<String, Class<?>> getSchemas() {
return ArrayAnyOf.schemas;
}
/**
* Set the instance that matches the anyOf child schema, check
* the instance parameter is valid against the anyOf child schemas:
* Integer, List<String>
*
* It could be an instance of the 'anyOf' schemas.
*/
@Override
public void setActualInstance(Object instance) {
if (instance instanceof Integer) {
super.setActualInstance(instance);
return;
}
if (instance instanceof List<?>) {
List<?> list = (List<?>) instance;
if (list.get(0) instanceof String) {
super.setActualInstance(instance);
return;
}
}
throw new RuntimeException("Invalid instance type. Must be Integer, List<String>");
}
/**
* Get the actual instance, which can be the following:
* Integer, List<String>
*
* @return The actual instance (Integer, List<String>)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `Integer`. If the actual instance is not `Integer`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Integer`
* @throws ClassCastException if the instance is not `Integer`
*/
public Integer getInteger() throws ClassCastException {
return (Integer)super.getActualInstance();
}
/**
* Get the actual instance of `List<String>`. If the actual instance is not `List<String>`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `List<String>`
* @throws ClassCastException if the instance is not `List<String>`
*/
public List<String> getListString() throws ClassCastException {
return (List<String>)super.getActualInstance();
}
/**
* 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 ArrayAnyOf
*/
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
// validate anyOf schemas one by one
ArrayList<String> errorMessages = new ArrayList<>();
// validate the json string with Integer
try {
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
return;
} catch (Exception e) {
errorMessages.add(String.format("Deserialization for Integer failed with `%s`.", e.getMessage()));
// continue to the next one
}
// validate the json string with List<String>
try {
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
}
JsonArray array = jsonElement.getAsJsonArray();
// validate array items
for(JsonElement element : array) {
if (!element.getAsJsonPrimitive().isString()) {
throw new IllegalArgumentException(String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString()));
}
}
return;
} catch (Exception e) {
errorMessages.add(String.format("Deserialization for List<String> failed with `%s`.", e.getMessage()));
// continue to the next one
}
throw new IOException(String.format("The JSON string is invalid for ArrayAnyOf with anyOf schemas: Integer, List<String>. no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s", errorMessages, jsonElement.toString()));
}
/**
* Create an instance of ArrayAnyOf given an JSON string
*
* @param jsonString JSON string
* @return An instance of ArrayAnyOf
* @throws IOException if the JSON string is invalid with respect to ArrayAnyOf
*/
public static ArrayAnyOf fromJson(String jsonString) throws IOException {
return JSON.getGson().fromJson(jsonString, ArrayAnyOf.class);
}
/**
* Convert an instance of ArrayAnyOf to an JSON string
*
* @return JSON string
*/
public String toJson() {
return JSON.getGson().toJson(this);
}
}

View File

@ -0,0 +1,299 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* 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 java.util.List;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonParseException;
import org.openapitools.client.JSON;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT")
public class ArrayOneOf extends AbstractOpenApiSchema {
private static final Logger log = Logger.getLogger(ArrayOneOf.class.getName());
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!ArrayOneOf.class.isAssignableFrom(type.getRawType())) {
return null; // this class only serializes 'ArrayOneOf' and its subtypes
}
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
final TypeAdapter<Integer> adapterInteger = gson.getDelegateAdapter(this, TypeToken.get(Integer.class));
final Type typeInstance = new TypeToken<List<String>>(){}.getType();
final TypeAdapter<List<String>> adapterListString = (TypeAdapter<List<String>>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
return (TypeAdapter<T>) new TypeAdapter<ArrayOneOf>() {
@Override
public void write(JsonWriter out, ArrayOneOf value) throws IOException {
if (value == null || value.getActualInstance() == null) {
elementAdapter.write(out, null);
return;
}
// check if the actual instance is of the type `Integer`
if (value.getActualInstance() instanceof Integer) {
JsonPrimitive primitive = adapterInteger.toJsonTree((Integer)value.getActualInstance()).getAsJsonPrimitive();
elementAdapter.write(out, primitive);
return;
}
// check if the actual instance is of the type `List<String>`
if (value.getActualInstance() instanceof List<?>) {
JsonPrimitive primitive = adapterListString.toJsonTree((List<String>)value.getActualInstance()).getAsJsonPrimitive();
elementAdapter.write(out, primitive);
return;
}
throw new IOException("Failed to serialize as the type doesn't match oneOf schemas: Integer, List<String>");
}
@Override
public ArrayOneOf read(JsonReader in) throws IOException {
Object deserialized = null;
JsonElement jsonElement = elementAdapter.read(in);
int match = 0;
ArrayList<String> errorMessages = new ArrayList<>();
TypeAdapter actualAdapter = elementAdapter;
// deserialize Integer
try {
// validate the JSON object to see if any exception is thrown
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
actualAdapter = adapterInteger;
match++;
log.log(Level.FINER, "Input data matches schema 'Integer'");
} catch (Exception e) {
// deserialization failed, continue
errorMessages.add(String.format("Deserialization for Integer failed with `%s`.", e.getMessage()));
log.log(Level.FINER, "Input data does not match schema 'Integer'", e);
}
// deserialize List<String>
try {
// validate the JSON object to see if any exception is thrown
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
}
JsonArray array = jsonElement.getAsJsonArray();
// validate array items
for(JsonElement element : array) {
if (!element.getAsJsonPrimitive().isString()) {
throw new IllegalArgumentException(String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString()));
}
}
actualAdapter = adapterListString;
match++;
log.log(Level.FINER, "Input data matches schema 'List<String>'");
} catch (Exception e) {
// deserialization failed, continue
errorMessages.add(String.format("Deserialization for List<String> failed with `%s`.", e.getMessage()));
log.log(Level.FINER, "Input data does not match schema 'List<String>'", e);
}
if (match == 1) {
ArrayOneOf ret = new ArrayOneOf();
ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement));
return ret;
}
throw new IOException(String.format("Failed deserialization for ArrayOneOf: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()));
}
}.nullSafe();
}
}
// store a list of schema names defined in oneOf
public static final Map<String, Class<?>> schemas = new HashMap<String, Class<?>>();
public ArrayOneOf() {
super("oneOf", Boolean.FALSE);
}
public ArrayOneOf(Integer o) {
super("oneOf", Boolean.FALSE);
setActualInstance(o);
}
public ArrayOneOf(List<String> o) {
super("oneOf", Boolean.FALSE);
setActualInstance(o);
}
static {
schemas.put("Integer", Integer.class);
schemas.put("List<String>", List.class);
}
@Override
public Map<String, Class<?>> getSchemas() {
return ArrayOneOf.schemas;
}
/**
* Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas:
* Integer, List<String>
*
* It could be an instance of the 'oneOf' schemas.
*/
@Override
public void setActualInstance(Object instance) {
if (instance instanceof Integer) {
super.setActualInstance(instance);
return;
}
if (instance instanceof List<?>) {
List<?> list = (List<?>) instance;
if (list.get(0) instanceof String) {
super.setActualInstance(instance);
return;
}
}
throw new RuntimeException("Invalid instance type. Must be Integer, List<String>");
}
/**
* Get the actual instance, which can be the following:
* Integer, List<String>
*
* @return The actual instance (Integer, List<String>)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `Integer`. If the actual instance is not `Integer`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Integer`
* @throws ClassCastException if the instance is not `Integer`
*/
public Integer getInteger() throws ClassCastException {
return (Integer)super.getActualInstance();
}
/**
* Get the actual instance of `List<String>`. If the actual instance is not `List<String>`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `List<String>`
* @throws ClassCastException if the instance is not `List<String>`
*/
public List<String> getListString() throws ClassCastException {
return (List<String>)super.getActualInstance();
}
/**
* 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 ArrayOneOf
*/
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
// validate oneOf schemas one by one
int validCount = 0;
ArrayList<String> errorMessages = new ArrayList<>();
// validate the json string with Integer
try {
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
validCount++;
} catch (Exception e) {
errorMessages.add(String.format("Deserialization for Integer failed with `%s`.", e.getMessage()));
// continue to the next one
}
// validate the json string with List<String>
try {
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
}
JsonArray array = jsonElement.getAsJsonArray();
// validate array items
for(JsonElement element : array) {
if (!element.getAsJsonPrimitive().isString()) {
throw new IllegalArgumentException(String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString()));
}
}
validCount++;
} catch (Exception e) {
errorMessages.add(String.format("Deserialization for List<String> failed with `%s`.", e.getMessage()));
// continue to the next one
}
if (validCount != 1) {
throw new IOException(String.format("The JSON string is invalid for ArrayOneOf with oneOf schemas: Integer, List<String>. %d class(es) match the result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", validCount, errorMessages, jsonElement.toString()));
}
}
/**
* Create an instance of ArrayOneOf given an JSON string
*
* @param jsonString JSON string
* @return An instance of ArrayOneOf
* @throws IOException if the JSON string is invalid with respect to ArrayOneOf
*/
public static ArrayOneOf fromJson(String jsonString) throws IOException {
return JSON.getGson().fromJson(jsonString, ArrayOneOf.class);
}
/**
* Convert an instance of ArrayOneOf to an JSON string
*
* @return JSON string
*/
public String toJson() {
return JSON.getGson().toJson(this);
}
}

View File

@ -199,6 +199,7 @@ public class Fruit extends AbstractOpenApiSchema {
*
* @return The actual instance (Apple, Banana)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -204,6 +204,7 @@ public class FruitReq extends AbstractOpenApiSchema {
*
* @return The actual instance (AppleReq, BananaReq)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -95,7 +95,7 @@ public class GmFruit extends AbstractOpenApiSchema {
elementAdapter.write(out, element);
return;
}
throw new IOException("Failed to serialize as the type doesn't match anyOf schemae: Apple, Banana");
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: Apple, Banana");
}
@Override
@ -194,6 +194,7 @@ public class GmFruit extends AbstractOpenApiSchema {
*
* @return The actual instance (Apple, Banana)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();
@ -246,7 +247,6 @@ public class GmFruit extends AbstractOpenApiSchema {
// continue to the next one
}
throw new IOException(String.format("The JSON string is invalid for GmFruit with anyOf schemas: Apple, Banana. no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s", errorMessages, jsonElement.toString()));
}
/**

View File

@ -255,6 +255,7 @@ public class Mammal extends AbstractOpenApiSchema {
*
* @return The actual instance (Pig, Whale, Zebra)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -0,0 +1,320 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* 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.ArrayAnyOf;
import org.openapitools.client.model.ArrayOneOf;
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;
/**
* ModelWithOneOfAnyOfProperties
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT")
public class ModelWithOneOfAnyOfProperties {
public static final String SERIALIZED_NAME_ONEOF_PROP = "oneof_prop";
@SerializedName(SERIALIZED_NAME_ONEOF_PROP)
private ArrayOneOf oneofProp;
public static final String SERIALIZED_NAME_ANYOF_PROP = "anyof_prop";
@SerializedName(SERIALIZED_NAME_ANYOF_PROP)
private ArrayAnyOf anyofProp;
public ModelWithOneOfAnyOfProperties() {
}
public ModelWithOneOfAnyOfProperties oneofProp(ArrayOneOf oneofProp) {
this.oneofProp = oneofProp;
return this;
}
/**
* Get oneofProp
* @return oneofProp
**/
@javax.annotation.Nullable
public ArrayOneOf getOneofProp() {
return oneofProp;
}
public void setOneofProp(ArrayOneOf oneofProp) {
this.oneofProp = oneofProp;
}
public ModelWithOneOfAnyOfProperties anyofProp(ArrayAnyOf anyofProp) {
this.anyofProp = anyofProp;
return this;
}
/**
* Get anyofProp
* @return anyofProp
**/
@javax.annotation.Nullable
public ArrayAnyOf getAnyofProp() {
return anyofProp;
}
public void setAnyofProp(ArrayAnyOf anyofProp) {
this.anyofProp = anyofProp;
}
/**
* 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 ModelWithOneOfAnyOfProperties instance itself
*/
public ModelWithOneOfAnyOfProperties 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;
}
ModelWithOneOfAnyOfProperties modelWithOneOfAnyOfProperties = (ModelWithOneOfAnyOfProperties) o;
return Objects.equals(this.oneofProp, modelWithOneOfAnyOfProperties.oneofProp) &&
Objects.equals(this.anyofProp, modelWithOneOfAnyOfProperties.anyofProp)&&
Objects.equals(this.additionalProperties, modelWithOneOfAnyOfProperties.additionalProperties);
}
@Override
public int hashCode() {
return Objects.hash(oneofProp, anyofProp, additionalProperties);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ModelWithOneOfAnyOfProperties {\n");
sb.append(" oneofProp: ").append(toIndentedString(oneofProp)).append("\n");
sb.append(" anyofProp: ").append(toIndentedString(anyofProp)).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("oneof_prop");
openapiFields.add("anyof_prop");
// a set of required properties/fields (JSON key names)
openapiRequiredFields = new HashSet<String>();
}
/**
* 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 ModelWithOneOfAnyOfProperties
*/
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
if (jsonElement == null) {
if (!ModelWithOneOfAnyOfProperties.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null
throw new IllegalArgumentException(String.format("The required field(s) %s in ModelWithOneOfAnyOfProperties is not found in the empty JSON string", ModelWithOneOfAnyOfProperties.openapiRequiredFields.toString()));
}
}
JsonObject jsonObj = jsonElement.getAsJsonObject();
// validate the optional field `oneof_prop`
if (jsonObj.get("oneof_prop") != null && !jsonObj.get("oneof_prop").isJsonNull()) {
ArrayOneOf.validateJsonElement(jsonObj.get("oneof_prop"));
}
// validate the optional field `anyof_prop`
if (jsonObj.get("anyof_prop") != null && !jsonObj.get("anyof_prop").isJsonNull()) {
ArrayAnyOf.validateJsonElement(jsonObj.get("anyof_prop"));
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!ModelWithOneOfAnyOfProperties.class.isAssignableFrom(type.getRawType())) {
return null; // this class only serializes 'ModelWithOneOfAnyOfProperties' and its subtypes
}
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
final TypeAdapter<ModelWithOneOfAnyOfProperties> thisAdapter
= gson.getDelegateAdapter(this, TypeToken.get(ModelWithOneOfAnyOfProperties.class));
return (TypeAdapter<T>) new TypeAdapter<ModelWithOneOfAnyOfProperties>() {
@Override
public void write(JsonWriter out, ModelWithOneOfAnyOfProperties 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 ModelWithOneOfAnyOfProperties read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
validateJsonElement(jsonElement);
JsonObject jsonObj = jsonElement.getAsJsonObject();
// store additional fields in the deserialized instance
ModelWithOneOfAnyOfProperties 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 ModelWithOneOfAnyOfProperties given an JSON string
*
* @param jsonString JSON string
* @return An instance of ModelWithOneOfAnyOfProperties
* @throws IOException if the JSON string is invalid with respect to ModelWithOneOfAnyOfProperties
*/
public static ModelWithOneOfAnyOfProperties fromJson(String jsonString) throws IOException {
return JSON.getGson().fromJson(jsonString, ModelWithOneOfAnyOfProperties.class);
}
/**
* Convert an instance of ModelWithOneOfAnyOfProperties to an JSON string
*
* @return JSON string
*/
public String toJson() {
return JSON.getGson().toJson(this);
}
}

View File

@ -225,6 +225,7 @@ public class NullableShape extends AbstractOpenApiSchema {
*
* @return The actual instance (Quadrilateral, Triangle)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -220,6 +220,7 @@ public class Pig extends AbstractOpenApiSchema {
*
* @return The actual instance (BasquePig, DanishPig)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -220,6 +220,7 @@ public class Quadrilateral extends AbstractOpenApiSchema {
*
* @return The actual instance (ComplexQuadrilateral, SimpleQuadrilateral)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -226,6 +226,7 @@ public class Scalar extends AbstractOpenApiSchema {
*
* @return The actual instance (BigDecimal, Boolean, String)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -93,7 +93,7 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
elementAdapter.write(out, primitive);
return;
}
throw new IOException("Failed to serialize as the type doesn't match anyOf schemae: BigDecimal, Boolean, String");
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: BigDecimal, Boolean, String");
}
@Override
@ -222,6 +222,7 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
*
* @return The actual instance (BigDecimal, Boolean, String)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();
@ -298,7 +299,6 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
// continue to the next one
}
throw new IOException(String.format("The JSON string is invalid for ScalarAnyOf with anyOf schemas: BigDecimal, Boolean, String. no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s", errorMessages, jsonElement.toString()));
}
/**

View File

@ -220,6 +220,7 @@ public class Shape extends AbstractOpenApiSchema {
*
* @return The actual instance (Quadrilateral, Triangle)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -225,6 +225,7 @@ public class ShapeOrNull extends AbstractOpenApiSchema {
*
* @return The actual instance (Quadrilateral, Triangle)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -255,6 +255,7 @@ public class Triangle extends AbstractOpenApiSchema {
*
* @return The actual instance (EquilateralTriangle, IsoscelesTriangle, ScaleneTriangle)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();

View File

@ -67,7 +67,7 @@ public class Value extends AbstractOpenApiSchema {
final TypeAdapter<Scalar> adapterScalar = gson.getDelegateAdapter(this, TypeToken.get(Scalar.class));
final Type typeInstance = new TypeToken<List<Scalar>>(){}.getType();
final TypeAdapter<List<Scalar>> adapterScalarList = (TypeAdapter<List<Scalar>>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
final TypeAdapter<List<Scalar>> adapterListScalar = (TypeAdapter<List<Scalar>>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
return (TypeAdapter<T>) new TypeAdapter<Value>() {
@Override
@ -87,7 +87,7 @@ public class Value extends AbstractOpenApiSchema {
if (value.getActualInstance() instanceof List<?>) {
List<?> list = (List<?>) value.getActualInstance();
if (list.get(0) instanceof Scalar) {
JsonArray array = adapterScalarList.toJsonTree((List<Scalar>)value.getActualInstance()).getAsJsonArray();
JsonArray array = adapterListScalar.toJsonTree((List<Scalar>)value.getActualInstance()).getAsJsonArray();
elementAdapter.write(out, array);
return;
}
@ -128,7 +128,7 @@ public class Value extends AbstractOpenApiSchema {
for(JsonElement element : array) {
Scalar.validateJsonElement(element);
}
actualAdapter = adapterScalarList;
actualAdapter = adapterListScalar;
match++;
log.log(Level.FINER, "Input data matches schema 'List<Scalar>'");
} catch (Exception e) {
@ -207,6 +207,7 @@ public class Value extends AbstractOpenApiSchema {
*
* @return The actual instance (List<Scalar>, Scalar)
*/
@SuppressWarnings("unchecked")
@Override
public Object getActualInstance() {
return super.getActualInstance();
@ -229,7 +230,7 @@ public class Value extends AbstractOpenApiSchema {
* @return The actual instance of `List<Scalar>`
* @throws ClassCastException if the instance is not `List<Scalar>`
*/
public List<Scalar> getScalarList() throws ClassCastException {
public List<Scalar> getListScalar() throws ClassCastException {
return (List<Scalar>)super.getActualInstance();
}

View File

@ -17,13 +17,7 @@ import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.*;
import okio.ByteString;
import org.junit.jupiter.api.*;
@ -615,4 +609,44 @@ public class JSONTest {
z.putAdditionalProperty("new_object_array", Arrays.asList(t));
assertEquals(z.toJson(), "{\"className\":\"zebra\",\"object_array\":[{\"id\":34.0,\"name\":\"just a tag\"}],\"empty_array\":[],\"array\":[\"1\",\"2\",\"3\"],\"new_array\":[\"1\",\"2\",\"3\"],\"new_empty_array\":[],\"new_object_array\":[{\"id\":34,\"name\":\"just a tag\"}]}");
}
/**
* Validate a oneOf, anyOf schema with array sub-schema can be deserialized into the expected class.
*/
@Test
public void testOneOfAnyOfArray() throws Exception {
{
String str = "{\"oneof_prop\":23,\"anyof_prop\":45}";
ModelWithOneOfAnyOfProperties m = json.getGson().fromJson(str, ModelWithOneOfAnyOfProperties.class);
Integer anyofProp = (Integer) m.getAnyofProp().getActualInstance();
assertEquals(anyofProp, 45);
Integer oneofProp = (Integer) m.getOneofProp().getActualInstance();
assertEquals(oneofProp, 23);
String str2 = "{ \"oneof_prop\": [\"test oneof\"], \"anyof_prop\": [\"test anyof\"] }";
ModelWithOneOfAnyOfProperties m2 = json.getGson().fromJson(str2, ModelWithOneOfAnyOfProperties.class);
List<String> anyofProp2 = (List<String>) m2.getAnyofProp().getActualInstance();
assertEquals(anyofProp2, Arrays.asList("test anyof"));
List<String> oneofProp2 = (List<String>) m2.getOneofProp().getActualInstance();
assertEquals(oneofProp2, Arrays.asList("test oneof"));
}
{
// incorrect payload results in exception
String str = "{ \"oneof_prop\": \"23\", \"anyof_prop\": \"45\" }";
Exception exception = assertThrows(com.google.gson.JsonSyntaxException.class, () -> {
ModelWithOneOfAnyOfProperties o = json.getGson().fromJson(str, ModelWithOneOfAnyOfProperties.class);
});
assertTrue(exception.getMessage().contains("java.io.IOException: The JSON string is invalid for"));
}
{
// incorrect payload (array item type mismatch) results in exception
String str = "{ \"oneof_prop\": [23], \"anyof_prop\": [true] }";
Exception exception = assertThrows(com.google.gson.JsonSyntaxException.class, () -> {
ModelWithOneOfAnyOfProperties o = json.getGson().fromJson(str, ModelWithOneOfAnyOfProperties.class);
});
assertTrue(exception.getMessage().contains("java.io.IOException: The JSON string is invalid for"));
}
}
}

View File

@ -0,0 +1,34 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* 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.List;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* Model tests for ArrayAnyOf
*/
public class ArrayAnyOfTest {
private final ArrayAnyOf model = new ArrayAnyOf();
/**
* Model tests for ArrayAnyOf
*/
@Test
public void testArrayAnyOf() {
// TODO: test ArrayAnyOf
}
}

View File

@ -0,0 +1,34 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* 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.List;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* Model tests for ArrayOneOf
*/
public class ArrayOneOfTest {
private final ArrayOneOf model = new ArrayOneOf();
/**
* Model tests for ArrayOneOf
*/
@Test
public void testArrayOneOf() {
// TODO: test ArrayOneOf
}
}

View File

@ -0,0 +1,58 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* 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.ArrayAnyOf;
import org.openapitools.client.model.ArrayOneOf;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* Model tests for ModelWithOneOfAnyOfProperties
*/
public class ModelWithOneOfAnyOfPropertiesTest {
private final ModelWithOneOfAnyOfProperties model = new ModelWithOneOfAnyOfProperties();
/**
* Model tests for ModelWithOneOfAnyOfProperties
*/
@Test
public void testModelWithOneOfAnyOfProperties() {
// TODO: test ModelWithOneOfAnyOfProperties
}
/**
* Test the property 'oneofProp'
*/
@Test
public void oneofPropTest() {
// TODO: test oneofProp
}
/**
* Test the property 'anyofProp'
*/
@Test
public void anyofPropTest() {
// TODO: test anyofProp
}
}