Search in sources :

Example 41 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 getTags.

public static Optional<Set<Tag>> getTags(io.swagger.v3.oas.annotations.tags.Tag[] tags, boolean skipOnlyName) {
    if (tags == null) {
        return Optional.empty();
    }
    Set<Tag> tagsList = new LinkedHashSet<>();
    for (io.swagger.v3.oas.annotations.tags.Tag tag : tags) {
        if (StringUtils.isBlank(tag.name())) {
            continue;
        }
        if (skipOnlyName && StringUtils.isBlank(tag.description()) && StringUtils.isBlank(tag.externalDocs().description()) && StringUtils.isBlank(tag.externalDocs().url())) {
            continue;
        }
        Tag tagObject = new Tag();
        if (StringUtils.isNotBlank(tag.description())) {
            tagObject.setDescription(tag.description());
        }
        tagObject.setName(tag.name());
        getExternalDocumentation(tag.externalDocs()).ifPresent(tagObject::setExternalDocs);
        if (tag.extensions().length > 0) {
            Map<String, Object> extensions = AnnotationsUtils.getExtensions(tag.extensions());
            if (extensions != null) {
                extensions.forEach(tagObject::addExtension);
            }
        }
        tagsList.add(tagObject);
    }
    if (tagsList.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(tagsList);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) Tag(io.swagger.v3.oas.models.tags.Tag)

Example 42 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 getLicense.

public static Optional<License> getLicense(io.swagger.v3.oas.annotations.info.License license) {
    if (license == null) {
        return Optional.empty();
    }
    License licenseObject = new License();
    boolean isEmpty = true;
    if (StringUtils.isNotBlank(license.name())) {
        licenseObject.setName(license.name());
        isEmpty = false;
    }
    if (StringUtils.isNotBlank(license.url())) {
        licenseObject.setUrl(license.url());
        isEmpty = false;
    }
    if (license.extensions() != null && license.extensions().length > 0) {
        Map<String, Object> extensions = AnnotationsUtils.getExtensions(license.extensions());
        if (extensions != null) {
            extensions.forEach(licenseObject::addExtension);
            isEmpty = false;
        }
    }
    if (isEmpty) {
        return Optional.empty();
    }
    return Optional.of(licenseObject);
}
Also used : License(io.swagger.v3.oas.models.info.License) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject)

Example 43 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 getHeader.

public static Optional<Header> getHeader(io.swagger.v3.oas.annotations.headers.Header header, JsonView jsonViewAnnotation) {
    if (header == null) {
        return Optional.empty();
    }
    Header headerObject = new Header();
    boolean isEmpty = !StringUtils.isNotBlank(header.name());
    if (StringUtils.isNotBlank(header.description())) {
        headerObject.setDescription(header.description());
        isEmpty = false;
    }
    if (StringUtils.isNotBlank(header.ref())) {
        headerObject.set$ref(header.ref());
        isEmpty = false;
    }
    if (header.deprecated()) {
        headerObject.setDeprecated(header.deprecated());
    }
    if (header.required()) {
        headerObject.setRequired(header.required());
        isEmpty = false;
    }
    headerObject.setStyle(Header.StyleEnum.SIMPLE);
    if (header.schema() != null) {
        if (header.schema().implementation().equals(Void.class)) {
            AnnotationsUtils.getSchemaFromAnnotation(header.schema(), jsonViewAnnotation).ifPresent(headerObject::setSchema);
        }
    }
    if (isEmpty) {
        return Optional.empty();
    }
    return Optional.of(headerObject);
}
Also used : Header(io.swagger.v3.oas.models.headers.Header)

Example 44 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 getContent.

