OpenAPITools / OpenAPITools/openapi-generator
How to use addtionalProperties in Open API Generator plugin for maven
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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 -
- private static final String serviceName = "{{serviceName}}";
- private static final String serviceName = "{{additionalProperties.serviceName}}";
- private static final String serviceName = "{{configOptions.serviceName}}";
- private static final String serviceName = "{{configOptions.additionalProperties.serviceName}}";
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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the OpenAPI Generator Maven plugin configuration and the custom ApiClient.mustache template described in the issue, then compare the plugin documentation and Java generator documentation linked there. Verify how user-defined additionalProperties are passed into templates and document the supported configuration and template syntax, with an example that renders serviceName.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100