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);
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations