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