如何为对象生成additionalProperties为false的swagger json?

zazmityj  于 2023-01-02  发布在  其他
关注(0)|答案(1)|浏览(150)

我有下面的对象与趾高气扬的描述:

/**
 * Response about request failure
 */
@Schema(description = "Response about request failure")
data class ErrorDtoRs(
        @field:Schema(description = "Error type", maxLength = 64)
        val type: String? = null,
        @field:Schema(description = "Error message", maxLength = 255)
        val message: String?)

此对象的Swagger json:

"components": {
    "schemas": {
      "ErrorDtoRs": {
        "type": "object",
        "properties": {
          "type": {
            "maxLength": 64,
            "type": "string",
            "description": "Error type"
          },
          "message": {
            "maxLength": 255,
            "type": "string",
            "description": "Error message"
          }
        },
        "description": "Response about request failure"
      },

如何为对象生成additionalProperties为false的swagger json,如下所示:

"components": {
    "schemas": {
      "ErrorDtoRs": {
        "type": "object",
        "additionalProperties": "false",
        "properties": {
          "type": {
            "maxLength": 64,
            "type": "string",
            "description": "Error type"
          },
          "message": {
            "maxLength": 255,
            "type": "string",
            "description": "Error message"
          }
        },
        "description": "Response about request failure"
      },

Swagger :2.1.4开放API:3.0.1

o4tp2gmn

o4tp2gmn1#

如果您使用的是sprindoc-openapi,您可以使用GroupedOpenApi添加一个OpenApiCustomier,并将其他属性设置为FALSE,如下所示:

@Bean
  public OpenApiCustomiser openApiCustomiser() {
    return openApi ->
        openApi
            .getComponents()
            .getSchemas()
            .values()
            .forEach(schema -> schema.setAdditionalProperties(false));
  }

  @Bean
  public GroupedOpenApi coreOpenApi() {
    return GroupedOpenApi.builder()
            .group("NAME")
            .pathsToMatch("/**")
            .addOpenApiCustomiser(this.openApiCustomiser())
            .build();
  }

这还将阻止安全问题ID:v3-schema-request-object-additionalproperties-true

相关问题