【问题标题】:@BeanParam not documented when annotated with @Parameter使用 @Parameter 注释时未记录 @BeanParam
【发布时间】:2020-02-26 16:24:46
【问题描述】:

我正在尝试使用 swagger-maven-plugin 记录我的 api。

当我用@Parameter 注释一个路由参数时,只要它没有用@BeanParam 注释,它就会在openapi 生成的文件中得到很好的记录。

正如swagger核心documentation中所说,

@Parameter 可以用来代替 JAX-RS 参数注释(@PathParam、@QueryParam、@HeaderParam、@FormParam 和 @BeanParam),也可以与它们一起使用。虽然 swagger-core / swagger-jaxrs2 默认扫描这些注解,但 @Parameter 允许为参数定义更多细节。

如何将我的 @BeanParam 参数记录在我的 openapi 文件中?

这是我的pom.xml

<dependencies>
    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-jaxrs2</artifactId>
        <version>2.1.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <outputFileName>openapi</outputFileName>
                <outputPath>target/doc</outputPath>
                <outputFormat>YAML</outputFormat>
                <resourcePackages>...</resourcePackages>
                <readAllResources>false</readAllResources>
            </configuration>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>resolve</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这里是我带注释的 api:

@GET
@Path("/authorize")
@Operation(summary = "Summary", description = "Description")
Response authorize(
        // Parameter doesn't show up in generated documentation
        @Parameter(
                description = "Request",
                name = "request",
                required = true
        )
        @BeanParam Oauth2AuthorizationRequest request,
        // Parameter shows up correctly in generated documentation
        @Parameter(
                description = "Session id",
                name = "sessionId",
                required = true
        )
        @CookieParam("sessionId") String sessionId
);

生成的文件是:

openapi: 3.0.1
paths:
  /auth/oauth2/authorize:
    get:
      summary: Summary
      description: Description
      operationId: authorize
      parameters:
      - name: sessionId
        in: cookie
        description: Session id
        required: true
        schema:
          type: string

【问题讨论】:

    标签: java swagger jax-rs


    【解决方案1】:

    经过一些试验,我设法在 openapi 文件中记录了我的 @BeanParam。注释必须放在注释为@BeanParam 的类中,如下所示:

    public class Oauth2AuthorizationRequest {
        // Use the @Parameter annotation to document the attribute.
        @HeaderParam("Authorization")
        @Parameter(description = "Authorization header")
        String authorization;
    
        // If the attribute is a @FormParam, use the @Schema annotation.
        @FormParam("client_id")
        @Schema(description = "The id of the client")
        String client_id;
    
        // If the attribute is a @FormParam and is required, 
        // use the @Schema annotation for the description
        // and the @Parameter one to set it as required.
        @FormParam("grant_type")
        @Schema(description = "Should be either \"credentials\" or \"password\"")
        @Parameter(required = true)
        String grant_type;
    }
    

    并且端点被简化为:

    @GET
    @Path("/authorize")
    @Operation(summary = "Summary", description = "Description")
    Response authorize(
        // No more annotation on the @BeanParam
        @BeanParam Oauth2AuthorizationRequest request,
        @Parameter(
            description = "Session id",
            name = "sessionId",
            required = true
        )
        @CookieParam("sessionId") String sessionId
    );
    

    【讨论】:

    • 可以反过来吗?有一个 openapi yml 文件生成一个带有 beanparam 的接口吗?
    • 看看swagger codegen。我尚未对其进行测试,但看起来就像您要找的一样。
    • 我知道。我只是还没有找到使用@beanparam 而不是单独的参数生成接口的方法
    • @Baboo_ 你明白为什么只有在 BeanParam 的情况下才需要自己用大张旗鼓的注释来注释对象的 (Oauth2AuthorizationRequest) 参数吗?我的意思是,如果不是 BeanParam,那么 swagger 会自动读取验证注释,因此您可以使用 NotNull 注释代替您使用的 Parameter(requires = true)。
    • 我不太确定为什么。一个 BeanParam 是一组参数,我认为 swagger 用简单的参数来区分。如果你知道为什么我很想知道