diff --git a/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml b/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml index d131a1c5001a..a0d815dd3556 100644 --- a/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml @@ -2202,6 +2202,13 @@ components: - $ref: '#/components/schemas/Pig' discriminator: propertyName: className + mammal_anyof: + anyOf: + - $ref: '#/components/schemas/whale' + - $ref: '#/components/schemas/zebra' + - $ref: '#/components/schemas/Pig' + discriminator: + propertyName: className whale: type: object properties: diff --git a/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml b/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml index c9a21cf8f277..40cb91d8c70c 100644 --- a/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml @@ -1981,6 +1981,13 @@ components: - $ref: '#/components/schemas/Pig' discriminator: propertyName: className + mammal_anyof: + anyOf: + - $ref: '#/components/schemas/whale' + - $ref: '#/components/schemas/zebra' + - $ref: '#/components/schemas/Pig' + discriminator: + propertyName: className whale: type: object properties: diff --git a/samples/client/petstore/java/jersey3/.openapi-generator/FILES b/samples/client/petstore/java/jersey3/.openapi-generator/FILES index 4fbcb8fd20b1..943457dcdbf3 100644 --- a/samples/client/petstore/java/jersey3/.openapi-generator/FILES +++ b/samples/client/petstore/java/jersey3/.openapi-generator/FILES @@ -46,6 +46,7 @@ docs/HasOnlyReadOnly.md docs/HealthCheckResult.md docs/IsoscelesTriangle.md docs/Mammal.md +docs/MammalAnyof.md docs/MapTest.md docs/MixedPropertiesAndAdditionalPropertiesClass.md docs/Model200Response.md @@ -158,6 +159,7 @@ src/main/java/org/openapitools/client/model/HasOnlyReadOnly.java src/main/java/org/openapitools/client/model/HealthCheckResult.java src/main/java/org/openapitools/client/model/IsoscelesTriangle.java src/main/java/org/openapitools/client/model/Mammal.java +src/main/java/org/openapitools/client/model/MammalAnyof.java src/main/java/org/openapitools/client/model/MapTest.java src/main/java/org/openapitools/client/model/MixedPropertiesAndAdditionalPropertiesClass.java src/main/java/org/openapitools/client/model/Model200Response.java @@ -195,3 +197,4 @@ src/main/java/org/openapitools/client/model/TriangleInterface.java src/main/java/org/openapitools/client/model/User.java src/main/java/org/openapitools/client/model/Whale.java src/main/java/org/openapitools/client/model/Zebra.java +src/test/java/org/openapitools/client/model/MammalAnyofTest.java diff --git a/samples/client/petstore/java/jersey3/README.md b/samples/client/petstore/java/jersey3/README.md index 0b1f5589c326..12225f352074 100644 --- a/samples/client/petstore/java/jersey3/README.md +++ b/samples/client/petstore/java/jersey3/README.md @@ -199,6 +199,7 @@ Class | Method | HTTP request | Description - [HealthCheckResult](docs/HealthCheckResult.md) - [IsoscelesTriangle](docs/IsoscelesTriangle.md) - [Mammal](docs/Mammal.md) + - [MammalAnyof](docs/MammalAnyof.md) - [MapTest](docs/MapTest.md) - [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md) - [Model200Response](docs/Model200Response.md) diff --git a/samples/client/petstore/java/jersey3/api/openapi.yaml b/samples/client/petstore/java/jersey3/api/openapi.yaml index 86a4762bc482..25e78fa9e305 100644 --- a/samples/client/petstore/java/jersey3/api/openapi.yaml +++ b/samples/client/petstore/java/jersey3/api/openapi.yaml @@ -2059,6 +2059,13 @@ components: - $ref: '#/components/schemas/whale' - $ref: '#/components/schemas/zebra' - $ref: '#/components/schemas/Pig' + mammal_anyof: + anyOf: + - $ref: '#/components/schemas/whale' + - $ref: '#/components/schemas/zebra' + - $ref: '#/components/schemas/Pig' + discriminator: + propertyName: className whale: properties: hasBaleen: diff --git a/samples/client/petstore/java/jersey3/docs/MammalAnyof.md b/samples/client/petstore/java/jersey3/docs/MammalAnyof.md new file mode 100644 index 000000000000..3fb117e293e2 --- /dev/null +++ b/samples/client/petstore/java/jersey3/docs/MammalAnyof.md @@ -0,0 +1,46 @@ + + +# MammalAnyof + +## anyOf schemas +* [Pig](Pig.md) +* [Whale](Whale.md) +* [Zebra](Zebra.md) + +## Example +```java +// Import classes: +import org.openapitools.client.model.MammalAnyof; +import org.openapitools.client.model.Pig; +import org.openapitools.client.model.Whale; +import org.openapitools.client.model.Zebra; + +public class Example { + public static void main(String[] args) { + MammalAnyof exampleMammalAnyof = new MammalAnyof(); + + // create a new Pig + Pig examplePig = new Pig(); + // set MammalAnyof to Pig + exampleMammalAnyof.setActualInstance(examplePig); + // to get back the Pig set earlier + Pig testPig = (Pig) exampleMammalAnyof.getActualInstance(); + + // create a new Whale + Whale exampleWhale = new Whale(); + // set MammalAnyof to Whale + exampleMammalAnyof.setActualInstance(exampleWhale); + // to get back the Whale set earlier + Whale testWhale = (Whale) exampleMammalAnyof.getActualInstance(); + + // create a new Zebra + Zebra exampleZebra = new Zebra(); + // set MammalAnyof to Zebra + exampleMammalAnyof.setActualInstance(exampleZebra); + // to get back the Zebra set earlier + Zebra testZebra = (Zebra) exampleMammalAnyof.getActualInstance(); + } +} +``` + + diff --git a/samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/model/MammalAnyof.java b/samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/model/MammalAnyof.java new file mode 100644 index 000000000000..ac8dd73b02d0 --- /dev/null +++ b/samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/model/MammalAnyof.java @@ -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.Map; +import java.util.HashMap; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import java.util.Objects; +import java.util.Map; +import java.util.HashMap; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.fasterxml.jackson.annotation.JsonValue; +import java.util.Arrays; +import org.openapitools.client.model.Pig; +import org.openapitools.client.model.Whale; +import org.openapitools.client.model.Zebra; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import jakarta.validation.constraints.*; +import jakarta.validation.Valid; +import org.openapitools.client.JSON; + + +import jakarta.ws.rs.core.GenericType; +import jakarta.ws.rs.core.Response; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import org.openapitools.client.JSON; + +@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.7.0-SNAPSHOT") +@JsonDeserialize(using=MammalAnyof.MammalAnyofDeserializer.class) +@JsonSerialize(using = MammalAnyof.MammalAnyofSerializer.class) +public class MammalAnyof extends AbstractOpenApiSchema { + private static final Logger log = Logger.getLogger(MammalAnyof.class.getName()); + + public static class MammalAnyofSerializer extends StdSerializer { + public MammalAnyofSerializer(Class t) { + super(t); + } + + public MammalAnyofSerializer() { + this(null); + } + + @Override + public void serialize(MammalAnyof value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { + jgen.writeObject(value.getActualInstance()); + } + } + + public static class MammalAnyofDeserializer extends StdDeserializer { + public MammalAnyofDeserializer() { + this(MammalAnyof.class); + } + + public MammalAnyofDeserializer(Class vc) { + super(vc); + } + + @Override + public MammalAnyof deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + JsonNode tree = jp.readValueAsTree(); + + Object deserialized = null; + Class cls = JSON.getClassForElement(tree, new MammalAnyof().getClass()); + if (cls != null) { + // When the OAS schema includes a discriminator, use the discriminator value to + // discriminate the anyOf schemas. + // Get the discriminator mapping value to get the class. + deserialized = tree.traverse(jp.getCodec()).readValueAs(cls); + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(deserialized); + return ret; + } + // deserialize Pig + try { + deserialized = tree.traverse(jp.getCodec()).readValueAs(Pig.class); + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(deserialized); + return ret; + } catch (Exception e) { + // deserialization failed, continue, log to help debugging + log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e); + } + + // deserialize Whale + try { + deserialized = tree.traverse(jp.getCodec()).readValueAs(Whale.class); + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(deserialized); + return ret; + } catch (Exception e) { + // deserialization failed, continue, log to help debugging + log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e); + } + + // deserialize Zebra + try { + deserialized = tree.traverse(jp.getCodec()).readValueAs(Zebra.class); + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(deserialized); + return ret; + } catch (Exception e) { + // deserialization failed, continue, log to help debugging + log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e); + } + + throw new IOException(String.format("Failed deserialization for MammalAnyof: no match found")); + } + + /** + * Handle deserialization of the 'null' value. + */ + @Override + public MammalAnyof getNullValue(DeserializationContext ctxt) throws JsonMappingException { + throw new JsonMappingException(ctxt.getParser(), "MammalAnyof cannot be null"); + } + } + + // store a list of schema names defined in anyOf + public static final Map schemas = new HashMap<>(); + + public MammalAnyof() { + super("anyOf", Boolean.FALSE); + } + /** + * A container for additional, undeclared properties. + * This is a holder for any undeclared properties as specified with + * the 'additionalProperties' keyword in the OAS document. + */ + private Map additionalProperties; + + /** + * Set the additional (undeclared) property with the specified name and value. + * If the property does not already exist, create it otherwise replace it. + */ + @JsonAnySetter + public MammalAnyof putAdditionalProperty(String key, Object value) { + if (this.additionalProperties == null) { + this.additionalProperties = new HashMap<>(); + } + this.additionalProperties.put(key, value); + return this; + } + + /** + * Return the additional (undeclared) property. + */ + @JsonAnyGetter + public Map getAdditionalProperties() { + return additionalProperties; + } + + /** + * Return the additional (undeclared) property with the specified name. + */ + public Object getAdditionalProperty(String key) { + if (this.additionalProperties == null) { + return null; + } + return this.additionalProperties.get(key); + } + + /** + * Return true if this mammal_anyof object is equal to o. + */ + @Override + public boolean equals(Object o) { + return super.equals(o) && Objects.equals(this.additionalProperties, ((MammalAnyof)o).additionalProperties); + } + + @Override + public int hashCode() { + return Objects.hash(getActualInstance(), isNullable(), getSchemaType(), additionalProperties); + } + public MammalAnyof(Pig o) { + super("anyOf", Boolean.FALSE); + setActualInstance(o); + } + + public MammalAnyof(Whale o) { + super("anyOf", Boolean.FALSE); + setActualInstance(o); + } + + public MammalAnyof(Zebra o) { + super("anyOf", Boolean.FALSE); + setActualInstance(o); + } + + static { + schemas.put("Pig", new GenericType() { + }); + schemas.put("Whale", new GenericType() { + }); + schemas.put("Zebra", new GenericType() { + }); + JSON.registerDescendants(MammalAnyof.class, Collections.unmodifiableMap(schemas)); + // Initialize and register the discriminator mappings. + Map> mappings = new HashMap<>(); + mappings.put("Pig", Pig.class); + mappings.put("whale", Whale.class); + mappings.put("zebra", Zebra.class); + mappings.put("mammal_anyof", MammalAnyof.class); + JSON.registerDiscriminator(MammalAnyof.class, "className", mappings); + } + + @Override + public Map getSchemas() { + return MammalAnyof.schemas; + } + + /** + * Set the instance that matches the anyOf child schema, check + * the instance parameter is valid against the anyOf child schemas: + * Pig, Whale, Zebra + * + * It could be an instance of the 'anyOf' schemas. + * The anyOf child schemas may themselves be a composed schema (allOf, anyOf, anyOf). + */ + @Override + public void setActualInstance(Object instance) { + if (JSON.isInstanceOf(Pig.class, instance, new HashSet<>())) { + super.setActualInstance(instance); + return; + } + + if (JSON.isInstanceOf(Whale.class, instance, new HashSet<>())) { + super.setActualInstance(instance); + return; + } + + if (JSON.isInstanceOf(Zebra.class, instance, new HashSet<>())) { + super.setActualInstance(instance); + return; + } + + throw new RuntimeException("Invalid instance type. Must be Pig, Whale, Zebra"); + } + + /** + * Get the actual instance, which can be the following: + * Pig, Whale, Zebra + * + * @return The actual instance (Pig, Whale, Zebra) + */ + @Override + public Object getActualInstance() { + return super.getActualInstance(); + } + + /** + * Get the actual instance of `Pig`. If the actual instance is not `Pig`, + * the ClassCastException will be thrown. + * + * @return The actual instance of `Pig` + * @throws ClassCastException if the instance is not `Pig` + */ + public Pig getPig() throws ClassCastException { + return (Pig)super.getActualInstance(); + } + + /** + * Get the actual instance of `Whale`. If the actual instance is not `Whale`, + * the ClassCastException will be thrown. + * + * @return The actual instance of `Whale` + * @throws ClassCastException if the instance is not `Whale` + */ + public Whale getWhale() throws ClassCastException { + return (Whale)super.getActualInstance(); + } + + /** + * Get the actual instance of `Zebra`. If the actual instance is not `Zebra`, + * the ClassCastException will be thrown. + * + * @return The actual instance of `Zebra` + * @throws ClassCastException if the instance is not `Zebra` + */ + public Zebra getZebra() throws ClassCastException { + return (Zebra)super.getActualInstance(); + } + +} + diff --git a/samples/client/petstore/java/jersey3/src/test/java/org/openapitools/client/model/MammalAnyofTest.java b/samples/client/petstore/java/jersey3/src/test/java/org/openapitools/client/model/MammalAnyofTest.java new file mode 100644 index 000000000000..a14bb5128030 --- /dev/null +++ b/samples/client/petstore/java/jersey3/src/test/java/org/openapitools/client/model/MammalAnyofTest.java @@ -0,0 +1,79 @@ +/* + * 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.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.fasterxml.jackson.annotation.JsonValue; +import java.util.Arrays; +import org.openapitools.client.model.Pig; +import org.openapitools.client.model.Whale; +import org.openapitools.client.model.Zebra; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * Model tests for MammalAnyof + */ +public class MammalAnyofTest { + private final MammalAnyof model = new MammalAnyof(); + + /** + * Model tests for MammalAnyof + */ + @Test + public void testMammalAnyof() { + // TODO: test MammalAnyof + } + + /** + * Test the property 'hasBaleen' + */ + @Test + public void hasBaleenTest() { + // TODO: test hasBaleen + } + + /** + * Test the property 'hasTeeth' + */ + @Test + public void hasTeethTest() { + // TODO: test hasTeeth + } + + /** + * Test the property 'className' + */ + @Test + public void classNameTest() { + // TODO: test className + } + + /** + * Test the property 'type' + */ + @Test + public void typeTest() { + // TODO: test type + } + +} diff --git a/samples/client/petstore/java/okhttp-gson/.openapi-generator/FILES b/samples/client/petstore/java/okhttp-gson/.openapi-generator/FILES index 02515104fd6b..113cd16b938e 100644 --- a/samples/client/petstore/java/okhttp-gson/.openapi-generator/FILES +++ b/samples/client/petstore/java/okhttp-gson/.openapi-generator/FILES @@ -60,6 +60,7 @@ docs/HasOnlyReadOnly.md docs/HealthCheckResult.md docs/IsoscelesTriangle.md docs/Mammal.md +docs/MammalAnyof.md docs/MapTest.md docs/MixedPropertiesAndAdditionalPropertiesClass.md docs/Model200Response.md @@ -203,6 +204,7 @@ src/main/java/org/openapitools/client/model/HasOnlyReadOnly.java src/main/java/org/openapitools/client/model/HealthCheckResult.java src/main/java/org/openapitools/client/model/IsoscelesTriangle.java src/main/java/org/openapitools/client/model/Mammal.java +src/main/java/org/openapitools/client/model/MammalAnyof.java src/main/java/org/openapitools/client/model/MapTest.java src/main/java/org/openapitools/client/model/MixedPropertiesAndAdditionalPropertiesClass.java src/main/java/org/openapitools/client/model/Model200Response.java diff --git a/samples/client/petstore/java/okhttp-gson/README.md b/samples/client/petstore/java/okhttp-gson/README.md index 23ac64866747..dafa7032b104 100644 --- a/samples/client/petstore/java/okhttp-gson/README.md +++ b/samples/client/petstore/java/okhttp-gson/README.md @@ -220,6 +220,7 @@ Class | Method | HTTP request | Description - [HealthCheckResult](docs/HealthCheckResult.md) - [IsoscelesTriangle](docs/IsoscelesTriangle.md) - [Mammal](docs/Mammal.md) + - [MammalAnyof](docs/MammalAnyof.md) - [MapTest](docs/MapTest.md) - [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md) - [Model200Response](docs/Model200Response.md) diff --git a/samples/client/petstore/java/okhttp-gson/api/openapi.yaml b/samples/client/petstore/java/okhttp-gson/api/openapi.yaml index 66b9001788ee..45059a376787 100644 --- a/samples/client/petstore/java/okhttp-gson/api/openapi.yaml +++ b/samples/client/petstore/java/okhttp-gson/api/openapi.yaml @@ -2298,6 +2298,13 @@ components: - $ref: '#/components/schemas/whale' - $ref: '#/components/schemas/zebra' - $ref: '#/components/schemas/Pig' + mammal_anyof: + anyOf: + - $ref: '#/components/schemas/whale' + - $ref: '#/components/schemas/zebra' + - $ref: '#/components/schemas/Pig' + discriminator: + propertyName: className whale: properties: hasBaleen: diff --git a/samples/client/petstore/java/okhttp-gson/docs/MammalAnyof.md b/samples/client/petstore/java/okhttp-gson/docs/MammalAnyof.md new file mode 100644 index 000000000000..7a366e10da49 --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson/docs/MammalAnyof.md @@ -0,0 +1,26 @@ + + +# MammalAnyof + + +## Properties + +| Name | Type | Description | Notes | +|------------ | ------------- | ------------- | -------------| +|**hasBaleen** | **Boolean** | | [optional] | +|**hasTeeth** | **Boolean** | | [optional] | +|**className** | **String** | | | +|**type** | [**TypeEnum**](#TypeEnum) | | [optional] | + + + +## Enum: TypeEnum + +| Name | Value | +|---- | -----| +| PLAINS | "plains" | +| MOUNTAIN | "mountain" | +| GREVYS | "grevys" | + + + diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java index d13cb5a17e5a..431ee642f413 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java @@ -117,6 +117,18 @@ public Class getClassForElement( return getClassByDiscriminator(classByDiscriminatorValue, getDiscriminatorValue(readElement, "className")); } + }) + .registerTypeSelector(org.openapitools.client.model.MammalAnyof.class, new TypeSelector() { + @Override + public Class getClassForElement(JsonElement readElement) { + Map classByDiscriminatorValue = new HashMap(); + classByDiscriminatorValue.put("Pig", org.openapitools.client.model.Pig.class); + classByDiscriminatorValue.put("whale", org.openapitools.client.model.Whale.class); + classByDiscriminatorValue.put("zebra", org.openapitools.client.model.Zebra.class); + classByDiscriminatorValue.put("mammal_anyof", org.openapitools.client.model.MammalAnyof.class); + return getClassByDiscriminator(classByDiscriminatorValue, + getDiscriminatorValue(readElement, "className")); + } }) .registerTypeSelector(org.openapitools.client.model.NullableShape.class, new TypeSelector() { @Override @@ -277,6 +289,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.HealthCheckResult.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.IsoscelesTriangle.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Mammal.CustomTypeAdapterFactory()); + gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.MammalAnyof.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.MapTest.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.MixedPropertiesAndAdditionalPropertiesClass.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Model200Response.CustomTypeAdapterFactory()); diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/MammalAnyof.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/MammalAnyof.java new file mode 100644 index 000000000000..19db3093bc53 --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/MammalAnyof.java @@ -0,0 +1,311 @@ +/* + * 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.Pig; +import org.openapitools.client.model.Whale; +import org.openapitools.client.model.Zebra; + + + +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.7.0-SNAPSHOT") +public class MammalAnyof extends AbstractOpenApiSchema { + private static final Logger log = Logger.getLogger(MammalAnyof.class.getName()); + + public static class CustomTypeAdapterFactory implements TypeAdapterFactory { + @SuppressWarnings("unchecked") + @Override + public TypeAdapter create(Gson gson, TypeToken type) { + if (!MammalAnyof.class.isAssignableFrom(type.getRawType())) { + return null; // this class only serializes 'MammalAnyof' and its subtypes + } + final TypeAdapter elementAdapter = gson.getAdapter(JsonElement.class); + final TypeAdapter adapterWhale = gson.getDelegateAdapter(this, TypeToken.get(Whale.class)); + final TypeAdapter adapterZebra = gson.getDelegateAdapter(this, TypeToken.get(Zebra.class)); + final TypeAdapter adapterPig = gson.getDelegateAdapter(this, TypeToken.get(Pig.class)); + + return (TypeAdapter) new TypeAdapter() { + @Override + public void write(JsonWriter out, MammalAnyof value) throws IOException { + if (value == null || value.getActualInstance() == null) { + elementAdapter.write(out, null); + return; + } + + // check if the actual instance is of the type `Whale` + if (value.getActualInstance() instanceof Whale) { + JsonElement element = adapterWhale.toJsonTree((Whale)value.getActualInstance()); + elementAdapter.write(out, element); + return; + } + // check if the actual instance is of the type `Zebra` + if (value.getActualInstance() instanceof Zebra) { + JsonElement element = adapterZebra.toJsonTree((Zebra)value.getActualInstance()); + elementAdapter.write(out, element); + return; + } + // check if the actual instance is of the type `Pig` + if (value.getActualInstance() instanceof Pig) { + JsonElement element = adapterPig.toJsonTree((Pig)value.getActualInstance()); + elementAdapter.write(out, element); + return; + } + throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: Pig, Whale, Zebra"); + } + + @Override + public MammalAnyof read(JsonReader in) throws IOException { + Object deserialized = null; + JsonElement jsonElement = elementAdapter.read(in); + + ArrayList errorMessages = new ArrayList<>(); + TypeAdapter actualAdapter = elementAdapter; + + // deserialize Whale + try { + // validate the JSON object to see if any exception is thrown + Whale.validateJsonElement(jsonElement); + actualAdapter = adapterWhale; + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement)); + return ret; + } catch (Exception e) { + // deserialization failed, continue + errorMessages.add(String.format("Deserialization for Whale failed with `%s`.", e.getMessage())); + log.log(Level.FINER, "Input data does not match schema 'Whale'", e); + } + // deserialize Zebra + try { + // validate the JSON object to see if any exception is thrown + Zebra.validateJsonElement(jsonElement); + actualAdapter = adapterZebra; + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement)); + return ret; + } catch (Exception e) { + // deserialization failed, continue + errorMessages.add(String.format("Deserialization for Zebra failed with `%s`.", e.getMessage())); + log.log(Level.FINER, "Input data does not match schema 'Zebra'", e); + } + // deserialize Pig + try { + // validate the JSON object to see if any exception is thrown + Pig.validateJsonElement(jsonElement); + actualAdapter = adapterPig; + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement)); + return ret; + } catch (Exception e) { + // deserialization failed, continue + errorMessages.add(String.format("Deserialization for Pig failed with `%s`.", e.getMessage())); + log.log(Level.FINER, "Input data does not match schema 'Pig'", e); + } + + throw new IOException(String.format("Failed deserialization for MammalAnyof: 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> schemas = new HashMap>(); + + public MammalAnyof() { + super("anyOf", Boolean.FALSE); + } + + public MammalAnyof(Object o) { + super("anyOf", Boolean.FALSE); + setActualInstance(o); + } + + static { + schemas.put("Whale", Whale.class); + schemas.put("Zebra", Zebra.class); + schemas.put("Pig", Pig.class); + } + + @Override + public Map> getSchemas() { + return MammalAnyof.schemas; + } + + /** + * Set the instance that matches the anyOf child schema, check + * the instance parameter is valid against the anyOf child schemas: + * Pig, Whale, Zebra + * + * It could be an instance of the 'anyOf' schemas. + */ + @Override + public void setActualInstance(Object instance) { + if (instance instanceof Whale) { + super.setActualInstance(instance); + return; + } + + if (instance instanceof Zebra) { + super.setActualInstance(instance); + return; + } + + if (instance instanceof Pig) { + super.setActualInstance(instance); + return; + } + + throw new RuntimeException("Invalid instance type. Must be Pig, Whale, Zebra"); + } + + /** + * Get the actual instance, which can be the following: + * Pig, Whale, Zebra + * + * @return The actual instance (Pig, Whale, Zebra) + */ + @SuppressWarnings("unchecked") + @Override + public Object getActualInstance() { + return super.getActualInstance(); + } + + /** + * Get the actual instance of `Whale`. If the actual instance is not `Whale`, + * the ClassCastException will be thrown. + * + * @return The actual instance of `Whale` + * @throws ClassCastException if the instance is not `Whale` + */ + public Whale getWhale() throws ClassCastException { + return (Whale)super.getActualInstance(); + } + /** + * Get the actual instance of `Zebra`. If the actual instance is not `Zebra`, + * the ClassCastException will be thrown. + * + * @return The actual instance of `Zebra` + * @throws ClassCastException if the instance is not `Zebra` + */ + public Zebra getZebra() throws ClassCastException { + return (Zebra)super.getActualInstance(); + } + /** + * Get the actual instance of `Pig`. If the actual instance is not `Pig`, + * the ClassCastException will be thrown. + * + * @return The actual instance of `Pig` + * @throws ClassCastException if the instance is not `Pig` + */ + public Pig getPig() throws ClassCastException { + return (Pig)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 MammalAnyof + */ + public static void validateJsonElement(JsonElement jsonElement) throws IOException { + // validate anyOf schemas one by one + ArrayList errorMessages = new ArrayList<>(); + // validate the json string with Whale + try { + Whale.validateJsonElement(jsonElement); + return; + } catch (Exception e) { + errorMessages.add(String.format("Deserialization for Whale failed with `%s`.", e.getMessage())); + // continue to the next one + } + // validate the json string with Zebra + try { + Zebra.validateJsonElement(jsonElement); + return; + } catch (Exception e) { + errorMessages.add(String.format("Deserialization for Zebra failed with `%s`.", e.getMessage())); + // continue to the next one + } + // validate the json string with Pig + try { + Pig.validateJsonElement(jsonElement); + return; + } catch (Exception e) { + errorMessages.add(String.format("Deserialization for Pig failed with `%s`.", e.getMessage())); + // continue to the next one + } + throw new IOException(String.format("The JSON string is invalid for MammalAnyof with anyOf schemas: Pig, Whale, Zebra. 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 MammalAnyof given an JSON string + * + * @param jsonString JSON string + * @return An instance of MammalAnyof + * @throws IOException if the JSON string is invalid with respect to MammalAnyof + */ + public static MammalAnyof fromJson(String jsonString) throws IOException { + return JSON.getGson().fromJson(jsonString, MammalAnyof.class); + } + + /** + * Convert an instance of MammalAnyof to an JSON string + * + * @return JSON string + */ + public String toJson() { + return JSON.getGson().toJson(this); + } +} + diff --git a/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/MammalAnyofTest.java b/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/MammalAnyofTest.java new file mode 100644 index 000000000000..c36c434de11f --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/MammalAnyofTest.java @@ -0,0 +1,75 @@ +/* + * 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.Pig; +import org.openapitools.client.model.Whale; +import org.openapitools.client.model.Zebra; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * Model tests for MammalAnyof + */ +public class MammalAnyofTest { + private final MammalAnyof model = new MammalAnyof(); + + /** + * Model tests for MammalAnyof + */ + @Test + public void testMammalAnyof() { + // TODO: test MammalAnyof + } + + /** + * Test the property 'hasBaleen' + */ + @Test + public void hasBaleenTest() { + // TODO: test hasBaleen + } + + /** + * Test the property 'hasTeeth' + */ + @Test + public void hasTeethTest() { + // TODO: test hasTeeth + } + + /** + * Test the property 'className' + */ + @Test + public void classNameTest() { + // TODO: test className + } + + /** + * Test the property 'type' + */ + @Test + public void typeTest() { + // TODO: test type + } + +} diff --git a/samples/client/petstore/typescript-axios/builds/test-petstore/api.ts b/samples/client/petstore/typescript-axios/builds/test-petstore/api.ts index 2d63feaab3a4..1506535fa573 100644 --- a/samples/client/petstore/typescript-axios/builds/test-petstore/api.ts +++ b/samples/client/petstore/typescript-axios/builds/test-petstore/api.ts @@ -910,6 +910,46 @@ export interface List { */ export type Mammal = Pig | Whale | Zebra; +/** + * + * @export + * @interface MammalAnyof + */ +export interface MammalAnyof { + /** + * + * @type {boolean} + * @memberof MammalAnyof + */ + 'hasBaleen'?: boolean; + /** + * + * @type {boolean} + * @memberof MammalAnyof + */ + 'hasTeeth'?: boolean; + /** + * + * @type {string} + * @memberof MammalAnyof + */ + 'className': string; + /** + * + * @type {string} + * @memberof MammalAnyof + */ + 'type'?: MammalAnyofTypeEnum; +} + +export const MammalAnyofTypeEnum = { + Plains: 'plains', + Mountain: 'mountain', + Grevys: 'grevys' +} as const; + +export type MammalAnyofTypeEnum = typeof MammalAnyofTypeEnum[keyof typeof MammalAnyofTypeEnum]; + /** * * @export diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/.openapi-generator/FILES b/samples/openapi3/client/petstore/java/jersey2-java8/.openapi-generator/FILES index 4fbcb8fd20b1..943457dcdbf3 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/java/jersey2-java8/.openapi-generator/FILES @@ -46,6 +46,7 @@ docs/HasOnlyReadOnly.md docs/HealthCheckResult.md docs/IsoscelesTriangle.md docs/Mammal.md +docs/MammalAnyof.md docs/MapTest.md docs/MixedPropertiesAndAdditionalPropertiesClass.md docs/Model200Response.md @@ -158,6 +159,7 @@ src/main/java/org/openapitools/client/model/HasOnlyReadOnly.java src/main/java/org/openapitools/client/model/HealthCheckResult.java src/main/java/org/openapitools/client/model/IsoscelesTriangle.java src/main/java/org/openapitools/client/model/Mammal.java +src/main/java/org/openapitools/client/model/MammalAnyof.java src/main/java/org/openapitools/client/model/MapTest.java src/main/java/org/openapitools/client/model/MixedPropertiesAndAdditionalPropertiesClass.java src/main/java/org/openapitools/client/model/Model200Response.java @@ -195,3 +197,4 @@ src/main/java/org/openapitools/client/model/TriangleInterface.java src/main/java/org/openapitools/client/model/User.java src/main/java/org/openapitools/client/model/Whale.java src/main/java/org/openapitools/client/model/Zebra.java +src/test/java/org/openapitools/client/model/MammalAnyofTest.java diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/README.md b/samples/openapi3/client/petstore/java/jersey2-java8/README.md index 5989b2f55538..3a182ed4750d 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/README.md +++ b/samples/openapi3/client/petstore/java/jersey2-java8/README.md @@ -224,6 +224,7 @@ Class | Method | HTTP request | Description - [HealthCheckResult](docs/HealthCheckResult.md) - [IsoscelesTriangle](docs/IsoscelesTriangle.md) - [Mammal](docs/Mammal.md) + - [MammalAnyof](docs/MammalAnyof.md) - [MapTest](docs/MapTest.md) - [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md) - [Model200Response](docs/Model200Response.md) diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/api/openapi.yaml b/samples/openapi3/client/petstore/java/jersey2-java8/api/openapi.yaml index 86a4762bc482..25e78fa9e305 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/api/openapi.yaml +++ b/samples/openapi3/client/petstore/java/jersey2-java8/api/openapi.yaml @@ -2059,6 +2059,13 @@ components: - $ref: '#/components/schemas/whale' - $ref: '#/components/schemas/zebra' - $ref: '#/components/schemas/Pig' + mammal_anyof: + anyOf: + - $ref: '#/components/schemas/whale' + - $ref: '#/components/schemas/zebra' + - $ref: '#/components/schemas/Pig' + discriminator: + propertyName: className whale: properties: hasBaleen: diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/docs/MammalAnyof.md b/samples/openapi3/client/petstore/java/jersey2-java8/docs/MammalAnyof.md new file mode 100644 index 000000000000..3fb117e293e2 --- /dev/null +++ b/samples/openapi3/client/petstore/java/jersey2-java8/docs/MammalAnyof.md @@ -0,0 +1,46 @@ + + +# MammalAnyof + +## anyOf schemas +* [Pig](Pig.md) +* [Whale](Whale.md) +* [Zebra](Zebra.md) + +## Example +```java +// Import classes: +import org.openapitools.client.model.MammalAnyof; +import org.openapitools.client.model.Pig; +import org.openapitools.client.model.Whale; +import org.openapitools.client.model.Zebra; + +public class Example { + public static void main(String[] args) { + MammalAnyof exampleMammalAnyof = new MammalAnyof(); + + // create a new Pig + Pig examplePig = new Pig(); + // set MammalAnyof to Pig + exampleMammalAnyof.setActualInstance(examplePig); + // to get back the Pig set earlier + Pig testPig = (Pig) exampleMammalAnyof.getActualInstance(); + + // create a new Whale + Whale exampleWhale = new Whale(); + // set MammalAnyof to Whale + exampleMammalAnyof.setActualInstance(exampleWhale); + // to get back the Whale set earlier + Whale testWhale = (Whale) exampleMammalAnyof.getActualInstance(); + + // create a new Zebra + Zebra exampleZebra = new Zebra(); + // set MammalAnyof to Zebra + exampleMammalAnyof.setActualInstance(exampleZebra); + // to get back the Zebra set earlier + Zebra testZebra = (Zebra) exampleMammalAnyof.getActualInstance(); + } +} +``` + + diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/model/MammalAnyof.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/model/MammalAnyof.java new file mode 100644 index 000000000000..aa316e4eb06e --- /dev/null +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/model/MammalAnyof.java @@ -0,0 +1,318 @@ +/* + * 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.Map; +import java.util.HashMap; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import java.util.Objects; +import java.util.Map; +import java.util.HashMap; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.fasterxml.jackson.annotation.JsonValue; +import java.util.Arrays; +import org.openapitools.client.model.Pig; +import org.openapitools.client.model.Whale; +import org.openapitools.client.model.Zebra; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import org.openapitools.client.JSON; + + +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.Response; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import org.openapitools.client.JSON; + +@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.7.0-SNAPSHOT") +@JsonDeserialize(using=MammalAnyof.MammalAnyofDeserializer.class) +@JsonSerialize(using = MammalAnyof.MammalAnyofSerializer.class) +public class MammalAnyof extends AbstractOpenApiSchema { + private static final Logger log = Logger.getLogger(MammalAnyof.class.getName()); + + public static class MammalAnyofSerializer extends StdSerializer { + public MammalAnyofSerializer(Class t) { + super(t); + } + + public MammalAnyofSerializer() { + this(null); + } + + @Override + public void serialize(MammalAnyof value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { + jgen.writeObject(value.getActualInstance()); + } + } + + public static class MammalAnyofDeserializer extends StdDeserializer { + public MammalAnyofDeserializer() { + this(MammalAnyof.class); + } + + public MammalAnyofDeserializer(Class vc) { + super(vc); + } + + @Override + public MammalAnyof deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + JsonNode tree = jp.readValueAsTree(); + + Object deserialized = null; + Class cls = JSON.getClassForElement(tree, new MammalAnyof().getClass()); + if (cls != null) { + // When the OAS schema includes a discriminator, use the discriminator value to + // discriminate the anyOf schemas. + // Get the discriminator mapping value to get the class. + deserialized = tree.traverse(jp.getCodec()).readValueAs(cls); + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(deserialized); + return ret; + } + // deserialize Pig + try { + deserialized = tree.traverse(jp.getCodec()).readValueAs(Pig.class); + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(deserialized); + return ret; + } catch (Exception e) { + // deserialization failed, continue, log to help debugging + log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e); + } + + // deserialize Whale + try { + deserialized = tree.traverse(jp.getCodec()).readValueAs(Whale.class); + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(deserialized); + return ret; + } catch (Exception e) { + // deserialization failed, continue, log to help debugging + log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e); + } + + // deserialize Zebra + try { + deserialized = tree.traverse(jp.getCodec()).readValueAs(Zebra.class); + MammalAnyof ret = new MammalAnyof(); + ret.setActualInstance(deserialized); + return ret; + } catch (Exception e) { + // deserialization failed, continue, log to help debugging + log.log(Level.FINER, "Input data does not match 'MammalAnyof'", e); + } + + throw new IOException(String.format("Failed deserialization for MammalAnyof: no match found")); + } + + /** + * Handle deserialization of the 'null' value. + */ + @Override + public MammalAnyof getNullValue(DeserializationContext ctxt) throws JsonMappingException { + throw new JsonMappingException(ctxt.getParser(), "MammalAnyof cannot be null"); + } + } + + // store a list of schema names defined in anyOf + public static final Map schemas = new HashMap<>(); + + public MammalAnyof() { + super("anyOf", Boolean.FALSE); + } + /** + * A container for additional, undeclared properties. + * This is a holder for any undeclared properties as specified with + * the 'additionalProperties' keyword in the OAS document. + */ + private Map additionalProperties; + + /** + * Set the additional (undeclared) property with the specified name and value. + * If the property does not already exist, create it otherwise replace it. + */ + @JsonAnySetter + public MammalAnyof putAdditionalProperty(String key, Object value) { + if (this.additionalProperties == null) { + this.additionalProperties = new HashMap<>(); + } + this.additionalProperties.put(key, value); + return this; + } + + /** + * Return the additional (undeclared) property. + */ + @JsonAnyGetter + public Map getAdditionalProperties() { + return additionalProperties; + } + + /** + * Return the additional (undeclared) property with the specified name. + */ + public Object getAdditionalProperty(String key) { + if (this.additionalProperties == null) { + return null; + } + return this.additionalProperties.get(key); + } + + /** + * Return true if this mammal_anyof object is equal to o. + */ + @Override + public boolean equals(Object o) { + return super.equals(o) && Objects.equals(this.additionalProperties, ((MammalAnyof)o).additionalProperties); + } + + @Override + public int hashCode() { + return Objects.hash(getActualInstance(), isNullable(), getSchemaType(), additionalProperties); + } + public MammalAnyof(Pig o) { + super("anyOf", Boolean.FALSE); + setActualInstance(o); + } + + public MammalAnyof(Whale o) { + super("anyOf", Boolean.FALSE); + setActualInstance(o); + } + + public MammalAnyof(Zebra o) { + super("anyOf", Boolean.FALSE); + setActualInstance(o); + } + + static { + schemas.put("Pig", new GenericType() { + }); + schemas.put("Whale", new GenericType() { + }); + schemas.put("Zebra", new GenericType() { + }); + JSON.registerDescendants(MammalAnyof.class, Collections.unmodifiableMap(schemas)); + // Initialize and register the discriminator mappings. + Map> mappings = new HashMap<>(); + mappings.put("Pig", Pig.class); + mappings.put("whale", Whale.class); + mappings.put("zebra", Zebra.class); + mappings.put("mammal_anyof", MammalAnyof.class); + JSON.registerDiscriminator(MammalAnyof.class, "className", mappings); + } + + @Override + public Map getSchemas() { + return MammalAnyof.schemas; + } + + /** + * Set the instance that matches the anyOf child schema, check + * the instance parameter is valid against the anyOf child schemas: + * Pig, Whale, Zebra + * + * It could be an instance of the 'anyOf' schemas. + * The anyOf child schemas may themselves be a composed schema (allOf, anyOf, anyOf). + */ + @Override + public void setActualInstance(Object instance) { + if (JSON.isInstanceOf(Pig.class, instance, new HashSet<>())) { + super.setActualInstance(instance); + return; + } + + if (JSON.isInstanceOf(Whale.class, instance, new HashSet<>())) { + super.setActualInstance(instance); + return; + } + + if (JSON.isInstanceOf(Zebra.class, instance, new HashSet<>())) { + super.setActualInstance(instance); + return; + } + + throw new RuntimeException("Invalid instance type. Must be Pig, Whale, Zebra"); + } + + /** + * Get the actual instance, which can be the following: + * Pig, Whale, Zebra + * + * @return The actual instance (Pig, Whale, Zebra) + */ + @Override + public Object getActualInstance() { + return super.getActualInstance(); + } + + /** + * Get the actual instance of `Pig`. If the actual instance is not `Pig`, + * the ClassCastException will be thrown. + * + * @return The actual instance of `Pig` + * @throws ClassCastException if the instance is not `Pig` + */ + public Pig getPig() throws ClassCastException { + return (Pig)super.getActualInstance(); + } + + /** + * Get the actual instance of `Whale`. If the actual instance is not `Whale`, + * the ClassCastException will be thrown. + * + * @return The actual instance of `Whale` + * @throws ClassCastException if the instance is not `Whale` + */ + public Whale getWhale() throws ClassCastException { + return (Whale)super.getActualInstance(); + } + + /** + * Get the actual instance of `Zebra`. If the actual instance is not `Zebra`, + * the ClassCastException will be thrown. + * + * @return The actual instance of `Zebra` + * @throws ClassCastException if the instance is not `Zebra` + */ + public Zebra getZebra() throws ClassCastException { + return (Zebra)super.getActualInstance(); + } + +} + diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/model/MammalAnyofTest.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/model/MammalAnyofTest.java new file mode 100644 index 000000000000..a14bb5128030 --- /dev/null +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/model/MammalAnyofTest.java @@ -0,0 +1,79 @@ +/* + * 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.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.fasterxml.jackson.annotation.JsonValue; +import java.util.Arrays; +import org.openapitools.client.model.Pig; +import org.openapitools.client.model.Whale; +import org.openapitools.client.model.Zebra; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * Model tests for MammalAnyof + */ +public class MammalAnyofTest { + private final MammalAnyof model = new MammalAnyof(); + + /** + * Model tests for MammalAnyof + */ + @Test + public void testMammalAnyof() { + // TODO: test MammalAnyof + } + + /** + * Test the property 'hasBaleen' + */ + @Test + public void hasBaleenTest() { + // TODO: test hasBaleen + } + + /** + * Test the property 'hasTeeth' + */ + @Test + public void hasTeethTest() { + // TODO: test hasTeeth + } + + /** + * Test the property 'className' + */ + @Test + public void classNameTest() { + // TODO: test className + } + + /** + * Test the property 'type' + */ + @Test + public void typeTest() { + // TODO: test type + } + +}