io.swagger.v3.core.util.AnnotationsUtils Maven / Gradle / Ivy
package io.swagger.v3.core.util;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.Annotated;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.converter.ResolvedSchema;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.links.LinkParameter;
import io.swagger.v3.oas.annotations.media.DiscriminatorMapping;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.links.Link;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.Encoding;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;
import io.swagger.v3.oas.models.servers.ServerVariables;
import io.swagger.v3.oas.models.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
public abstract class AnnotationsUtils {
private static Logger LOGGER = LoggerFactory.getLogger(AnnotationsUtils.class);
public static final String COMPONENTS_REF = "#/components/schemas/";
public static boolean hasSchemaAnnotation(io.swagger.v3.oas.annotations.media.Schema schema) {
if (schema == null) {
return false;
}
if (StringUtils.isBlank(schema.type())
&& StringUtils.isBlank(schema.format())
&& StringUtils.isBlank(schema.title())
&& StringUtils.isBlank(schema.description())
&& StringUtils.isBlank(schema.ref())
&& StringUtils.isBlank(schema.name())
&& schema.multipleOf() == 0
&& StringUtils.isBlank(schema.maximum())
&& StringUtils.isBlank(schema.minimum())
&& !schema.exclusiveMinimum()
&& !schema.exclusiveMaximum()
&& schema.maxLength() == Integer.MAX_VALUE
&& schema.minLength() == 0
&& schema.minProperties() == 0
&& schema.maxProperties() == 0
&& schema.requiredProperties().length == 0
&& !schema.required()
&& !schema.nullable()
&& !schema.readOnly()
&& !schema.writeOnly()
&& schema.accessMode().equals(io.swagger.v3.oas.annotations.media.Schema.AccessMode.AUTO)
&& !schema.deprecated()
&& schema.allowableValues().length == 0
&& StringUtils.isBlank(schema.defaultValue())
&& schema.implementation().equals(Void.class)
&& StringUtils.isBlank(schema.example())
&& StringUtils.isBlank(schema.pattern())
&& schema.not().equals(Void.class)
&& schema.allOf().length == 0
&& schema.oneOf().length == 0
&& schema.anyOf().length == 0
&& schema.subTypes().length == 0
&& !getExternalDocumentation(schema.externalDocs()).isPresent()
&& StringUtils.isBlank(schema.discriminatorProperty())
&& schema.discriminatorMapping().length == 0
&& schema.extensions().length == 0
&& !schema.hidden()
&& !schema.enumAsRef()
) {
return false;
}
return true;
}
public static boolean equals(Annotation thisAnnotation, Annotation thatAnnotation) {
if (thisAnnotation == null && thatAnnotation == null) {
return true;
}
else if (thisAnnotation == null || thatAnnotation == null) {
return false;
}
if (!thisAnnotation.annotationType().equals(thatAnnotation.annotationType())) {
return false;
}
if (thisAnnotation instanceof io.swagger.v3.oas.annotations.media.Schema) {
return equals((io.swagger.v3.oas.annotations.media.Schema) thisAnnotation, (io.swagger.v3.oas.annotations.media.Schema) thatAnnotation);
} else if (thisAnnotation instanceof io.swagger.v3.oas.annotations.media.ArraySchema) {
return equals((io.swagger.v3.oas.annotations.media.ArraySchema)thisAnnotation, (io.swagger.v3.oas.annotations.media.ArraySchema)thatAnnotation);
}
return true;
}
public static boolean equals(io.swagger.v3.oas.annotations.media.ArraySchema thisArraySchema, io.swagger.v3.oas.annotations.media.ArraySchema thatArraySchema) {
if (thisArraySchema == null && thatArraySchema == null) {
return true;
}
else if (thisArraySchema == null || thatArraySchema == null) {
return false;
}
if (thisArraySchema.maxItems() != thatArraySchema.maxItems()) {
return false;
}
if (thisArraySchema.minItems() != thatArraySchema.minItems()) {
return false;
}
if (thisArraySchema.uniqueItems() != thatArraySchema.uniqueItems()) {
return false;
}
if (!Arrays.equals(thisArraySchema.extensions(), thatArraySchema.extensions())) {
return false;
}
if (!equals(thisArraySchema.schema(), thatArraySchema.schema())) {
return false;
}
return true;
}
public static boolean equals(io.swagger.v3.oas.annotations.media.Schema thisSchema, io.swagger.v3.oas.annotations.media.Schema thatSchema) {
if (thisSchema == null && thatSchema == null) {
return true;
}
else if (thisSchema == null || thatSchema == null) {
return false;
}
if (!StringUtils.equals(thisSchema.type(), thatSchema.type())) {
return false;
}
if (!StringUtils.equals(thisSchema.format(), thatSchema.format())) {
return false;
}
if (!StringUtils.equals(thisSchema.title(), thatSchema.title())) {
return false;
}
if (!StringUtils.equals(thisSchema.description(), thatSchema.description())) {
return false;
}
if (!StringUtils.equals(thisSchema.ref(), thatSchema.ref())) {
return false;
}
if (!StringUtils.equals(thisSchema.name(), thatSchema.name())) {
return false;
}
if (!StringUtils.equals(thisSchema.defaultValue(), thatSchema.defaultValue())) {
return false;
}
if (!StringUtils.equals(thisSchema.maximum(), thatSchema.maximum())) {
return false;
}
if (!StringUtils.equals(thisSchema.minimum(), thatSchema.minimum())) {
return false;
}
if (!StringUtils.equals(thisSchema.example(), thatSchema.example())) {
return false;
}
if (!StringUtils.equals(thisSchema.pattern(), thatSchema.pattern())) {
return false;
}
if (!StringUtils.equals(thisSchema.discriminatorProperty(), thatSchema.discriminatorProperty())) {
return false;
}
if (thisSchema.multipleOf() != thatSchema.multipleOf()) {
return false;
}
if (thisSchema.minLength() != thatSchema.minLength()) {
return false;
}
if (thisSchema.minProperties() != thatSchema.minProperties()) {
return false;
}
if (thisSchema.maxProperties() != thatSchema.maxProperties()) {
return false;
}
if (thisSchema.maxLength() != thatSchema.maxLength()) {
return false;
}
if (!Arrays.equals(thisSchema.allOf(), thatSchema.allOf())) {
return false;
}
if (!Arrays.equals(thisSchema.oneOf(), thatSchema.oneOf())) {
return false;
}
if (!Arrays.equals(thisSchema.anyOf(), thatSchema.anyOf())) {
return false;
}
if (!Arrays.equals(thisSchema.subTypes(), thatSchema.subTypes())) {
return false;
}
if (!Arrays.equals(thisSchema.discriminatorMapping(), thatSchema.discriminatorMapping())) {
return false;
}
if (!Arrays.equals(thisSchema.extensions(), thatSchema.extensions())) {
return false;
}
if (!Arrays.equals(thisSchema.allowableValues(), thatSchema.allowableValues())) {
return false;
}
if (!Arrays.equals(thisSchema.requiredProperties(), thatSchema.requiredProperties())) {
return false;
}
if (thisSchema.exclusiveMinimum() != thatSchema.exclusiveMinimum()) {
return false;
}
if (thisSchema.exclusiveMaximum() != thatSchema.exclusiveMaximum()) {
return false;
}
if (thisSchema.required() != thatSchema.required()) {
return false;
}
if (thisSchema.nullable() != thatSchema.nullable()) {
return false;
}
if (thisSchema.readOnly() != thatSchema.readOnly()) {
return false;
}
if (thisSchema.writeOnly() != thatSchema.writeOnly()) {
return false;
}
if (!thisSchema.accessMode().equals(thatSchema.accessMode())) {
return false;
}
if (thisSchema.deprecated() != thatSchema.deprecated()) {
return false;
}
if (thisSchema.hidden() != thatSchema.hidden()) {
return false;
}
if (thisSchema.enumAsRef() != thatSchema.enumAsRef()) {
return false;
}
if (!thisSchema.implementation().equals(thatSchema.implementation())) {
return false;
}
if (!thisSchema.not().equals(thatSchema.not())) {
return false;
}
if (!StringUtils.equals(thisSchema.externalDocs().description(), thatSchema.externalDocs().description())) {
return false;
}
if (!StringUtils.equals(thisSchema.externalDocs().url(), thatSchema.externalDocs().url())) {
return false;
}
if (thisSchema.externalDocs().extensions().length != thatSchema.externalDocs().extensions().length) {
return false;
}
if (!Arrays.equals(thisSchema.extensions(), thatSchema.extensions())) {
return false;
}
return true;
}
public static boolean hasArrayAnnotation(io.swagger.v3.oas.annotations.media.ArraySchema array) {
if (array == null) {
return false;
}
if (!array.uniqueItems()
&& array.maxItems() == Integer.MIN_VALUE
&& array.minItems() == Integer.MAX_VALUE
&& !hasSchemaAnnotation(array.schema())
&& !hasSchemaAnnotation(array.arraySchema())
) {
return false;
}
return true;
}
public static Optional getExample(ExampleObject example) {
return getExample(example, false);
}
public static Optional getExample(ExampleObject example, boolean ignoreName) {
if (example == null) {
return Optional.empty();
}
Example exampleObject = new Example();
if (!ignoreName && StringUtils.isNotBlank(example.name())) {
if (StringUtils.isNotBlank(example.name())) {
exampleObject.setDescription(example.name());
}
resolveExample(exampleObject, example);
return Optional.of(exampleObject);
} else if (ignoreName){
if (resolveExample(exampleObject, example)) {
return Optional.of(exampleObject);
}
}
return Optional.empty();
}
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 extensions = AnnotationsUtils.getExtensions(example.extensions());
if (extensions != null) {
extensions.forEach(exampleObject::addExtension);
}
}
return !isEmpty;
}
public static Optional getArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema arraySchema, JsonView jsonViewAnnotation) {
return getArraySchema(arraySchema, null, jsonViewAnnotation);
}
public static Optional getArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema arraySchema, Components components, JsonView jsonViewAnnotation) {
if (arraySchema == null || !hasArrayAnnotation(arraySchema)) {
return Optional.empty();
}
ArraySchema arraySchemaObject = new ArraySchema();
if (arraySchema.uniqueItems()) {
arraySchemaObject.setUniqueItems(arraySchema.uniqueItems());
}
if (arraySchema.maxItems() > 0) {
arraySchemaObject.setMaxItems(arraySchema.maxItems());
}
if (arraySchema.minItems() < Integer.MAX_VALUE) {
arraySchemaObject.setMinItems(arraySchema.minItems());
}
if (arraySchema.extensions().length > 0) {
Map extensions = AnnotationsUtils.getExtensions(arraySchema.extensions());
if (extensions != null) {
extensions.forEach(arraySchemaObject::addExtension);
}
}
if (arraySchema.schema() != null) {
if (arraySchema.schema().implementation().equals(Void.class)) {
getSchemaFromAnnotation(arraySchema.schema(), components, jsonViewAnnotation).ifPresent(arraySchemaObject::setItems);
} // if present, schema implementation handled upstream
}
return Optional.of(arraySchemaObject);
}
public static Optional getSchemaFromAnnotation(io.swagger.v3.oas.annotations.media.Schema schema, JsonView jsonViewAnnotation) {
return getSchemaFromAnnotation(schema, null, jsonViewAnnotation);
}
public static Optional 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 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);
}
}
return Optional.of(schemaObject);
}
public static Schema resolveSchemaFromType(Class> schemaImplementation, Components components, JsonView jsonViewAnnotation) {
Schema schemaObject;
PrimitiveType primitiveType = PrimitiveType.fromType(schemaImplementation);
if (primitiveType != null) {
schemaObject = primitiveType.createProperty();
} else {
schemaObject = new Schema();
ResolvedSchema resolvedSchema = ModelConverters.getInstance().readAllAsResolvedSchema(new AnnotatedType().type(schemaImplementation).jsonViewAnnotation(jsonViewAnnotation));
Map schemaMap;
if (resolvedSchema != null) {
schemaMap = resolvedSchema.referencedSchemas;
if (schemaMap != null) {
schemaMap.forEach((key, referencedSchema) -> {
if (components != null) {
components.addSchemas(key, referencedSchema);
}
});
}
if (resolvedSchema.schema != null) {
if (StringUtils.isNotBlank(resolvedSchema.schema.getName())) {
schemaObject.set$ref(COMPONENTS_REF + resolvedSchema.schema.getName());
} else {
schemaObject = resolvedSchema.schema;
}
}
}
}
if (StringUtils.isBlank(schemaObject.get$ref()) && StringUtils.isBlank(schemaObject.getType())) {
// default to string
schemaObject.setType("string");
}
return schemaObject;
}
public static Optional> getTags(io.swagger.v3.oas.annotations.tags.Tag[] tags, boolean skipOnlyName) {
if (tags == null) {
return Optional.empty();
}
Set 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 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);
}
public static Optional> getServers(io.swagger.v3.oas.annotations.servers.Server[] servers) {
if (servers == null) {
return Optional.empty();
}
List serverObjects = new ArrayList<>();
for (io.swagger.v3.oas.annotations.servers.Server server : servers) {
getServer(server).ifPresent(serverObjects::add);
}
if (serverObjects.isEmpty()) {
return Optional.empty();
}
return Optional.of(serverObjects);
}
public static Optional getServer(io.swagger.v3.oas.annotations.servers.Server server) {
if (server == null) {
return Optional.empty();
}
Server serverObject = new Server();
boolean isEmpty = true;
if (StringUtils.isNotBlank(server.url())) {
serverObject.setUrl(server.url());
isEmpty = false;
}
if (StringUtils.isNotBlank(server.description())) {
serverObject.setDescription(server.description());
isEmpty = false;
}
if (server.extensions().length > 0) {
Map extensions = AnnotationsUtils.getExtensions(server.extensions());
if (extensions != null) {
extensions.forEach(serverObject::addExtension);
}
isEmpty = false;
}
if (isEmpty) {
return Optional.empty();
}
io.swagger.v3.oas.annotations.servers.ServerVariable[] serverVariables = server.variables();
ServerVariables serverVariablesObject = new ServerVariables();
for (io.swagger.v3.oas.annotations.servers.ServerVariable serverVariable : serverVariables) {
ServerVariable serverVariableObject = new ServerVariable();
if (StringUtils.isNotBlank(serverVariable.description())) {
serverVariableObject.setDescription(serverVariable.description());
}
if (StringUtils.isNotBlank(serverVariable.defaultValue())) {
serverVariableObject.setDefault(serverVariable.defaultValue());
}
if (serverVariable.allowableValues() != null && serverVariable.allowableValues().length > 0) {
if (StringUtils.isNotBlank(serverVariable.allowableValues()[0])) {
serverVariableObject.setEnum(Arrays.asList(serverVariable.allowableValues()));
}
}
if (serverVariable.extensions() != null && serverVariable.extensions().length > 0) {
Map extensions = AnnotationsUtils.getExtensions(serverVariable.extensions());
if (extensions != null) {
extensions.forEach(serverVariableObject::addExtension);
}
}
serverVariablesObject.addServerVariable(serverVariable.name(), serverVariableObject);
}
serverObject.setVariables(serverVariablesObject);
return Optional.of(serverObject);
}
public static Optional getExternalDocumentation(io.swagger.v3.oas.annotations.ExternalDocumentation externalDocumentation) {
if (externalDocumentation == null) {
return Optional.empty();
}
boolean isEmpty = true;
ExternalDocumentation external = new ExternalDocumentation();
if (StringUtils.isNotBlank(externalDocumentation.description())) {
isEmpty = false;
external.setDescription(externalDocumentation.description());
}
if (StringUtils.isNotBlank(externalDocumentation.url())) {
isEmpty = false;
external.setUrl(externalDocumentation.url());
}
if (externalDocumentation.extensions() != null && externalDocumentation.extensions().length > 0) {
Map extensions = AnnotationsUtils.getExtensions(externalDocumentation.extensions());
if (extensions != null) {
extensions.forEach(external::addExtension);
isEmpty = false;
}
}
if (isEmpty) {
return Optional.empty();
}
return Optional.of(external);
}
public static Optional getInfo(io.swagger.v3.oas.annotations.info.Info info) {
if (info == null) {
return Optional.empty();
}
boolean isEmpty = true;
Info infoObject = new Info();
if (StringUtils.isNotBlank(info.description())) {
infoObject.setDescription(info.description());
isEmpty = false;
}
if (StringUtils.isNotBlank(info.termsOfService())) {
infoObject.setTermsOfService(info.termsOfService());
isEmpty = false;
}
if (StringUtils.isNotBlank(info.title())) {
infoObject.setTitle(info.title());
isEmpty = false;
}
if (StringUtils.isNotBlank(info.version())) {
infoObject.setVersion(info.version());
isEmpty = false;
}
if (info.extensions() != null && info.extensions().length > 0) {
Map extensions = AnnotationsUtils.getExtensions(info.extensions());
if (extensions != null) {
extensions.forEach(infoObject::addExtension);
isEmpty = false;
}
}
if (isEmpty) {
return Optional.empty();
}
getContact(info.contact()).ifPresent(infoObject::setContact);
getLicense(info.license()).ifPresent(infoObject::setLicense);
return Optional.of(infoObject);
}
public static Optional getContact(io.swagger.v3.oas.annotations.info.Contact contact) {
if (contact == null) {
return Optional.empty();
}
boolean isEmpty = true;
Contact contactObject = new Contact();
if (StringUtils.isNotBlank(contact.email())) {
contactObject.setEmail(contact.email());
isEmpty = false;
}
if (StringUtils.isNotBlank(contact.name())) {
contactObject.setName(contact.name());
isEmpty = false;
}
if (StringUtils.isNotBlank(contact.url())) {
contactObject.setUrl(contact.url());
isEmpty = false;
}
if (contact.extensions() != null && contact.extensions().length > 0) {
Map extensions = AnnotationsUtils.getExtensions(contact.extensions());
if (extensions != null) {
extensions.forEach(contactObject::addExtension);
isEmpty = false;
}
}
if (isEmpty) {
return Optional.empty();
}
return Optional.of(contactObject);
}
public static Optional 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 extensions = AnnotationsUtils.getExtensions(license.extensions());
if (extensions != null) {
extensions.forEach(licenseObject::addExtension);
isEmpty = false;
}
}
if (isEmpty) {
return Optional.empty();
}
return Optional.of(licenseObject);
}
public static Map getLinks(io.swagger.v3.oas.annotations.links.Link[] links) {
Map linkMap = new HashMap<>();
if (links == null) {
return linkMap;
}
for (io.swagger.v3.oas.annotations.links.Link link : links) {
getLink(link).ifPresent(linkResult -> linkMap.put(link.name(), linkResult));
}
return linkMap;
}
public static Optional getLink(io.swagger.v3.oas.annotations.links.Link link) {
if (link == null) {
return Optional.empty();
}
boolean isEmpty = true;
Link linkObject = new Link();
if (StringUtils.isNotBlank(link.description())) {
linkObject.setDescription(link.description());
isEmpty = false;
}
if (StringUtils.isNotBlank(link.operationId())) {
linkObject.setOperationId(link.operationId());
isEmpty = false;
if (StringUtils.isNotBlank(link.operationRef())) {
LOGGER.debug("OperationId and OperatonRef are mutually exclusive, there must be only one setted");
}
} else {
if (StringUtils.isNotBlank(link.operationRef())) {
linkObject.setOperationRef(link.operationRef());
isEmpty = false;
}
}
if (StringUtils.isNotBlank(link.ref())) {
linkObject.set$ref(link.ref());
isEmpty = false;
}
if (link.extensions() != null && link.extensions().length > 0) {
Map extensions = AnnotationsUtils.getExtensions(link.extensions());
if (extensions != null) {
extensions.forEach(linkObject::addExtension);
isEmpty = false;
}
}
if (isEmpty) {
return Optional.empty();
}
Map linkParameters = getLinkParameters(link.parameters());
if (linkParameters.size() > 0) {
linkObject.setParameters(linkParameters);
}
if (StringUtils.isNotBlank(link.requestBody())) {
JsonNode processedValue = null;
try {
processedValue = Json.mapper().readTree(link.requestBody());
} catch (Exception e) {
// not a json string
}
if (processedValue == null) {
linkObject.requestBody(link.requestBody());
} else {
linkObject.requestBody(processedValue);
}
}
return Optional.of(linkObject);
}
public static Map getLinkParameters(LinkParameter[]
linkParameter) {
Map linkParametersMap = new HashMap<>();
if (linkParameter == null) {
return linkParametersMap;
}
for (LinkParameter parameter : linkParameter) {
if (StringUtils.isNotBlank(parameter.name())) {
linkParametersMap.put(parameter.name(), parameter.expression());
}
}
return linkParametersMap;
}
public static Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy