Search in sources :

Example 6 with ModelConverterContext

use of io.swagger.v3.core.converter.ModelConverterContext 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 7 with ModelConverterContext

use of io.swagger.v3.core.converter.ModelConverterContext 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 8 with ModelConverterContext

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

the class ModelResolver method resolveDiscriminatorProperty.

protected void resolveDiscriminatorProperty(JavaType type, ModelConverterContext context, Schema model) {
    // add JsonTypeInfo.property if not member of bean
    JsonTypeInfo typeInfo = type.getRawClass().getDeclaredAnnotation(JsonTypeInfo.class);
    if (typeInfo != null) {
        String typeInfoProp = typeInfo.property();
        if (StringUtils.isNotBlank(typeInfoProp)) {
            Schema modelToUpdate = model;
            if (StringUtils.isNotBlank(model.get$ref())) {
                modelToUpdate = context.getDefinedModels().get(model.get$ref().substring(21));
            }
            if (modelToUpdate.getProperties() == null || !modelToUpdate.getProperties().keySet().contains(typeInfoProp)) {
                Schema discriminatorSchema = new StringSchema().name(typeInfoProp);
                modelToUpdate.addProperties(typeInfoProp, discriminatorSchema);
                if (modelToUpdate.getRequired() == null || !modelToUpdate.getRequired().contains(typeInfoProp)) {
                    modelToUpdate.addRequiredItem(typeInfoProp);
                }
            }
        }
    }
}
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) StringSchema(io.swagger.v3.oas.models.media.StringSchema) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo)

Example 9 with ModelConverterContext

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

the class ModelResolver method resolveWrapping.

/*
     TODO partial implementation supporting WRAPPER_OBJECT with JsonTypeInfo.Id.CLASS and JsonTypeInfo.Id.NAME

     Also note that JsonTypeInfo on interfaces are not considered as multiple interfaces might have conflicting
     annotations, although Jackson seems to apply them if present on an interface
     */
protected Schema resolveWrapping(JavaType type, ModelConverterContext context, Schema model) {
    // add JsonTypeInfo.property if not member of bean
    JsonTypeInfo typeInfo = type.getRawClass().getDeclaredAnnotation(JsonTypeInfo.class);
    if (typeInfo != null) {
        JsonTypeInfo.Id id = typeInfo.use();
        JsonTypeInfo.As as = typeInfo.include();
        if (JsonTypeInfo.As.WRAPPER_OBJECT.equals(as)) {
            String name = model.getName();
            if (JsonTypeInfo.Id.CLASS.equals(id)) {
                name = type.getRawClass().getName();
            }
            Schema wrapperSchema = new ObjectSchema();
            wrapperSchema.name(model.getName());
            wrapperSchema.addProperties(name, model);
            return wrapperSchema;
        }
    }
    return model;
}
Also used : ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) 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) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo)

Aggregations

Schema (io.swagger.v3.oas.models.media.Schema)8 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)6 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)6 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)6 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)6 MapSchema (io.swagger.v3.oas.models.media.MapSchema)6 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)6 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)6 StringSchema (io.swagger.v3.oas.models.media.StringSchema)6 UUIDSchema (io.swagger.v3.oas.models.media.UUIDSchema)6 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)4 Annotation (java.lang.annotation.Annotation)3 LinkedHashMap (java.util.LinkedHashMap)3 JavaType (com.fasterxml.jackson.databind.JavaType)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)2 PatternProperties (io.swagger.v3.oas.annotations.media.PatternProperties)2 PatternProperty (io.swagger.v3.oas.annotations.media.PatternProperty)2 JsonIdentityInfo (com.fasterxml.jackson.annotation.JsonIdentityInfo)1 JsonIdentityReference (com.fasterxml.jackson.annotation.JsonIdentityReference)1 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1