Search in sources :

Example 26 with Discriminator

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

Aggregations

Schema (io.swagger.v3.oas.models.media.Schema)19 Test (org.testng.annotations.Test)19 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)15 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)15 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)15 MapSchema (io.swagger.v3.oas.models.media.MapSchema)14 StringSchema (io.swagger.v3.oas.models.media.StringSchema)14 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)13 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)13 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)12 OpenAPI (io.swagger.v3.oas.models.OpenAPI)11 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)6 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)6 Discriminator (io.swagger.v3.oas.models.media.Discriminator)5 DateSchema (io.swagger.v3.oas.models.media.DateSchema)4 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)4 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)3 HashSet (java.util.HashSet)3 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2