@BeanParam не документируется, когда аннотируется @Parameter - PullRequest
0 голосов
/ 26 февраля 2020

Я пытаюсь документировать свой API, используя swagger-maven-plugin.

Когда я аннотирую параметр маршрута с помощью @Parameter, он хорошо документирован в сгенерированном файле openapi, если он не помечен @BeanParam.

Как указано в ядре сваггера в документации ,

@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

1 Ответ

0 голосов
/ 27 февраля 2020

После некоторых испытаний мне удалось документировать мой @BeanParam в файле openapi. Аннотации должны быть помещены внутри аннотированного класса @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
);
...