繁体   English   中英

如何使用 springdoc 在 Swagger 文档中更改 LocalDateTime 的默认架构?

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

我们使用 Spring Boot 和https://springdoc.org/来生成 OpenApi 文档。 我们想要更改 LocalDateTime 的默认架构,因此每次使用 LocalDateTime 时我们都没有相同的注释。 所以,我补充说:

    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}")); 
    } 

有效。 问题是现在无法为特定字段添加自定义描述或示例:

    @Schema(description = "important date") 
    private LocalDateTime aDate; 

如您所见,Swagger-UI 中缺少以下描述:缺少描述的屏幕截图

有没有可能修复? 还有另一种方法可以为 LocalDateTime 设置默认的自定义架构吗?

您可以使用 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()));
                }​​​​​​​​​
            }​​​​​​​​​
        }​​​​​​​​​);
    }​​​​​​​​​;
}​​​​​​​​​

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM