OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.87k stars 6.59k forks source link

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

Open dkirrane opened 5 years ago

dkirrane commented 5 years ago

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.

admoca60 commented 3 years ago

Any news about it? I'm blocking for that.

Could anyone add any workaround?

Thank you!

admoca60 commented 3 years ago

I could see that if you use the configOption "true" you wont be that problem.

flemming-n-larsen commented 2 years ago

It seems to work with current version 5.3.0 of the eopenapi-generator-maven-plugin when setting <generateSupportingFiles>false</generateSupportingFiles> in the <configuration> section. šŸ‘

So I wonder why this issue is still open?

fnl-trackunit commented 2 years ago

It seems to work with current version 5.3.0 of the eopenapi-generator-maven-plugin when setting <generateSupportingFiles>false</generateSupportingFiles> in the <configuration> section. šŸ‘

So I wonder why this issue is still open?

Sorry. I see it still generates a DefaultAPI using the ApiUtil class, even when I managed to omit to generate it by setting <generateSupportingFiles> to false.

jspetrak commented 2 years ago

I use 5.4.0 in Gradle and generateSupportingFiles does not have affect.

AlekseyZakharov94 commented 1 year ago

I use 5.4.0 in Gradle and generateSupportingFiles does not have affect.

In order not to generate example response try to use <skipOperationExample>true</skipOperationExample>