public static Optional<Content> getContent(io.swagger.v3.oas.annotations.media.Content[] annotationContents, String[] classTypes, String[] methodTypes, Schema schema, Components components, JsonView jsonViewAnnotation) {
    if (annotationContents == null || annotationContents.length == 0) {
        return Optional.empty();
    }
    // Encapsulating Content model
    Content content = new Content();
    for (io.swagger.v3.oas.annotations.media.Content annotationContent : annotationContents) {
        MediaType mediaType = new MediaType();
        if (components != null) {
            getSchema(annotationContent, components, jsonViewAnnotation).ifPresent(mediaType::setSchema);
            if (annotationContent.schemaProperties().length > 0) {
                if (mediaType.getSchema() == null) {
                    mediaType.schema(new Schema<Object>().type("object"));
                }
                Schema oSchema = mediaType.getSchema();
                for (SchemaProperty sp : annotationContent.schemaProperties()) {
                    Class<?> schemaImplementation = sp.schema().implementation();
                    boolean isArray = false;
                    if (schemaImplementation == Void.class) {
                        schemaImplementation = sp.array().schema().implementation();
                        if (schemaImplementation != Void.class) {
                            isArray = true;
                        }
                    }
                    getSchema(sp.schema(), sp.array(), isArray, schemaImplementation, components, jsonViewAnnotation).ifPresent(s -> {
                        if ("array".equals(oSchema.getType())) {
                            oSchema.getItems().addProperty(sp.name(), s);
                        } else {
                            oSchema.addProperty(sp.name(), s);
                        }
                    });
                }
            }
            if (hasSchemaAnnotation(annotationContent.additionalPropertiesSchema()) && mediaType.getSchema() != null && !Boolean.TRUE.equals(mediaType.getSchema().getAdditionalProperties()) && !Boolean.FALSE.equals(mediaType.getSchema().getAdditionalProperties())) {
                getSchemaFromAnnotation(annotationContent.additionalPropertiesSchema(), components, jsonViewAnnotation).ifPresent(s -> {
                    if ("array".equals(mediaType.getSchema().getType())) {
                        mediaType.getSchema().getItems().additionalProperties(s);
                    } else {
                        mediaType.getSchema().additionalProperties(s);
                    }
                });
            }
        } else {
            mediaType.setSchema(schema);
        }
        ExampleObject[] examples = annotationContent.examples();
        if (examples.length == 1 && StringUtils.isBlank(examples[0].name())) {
            getExample(examples[0], true).ifPresent(exampleObject -> mediaType.example(exampleObject.getValue()));
        } else {
            for (ExampleObject example : examples) {
                getExample(example).ifPresent(exampleObject -> mediaType.addExamples(example.name(), exampleObject));
            }
        }
        if (annotationContent.extensions() != null && annotationContent.extensions().length > 0) {
            Map<String, Object> extensions = AnnotationsUtils.getExtensions(annotationContent.extensions());
            if (extensions != null) {
                extensions.forEach(mediaType::addExtension);
            }
        }
        io.swagger.v3.oas.annotations.media.Encoding[] encodings = annotationContent.encoding();
        for (io.swagger.v3.oas.annotations.media.Encoding encoding : encodings) {
            addEncodingToMediaType(mediaType, encoding, jsonViewAnnotation);
        }
        if (StringUtils.isNotBlank(annotationContent.mediaType())) {
            content.addMediaType(annotationContent.mediaType(), mediaType);
        } else {
            applyTypes(classTypes, methodTypes, content, mediaType);
        }
    }
    if (content.size() == 0) {
        return Optional.empty();
    }
    return Optional.of(content);
}
Also used : SchemaProperty(io.swagger.v3.oas.annotations.media.SchemaProperty) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) 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) Encoding(io.swagger.v3.oas.models.media.Encoding) Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject)

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

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

the class ParameterProcessor method getParamSchemaAnnotation.

public static Annotation getParamSchemaAnnotation(List<Annotation> annotations) {
    if (annotations == null) {
        return null;
    }
    io.swagger.v3.oas.annotations.media.Schema rootSchema = null;
    io.swagger.v3.oas.annotations.media.ArraySchema rootArraySchema = null;
    io.swagger.v3.oas.annotations.media.Schema contentSchema = null;
    io.swagger.v3.oas.annotations.media.Schema paramSchema = null;
    io.swagger.v3.oas.annotations.media.ArraySchema paramArraySchema = null;
    for (Annotation annotation : annotations) {
        if (annotation instanceof io.swagger.v3.oas.annotations.media.Schema) {
            rootSchema = (io.swagger.v3.oas.annotations.media.Schema) annotation;
        } else if (annotation instanceof io.swagger.v3.oas.annotations.media.ArraySchema) {
            rootArraySchema = (io.swagger.v3.oas.annotations.media.ArraySchema) annotation;
        } else if (annotation instanceof io.swagger.v3.oas.annotations.Parameter) {
            io.swagger.v3.oas.annotations.Parameter paramAnnotation = (io.swagger.v3.oas.annotations.Parameter) annotation;
            if (paramAnnotation.content().length > 0) {
                if (AnnotationsUtils.hasSchemaAnnotation(paramAnnotation.content()[0].schema())) {
                    contentSchema = paramAnnotation.content()[0].schema();
                }
            }
            if (AnnotationsUtils.hasSchemaAnnotation(paramAnnotation.schema())) {
                paramSchema = paramAnnotation.schema();
            }
            if (AnnotationsUtils.hasArrayAnnotation(paramAnnotation.array())) {
                paramArraySchema = paramAnnotation.array();
            }
        }
    }
    if (rootSchema != null || rootArraySchema != null) {
        return null;
    }
    if (contentSchema != null) {
        return contentSchema;
    }
    if (paramSchema != null) {
        return paramSchema;
    }
    if (paramArraySchema != null) {
        return paramArraySchema;
    }
    return null;
}
Also used : ResolvedSchema(io.swagger.v3.core.converter.ResolvedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) Annotation(java.lang.annotation.Annotation) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Parameter(io.swagger.v3.oas.models.parameters.Parameter)

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