Search in sources :

Example 1 with ExampleObject

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

the class ParameterProcessor method applyAnnotations.

public static Parameter applyAnnotations(Parameter parameter, Type type, List<Annotation> annotations, Components components, String[] classTypes, String[] methodTypes, JsonView jsonViewAnnotation) {
    final AnnotationsHelper helper = new AnnotationsHelper(annotations, type);
    if (helper.isContext()) {
        return null;
    }
    if (parameter == null) {
        // consider it to be body param
        parameter = new Parameter();
    }
    // first handle schema
    List<Annotation> reworkedAnnotations = new ArrayList<>(annotations);
    Annotation paramSchemaOrArrayAnnotation = getParamSchemaAnnotation(annotations);
    if (paramSchemaOrArrayAnnotation != null) {
        reworkedAnnotations.add(paramSchemaOrArrayAnnotation);
    }
    AnnotatedType annotatedType = new AnnotatedType().type(type).resolveAsRef(true).skipOverride(true).jsonViewAnnotation(jsonViewAnnotation).ctxAnnotations(reworkedAnnotations.toArray(new Annotation[reworkedAnnotations.size()]));
    ResolvedSchema resolvedSchema = ModelConverters.getInstance().resolveAsResolvedSchema(annotatedType);
    if (resolvedSchema.schema != null) {
        parameter.setSchema(resolvedSchema.schema);
    }
    resolvedSchema.referencedSchemas.forEach(components::addSchemas);
    // handle first FormParam as it affects Explode resolving
    for (Annotation annotation : annotations) {
        if (annotation.annotationType().getName().equals("javax.ws.rs.FormParam")) {
            try {
                String name = (String) annotation.annotationType().getMethod("value").invoke(annotation);
                if (StringUtils.isNotBlank(name)) {
                    parameter.setName(name);
                }
            } catch (Exception e) {
            }
            // set temporarily to "form" to inform caller that we need to further process along other form parameters
            parameter.setIn("form");
        } else if (annotation.annotationType().getName().endsWith("FormDataParam")) {
            try {
                String name = (String) annotation.annotationType().getMethod("value").invoke(annotation);
                if (StringUtils.isNotBlank(name)) {
                    parameter.setName(name);
                }
            } catch (Exception e) {
            }
            // set temporarily to "form" to inform caller that we need to further process along other form parameters
            parameter.setIn("form");
        }
    }
    for (Annotation annotation : annotations) {
        if (annotation instanceof io.swagger.v3.oas.annotations.Parameter) {
            io.swagger.v3.oas.annotations.Parameter p = (io.swagger.v3.oas.annotations.Parameter) annotation;
            if (p.hidden()) {
                return null;
            }
            if (StringUtils.isNotBlank(p.ref())) {
                parameter = new Parameter().$ref(p.ref());
                return parameter;
            }
            if (StringUtils.isNotBlank(p.description())) {
                parameter.setDescription(p.description());
            }
            if (StringUtils.isNotBlank(p.name())) {
                parameter.setName(p.name());
            }
            if (StringUtils.isNotBlank(p.in().toString())) {
                parameter.setIn(p.in().toString());
            }
            if (StringUtils.isNotBlank(p.example())) {
                try {
                    parameter.setExample(Json.mapper().readTree(p.example()));
                } catch (IOException e) {
                    parameter.setExample(p.example());
                }
            }
            if (p.deprecated()) {
                parameter.setDeprecated(p.deprecated());
            }
            if (p.required()) {
                parameter.setRequired(p.required());
            }
            if (p.allowEmptyValue()) {
                parameter.setAllowEmptyValue(p.allowEmptyValue());
            }
            if (p.allowReserved()) {
                parameter.setAllowReserved(p.allowReserved());
            }
            Map<String, Example> exampleMap = new LinkedHashMap<>();
            if (p.examples().length == 1 && StringUtils.isBlank(p.examples()[0].name())) {
                Optional<Example> exampleOptional = AnnotationsUtils.getExample(p.examples()[0], true);
                if (exampleOptional.isPresent()) {
                    parameter.setExample(exampleOptional.get());
                }
            } else {
                for (ExampleObject exampleObject : p.examples()) {
                    AnnotationsUtils.getExample(exampleObject).ifPresent(example -> exampleMap.put(exampleObject.name(), example));
                }
            }
            if (exampleMap.size() > 0) {
                parameter.setExamples(exampleMap);
            }
            if (p.extensions().length > 0) {
                Map<String, Object> extensionMap = AnnotationsUtils.getExtensions(p.extensions());
                if (extensionMap != null && !extensionMap.isEmpty()) {
                    extensionMap.forEach(parameter::addExtension);
                }
            }
            Optional<Content> content = AnnotationsUtils.getContent(p.content(), classTypes, methodTypes, parameter.getSchema(), null, jsonViewAnnotation);
            if (content.isPresent()) {
                parameter.setContent(content.get());
                parameter.setSchema(null);
            }
            setParameterStyle(parameter, p);
            setParameterExplode(parameter, p);
        } else if (annotation.annotationType().getName().equals("javax.ws.rs.PathParam")) {
            try {
                String name = (String) annotation.annotationType().getMethod("value").invoke(annotation);
                if (StringUtils.isNotBlank(name)) {
                    parameter.setName(name);
                }
            } catch (Exception e) {
            }
        } else if (annotation.annotationType().getName().equals("javax.validation.constraints.Size")) {
            try {
                if (parameter.getSchema() == null) {
                    parameter.setSchema(new ArraySchema());
                }
                if (parameter.getSchema() instanceof ArraySchema) {
                    ArraySchema as = (ArraySchema) parameter.getSchema();
                    Integer min = (Integer) annotation.annotationType().getMethod("min").invoke(annotation);
                    if (min != null) {
                        as.setMinItems(min);
                    }
                    Integer max = (Integer) annotation.annotationType().getMethod("max").invoke(annotation);
                    if (max != null) {
                        as.setMaxItems(max);
                    }
                }
            } catch (Exception e) {
                LOGGER.error("failed on " + annotation.annotationType().getName(), e);
            }
        } else if (ModelResolver.NOT_NULL_ANNOTATIONS.contains(annotation.annotationType().getSimpleName())) {
            parameter.setRequired(true);
        }
    }
    final String defaultValue = helper.getDefaultValue();
    Schema paramSchema = parameter.getSchema();
    if (paramSchema == null) {
        if (parameter.getContent() != null && !parameter.getContent().values().isEmpty()) {
            paramSchema = parameter.getContent().values().iterator().next().getSchema();
        }
    }
    if (paramSchema != null) {
        if (paramSchema instanceof ArraySchema) {
            ArraySchema as = (ArraySchema) paramSchema;
            if (defaultValue != null) {
                as.getItems().setDefault(defaultValue);
            }
        } else {
            if (defaultValue != null) {
                paramSchema.setDefault(defaultValue);
            }
        }
    }
    return parameter;
}
Also used : ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) ResolvedSchema(io.swagger.v3.core.converter.ResolvedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Example(io.swagger.v3.oas.models.examples.Example) ResolvedSchema(io.swagger.v3.core.converter.ResolvedSchema) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) IOException(java.io.IOException) AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject)

