Search in sources :

Example 96 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-core by swagger-api.

the class SwaggerAnnotationIntrospector method hasRequiredMarker.

@Override
public Boolean hasRequiredMarker(AnnotatedMember m) {
    XmlElement elem = m.getAnnotation(XmlElement.class);
    if (elem != null) {
        if (elem.required()) {
            return true;
        }
    }
    JsonProperty jsonProperty = m.getAnnotation(JsonProperty.class);
    if (jsonProperty != null) {
        if (jsonProperty.required()) {
            return true;
        }
    }
    Schema ann = m.getAnnotation(Schema.class);
    if (ann != null) {
        if (ann.required()) {
            return ann.required();
        }
    }
    ArraySchema arraySchema = m.getAnnotation(ArraySchema.class);
    if (arraySchema != null) {
        if (arraySchema.arraySchema().required()) {
            return arraySchema.arraySchema().required();
        }
        if (arraySchema.schema().required()) {
            return arraySchema.schema().required();
        }
    }
    return null;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) Schema(io.swagger.v3.oas.annotations.media.Schema) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 97 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-core by swagger-api.

the class SwaggerAnnotationIntrospector method findSubtypes.

@Override
public List<NamedType> findSubtypes(Annotated a) {
    Schema schema = a.getAnnotation(Schema.class);
    if (schema == null) {
        final ArraySchema arraySchema = a.getAnnotation(ArraySchema.class);
        if (arraySchema != null) {
            schema = arraySchema.schema();
        }
    }
    if (AnnotationsUtils.hasSchemaAnnotation(schema)) {
        final Class<?>[] classes = schema.subTypes();
        final List<NamedType> names = new ArrayList<>(classes.length);
        for (Class<?> subType : classes) {
            names.add(new NamedType(subType));
        }
        if (!names.isEmpty()) {
            return names;
        }
    }
    return Collections.emptyList();
}
Also used : ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) Schema(io.swagger.v3.oas.annotations.media.Schema) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) ArrayList(java.util.ArrayList) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass)

Example 98 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-core by swagger-api.

the class ObjectMapperFactory method create.

