lan*_*sto 7 java swagger openapi
我正在尝试为我的 Java 代码生成 OpenAPI(版本 3.0.1)规范。为了实现这一点,我使用 Swagger Annotations(版本 2.0.8)和 Swagger Maven 插件。
我对枚举有疑问。比如说,我有两个方法返回完全相同的枚举。在 OpenAPI 规范中,我希望有这个 Enum 的单一模式定义,并且$ref在两个 API 操作中都有此枚举和链接的单一架构定义。但我在每个 API 操作中都有重复的 Enum 定义。如何在不手动编辑规范文件的情况下避免这种重复?
下面是 Java 代码,其中两个方法返回相同的 Enum:
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
}
}
Run Code Online (Sandbox Code Playgroud)
这是我想获得的规格:
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'
Run Code Online (Sandbox Code Playgroud)
这是我得到的规范:
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
Run Code Online (Sandbox Code Playgroud)
我在 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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5109 次 |
| 最近记录: |