Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
915 views
in Technique[技术] by (71.8m points)

spring - springdoc-openapi: publish enum as reference when enum comes from generated code

I'm using

  <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.4.8</version>
        </dependency>

For generating client-stubs via openapi-specs and generating my own openapi docoumentation.

I do have an API, lets call it just API-1, which I'm using within my project.

This API does provide an enum, simpfified this one:

@Schema(enumAsRef=true)
public enum SomethingEnum {
 A,
 B,
 C
}

API one does provide an openapi-specification, there the enum is included as schema and referenced. That is all fine.

This enum I'm using in API-2. I let all models from API-1 generate with the openapi-generator-maven-plugin.

API-2 does provide an openapi specification, which looks simplified like this:

{
  "paths": {
    "/request": {
      "get": {
        "tags": [
          "requests"
        ],
        "operationId": "getSomething",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Something"
                }
              }
            }
          }
        }
      }
    }
  },
  "schemas": {
    "Something": {
      "type": "object",
      "properties": {
        "somethingEnum": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "A",
              "B",
              "C"
            ]
          }
        },
        ,
        "id": {
          "type": "string"
        }
      }
    }
  }
}

And here is the problem: The SomethingEnum is not referenced via an Schema. It should rather look like this:

{
  "paths": {
    "/request": {
      "get": {
        "tags": [
          "requests"
        ],
        "operationId": "getSomething",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Something"
                }
              }
            }
          }
        }
      }
    }
  },
  "schemas": {
    "Something": {
      "type": "object",
      "properties": {
        "SomethingEnum ": {
          "$ref": "#/components/schemas/SomethingEnum "
        },
        "id": {
          "type": "string"
        }
      }
    },
    "SomethingEnum": {
      "type": "string",
      "enum": [
        "A",
        "B",
        "C",
      ]
    }
  }
}

How can I achieve this? Is there a way I can either

  • configure the openapi-generator-maven-plugin to annote generated enums automatically with @Schema(enumAsRef=true)
  • configure springdoc somehow ?

I hope my problem is clear. Thanks for every suggestion.

question from:https://stackoverflow.com/questions/65847832/springdoc-openapi-publish-enum-as-reference-when-enum-comes-from-generated-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...