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

如何在由Java代码生成的Swagger规范中创建可重用的枚举?,java,swagger,Java,Swagger,我正在尝试为我的Java代码生成OpenAPI(版本3.0.1)规范。为了实现这一点,我使用了Swagger注释(版本2.0.8)和Swagger Maven插件 我对枚举有问题。比如说,我有两个方法返回相同的枚举。在OpenAPI规范中,我希望在这两个API操作中都有这个枚举和$ref链接的单一模式定义。但我在每个API操作中都复制了枚举定义。在不手动编辑规范文件的情况下,如何避免这种重复 以下是Java代码,其中两个方法返回相同的枚举: import io.swagger.v3.oas.an

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

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

以下是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
    }
}

以下是我想要得到的规格:

以下是我得到的规格:


我在kotlin,但我成功地将(相对较新的)
@Schema(enumAsRef=true)
添加到了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();
    }

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

        GREEN,

        BLUE
    }
}

问:你考虑过使用裁判吗?问题是我不喜欢手动编写Swagger文件。我想让它自动生成从我的代码。实际上,
$ref
是我所需要的,但我想通过招摇过市的Java注释来表达它。如果$ref是您所需要的,那么$ref就是您应该使用的:)看看注释示例,您应该能够非常容易地适应。请把你学到的东西发回来。对不起,我不够准确。是的,
$ref
是我需要的。但是,我还需要一件事——这个
$ref
将引用的模式定义。谢谢你的链接,但是有一点不同的问题。它是关于如何引用(使用
$ref
)现有模式的。对我来说,我需要生成这个模式(不是在操作定义内部,而是以可重用的方式)。你成功地做到了吗?
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

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
    }
}