Search in sources :

Example 11 with Discriminator

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;
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) Schema(io.swagger.v3.oas.models.media.Schema) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) ModelResolver(io.swagger.v3.core.jackson.ModelResolver) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest)

Example 12 with Discriminator

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;
}
Also used : DiscriminatorMapping(io.swagger.v3.oas.annotations.media.DiscriminatorMapping) AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) Discriminator(io.swagger.v3.oas.models.media.Discriminator)

Example 13 with Discriminator

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" + "}");
}
Also used : Components(io.swagger.v3.oas.models.Components) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Discriminator(io.swagger.v3.oas.models.media.Discriminator) Test(org.testng.annotations.Test)

Example 14 with Discriminator

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);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 15 with Discriminator

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");
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Aggregations

Schema (io.swagger.v3.oas.models.media.Schema)19 Test (org.testng.annotations.Test)19 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)15 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)15 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)15 MapSchema (io.swagger.v3.oas.models.media.MapSchema)14 StringSchema (io.swagger.v3.oas.models.media.StringSchema)14 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)13 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)13 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)12 OpenAPI (io.swagger.v3.oas.models.OpenAPI)11 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)6 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)6 Discriminator (io.swagger.v3.oas.models.media.Discriminator)5 DateSchema (io.swagger.v3.oas.models.media.DateSchema)4 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)4 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)3 HashSet (java.util.HashSet)3 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2