Skip to content

Commit

Permalink
Refine condition, for ignoring types when using PolymorphicModelConve…
Browse files Browse the repository at this point in the history
…rter. Fixes #2851
  • Loading branch information
bnasslahsen committed Jan 12, 2025
1 parent aea694a commit 8648879
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springdoc.core.providers.ObjectMapperProvider;

Expand All @@ -68,7 +69,7 @@ public class PolymorphicModelConverter implements ModelConverter {
* The constant PARENT_TYPES_TO_IGNORE.
*/
private static final List<String> TYPES_TO_SKIP = Collections.synchronizedList(new ArrayList<>());

static {
PARENT_TYPES_TO_IGNORE.add("JsonSchema");
PARENT_TYPES_TO_IGNORE.add("Pageable");
Expand Down Expand Up @@ -121,7 +122,10 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getSimpleName());
}
else if (field.isAnnotationPresent(io.swagger.v3.oas.annotations.media.Schema.class)) {
TYPES_TO_SKIP.add(field.getType().getSimpleName());
io.swagger.v3.oas.annotations.media.Schema declaredSchema = field.getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
if (ArrayUtils.isNotEmpty(declaredSchema.oneOf()) || ArrayUtils.isNotEmpty(declaredSchema.allOf())) {
TYPES_TO_SKIP.add(field.getType().getSimpleName());
}
}
}
if (chain.hasNext()) {
Expand All @@ -133,7 +137,7 @@ else if (field.isAnnotationPresent(io.swagger.v3.oas.annotations.media.Schema.cl
if (resolvedSchema == null || resolvedSchema.get$ref() == null) {
return resolvedSchema;
}
if(resolvedSchema.get$ref().contains(Components.COMPONENTS_SCHEMAS_REF)) {
if (resolvedSchema.get$ref().contains(Components.COMPONENTS_SCHEMAS_REF)) {
String schemaName = resolvedSchema.get$ref().substring(Components.COMPONENTS_SCHEMAS_REF.length());
Schema existingSchema = context.getDefinedModels().get(schemaName);
if (existingSchema != null && (existingSchema.getOneOf() != null || existingSchema.getAllOf() != null)) {
Expand Down Expand Up @@ -162,7 +166,7 @@ private Schema composePolymorphicSchema(AnnotatedType type, Schema schema, Colle
if (isConcreteClass(type)) result.addOneOfItem(schema);
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
Class<?> clazz = javaType.getRawClass();
if(TYPES_TO_SKIP.stream().noneMatch(typeToSkip -> typeToSkip.equals(clazz.getSimpleName())))
if (TYPES_TO_SKIP.stream().noneMatch(typeToSkip -> typeToSkip.equals(clazz.getSimpleName())))
composedSchemas.forEach(result::addOneOfItem);
return result;
}
Expand Down

0 comments on commit 8648879

Please sign in to comment.