[英]How to change default schema of LocalDateTime in Swagger documentation using springdoc?
We use Spring Boot and https://springdoc.org/ to generate OpenApi documentation.我们使用 Spring Boot 和https://springdoc.org/来生成 OpenApi 文档。 We want to change default schema for LocalDateTime, so we don't have the same annotation every time LocalDateTime is used.
我们想要更改 LocalDateTime 的默认架构,因此每次使用 LocalDateTime 时我们都没有相同的注释。 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如您所见,Swagger-UI 中缺少以下描述:缺少描述的屏幕截图
Is it possible to fix?有没有可能修复? Is there another way to have default custom schema for LocalDateTime?
还有另一种方法可以为 LocalDateTime 设置默认的自定义架构吗?
You could use OpenAPICustomerCustomiser您可以使用 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.