private static ObjectMapper create(JsonFactory jsonFactory, boolean openapi31) {
    ObjectMapper mapper = jsonFactory == null ? new ObjectMapper() : new ObjectMapper(jsonFactory);
    if (!openapi31) {
        // handle ref schema serialization skipping all other props
        mapper.registerModule(new SimpleModule() {

            @Override
            public void setupModule(SetupContext context) {
                super.setupModule(context);
                context.addBeanSerializerModifier(new BeanSerializerModifier() {

                    @Override
                    public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription desc, JsonSerializer<?> serializer) {
                        if (Schema.class.isAssignableFrom(desc.getBeanClass())) {
                            return new SchemaSerializer((JsonSerializer<Object>) serializer);
                        } else if (MediaType.class.isAssignableFrom(desc.getBeanClass())) {
                            return new MediaTypeSerializer((JsonSerializer<Object>) serializer);
                        }
                        return serializer;
                    }
                });
            }
        });
    } else {
        mapper.registerModule(new SimpleModule() {

            @Override
            public void setupModule(SetupContext context) {
                super.setupModule(context);
                context.addBeanSerializerModifier(new BeanSerializerModifier() {

                    @Override
                    public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription desc, JsonSerializer<?> serializer) {
                        if (Schema.class.isAssignableFrom(desc.getBeanClass())) {
                            return new Schema31Serializer((JsonSerializer<Object>) serializer);
                        } else if (MediaType.class.isAssignableFrom(desc.getBeanClass())) {
                            return new MediaTypeSerializer((JsonSerializer<Object>) serializer);
                        }
                        return serializer;
                    }
                });
            }
        });
    }
    if (!openapi31) {
        Module deserializerModule = new DeserializationModule();
        mapper.registerModule(deserializerModule);
    } else {
        Module deserializerModule = new DeserializationModule31();
        mapper.registerModule(deserializerModule);
    }
    mapper.registerModule(new JavaTimeModule());
    Map<Class<?>, Class<?>> sourceMixins = new LinkedHashMap<>();
    sourceMixins.put(ApiResponses.class, ExtensionsMixin.class);
    sourceMixins.put(Contact.class, ExtensionsMixin.class);
    sourceMixins.put(Encoding.class, ExtensionsMixin.class);
    sourceMixins.put(EncodingProperty.class, ExtensionsMixin.class);
    sourceMixins.put(Example.class, ExampleMixin.class);
    sourceMixins.put(ExternalDocumentation.class, ExtensionsMixin.class);
    sourceMixins.put(Link.class, ExtensionsMixin.class);
    sourceMixins.put(LinkParameter.class, ExtensionsMixin.class);
    sourceMixins.put(MediaType.class, MediaTypeMixin.class);
    sourceMixins.put(OAuthFlow.class, ExtensionsMixin.class);
    sourceMixins.put(OAuthFlows.class, ExtensionsMixin.class);
    sourceMixins.put(Operation.class, OperationMixin.class);
    sourceMixins.put(PathItem.class, ExtensionsMixin.class);
    sourceMixins.put(Paths.class, ExtensionsMixin.class);
    sourceMixins.put(Scopes.class, ExtensionsMixin.class);
    sourceMixins.put(Server.class, ExtensionsMixin.class);
    sourceMixins.put(ServerVariable.class, ExtensionsMixin.class);
    sourceMixins.put(ServerVariables.class, ExtensionsMixin.class);
    sourceMixins.put(Tag.class, ExtensionsMixin.class);
    sourceMixins.put(XML.class, ExtensionsMixin.class);
    sourceMixins.put(ApiResponse.class, ExtensionsMixin.class);
    sourceMixins.put(Parameter.class, ExtensionsMixin.class);
    sourceMixins.put(RequestBody.class, ExtensionsMixin.class);
    sourceMixins.put(Header.class, ExtensionsMixin.class);
    sourceMixins.put(SecurityScheme.class, ExtensionsMixin.class);
    sourceMixins.put(Callback.class, ExtensionsMixin.class);
    if (!openapi31) {
        sourceMixins.put(Schema.class, SchemaMixin.class);
        sourceMixins.put(DateSchema.class, DateSchemaMixin.class);
        sourceMixins.put(Components.class, ComponentsMixin.class);
        sourceMixins.put(Info.class, InfoMixin.class);
        sourceMixins.put(License.class, LicenseMixin.class);
        sourceMixins.put(OpenAPI.class, OpenAPIMixin.class);
        sourceMixins.put(Discriminator.class, DiscriminatorMixin.class);
    } else {
        sourceMixins.put(Info.class, ExtensionsMixin.class);
        sourceMixins.put(Schema.class, Schema31Mixin.class);
        sourceMixins.put(Components.class, Components31Mixin.class);
        sourceMixins.put(OpenAPI.class, OpenAPI31Mixin.class);
        sourceMixins.put(DateSchema.class, DateSchemaMixin.class);
        sourceMixins.put(Discriminator.class, Discriminator31Mixin.class);
    }
    mapper.setMixIns(sourceMixins);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    mapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper;
}
Also used : MediaTypeSerializer(io.swagger.v3.core.jackson.MediaTypeSerializer) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) Schema31Serializer(io.swagger.v3.core.jackson.Schema31Serializer) LinkedHashMap(java.util.LinkedHashMap) MediaType(io.swagger.v3.oas.models.media.MediaType) SchemaSerializer(io.swagger.v3.core.jackson.SchemaSerializer) Module(com.fasterxml.jackson.databind.Module) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier)

Example 99 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-core by swagger-api.

the class ModelConverterContextImpl method resolve.

@Override
public Schema resolve(AnnotatedType type) {
    AnnotatedType aType = OptionalUtils.unwrapOptional(type);
    if (aType != null) {
        return resolve(aType);
    }
    if (processedTypes.contains(type)) {
        return modelByType.get(type);
    } else {
        processedTypes.add(type);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("resolve %s", type.getType()));
    }
    Iterator<ModelConverter> converters = this.getConverters();
    Schema resolved = null;
    if (converters.hasNext()) {
        ModelConverter converter = converters.next();
        LOGGER.trace("trying extension {}", converter);
        resolved = converter.resolve(type, this, converters);
    }
    if (resolved != null) {
        modelByType.put(type, resolved);
        Schema resolvedImpl = resolved;
        if (resolvedImpl.getName() != null) {
            modelByName.put(resolvedImpl.getName(), resolved);
        }
    } else {
        processedTypes.remove(type);
    }
    return resolved;
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema)

