6

I have a OpenAPI (version 2) specification file and I want to generate a client with openapi-generator-maven-plugin. Unfortunately, it doesn't generate all neccessary classes, therefore the generated sources can not be compiled.

POM

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.1.0</version>
    <executions>
        <execution>
            <id>generate-client</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
                <inputSpec>${project.basedir}/src/main/openapi/api.json</inputSpec>
                <generatorName>java</generatorName>
                <generateApiDocumentation>false</generateApiDocumentation>
                <generateApiTests>false</generateApiTests>
                <generateSupportingFiles>false</generateSupportingFiles>
                <configOptions>
                    <sourceFolder>/</sourceFolder>
                </configOptions>
                <library>resttemplate</library>
            </configuration>
        </execution>
    </executions>
</plugin>

OpenAPI

{
    "swagger": "2.0",
    "info": {
        "title": "Test"
    },
    "host": "localhost:8080",
    "basePath": "/",
    "tags": [
        {
            "name": "test-controller"
        }
    ],
    "paths": {
        "/test": {
            "get": {
                "tags": [
                    "test-controller"
                ],
                "operationId": "test",
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        }
    }
}

Generated sources

In the directory target/generated-sources/openapi only the file TestControllerApi of the sub package api is generated. The class org.openapitools.client.ApiClient is not generated.

directory

Logs

[INFO] --- openapi-generator-maven-plugin:5.1.0:generate (generate-client) @ test ---
[INFO] Generating with dryRun=false
[INFO] No .openapi-generator-ignore file found.
[INFO] OpenAPI Generator: java (client)
[INFO] Generator 'java' is considered stable.
[INFO] Environment variable JAVA_POST_PROCESS_FILE not defined so the Java code may not be properly formatted. To define it, try 'export JAVA_POST_PROCESS_FILE="/usr/local/bin/clang-format -i"' (Linux/Mac)
[INFO] NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[INFO] Processing operation test
[WARNING] 'scheme' not defined in the spec (2.0). Default to [http] for server URL [http://localhost:8080/]
[WARNING] 'scheme' not defined in the spec (2.0). Default to [http] for server URL [http://localhost:8080/]
[ERROR] Missing required field info version. Default appVersion set to 1.0.0
[ERROR] Missing required field info version. Default version set to 1.0.0
[WARNING] 'scheme' not defined in the spec (2.0). Default to [http] for server URL [http://localhost:8080/]
[INFO] writing file D:\tmp\workspace\test\target\generated-sources\openapi\org\openapitools\client\api\TestControllerApi.java
[INFO] Skipped D:\tmp\workspace\test\target\generated-sources\openapi\src\test\java\org\openapitools\client\api\TestControllerApiTest.java (Skipped by apiTests options supplied by user.)
[INFO] Skipped D:\tmp\workspace\test\target\generated-sources\openapi\docs\TestControllerApi.md (Skipped by apiDocs options supplied by user.)
[WARNING] 'scheme' not defined in the spec (2.0). Default to [http] for server URL [http://localhost:8080/]
[INFO] Skipping generation of supporting files.
[...]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 51 source files to D:\tmp\workspace\test\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /D:/tmp/workspace/test/target/generated-sources/openapi/org/openapitools/client/api/TestControllerApi.java:[3,31] cannot find symbol
  symbol:   class ApiClient
  location: package org.openapitools.client

Research

I found a related issue: 10048, but it contains no solution.

Question

How to configure the plugin to generate all neccessary classes?

6
  • Not that this solves your issue but why do you have .../src/main/openapi/api.json? I'd expect a JSON in .../src/main/resources/.... And that's also what's declared at <inputSec>in the usage example on GitHub. Commented Sep 17, 2021 at 22:52
  • @GeroldBroser It is not neccessary to package the JSON file. With this path the JSON is not in the JAR file. Commented Sep 17, 2021 at 22:58
  • jar:jar has an <excludes> property. Commented Sep 17, 2021 at 23:00
  • @GeroldBroser Sure, but it would be an additional configuration in POM.xml. It is easier to use another path. Commented Sep 17, 2021 at 23:01
  • The declarative nature of a POM describes how your project looks like. "Hiding" information in a path in a <configuration> <...property...> of a <build> <plugin>, well, hides the information (at first sight) that your project's artifact doesn't contain this JSON. This might sound puristic but there's also the Standard Directory Layout and Convention Over Configuration. Commented Sep 17, 2021 at 23:35

3 Answers 3

4

generateSupportingFiles should be true.

Sign up to request clarification or add additional context in comments.

1 Comment

I switch off this flag, because I don't want to generate files like: build.gradle, git_push.sh, ... It is not neccessary and it is also in the wrong place under generated sources. That makes only sense if I generate a whole project, but I want to include the client into an existing project.
3

This is what you are missing in your configuration

<supportingFilesToGenerate>
ApiCallback.java,ApiClient.java,ApiException.java,ApiResponse.java,Configuration.java,Pair.java,ProgressRequestBody.java,ProgressResponseBody.java,StringUtil.java,ApiKeyAuth.java,Authentication.java,HttpBasicAuth.java,JSON.java,OAuth.java,EncodingUtils.java
</supportingFilesToGenerate>

1 Comment

They also need to remove <generateSupportingFiles>false</generateSupportingFiles> or change it to true. And I needed to add ApiUtil.java in supportingFilesToGenerate besides the list that you provide.
0

I think java generator does not generate an API class for all the endpoints, instead, it generates an interface for each and every endpoint. Try checking the generated classes again, you may find GetBooksApi or FindCarApi instead of ApiClient. Just tested and it does not, indeed. Check the api.package.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.