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.65k stars 6.53k forks source link

How to use addtionalProperties in Open API Generator plugin for maven #15615

Open prabhuaxm opened 1 year ago

prabhuaxm commented 1 year ago

Context

I could not find anything in the docs (here and here) related to using user-defined properties in the mustache template, so opening the issue here.

Description

I am trying to modify the mustache template for webclient in Java. Basic stuff works, like making the ApiClient a Spring component, adding methods, etc. But I want to pass service name in the template. For that, I was looking at passing the service name through pom.xml but there is no such variable for this. So I decided to use additionalProperties but could not find a way that worked.

I created a variable like this

<execution>
    <id>client-webclient-attributes-metadata</id>
    <goals>
      <goal>generate</goal>
    </goals>
    <configuration>
      <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
      <generatorName>java</generatorName>
      <generateModels>false</generateModels>
      <generateApiTests>false</generateApiTests>
      <apiPackage>com.appName.core.services.core.api</apiPackage>
      <modelPackage>com.appName.core.services.core.model.Apimodel</modelPackage>
      <invokerPackage>com.appName.core.services.core.client</invokerPackage>
      <modelNameSuffix>DTO</modelNameSuffix>
      <additionalProperties>
        <serviceName>core-service</property>
      </additionalProperties>
      <library>webclient</library>
      <templateDirectory>${project.basedir}/src/main/resources/openapi-generator/templates/openapi</templateDirectory>
      <configOptions>
        <dateLibrary>java8</dateLibrary>
        <additionalProperties>
          <springAnnotation>true</springAnnotation>
        </additionalProperties>
        <templateName>ApiClient.mustache</templateName>
      </configOptions>
    </configuration>
  </execution>

but the mustache template for ApiClient does not pick up the serviceName property.

Here is how I am using the serviceName property in the mustache template

private static final String serviceName = "{{serviceName}}";

I have tried the following combinations when additionalProperties is under configuration tag and also placed it under configOptions just to get it workin -

This is currently blocking me. As a workaround, I decided to store the service name in packageName tag but I worry if in some upcoming version this is deprecated, then our changes will start to break, or some other use-case for packageName comes up then we might have to scrape all the changes.

Could you please tell how to use user-defined properties in the mustache template?

chibat commented 1 year ago

Please see https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md#:~:text=name%3Dvalue%2Cname%3Dvalue

I think it will probably look like this:

<additionalProperties>serviceName=core-service</additionalProperties>
prabhuaxm commented 1 year ago

@chibat Thanks for the reply! I tried using this but was not able to access the value in the mustache code by doing private static final String serviceName = "{{serviceName}}"; or private static final String serviceName = "{{additionalProperties.serviceName}}";

Can you tell me how to access an additional property?

chibat commented 1 year ago

I believe the following is correct

private static final String serviceName = "{{serviceName}}";
prabhupant commented 1 year ago

@chibat I tried both the ways but didn't work. Can you me point to any documentation where how to use additionalProperties is listed? The table in README does not provide much info

vishalkotecha commented 10 months ago

@prabhupant I can confirm that following works just fine with openapi generator 7.2.0. You can pass multiple parameter with comma as shown below. Just make sure there is no space or double quotes. It should work just fine.


         <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.2.0-SNAPSHOT</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.baseDir}/src/main/resources/openapi.json</inputSpec>
                            <generatorName>java</generatorName>
                            <library>resttemplate</library>
                            <additionalProperties>serviceName=core-service,otherParam=value1</additionalProperties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
der-eismann commented 7 months ago

I stumbled over this as well. In the docs it says

You can also have multiple occurrences of this option

But when I define it multiple times, only the last entry is used. When I join them with a comma like Vishal posted above it's working.