Как я могу объявить тип массива ответов для определения OpenAPI с JAX-RS? - PullRequest
0 голосов
/ 04 марта 2019

Я создаю REST-сервис с JAX-RS, Microprofile и Payara 5. Мой метод возвращает объект типа Response.Сам ответ содержит список MyClass.Реализация выглядит следующим образом:

import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;

@GET
@Path("/{a}/{b}/{c}")
@APIResponse(content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = MyClass.class)))
public Response getMyClass(@PathParam("a") String a,
                           @PathParam("b") String b,
                           @PathParam("c") String c) {
    return Response
            .ok()
            .entity(new ArrayList<>())
            .build();
}

Сгенерированное определение OpenAPI выглядит следующим образом:

/api/translations/{a}/{b}/{c}:
  get:
    operationId: getMyClass
    parameters:
    - name: a
      in: path
      required: true
      style: simple
      schema:
        type: string
    - [...]
    responses:
      default:
        description: Default Response.
        content:
          '*/*':
            schema:
              type: array
              items: {}

Как видите, определение MyClass.class отсутствует в типе ответа.Как я могу добавить этот тип в определение?Является ли аннотация @ApiResponse правильным способом для достижения этой цели?

1 Ответ

0 голосов
/ 10 марта 2019

Я протестировал это сегодня с новейшей Payara 5.191, и это не сработало для меня тоже.Кажется, что в текущей реализации Payara есть ошибка, потому что я проверил пример на этой странице guide-microprofile-openapi

Одна и та же реализация имеет 2 разных поколения openapi (Payara и OpenLiberty)

Payara:

openapi: 3.0.0
info:
  title: Deployed Resources
  version: 1.0.0
servers:
- url: https://10.0.0.72:8080/ipma
  description: Default Server.
paths:
  /resources/server:
    get:
      summary: List servers.
      description: 'Returns all servers '
      operationId: getServers
      responses:
        default:
          description: Special description
          content:
            application/json:
              schema:
                type: array
  /resources/server/{id}:
    get:
      summary: get server by id.
      description: 'return one server with the specified id'
      operationId: getServerById
      parameters:
      - name: id
        in: query
        style: simple
        schema:
          type: number
      responses:
        default:
          description: Special description
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Server'
components:
  schemas:
    Server:
      properties:
        name:
          type: string
          example: test
        id:
          type: number
          example: "0"
      description: foo

OpenLiberty:

openapi: 3.0.0
info:
  title: Deployed APIs
  version: 1.0.0
servers:
- url: http://localhost:9080
paths:
  /resources/server/{id}:
    get:
      summary: get server by id.
      description: 'return one server with the specified id'
      operationId: getServerById
      parameters:
      - name: id
        in: query
        schema:
          type: integer
          format: int64
      responses:
        default:
          description: Special description
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Server'
  /resources/server:
    get:
      summary: List servers.
      description: 'Returns all servers '
      operationId: getServers
      responses:
        default:
          description: Special description
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Server'
components:
  schemas:
    Server:
      required:
      - id
      - name
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 0
        name:
          type: string
          example: test
      description: foo
...