Example 100 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-core by swagger-api.

the class OpenAPI3_1SerializationTest method testSerializePetstore.

@Test
public void testSerializePetstore() throws Exception {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1.yaml");
    final OpenAPI swagger = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(swagger);
    assertEquals(swagger.getInfo().getLicense().getIdentifier(), "test");
    SerializationMatchers.assertEqualsToYaml31(swagger, "openapi: 3.1.0\n" + "info:\n" + "  title: Swagger Petstore\n" + "  license:\n" + "    name: MIT\n" + "    identifier: test\n" + "  version: 1.0.0\n" + "servers:\n" + "- url: http://petstore.swagger.io/v1\n" + "paths:\n" + "  /pets:\n" + "    get:\n" + "      tags:\n" + "      - pets\n" + "      summary: List all pets\n" + "      operationId: listPets\n" + "      parameters:\n" + "      - name: limit\n" + "        in: query\n" + "        description: How many items to return at one time (max 100)\n" + "        required: false\n" + "        schema:\n" + "          type: integer\n" + "          format: int32\n" + "      responses:\n" + "        \"200\":\n" + "          description: An paged array of pets\n" + "          headers:\n" + "            x-next:\n" + "              description: A link to the next page of responses\n" + "              schema:\n" + "                type: string\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Pets'\n" + "        default:\n" + "          description: unexpected error\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Error'\n" + "    post:\n" + "      tags:\n" + "      - pets\n" + "      summary: Create a pet\n" + "      operationId: createPets\n" + "      responses:\n" + "        \"201\":\n" + "          description: Null response\n" + "        default:\n" + "          description: unexpected error\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Error'\n" + "  /pets/{petId}:\n" + "    get:\n" + "      tags:\n" + "      - pets\n" + "      summary: Info for a specific pet\n" + "      operationId: showPetById\n" + "      parameters:\n" + "      - name: petId\n" + "        in: path\n" + "        description: The id of the pet to retrieve\n" + "        required: true\n" + "        schema:\n" + "          type: string\n" + "      responses:\n" + "        \"200\":\n" + "          description: Expected response to a valid request\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Pets'\n" + "        default:\n" + "          description: unexpected error\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Error'\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      required:\n" + "      - id\n" + "      - name\n" + "      properties:\n" + "        id:\n" + "          type: integer\n" + "          format: int64\n" + "        name:\n" + "          type:\n" + "          - string\n" + "          - integer\n" + "        tag:\n" + "          type: string\n" + "    Pets:\n" + "      type: array\n" + "      items:\n" + "        $ref: '#/components/schemas/Pet'\n" + "    Error:\n" + "      required:\n" + "      - code\n" + "      - message\n" + "      properties:\n" + "        code:\n" + "          type: integer\n" + "          format: int32\n" + "        message:\n" + "          type: string\n" + "webhooks:\n" + "  newPet:\n" + "    post:\n" + "      requestBody:\n" + "        description: Information about a new pet in the system\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/Pet'\n" + "      responses:\n" + "        \"200\":\n" + "          description: Return a 200 status to indicate that the data was received\n" + "            successfully");
    SerializationMatchers.assertEqualsToJson31(swagger, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"info\" : {\n" + "    \"title\" : \"Swagger Petstore\",\n" + "    \"license\" : {\n" + "      \"name\" : \"MIT\",\n" + "      \"identifier\" : \"test\"\n" + "    },\n" + "    \"version\" : \"1.0.0\"\n" + "  },\n" + "  \"servers\" : [ {\n" + "    \"url\" : \"http://petstore.swagger.io/v1\"\n" + "  } ],\n" + "  \"paths\" : {\n" + "    \"/pets\" : {\n" + "      \"get\" : {\n" + "        \"tags\" : [ \"pets\" ],\n" + "        \"summary\" : \"List all pets\",\n" + "        \"operationId\" : \"listPets\",\n" + "        \"parameters\" : [ {\n" + "          \"name\" : \"limit\",\n" + "          \"in\" : \"query\",\n" + "          \"description\" : \"How many items to return at one time (max 100)\",\n" + "          \"required\" : false,\n" + "          \"schema\" : {\n" + "            \"type\" : \"integer\",\n" + "            \"format\" : \"int32\"\n" + "          }\n" + "        } ],\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"An paged array of pets\",\n" + "            \"headers\" : {\n" + "              \"x-next\" : {\n" + "                \"description\" : \"A link to the next page of responses\",\n" + "                \"schema\" : {\n" + "                  \"type\" : \"string\"\n" + "                }\n" + "              }\n" + "            },\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Pets\"\n" + "                }\n" + "              }\n" + "            }\n" + "          },\n" + "          \"default\" : {\n" + "            \"description\" : \"unexpected error\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Error\"\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      },\n" + "      \"post\" : {\n" + "        \"tags\" : [ \"pets\" ],\n" + "        \"summary\" : \"Create a pet\",\n" + "        \"operationId\" : \"createPets\",\n" + "        \"responses\" : {\n" + "          \"201\" : {\n" + "            \"description\" : \"Null response\"\n" + "          },\n" + "          \"default\" : {\n" + "            \"description\" : \"unexpected error\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Error\"\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"/pets/{petId}\" : {\n" + "      \"get\" : {\n" + "        \"tags\" : [ \"pets\" ],\n" + "        \"summary\" : \"Info for a specific pet\",\n" + "        \"operationId\" : \"showPetById\",\n" + "        \"parameters\" : [ {\n" + "          \"name\" : \"petId\",\n" + "          \"in\" : \"path\",\n" + "          \"description\" : \"The id of the pet to retrieve\",\n" + "          \"required\" : true,\n" + "          \"schema\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        } ],\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"Expected response to a valid request\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Pets\"\n" + "                }\n" + "              }\n" + "            }\n" + "          },\n" + "          \"default\" : {\n" + "            \"description\" : \"unexpected error\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Error\"\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"Pet\" : {\n" + "        \"required\" : [ \"id\", \"name\" ],\n" + "        \"properties\" : {\n" + "          \"id\" : {\n" + "            \"type\" : \"integer\",\n" + "            \"format\" : \"int64\"\n" + "          },\n" + "          \"name\" : {\n" + "            \"type\" : [\"string\", \"integer\"]\n" + "          },\n" + "          \"tag\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        }\n" + "      },\n" + "      \"Pets\" : {\n" + "        \"type\" : \"array\",\n" + "        \"items\" : {\n" + "          \"$ref\" : \"#/components/schemas/Pet\"\n" + "        }\n" + "      },\n" + "      \"Error\" : {\n" + "        \"required\" : [ \"code\", \"message\" ],\n" + "        \"properties\" : {\n" + "          \"code\" : {\n" + "            \"type\" : \"integer\",\n" + "            \"format\" : \"int32\"\n" + "          },\n" + "          \"message\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"webhooks\" : {\n" + "    \"newPet\" : {\n" + "      \"post\" : {\n" + "        \"requestBody\" : {\n" + "          \"description\" : \"Information about a new pet in the system\",\n" + "          \"content\" : {\n" + "            \"application/json\" : {\n" + "              \"schema\" : {\n" + "                \"$ref\" : \"#/components/schemas/Pet\"\n" + "              }\n" + "            }\n" + "          }\n" + "        },\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"Return a 200 status to indicate that the data was received successfully\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)257 Schema (io.swagger.v3.oas.models.media.Schema)234 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)141 StringSchema (io.swagger.v3.oas.models.media.StringSchema)126 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)112 OpenAPI (io.swagger.v3.oas.models.OpenAPI)68 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)65 MapSchema (io.swagger.v3.oas.models.media.MapSchema)62 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)60 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)58 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)51 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)47 Operation (io.swagger.v3.oas.annotations.Operation)44 DateSchema (io.swagger.v3.oas.models.media.DateSchema)44 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)42 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)42 Parameter (io.swagger.v3.oas.models.parameters.Parameter)33 BooleanSchema (io.swagger.v3.oas.models.media.BooleanSchema)32 ModelConverterContextImpl (io.swagger.v3.core.converter.ModelConverterContextImpl)25 Components (io.swagger.v3.oas.models.Components)23