gpt4 book ai didi

java - 如何在从 Java 代码生成的 Swagger 规范中创建可重用的枚举?

转载 作者:行者123 更新时间:2023-12-03 19:26:33 25 4
gpt4 key购买 nike

我正在尝试为我的 Java 代码生成 OpenAPI(版本 3.0.1)规范。为了实现这一点,我使用 Swagger Annotations(2.0.8 版)和 Swagger Maven 插件。

我对枚举有问题。说,我有两种方法返回相同的枚举。在 OpenAPI 规范中,我希望此 Enum 和 $ref 具有单个模式定义。两个 API 操作中的链接。但相反,我在每个 API 操作中都重复了 Enum 定义。如何在不手动编辑规范文件的情况下避免这种重复?

这是带有两个方法返回相同 Enum 的 Java 代码:

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/properties")
@Produces("application/json")
public class TestService {
@GET
@Path("/enum1")
@Operation
@ApiResponses({
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = Color.class)))
})
public Color getColor1() {
throw new UnsupportedOperationException();
}

@GET
@Path("/enum2")
@Operation
@ApiResponses({
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = Color.class)))
})
public Color getColor2() {
throw new UnsupportedOperationException();
}

public enum Color {
RED,

GREEN,

BLUE
}
}


这是我想得到的规范:
openapi: 3.0.1
components:
schemas:
Color:
type: string
enum:
- RED
- GREEN
- BLUE
paths:
/properties/enum2:
get:
operationId: getColor2
responses:
200:
content:
application/json:
schema:
$ref: '#/components/schemas/Color'
/properties/enum1:
get:
operationId: getColor1
responses:
200:
content:
application/json:
schema:
$ref: '#/components/schemas/Color'


这是我得到的规范:
openapi: 3.0.1
paths:
/properties/enum2:
get:
operationId: getColor2
responses:
200:
content:
application/json:
schema:
type: string
enum:
- RED
- GREEN
- BLUE
/properties/enum1:
get:
operationId: getColor1
responses:
200:
content:
application/json:
schema:
type: string
enum:
- RED
- GREEN
- BLUE

最佳答案

我在 kotlin,但我添加了(相对较新的)@Schema(enumAsRef = true)到枚举类成功。

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/properties")
@Produces("application/json")
public class TestService {
@GET
@Path("/enum1")
@Operation
@ApiResponses({
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = Color.class)))
})
public Color getColor1() {
throw new UnsupportedOperationException();
}

@GET
@Path("/enum2")
@Operation
@ApiResponses({
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = Color.class)))
})
public Color getColor2() {
throw new UnsupportedOperationException();
}

@Schema(enumAsRef = true) // THIS MAKES ENUM REF
public enum Color {
RED,

GREEN,

BLUE
}
}

关于java - 如何在从 Java 代码生成的 Swagger 规范中创建可重用的枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57079808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com