Skip to content

How to change default schema of LocalDateTime in Swagger documentation using springdoc?

We use Spring Boot and 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 { 
                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

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()
                            //copies original description