我正在从带注释的Java代码中生成OpenAPI 3.0文档。但是问题是,当我添加@Schema批注进行枚举时,所有值都消失了。我正在使用Thorntail 2.3.0.Final和microprofile-openapi分数。
我知道我只能更改.yaml文件,但是我需要直接从Java代码生成yaml。
这是我在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
但是,如果我在Developer类中的枚举之前删除@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
是否可以同时拥有枚举值和描述,或者我做错了什么?
答案 0 :(得分:1)
您可以通过指定实现类,将@Schema用于枚举,以指示它使用value属性而不是name属性。
例如
@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;
}
:
}