CoderJammer
CoderJammer

Reputation: 715

springdoc-openapi-generator template customization

I'm using SpringBoot 3.4.1, Java 21.0.5 and openapi-generator-maven-plugin ver 7.10.0. I want to customize the Spring (server) generator template and, to do that, I installing the openapi-cli following the official docs here.

Therefore to grab the template I have typed the follow command:

openapi-generator-cli author template -g spring --library spring-boot -o mytemplates

the above command download the template under mytemplates folder. I have two questions:

  1. Inside this I found only *.mustache file and no *.class or *.java file. Is it correct?
  2. Can I add my custom option inside maven build configuration and referencing it inside a .mustache file? *¹

*¹ Example

Suppose this is my openapi-generator-maven-plugin configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>7.10.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/api-docs.yaml</inputSpec>
                        <templateResourcePath>${project.basedir}/src/templates/mytemplates</templateResourcePath>
                        <output>${project.build.directory}/generated-sources</output>
                        <generatorName>spring</generatorName>
                        <apiPackage>resources</apiPackage>
                        <modelPackage>model</modelPackage>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <myCustomProperty>true</myCustomProperty> <!-- my property -->
                        <configOptions>
                            <!-- options -->
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

If I define a <myCustomProperty> inside maven build config (as above) and reference it in a *.mustache file this way:

{{#myCustomProperty}}
  .. do some if myProperty is true!
{{/myCustomProperty}}

will it work?


No It doesn't!

I tried writing my own option <useLombok> this way:

<configuration>
   <inputSpec>${project.basedir}/src/main/resources/api-docs.yaml</inputSpec>
   <output>${project.build.directory}/generated-sources</output>
   <generatorName>spring</generatorName>
   ....
   ....
   <templateResourcePath>${project.basedir}/src/templates</templateResourcePath>
   <configOptions>
     <useLombok>true</useLombok> <!-- my option -->
   </configOptions>
</configuration>

and this way:

<configuration>
   <inputSpec>${project.basedir}/src/main/resources/api-docs.yaml</inputSpec>
   <output>${project.build.directory}/generated-sources</output>
   <generatorName>spring</generatorName>
   <useLombok>true</useLombok> <!-- my option -->
   ....
   <templateResourcePath>${project.basedir}/src/templates</templateResourcePath>
   <configOptions>
     ....
   </configOptions>
</configuration>

and in my pojo.mustache I wrote:

{{#useLombok}}
  @lombok.Getter
{{/useLombok}}

but nothing happened! I'm pretty sure that the custom template is read correctly beacuse if I write something without condition I can see it on my model classes after the generation.

Did I do something wrong or is not possible realizing this?

Upvotes: 0

Views: 127

Answers (1)

d0x
d0x

Reputation: 11

Yes, it will. Your custom properties get passed to the mustache template so you can reference it there too.

Sorry about the short answer I gotta go somewhere but I'll try to remember to fill it out a bit if you want & nobody else has by then.

Upvotes: 1

Related Questions