We use Spring Boot and https://springdoc.org/ to generate OpenApi documentation. We want to change default schema for LocalDateTime, so we don't have the same annotation every time LocalDateTime is used. So, I added:
static {
SpringDocUtils.getConfig().replaceWithSchema(LocalDateTime.class,
new StringSchema().example("2021-07-05T10:35:17.000").pattern("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[.]\\d{3}"));
}
it worked. The issue is that now it's impossible to add custom description or example for specific field:
@Schema(description = "important date")
private LocalDateTime aDate;
As you can see below description is missing in Swagger-UI: screenshot with missing description
Is it possible to fix? Is there another way to have default custom schema for LocalDateTime?
You could use OpenAPICustomerCustomiser
@Bean
public OpenApiCustomiser openAPICustomiser() {
return openApi -> {
openApi.getComponents().getSchemas().forEach((s, schema) -> {
Map<String, Schema> properties = schema.getProperties();
if (properties == null) {
properties = Map.of();
}
for (String propertyName : properties.keySet()) {
Schema propertySchema = properties.get(propertyName);
if (propertySchema instanceof DateTimeSchema) {
properties.replace(propertyName, new StringSchema()
.example("2021-07-05T10:35:17.000")
.pattern("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[.]\\d{3}$")
//copies original description
.description(propertySchema.getDescription()));
}
}
});
};
}