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.45k stars 6.49k forks source link

[BUG][JAVA] Missing Api interface while using apisToGenerate Tag #14906

Open tobiashartmann83 opened 1 year ago

tobiashartmann83 commented 1 year ago

Bug Report Checklist

Description

I have a very complex openApi file with 17 paths and only have to use a 3 of them. I would like only to generate these 3 endpoints in the Api Interface and related model/DTO classes. I found the xml<apisToGenerate> and xml<modelsToGenerate> Tags. But when I use these tags there is no interface class generated anymore. I used the pets example in of the repository and modified it a little bit and get the same behavior as my 2000 loc openApi.yml file.

openapi-generator version

Version: 6.0.1 is in use, but I also tried Version 6.4.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: '1'
  title: 'ServiceAPI'

paths:
  /v1/dogs:
    post:
      summary: Create a dog
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /v1/cats:
    post:
      summary: Create a cat
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Dog:
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Dogs:
      type: array
      items:
        $ref: "#/components/schemas/Dog"
    Cat:
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Cats:
      type: array
      items:
        $ref: "#/components/schemas/Cat"
    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
Generation Details

Pom.xml

<plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.4.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/../../documentation/contracts/openapi/dummy.openapi.yml</inputSpec>
                            <generatorName>jaxrs-spec</generatorName>
                            <output>${project.basedir}</output>
                            <apisToGenerate>v1dogs</apisToGenerate>
                            <modelsToGenerate>Dog,Dogs,Error</modelsToGenerate>
                            <configOptions>
                                <sourceFolder>src/main/java</sourceFolder>
                                <dateLibrary>java8</dateLibrary>
                                <useSwaggerAnnotations>false</useSwaggerAnnotations>
                                <supportAsync>true</supportAsync>
                                <!--ensure we only generate java interfaces and no implementation skeleton-->
                                <interfaceOnly>true</interfaceOnly>
                                <returnResponse>true</returnResponse>
                                <hideGenerationTimestam>true</hideGenerationTimestam>
                                <generateBuilders>true</generateBuilders>
                                <delegatePattern>true</delegatePattern>
                            </configOptions>
                            <apiPackage>de.dummy.generated.notifier.rest</apiPackage>
                            <modelPackage>de.dummy.generated.notifier.rest.domain</modelPackage>
                            <generateModelTests>true</generateModelTests>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <generateSupportingFiles>false</generateSupportingFiles>
                            <generateApiTests>false</generateApiTests>
                            <generateApiDocumentation>false</generateApiDocumentation>
                            <auth>false</auth>
                            <library>quarkus</library>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce
  1. added the openApi.yml
  2. configured the plugin in the pom.xml
  3. executed code generation

Without the configuration of

<apisToGenerate>v1dogs</apisToGenerate>
<modelsToGenerate>Dog,Dogs,Error</modelsToGenerate>

I get the expected result of image But with the configuration I get only the classes for models that I congfigured image the interface file i missing.

Related issues/PRs
Suggest a fix

Maybe it is just a misconfiguration, but I don't see it and i can't find any solution. Or it is a Bug in the Plugin.

arburk commented 1 year ago

Try to align your tag name in configuration with your yaml file: e.g. <apisToGenerate>Pets</apisToGenerate> Please be aware of title-cased tag names due to https://github.com/OpenAPITools/openapi-generator/issues/5591

robp94 commented 1 year ago

I have the same problem, the code does only look for the basename. So it is only possible to filter for v1. Which is more or less useless, in my opinion, if you have a versioned API.

The corresponding code is in org.openapitools.codegen.DefaultGenerator#generateApis

        Map<String, List<CodegenOperation>> paths = processPaths(this.openAPI.getPaths());
        Set<String> apisToGenerate = null;
        String apiNames = GlobalSettings.getProperty("apis");
        if (apiNames != null && !apiNames.isEmpty()) {
            apisToGenerate = new HashSet<>(Arrays.asList(apiNames.split(",")));
        }
        if (apisToGenerate != null && !apisToGenerate.isEmpty()) {
            Map<String, List<CodegenOperation>> updatedPaths = new TreeMap<>();
            for (String m : paths.keySet()) {
                if (apisToGenerate.contains(m)) {
                    updatedPaths.put(m, paths.get(m));
                }
            }
            paths = updatedPaths;
        }

Unfortunately, this seems not be easily fixable. One option could be to filter in the first line this.openAPI.getPaths() This returns a map with the full path like /v1/dogs. Then maybe only interpret the filter if it contains at least one \

TheHandOfNOD commented 3 months ago

Try to align your tag name in configuration with your yaml file: e.g. <apisToGenerate>Pets</apisToGenerate> Please be aware of title-cased tag names due to #5591

that solved it for me. Thx