In our spring project we have over hundred of schemas I have a task to set all types in schemas annotated with @Nonnull to required so they appear is required (with a *) in Swagger. I've overridden the method customize from springdoc's PropertyCustomizer the following way:
@Override
public Schema customize(Schema property, AnnotatedType type) {
Annotation[] ctxAnnotations = type.getCtxAnnotations();
if (ctxAnnotations == null) {
return property;
}
boolean isNonnull = Arrays.stream(ctxAnnotations)
.anyMatch(annotation -> annotation.annotationType().equals(Nonnull.class));
if (isNonnull) {
if (type.getParent().getRequired() == null) {
List<String> list = new ArrayList<>();
list.add(type.getPropertyName());
type.getParent().setRequired(list);
} else {
type.getParent().getRequired().add(type.getPropertyName());
}
}
return property;
}
It works succesfully for most of the types but for some of them the method customize seems not be inwoked at all. Giving an example we have a class TripListingOgItem:
@Value
@Builder
public class TripListingOgItem {
@Nonnull String name;
@Nullable String image;
@Nonnull String url;
}
So according to it both name and url should appear as required in swagger but only name does. I even tried to print all the types with their parents and bool if isNonnull for which the method is inwoked and url was nowhere to be found:
System.out.println(
"\nParent: " + type.getParent().getName() +
"\nType: " + type.getPropertyName() +
"\nIsNonnull: " + isNonnull
);
I have no idea why the method does not get inwoked for some types. The class with this method is being registered with a simple bean in a configuration class:
@Bean
public PropertyCustomizer propertyCustomizer() {
return new PropertyCustomizer();
}
I am using the following dependency:
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.12</version>
<scope>compile</scope>
</dependency>
I know I can simply annotate all the types with schema(required=true) but since it would be a long process and require constant maintenance we consider that a last resort solution. Thanks anyone in advance and have a nice day!