Example 2 with ExampleObject

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

the class AnnotationsUtils method resolveExample.

private static boolean resolveExample(Example exampleObject, ExampleObject example) {
    boolean isEmpty = true;
    if (StringUtils.isNotBlank(example.summary())) {
        isEmpty = false;
        exampleObject.setSummary(example.summary());
    }
    if (StringUtils.isNotBlank(example.description())) {
        isEmpty = false;
        exampleObject.setDescription(example.description());
    }
    if (StringUtils.isNotBlank(example.externalValue())) {
        isEmpty = false;
        exampleObject.setExternalValue(example.externalValue());
    }
    if (StringUtils.isNotBlank(example.value())) {
        isEmpty = false;
        try {
            ObjectMapper mapper = ObjectMapperFactory.buildStrictGenericObjectMapper();
            exampleObject.setValue(mapper.readTree(example.value()));
        } catch (IOException e) {
            exampleObject.setValue(example.value());
        }
    }
    if (StringUtils.isNotBlank(example.ref())) {
        isEmpty = false;
        exampleObject.set$ref(example.ref());
    }
    if (example.extensions().length > 0) {
        isEmpty = false;
        Map<String, Object> extensions = AnnotationsUtils.getExtensions(example.extensions());
        if (extensions != null) {
            extensions.forEach(exampleObject::addExtension);
        }
    }
    return !isEmpty;
}
Also used : ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ExampleObject

use of io.swagger.v3.oas.annotations.media.ExampleObject 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)

Aggregations

ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)3 ResolvedSchema (io.swagger.v3.core.converter.ResolvedSchema)2 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)2 Content (io.swagger.v3.oas.models.media.Content)2 Schema (io.swagger.v3.oas.models.media.Schema)2 IOException (java.io.IOException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)1 SchemaProperty (io.swagger.v3.oas.annotations.media.SchemaProperty)1 Example (io.swagger.v3.oas.models.examples.Example)1 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)1 Encoding (io.swagger.v3.oas.models.media.Encoding)1 MediaType (io.swagger.v3.oas.models.media.MediaType)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1