Skip to content

[JAVA][SPRING] Arrays are not rendered correctly in responses #14178

@superplan

Description

@superplan
Description

Objects of type "array" are not rendered correctly in responses, see example below. The UI shows only an item of the array, which is misleading.

openapi-generator version

6.2.1

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  version: 1.0.0
  title: See, thats an array!
  description: An example to demonstrate how array responses are rendered in Swagger-UI.
paths:
  /free-donuts:
    get:
      description: Process data
      operationId: getDonuts
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Donut'
components:
  schemas:
    Donut:
      type: object
      required:
        - menuName
      properties:
        name:
          type: string
          description: key
        glazed:
          type: boolean
          description: value
Command line used for generation

java -jar openapi-generator-cli-6.2.1.jar generate -g spring -i input.yaml -o "spring_6.2.1" -c config.yaml

Here is my config.yaml:

additionalProperties:
  useTags: true
  java8: true
  useOptional: true
  useBeanValidation: true
  delegatePattern: true

Produces:
image

Expect:
image

Steps to reproduce

Generate spring-Server und run as java application. Schema for the response code 200 is "Donut" and not an array of "Donut".

Related issues/PRs
Suggest a fix/enhancement

Incorporate @ArraySchema into api.mustache. Here the fix is code enclosed by tag "{{#isArray}}":

...
import io.swagger.v3.oas.annotations.media.ArraySchema;
...
        responses = {
            {{#responses}}
            @ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{#baseType}}, content = {
                {{#produces}}
                @Content(mediaType = "{{{mediaType}}}", {{#isArray}}array = @ArraySchema( {{/isArray}}schema = @Schema(implementation = {{{baseType}}}.class)){{#isArray}}){{/isArray}}{{^-last}},{{/-last}}
                {{/produces}}
            }{{/baseType}}){{^-last}},{{/-last}}
            {{/responses}}
        }

Now the DefaultApi.java should look like this:

...
    @ApiResponses(value = { 
        @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
        		mediaType = "application/json", 
        		array = @ArraySchema(schema = @Schema(implementation = DonutBox.class))
        		))
        })
...

Instead of:

...
    @ApiResponses(value = { 
        @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
        		mediaType = "application/json", 
        		schema = @Schema(implementation = DonutBox.class)
        		))
        })
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions