Search in sources :

Example 6 with io.swagger.v3.oas.models.media

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

the class AnnotationsUtils method getSchemaFromAnnotation.

public static Optional<Schema> getSchemaFromAnnotation(io.swagger.v3.oas.annotations.media.Schema schema, Components components, JsonView jsonViewAnnotation) {
    if (schema == null || !hasSchemaAnnotation(schema)) {
        return Optional.empty();
    }
    Schema schemaObject = null;
    if (schema.oneOf().length > 0 || schema.allOf().length > 0 || schema.anyOf().length > 0) {
        schemaObject = new ComposedSchema();
    } else {
        schemaObject = new Schema();
    }
    if (StringUtils.isNotBlank(schema.description())) {
        schemaObject.setDescription(schema.description());
    }
    if (StringUtils.isNotBlank(schema.ref())) {
        schemaObject.set$ref(schema.ref());
    }
    if (StringUtils.isNotBlank(schema.type())) {
        schemaObject.setType(schema.type());
    }
    if (StringUtils.isNotBlank(schema.defaultValue())) {
        schemaObject.setDefault(schema.defaultValue());
    }
    if (StringUtils.isNotBlank(schema.example())) {
        try {
            schemaObject.setExample(Json.mapper().readTree(schema.example()));
        } catch (IOException e) {
            schemaObject.setExample(schema.example());
        }
    }
    if (StringUtils.isNotBlank(schema.format())) {
        schemaObject.setFormat(schema.format());
    }
    if (StringUtils.isNotBlank(schema.pattern())) {
        schemaObject.setPattern(schema.pattern());
    }
    if (schema.readOnly()) {
        schemaObject.setReadOnly(schema.readOnly());
    }
    if (schema.deprecated()) {
        schemaObject.setDeprecated(schema.deprecated());
    }
    if (schema.exclusiveMaximum()) {
        schemaObject.setExclusiveMaximum(schema.exclusiveMaximum());
    }
    if (schema.exclusiveMinimum()) {
        schemaObject.setExclusiveMinimum(schema.exclusiveMinimum());
    }
    if (schema.maxProperties() > 0) {
        schemaObject.setMaxProperties(schema.maxProperties());
    }
    if (schema.maxLength() != Integer.MAX_VALUE && schema.maxLength() > 0) {
        schemaObject.setMaxLength(schema.maxLength());
    }
    if (schema.minProperties() > 0) {
        schemaObject.setMinProperties(schema.minProperties());
    }
    if (schema.minLength() > 0) {
        schemaObject.setMinLength(schema.minLength());
    }
    if (schema.multipleOf() != 0) {
        schemaObject.setMultipleOf(BigDecimal.valueOf(schema.multipleOf()));
    }
    if (NumberUtils.isCreatable(schema.maximum())) {
        String filteredMaximum = schema.maximum().replace(Constants.COMMA, StringUtils.EMPTY);
        schemaObject.setMaximum(new BigDecimal(filteredMaximum));
    }
    if (NumberUtils.isCreatable(schema.minimum())) {
        String filteredMinimum = schema.minimum().replace(Constants.COMMA, StringUtils.EMPTY);
        schemaObject.setMinimum(new BigDecimal(filteredMinimum));
    }
    if (schema.nullable()) {
        schemaObject.setNullable(schema.nullable());
    }
    if (StringUtils.isNotBlank(schema.title())) {
        schemaObject.setTitle(schema.title());
    }
    if (schema.writeOnly()) {
        schemaObject.setWriteOnly(schema.writeOnly());
    }
    // process after readOnly and writeOnly
    if (schema.accessMode().equals(io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY)) {
        schemaObject.setReadOnly(true);
        schemaObject.setWriteOnly(null);
    } else if (schema.accessMode().equals(io.swagger.v3.oas.annotations.media.Schema.AccessMode.WRITE_ONLY)) {
        schemaObject.setReadOnly(false);
        schemaObject.setWriteOnly(null);
    } else if (schema.accessMode().equals(io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE)) {
        schemaObject.setReadOnly(null);
        schemaObject.setWriteOnly(null);
    }
    if (schema.requiredProperties().length > 0) {
        schemaObject.setRequired(Arrays.asList(schema.requiredProperties()));
    }
    if (schema.allowableValues().length > 0) {
        schemaObject.setEnum(Arrays.asList(schema.allowableValues()));
    }
    if (schema.extensions().length > 0) {
        Map<String, Object> extensions = AnnotationsUtils.getExtensions(schema.extensions());
        if (extensions != null) {
            extensions.forEach(schemaObject::addExtension);
        }
    }
    getExternalDocumentation(schema.externalDocs()).ifPresent(schemaObject::setExternalDocs);
    if (!schema.not().equals(Void.class)) {
        Class<?> schemaImplementation = schema.not();
        Schema notSchemaObject = resolveSchemaFromType(schemaImplementation, components, jsonViewAnnotation);
        schemaObject.setNot(notSchemaObject);
    }
    if (schema.oneOf().length > 0) {
        Class<?>[] schemaImplementations = schema.oneOf();
        for (Class<?> schemaImplementation : schemaImplementations) {
            Schema oneOfSchemaObject = resolveSchemaFromType(schemaImplementation, components, jsonViewAnnotation);
            ((ComposedSchema) schemaObject).addOneOfItem(oneOfSchemaObject);
        }
    }
    if (schema.anyOf().length > 0) {
        Class<?>[] schemaImplementations = schema.anyOf();
        for (Class<?> schemaImplementation : schemaImplementations) {
            Schema anyOfSchemaObject = resolveSchemaFromType(schemaImplementation, components, jsonViewAnnotation);
            ((ComposedSchema) schemaObject).addAnyOfItem(anyOfSchemaObject);
        }
    }
    if (schema.allOf().length > 0) {
        Class<?>[] schemaImplementations = schema.allOf();
        for (Class<?> schemaImplementation : schemaImplementations) {
            Schema allOfSchemaObject = resolveSchemaFromType(schemaImplementation, components, jsonViewAnnotation);
            ((ComposedSchema) schemaObject).addAllOfItem(allOfSchemaObject);
        }
    }
    if (schema.additionalProperties().equals(io.swagger.v3.oas.annotations.media.Schema.AdditionalPropertiesValue.TRUE)) {
        schemaObject.additionalProperties(true);
    } else if (schema.additionalProperties().equals(io.swagger.v3.oas.annotations.media.Schema.AdditionalPropertiesValue.FALSE)) {
        schemaObject.additionalProperties(false);
    }
    return Optional.of(schemaObject);
}
Also used : ResolvedSchema(io.swagger.v3.core.converter.ResolvedSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) IOException(java.io.IOException) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) BigDecimal(java.math.BigDecimal)

