Search in sources :

Example 56 with AnnotatedType

use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.

the class ModelResolver method resolvePatternProperties.

protected Map<String, Schema> resolvePatternProperties(JavaType a, Annotation[] annotations, ModelConverterContext context) {
    final Map<String, PatternProperty> propList = new LinkedHashMap<>();
    PatternProperties props = a.getRawClass().getAnnotation(PatternProperties.class);
    if (props != null && props.value().length > 0) {
        for (PatternProperty prop : props.value()) {
            propList.put(prop.regex(), prop);
        }
    }
    PatternProperty singleProp = a.getRawClass().getAnnotation(PatternProperty.class);
    if (singleProp != null) {
        propList.put(singleProp.regex(), singleProp);
    }
    props = AnnotationsUtils.getAnnotation(PatternProperties.class, annotations);
    if (props != null && props.value().length > 0) {
        for (PatternProperty prop : props.value()) {
            propList.put(prop.regex(), prop);
        }
    }
    singleProp = AnnotationsUtils.getAnnotation(PatternProperty.class, annotations);
    if (singleProp != null) {
        propList.put(singleProp.regex(), singleProp);
    }
    if (propList.isEmpty()) {
        return null;
    }
    Map<String, Schema> patternProperties = new LinkedHashMap<>();
    for (PatternProperty prop : propList.values()) {
        String key = prop.regex();
        if (StringUtils.isBlank(key)) {
            continue;
        }
        Annotation[] propAnnotations = new Annotation[] { prop.schema(), prop.array() };
        AnnotatedType propType = new AnnotatedType().type(String.class).ctxAnnotations(propAnnotations).resolveAsRef(true);
        Schema resolvedPropSchema = context.resolve(propType);
        if (resolvedPropSchema != null) {
            patternProperties.put(key, resolvedPropSchema);
        }
    }
    return patternProperties;
}
Also used : PatternProperty(io.swagger.v3.oas.annotations.media.PatternProperty) AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) PatternProperties(io.swagger.v3.oas.annotations.media.PatternProperties) UUIDSchema(io.swagger.v3.oas.models.media.UUIDSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) Annotation(java.lang.annotation.Annotation) LinkedHashMap(java.util.LinkedHashMap)

Example 57 with AnnotatedType

use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.

the class ModelResolver method resolveSubtypes.

private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConverterContext context) {
    final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
    if (types == null) {
        return false;
    }
    /**
     * Remove the current class from the child classes. This happens if @JsonSubTypes references
     * the annotated class as a subtype.
     */
    removeSelfFromSubTypes(types, bean);
    /**
     * As the introspector will find @JsonSubTypes for a child class that are present on its super classes, the
     * code segment below will also run the introspector on the parent class, and then remove any sub-types that are
     * found for the parent from the sub-types found for the child. The same logic all applies to implemented
     * interfaces, and is accounted for below.
     */
    removeSuperClassAndInterfaceSubTypes(types, bean);
    int count = 0;
    final Class<?> beanClass = bean.getClassInfo().getAnnotated();
    for (NamedType subtype : types) {
        final Class<?> subtypeType = subtype.getType();
        if (!beanClass.isAssignableFrom(subtypeType)) {
            continue;
        }
        final Schema subtypeModel = context.resolve(new AnnotatedType().type(subtypeType));
        if (StringUtils.isBlank(subtypeModel.getName()) || subtypeModel.getName().equals(model.getName())) {
            subtypeModel.setName(_typeNameResolver.nameForType(_mapper.constructType(subtypeType), TypeNameResolver.Options.SKIP_API_MODEL));
        }
        // here schema could be not composed, but we want it to be composed, doing same work as done
        // in resolve method??
        ComposedSchema composedSchema = null;
        if (!(subtypeModel instanceof ComposedSchema)) {
            // create composed schema
            // TODO #2312 - smarter way needs clone implemented in #2227
            composedSchema = (ComposedSchema) new ComposedSchema().title(subtypeModel.getTitle()).name(subtypeModel.getName()).deprecated(subtypeModel.getDeprecated()).additionalProperties(subtypeModel.getAdditionalProperties()).description(subtypeModel.getDescription()).discriminator(subtypeModel.getDiscriminator()).exclusiveMaximum(subtypeModel.getExclusiveMaximum()).exclusiveMinimum(subtypeModel.getExclusiveMinimum()).externalDocs(subtypeModel.getExternalDocs()).format(subtypeModel.getFormat()).maximum(subtypeModel.getMaximum()).maxItems(subtypeModel.getMaxItems()).maxLength(subtypeModel.getMaxLength()).maxProperties(subtypeModel.getMaxProperties()).minimum(subtypeModel.getMinimum()).minItems(subtypeModel.getMinItems()).minLength(subtypeModel.getMinLength()).minProperties(subtypeModel.getMinProperties()).multipleOf(subtypeModel.getMultipleOf()).not(subtypeModel.getNot()).nullable(subtypeModel.getNullable()).pattern(subtypeModel.getPattern()).properties(subtypeModel.getProperties()).readOnly(subtypeModel.getReadOnly()).required(subtypeModel.getRequired()).type(subtypeModel.getType()).uniqueItems(subtypeModel.getUniqueItems()).writeOnly(subtypeModel.getWriteOnly()).xml(subtypeModel.getXml()).extensions(subtypeModel.getExtensions());
            if (subtypeModel.getExample() != null || subtypeModel.getExampleSetFlag()) {
                composedSchema.example(subtypeModel.getExample());
            }
            composedSchema.setEnum(subtypeModel.getEnum());
        } else {
            composedSchema = (ComposedSchema) subtypeModel;
        }
        Schema refSchema = new Schema().$ref(model.getName());
        // allOf could have already being added during type resolving when @Schema(allOf..) is declared
        if (composedSchema.getAllOf() == null || !composedSchema.getAllOf().contains(refSchema)) {
            composedSchema.addAllOfItem(refSchema);
        }
        removeParentProperties(composedSchema, model);
        if (!composedModelPropertiesAsSibling) {
            if (composedSchema.getAllOf() != null && !composedSchema.getAllOf().isEmpty()) {
                if (composedSchema.getProperties() != null && !composedSchema.getProperties().isEmpty()) {
                    ObjectSchema propSchema = new ObjectSchema();
                    propSchema.properties(composedSchema.getProperties());
                    composedSchema.setProperties(null);
                    composedSchema.addAllOfItem(propSchema);
                }
            }
        }
        // replace previous schema..
        Class<?> currentType = subtype.getType();
        if (StringUtils.isNotBlank(composedSchema.getName())) {
            context.defineModel(composedSchema.getName(), composedSchema, new AnnotatedType().type(currentType), null);
        }
    }
    return count != 0;
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) UUIDSchema(io.swagger.v3.oas.models.media.UUIDSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema)

