Skip to content

Commit d939561

Browse files
committed
fix nullability customizer for polymorphic types
1 parent 2e9413c commit d939561

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/default_not_null/NullableCustomizer.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springdoc.core.customizers.OpenApiCustomizer;
66

77
import java.util.ArrayList;
8+
import java.util.Map;
89

910
public class NullableCustomizer implements OpenApiCustomizer {
1011
@Override
@@ -15,18 +16,32 @@ public void customise(OpenAPI openApi) {
1516
}
1617
openApi.getComponents().getSchemas().values()
1718
.forEach(schema -> {
19+
var requiredProperties = new ArrayList<String>();
1820
if (((Schema<?>) schema).getProperties() != null) {
19-
var requiredProperties = new ArrayList<String>();
20-
((Schema<?>) schema).getProperties().forEach((propertyName, property) -> {
21-
if (property.getTitle() == null || !property.getTitle().equals("NULLABLE")) {
22-
requiredProperties.add(propertyName);
23-
}
24-
if (property.getTitle() != null && property.getTitle().equals("NULLABLE")) {
25-
property.setTitle(null);
21+
var properties = ((Schema<?>) schema).getProperties();
22+
processProperties(properties, requiredProperties);
23+
}
24+
if (schema.getAllOf() != null) {
25+
schema.getAllOf().forEach(allOfSchema -> {
26+
var allOfSchemaTyped = (Schema<?>) allOfSchema;
27+
if (allOfSchemaTyped.getProperties() != null) {
28+
var properties = allOfSchemaTyped.getProperties();
29+
processProperties(properties, requiredProperties);
2630
}
2731
});
28-
schema.setRequired(requiredProperties);
2932
}
33+
schema.setRequired(requiredProperties);
3034
});
3135
}
36+
37+
private static void processProperties(Map<String, Schema> properties, ArrayList<String> requiredProperties) {
38+
properties.forEach((propertyName, property) -> {
39+
if (property.getTitle() == null || !property.getTitle().equals("NULLABLE")) {
40+
requiredProperties.add(propertyName);
41+
}
42+
if (property.getTitle() != null && property.getTitle().equals("NULLABLE")) {
43+
property.setTitle(null);
44+
}
45+
});
46+
}
3247
}

0 commit comments

Comments
 (0)