OpenAPITools / OpenAPITools/openapi-generator

[BUG] [SPRING] ApiUtil does not respect generateSupportingFiles flag

Open
#4,411 6 comments 0 reactions 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

Description
ApiUtil should not be used when generateSupportingFiles=false

Generator config

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>4.2.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                <output>${project.build.directory}/generated-sources/openapi</output>
                <generatorName>spring</generatorName>
                <generateSupportingFiles>false</generateSupportingFiles>
                <configOptions>
                    <library>spring-boot</library>
                    <java8>true</java8>
                    <interfaceOnly>true</interfaceOnly>
                    <delegatePattern>true</delegatePattern>
                    <useOptional>true</useOptional>
                    <unhandledException>true</unhandledException>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

Sample Spec

openapi: 3.0.2
info:
  title: mysample
  version: '1.0'
servers:
  - url: 'http://localhost:3000'
paths:
  /users:add:
    post:
      tags:
        - user
      summary: 'Create User'
      operationId: createUser
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
        'name':
          type: string

Actual output for delegate method

    default  ResponseEntity<List<User>> createUser(User user) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    ApiUtil.setExampleResponse(request, "application/json", "{  \"name\" : \"name\",  \"id\" : \"id\"}");
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

Expected output for delegate method

    default  ResponseEntity<List<User>> createUser(User user) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }

Suggested fix in
https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/main/resources/JavaSpring/methodBody.mustache

The examples section in methodBody.mustache should probably check for generateSupportingFiles=false.

Also, note that the response in the OpenAPI Spec provided. It doesn't define any examples so not sure why it's getting into the examples block. It seems to just use the User Schema if an example is not defined, which seems like another bug.

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

Start with modules/openapi-generator/src/main/resources/JavaSpring/methodBody.mustache and inspect the examples section's handling of generateSupportingFiles=false. Run the Spring generator using the Maven configuration and sample OpenAPI spec from the issue, then verify that the generated delegate method omits the ApiUtil example-response block when supporting files are disabled. The separate behavior of generating an example from the User schema should be assessed independently.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.