简体   繁体   English

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

[英]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.

相关问题 Petstore url(swagger 默认应用程序)未在 Springdoc 中禁用 - Petstore url ( swagger default app ) not disabled in Springdoc 如何更改 springdoc 中的命名策略? - How to change namingStrategy in springdoc? 如何在 springdoc Swagger UI 页面中禁用默认 /v3/api-docs 定义? - How to disable default /v3/api-docs definition in springdoc Swagger UI page? springdoc swagger ui 未显示实体组件 model 架构 - springdoc swagger ui not showing entity component model schema Java springdoc-openapi 在 Swagger UI 示例值中显示带有附加日期/时间字段的 LocalDateTime 字段 - Java springdoc-openapi show LocalDateTime field with additional date/time fields in Swagger UI Example Value springdoc-openapi-ui生成的swagger-ui中如何更改请求header中授权key的名称 - How to change the name of the authorization key in the request header in the swagger-ui generated by springdoc-openapi-ui 如何使用 Springdoc 从 OpenAPI 文档中隐藏端点 - How to hide endpoints from OpenAPI documentation with Springdoc 如何使用带有 Lombok getter 的 springdoc-openapi 将 @JsonValue 用于 Swagger 枚举值 - How can @JsonValue be used for Swagger enum values using springdoc-openapi with a Lombok getter 如何使用 springdoc-openapi-maven-plugin 和 swagger-codegen-maven-plugin 生成客户端代码? - How to generate client code using springdoc-openapi-maven-plugin and swagger-codegen-maven-plugin? 如何使用 springdoc-openapi-webflux-ui 显示应用程序 API 文档? - How to display app API documentation by using springdoc-openapi-webflux-ui?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM