Java Enum上的Openapi和@schema注释



我正在从注释的Java代码中生成OpenAPI 3.0文档。但是问题在于,当我添加@schema注释以枚举所有值时,所有值就消失了。我使用的是Thorntail 2.3.0.-final与微纹状体 - openapi分数。

我知道我可以更改.yaml文件,但是我需要我的yaml直接从Java代码生成。

这是我在github上的最小示例:https://github.com/pkristja/openapienumschema

枚举的源代码:

package com.example.openapiexample.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
@Schema(description = "<div class=\"renderedMarkdown\"><p>Rank of developer.</p>\n" +n" +
        "        "<p>Valid values are:</p>\n" +n" +
        "        "<ul>\n" +n" +
        "        "<li>'JUNIOR_DEVELOPER_1': Text for junior 1.\n" +n" +
        "        "<li>'JUNIOR_DEVELOPER_2': Text for junior 2.\n" +n" +
        "        "<li>'JUNIOR_DEVELOPER_3': Text for junior 3.\n" +n" +
        "        "<li>'SENIOR_DEVELOPER_1': Text for senior 1.\n" +n" +
        "        "<li>'SENIOR_DEVELOPER_2': Text for senior 1.\n" +n" +
        "        "<li>'SENIOR_DEVELOPER_3': Text for senior 1.\n" +n" +
        "        "</ul>\n" +n" +
        "        "<p>Random text...\n" +n" +
        "        "and has to be added to this API definition as well.</p></div>",
        enumeration = {"junior_developer_1", "junior_developer_2", "junior_developer_3",
                "senior_developer_1", "senior_developer_2", "senior_developer_3"})
public enum Rank {
    JUNIOR_DEVELOPER_1("junior_developer_1"),
    JUNIOR_DEVELOPER_2("junior_developer_2"),
    JUNIOR_DEVELOPER_3("junior_developer_3"),
    SENIOR_DEVELOPER_1("senior_developer_1"),
    SENIOR_DEVELOPER_2("senior_developer_2"),
    SENIOR_DEVELOPER_3("senior_developer_3");
    private String value;
    Rank(String value) {
        this.value = value;
    }
    @Override
    @JsonValue
    public String toString() {
        return String.valueOf(value);
    }
    @JsonCreator
    public static Rank fromValue(String text) {
        for (Rank b : Rank.values()) {
            if (String.valueOf(b.value).equals(text)) {
                return b;
            }
        }
        return null;
    }
}

和对象的源代码,包括枚举:

package com.example.openapiexample.model;
import lombok.Data;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
@Data
@Schema(description = "Schema for Developer object...")
public class Developer {
    @Schema(required = true, description = "First name of the developer")
    private String firstName;
    @Schema(required = true, description = "Last name of the developer")
    private String lastName;
    @Schema(required = true, implementation = Rank.class)
    private Rank developerRank;
}

生成的OpenAPI 3.0文档:

 schemas:
    Developer:
      description: Schema for Developer object...
      required:
      - developerRank
      - firstName
      - lastName
      properties:
        developerRank:
          description: |-
            <div class="renderedMarkdown"><p>Rank of developer.</p>n" +
                    "<p>Valid values are:</p>n" +
                    "<ul>n" +
                    "<li>'JUNIOR_DEVELOPER_1': Text for junior 1.n" +
                    "<li>'JUNIOR_DEVELOPER_2': Text for junior 2.n" +
                    "<li>'JUNIOR_DEVELOPER_3': Text for junior 3.n" +
                    "<li>'SENIOR_DEVELOPER_1': Text for senior 1.n" +
                    "<li>'SENIOR_DEVELOPER_2': Text for senior 2.n" +
                    "<li>'SENIOR_DEVELOPER_3': Text for senior 3.n" +
                    "</ul>n" +
                    "<p>Random text...n" +
                    "and has to be added to this API definition as well.</p></div>
          type: string
          properties:
            value:
              type: string
        firstName:
          description: First name of the developer
          type: string
        lastName:
          description: Last name of the developer
          type: string

但是,如果我在开发人员类中的枚举之前删除@schema注释,则我将获得枚举值,但没有描述和必需的值:

schemas:
    Developer:
      description: Schema for Developer object...
      required:
      - firstName
      - lastName
      properties:
        developerRank:
          enum:
          - JUNIOR_DEVELOPER_1
          - JUNIOR_DEVELOPER_2
          - JUNIOR_DEVELOPER_3
          - SENIOR_DEVELOPER_1
          - SENIOR_DEVELOPER_2
          - SENIOR_DEVELOPER_3
          type: string
        firstName:
          description: First name of the developer
          type: string
        lastName:
          description: Last name of the developer
          type: string

有什么方法可以拥有枚举价值和描述,还是我做错了什么?

您可以使用@schema来指示使用value属性,而不是指定实现类。

例如

@Schema(implementation = Rank::class)
public enum Rank {
    JUNIOR_DEVELOPER_1("junior_developer_1"),
    JUNIOR_DEVELOPER_2("junior_developer_2"),
    JUNIOR_DEVELOPER_3("junior_developer_3"),
    SENIOR_DEVELOPER_1("senior_developer_1"),
    SENIOR_DEVELOPER_2("senior_developer_2"),
    SENIOR_DEVELOPER_3("senior_developer_3");
    private String value;
    Rank(String value) {
        this.value = value;
    }
    :
}

相关内容

  • 没有找到相关文章

最新更新