Example 7 with io.swagger.v3.oas.models.media

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

the class CustomResolverTest method testCustomConverter.

@Test(description = "it should ignore properties with type Bar")
public void testCustomConverter() {
    // add the custom converter
    final ModelConverters converters = new ModelConverters();
    converters.addConverter(new CustomConverter(Json.mapper()));
    Map<String, Schema> models = converters.readAll(Foo.class);
    Schema model = models.get("io.swagger.v3.core.converting.override.CustomResolverTest$Foo");
    assertNotNull(model);
    assertEquals(model.getProperties().size(), 2);
    final Schema barProperty = (Schema) model.getProperties().get("bar");
    assertEquals(barProperty.get$ref(), "#/components/schemas/io.swagger.v3.core.converting.override.CustomResolverTest$Bar");
    final Schema titleProperty = (Schema) model.getProperties().get("title");
    assertNotNull(titleProperty);
    model = models.get("io.swagger.v3.core.converting.override.CustomResolverTest$Bar");
    assertNotNull(model);
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) ModelConverters(io.swagger.v3.core.converter.ModelConverters) Test(org.testng.annotations.Test)

Example 8 with io.swagger.v3.oas.models.media

use of io.swagger.v3.oas.models.media 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 9 with io.swagger.v3.oas.models.media

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

the class ModelResolver method resolveExternalDocumentation.

protected ExternalDocumentation resolveExternalDocumentation(Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schema) {
    ExternalDocumentation external = null;
    if (a != null) {
        io.swagger.v3.oas.annotations.ExternalDocumentation externalDocumentation = a.getAnnotation(io.swagger.v3.oas.annotations.ExternalDocumentation.class);
        external = resolveExternalDocumentation(externalDocumentation);
    }
    if (external == null) {
        if (schema != null) {
            external = resolveExternalDocumentation(schema.externalDocs());
        }
    }
    return external;
}
Also used : ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation)

Example 10 with io.swagger.v3.oas.models.media

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

the class AnnotationsUtils method addEncodingToMediaType.

public static void addEncodingToMediaType(MediaType mediaType, io.swagger.v3.oas.annotations.media.Encoding encoding, JsonView jsonViewAnnotation) {
    if (encoding == null) {
        return;
    }
    if (StringUtils.isNotBlank(encoding.name())) {
        Encoding encodingObject = new Encoding();
        if (StringUtils.isNotBlank(encoding.contentType())) {
            encodingObject.setContentType(encoding.contentType());
        }
        if (StringUtils.isNotBlank(encoding.style())) {
            encodingObject.setStyle(Encoding.StyleEnum.valueOf(encoding.style()));
        }
        if (encoding.explode()) {
            encodingObject.setExplode(encoding.explode());
        }
        if (encoding.allowReserved()) {
            encodingObject.setAllowReserved(encoding.allowReserved());
        }
        if (encoding.headers() != null) {
            getHeaders(encoding.headers(), jsonViewAnnotation).ifPresent(encodingObject::headers);
        }
        if (encoding.extensions() != null && encoding.extensions().length > 0) {
            Map<String, Object> extensions = AnnotationsUtils.getExtensions(encoding.extensions());
            if (extensions != null) {
                extensions.forEach(encodingObject::addExtension);
            }
        }
        mediaType.addEncoding(encoding.name(), encodingObject);
    }
}
Also used : Encoding(io.swagger.v3.oas.models.media.Encoding) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject)

Aggregations

Schema (io.swagger.v3.oas.models.media.Schema)13 ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)11 OpenAPI (io.swagger.v3.oas.models.OpenAPI)10 Annotation (java.lang.annotation.Annotation)8 Test (org.testng.annotations.Test)8 ResolvedSchema (io.swagger.v3.core.converter.ResolvedSchema)7 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)7 Content (io.swagger.v3.oas.models.media.Content)7 ArrayList (java.util.ArrayList)7 PathItem (io.swagger.v3.oas.models.PathItem)6 Parameter (io.swagger.v3.oas.models.parameters.Parameter)6 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)5 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)5 Hidden (io.swagger.v3.oas.annotations.Hidden)5 MediaType (io.swagger.v3.oas.models.media.MediaType)5 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)5 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)5 Map (java.util.Map)5 JavaType (com.fasterxml.jackson.databind.JavaType)4 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)4