Example 58 with AnnotatedType

use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.

the class ModelResolver method resolveSchemaMembers.

protected void resolveSchemaMembers(Schema schema, AnnotatedType annotatedType) {
    final JavaType type;
    if (annotatedType.getType() instanceof JavaType) {
        type = (JavaType) annotatedType.getType();
    } else {
        type = _mapper.constructType(annotatedType.getType());
    }
    final Annotation resolvedSchemaOrArrayAnnotation = AnnotationsUtils.mergeSchemaAnnotations(annotatedType.getCtxAnnotations(), type);
    final io.swagger.v3.oas.annotations.media.Schema schemaAnnotation = resolvedSchemaOrArrayAnnotation == null ? null : resolvedSchemaOrArrayAnnotation instanceof io.swagger.v3.oas.annotations.media.ArraySchema ? ((io.swagger.v3.oas.annotations.media.ArraySchema) resolvedSchemaOrArrayAnnotation).schema() : (io.swagger.v3.oas.annotations.media.Schema) resolvedSchemaOrArrayAnnotation;
    final BeanDescription beanDesc = _mapper.getSerializationConfig().introspect(type);
    Annotated a = beanDesc.getClassInfo();
    Annotation[] annotations = annotatedType.getCtxAnnotations();
    resolveSchemaMembers(schema, a, annotations, schemaAnnotation);
}
Also used : UUIDSchema(io.swagger.v3.oas.models.media.UUIDSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) Annotation(java.lang.annotation.Annotation) Annotated(com.fasterxml.jackson.databind.introspect.Annotated) JavaType(com.fasterxml.jackson.databind.JavaType) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema)

Example 59 with AnnotatedType

use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.

the class AnnotationsUtils method resolveSchemaFromType.

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<String, Schema> 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;
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) 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) ResolvedSchema(io.swagger.v3.core.converter.ResolvedSchema)

Example 60 with AnnotatedType

use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.

the class EnumPropertyTest method testEnumRefPropertyGlobal.

@Test(description = "it should read a model with an enum property as a reference, set via static var or sys prop")
public void testEnumRefPropertyGlobal() {
    ModelResolver.enumsAsRef = true;
    Schema schema = context.resolve(new AnnotatedType(ModelWithEnumProperty.class));
    final Map<String, Schema> models = context.getDefinedModels();
    final String yaml = "ModelWithEnumProperty:\n" + "  type: object\n" + "  properties:\n" + "    enumValue:\n" + "      $ref: '#/components/schemas/TestEnum'\n" + "TestEnum:\n" + "  type: string\n" + "  enum:\n" + "  - PRIVATE\n" + "  - PUBLIC\n" + "  - SYSTEM\n" + "  - INVITE_ONLY\n";
    SerializationMatchers.assertEqualsToYaml(models, yaml);
    ModelResolver.enumsAsRef = false;
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) ModelWithEnumProperty(io.swagger.v3.core.oas.models.ModelWithEnumProperty) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest)

Aggregations

Schema (io.swagger.v3.oas.models.media.Schema)60 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)57 Test (org.testng.annotations.Test)47 ModelConverterContextImpl (io.swagger.v3.core.converter.ModelConverterContextImpl)25 ModelResolver (io.swagger.v3.core.jackson.ModelResolver)21 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)14 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)13 AfterTest (org.testng.annotations.AfterTest)13 StringSchema (io.swagger.v3.oas.models.media.StringSchema)11 ModelConverter (io.swagger.v3.core.converter.ModelConverter)6 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)6 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)6 Annotation (java.lang.annotation.Annotation)6 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 JavaType (com.fasterxml.jackson.databind.JavaType)4 MapSchema (io.swagger.v3.oas.models.media.MapSchema)4 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)4 UUIDSchema (io.swagger.v3.oas.models.media.UUIDSchema)4 ArrayList (java.util.ArrayList)4