OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][spring] array/list missing in generated @ApiResponse and example

Open
#13,332 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Generated method with response/return of type array is missing the array and instead only using the wrapped object. The following locations are affected:

@ApiResponse

expected:

@ApiResponse(responseCode = "200", description = "Get an array/list of objects", content = {
    @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = ReturnArrayGet200ResponseInner.class)))
})

actual:

@ApiResponse(responseCode = "200", description = "Get an array/list of objects", content = {
    @Content(mediaType = "application/json", schema = @Schema(implementation = ReturnArrayGet200ResponseInner.class))
})
exampleString

expected:

String exampleString = "[{ \"property2\" : \"property2\", \"property1\" : 0 }]";

actual:

String exampleString = "{ \"property2\" : \"property2\", \"property1\" : 0 }";
additional notes
  • the actual return type is correctly wrapped in a List: List<ReturnArrayGet200ResponseInner>
  • generation via https://editor.swagger.io/ doesn't have the problem
openapi-generator version

v6.0.1

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Demo
  description: Demo for Bugreport
  version: 1.0.0

paths:
  /return-array:
    get:
      responses:
        '200':
          description: 'Get an array/list of objects'
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    property1:
                      type: integer
                      format: int64
                    property2:
                      type: string
                      maxLength: 255
Generation Details

Generated via openapi-generator-maven-plugin:

<plugin>
	<groupId>org.openapitools</groupId>
	<artifactId>openapi-generator-maven-plugin</artifactId>
	<version>6.0.1</version>
	<executions>
		<execution>
			<id>generate-test</id>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<inputSpec>${project.basedir}/api/test.yml</inputSpec>
				<generatorName>spring</generatorName>
				<library>spring-boot</library>
				<output>${project.build.directory}/generated-sources/openapi/test</output>
			</configuration>
		</execution>
	</executions>
</plugin>
Steps to reproduce
  1. create a new maven project
  2. place the provided test.yml into its api directory
  3. add the provided plugin configuration to your pom.xml
  4. generate the API, e.g.: mvn clean package
  5. check the generated ReturnArrayApi.java
Suggest a fix

The Mustache files responsible for the marked locations might ignore the array type or detect it incorrectly.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided test.yml and openapi-generator-maven-plugin configuration, then inspect the generated ReturnArrayApi.java and the responsible Mustache files. Done means the generated @ApiResponse and exampleString preserve the array shape while the return type remains List.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.