use of io.swagger.v3.oas.models.media.Discriminator in project swagger-core by swagger-api.
the class Ticket3197Test method testTicket3197AsSibling.
@Test
public void testTicket3197AsSibling() throws Exception {
ModelResolver.composedModelPropertiesAsSibling = true;
ModelResolver myModelResolver = new ModelResolver(new ObjectMapper());
ModelResolver.composedModelPropertiesAsSibling = true;
ModelConverterContextImpl myContext = new ModelConverterContextImpl(myModelResolver);
final Schema model = myContext.resolve(new AnnotatedType(Car.class));
assertNotNull(model);
String yaml = "Car:\n" + " required:\n" + " - type\n" + " type: object\n" + " properties:\n" + " carMetaData:\n" + " type: string\n" + " type:\n" + " type: string\n" + " discriminator:\n" + " propertyName: type\n" + " mapping:\n" + " RaceCar: '#/components/schemas/RaceCar'\n" + " SportCar: '#/components/schemas/SportCar'\n" + " oneOf:\n" + " - $ref: '#/components/schemas/RaceCar'\n" + " - $ref: '#/components/schemas/SportCar'\n" + "RaceCar:\n" + " required:\n" + " - carMetaData\n" + " - id\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int64\n" + " model:\n" + " type: string\n" + " allOf:\n" + " - $ref: '#/components/schemas/Car'\n" + "SportCar:\n" + " required:\n" + " - id\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int64\n" + " model:\n" + " type: string\n" + " allOf:\n" + " - $ref: '#/components/schemas/Car'\n";
SerializationMatchers.assertEqualsToYaml(myContext.getDefinedModels(), yaml);
ModelResolver.composedModelPropertiesAsSibling = false;
}
use of io.swagger.v3.oas.models.media.Discriminator in project swagger-core by swagger-api.
the class ModelResolver method resolveDiscriminator.
protected Discriminator resolveDiscriminator(JavaType type, ModelConverterContext context) {
io.swagger.v3.oas.annotations.media.Schema declaredSchemaAnnotation = AnnotationsUtils.getSchemaDeclaredAnnotation(type.getRawClass());
String disc = (declaredSchemaAnnotation == null) ? "" : declaredSchemaAnnotation.discriminatorProperty();
if (disc.isEmpty()) {
// longer method would involve AnnotationIntrospector.findTypeResolver(...) but:
JsonTypeInfo typeInfo = type.getRawClass().getDeclaredAnnotation(JsonTypeInfo.class);
if (typeInfo != null) {
disc = typeInfo.property();
}
}
if (!disc.isEmpty()) {
Discriminator discriminator = new Discriminator().propertyName(disc);
if (declaredSchemaAnnotation != null) {
DiscriminatorMapping[] mappings = declaredSchemaAnnotation.discriminatorMapping();
if (mappings != null && mappings.length > 0) {
for (DiscriminatorMapping mapping : mappings) {
if (!mapping.value().isEmpty() && !mapping.schema().equals(Void.class)) {
discriminator.mapping(mapping.value(), constructRef(context.resolve(new AnnotatedType().type(mapping.schema())).getName()));
}
}
}
}
return discriminator;
}
return null;
}
use of io.swagger.v3.oas.models.media.Discriminator in project swagger-core by swagger-api.
the class OpenAPI3_1SerializationTest method testDiscriminatorSerialization.
@Test
public void testDiscriminatorSerialization() {
Schema<String> propertySchema1 = new StringSchema();
propertySchema1.addType(propertySchema1.getType());
Schema<String> propertySchema2 = new StringSchema();
propertySchema2.addType(propertySchema2.getType());
Discriminator discriminator = new Discriminator().propertyName("type");
discriminator.addExtension("x-otherName", "discriminationType");
Schema schema = new ObjectSchema().addProperties("name", propertySchema1).addProperties("type", propertySchema1).discriminator(discriminator);
schema.addType(schema.getType());
OpenAPI openAPI = new OpenAPI().openapi("3.1.0").components(new Components().addSchemas("pet", schema));
SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "components:\n" + " schemas:\n" + " pet:\n" + " properties:\n" + " name:\n" + " type: string\n" + " type:\n" + " type: string\n" + " discriminator:\n" + " propertyName: type\n" + " x-otherName: discriminationType\n" + " type: object");
SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + " \"openapi\" : \"3.1.0\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"pet\" : {\n" + " \"properties\" : {\n" + " \"name\" : {\n" + " \"type\" : \"string\"\n" + " },\n" + " \"type\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " },\n" + " \"discriminator\" : {\n" + " \"propertyName\" : \"type\",\n" + " \"x-otherName\" : \"discriminationType\"\n" + " },\n" + " \"type\" : \"object\"\n" + " }\n" + " }\n" + " }\n" + "}");
openAPI.openapi("3.0.3");
SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.3\n" + "components:\n" + " schemas:\n" + " pet:\n" + " properties:\n" + " name:\n" + " type: string\n" + " type:\n" + " type: string\n" + " discriminator:\n" + " propertyName: type\n" + " type: object");
SerializationMatchers.assertEqualsToJson(openAPI, "{\n" + " \"openapi\" : \"3.0.3\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"pet\" : {\n" + " \"properties\" : {\n" + " \"name\" : {\n" + " \"type\" : \"string\"\n" + " },\n" + " \"type\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " },\n" + " \"discriminator\" : {\n" + " \"propertyName\" : \"type\"\n" + " },\n" + " \"type\" : \"object\"\n" + " }\n" + " }\n" + " }\n" + "}");
}
use of io.swagger.v3.oas.models.media.Discriminator in project swagger-core by swagger-api.
the class ReaderTest method testTicket2340.
@Test(description = "Responses with array schema")
public void testTicket2340() {
Reader reader = new Reader(new OpenAPI());
OpenAPI openAPI = reader.read(Ticket2340Resource.class);
String yaml = "openapi: 3.0.1\n" + "paths:\n" + " /test/test:\n" + " post:\n" + " operationId: getAnimal\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/Animal'\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " application/json:\n" + " schema:\n" + " type: string\n" + "components:\n" + " schemas:\n" + " Animal:\n" + " required:\n" + " - type\n" + " type: object\n" + " properties:\n" + " type:\n" + " type: string\n" + " discriminator:\n" + " propertyName: type\n" + " Cat:\n" + " type: object\n" + " allOf:\n" + " - $ref: '#/components/schemas/Animal'\n" + " - type: object\n" + " properties:\n" + " lives:\n" + " type: integer\n" + " format: int32\n" + " Dog:\n" + " type: object\n" + " allOf:\n" + " - $ref: '#/components/schemas/Animal'\n" + " - type: object\n" + " properties:\n" + " barkVolume:\n" + " type: number\n" + " format: double\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.oas.models.media.Discriminator in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testDeserializeWithBooleanEnumDiscriminator.
@Test
public void testDeserializeWithBooleanEnumDiscriminator() {
String yaml = "openapi: 3.0.0\n" + "components:\n" + " schemas:\n" + " Animal:\n" + " type: object\n" + " discriminator:\n" + " propertyName: petType\n" + " description: |\n" + " A basic `Animal` object which can extend to other animal types.\n" + " required:\n" + " - commonName\n" + " - petType\n" + " properties:\n" + " commonName:\n" + " description: the household name of the animal\n" + " type: string\n" + " petType:\n" + " enum:\n" + " - true\n" + " - false";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
Map<String, Schema> properties = result.getOpenAPI().getComponents().getSchemas().get("Animal").getProperties();
assertTrue(properties.containsKey("commonName"));
assertTrue(properties.containsKey("petType"));
assertEquals(properties.get("petType").getType(), "boolean");
}
Aggregations