Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add userDefinedTemplateFiles option to maven generator plugin #20490

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 85 additions & 74 deletions modules/openapi-generator-maven-plugin/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "templateResourcePath", property = "openapi.generator.maven.plugin.templateResourcePath")
private String templateResourcePath;

/**
* Specifies userDefinedTemplates relative to templateDir, see Customizing for more info
*/
@Parameter(name = "userDefinedTemplateFiles")
private List<String> userDefinedTemplateFiles;

/**
* The name of templating engine to use, "mustache" (default) or "handlebars" (beta)
*/
Expand Down Expand Up @@ -723,6 +729,7 @@ public void execute() throws MojoExecutionException {
configurator.setApiNameSuffix(apiNameSuffix);
}


if (null != templateDirectory) {
configurator.setTemplateDir(templateDirectory.getAbsolutePath());
}
Expand Down Expand Up @@ -937,6 +944,10 @@ public void execute() throws MojoExecutionException {
}
}

if(userDefinedTemplateFiles != null && userDefinedTemplateFiles.size() > 0) {
applyUserDefinedTemplateFilesKvpList(userDefinedTemplateFiles, configurator);
}

final ClientOptInput input = configurator.toClientOptInput();
final CodegenConfig config = input.getConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.aether.repository.LocalRepository;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -255,6 +256,18 @@ public void test_skipIfSpecIsUnchanged_recognizesUpdatesInExternalReferencedFile
assertTrue("Src directory should have been regenerated", Files.exists(generatedDir.resolve("src")));
}

public void test_userTemplateFiles() throws Exception {

final Path tempDir = newTempFolder();
final CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/user-defined-templates", null);

mojo.execute();

assertTrue(Files.exists(tempDir.resolve(
"target/generated-sources/common-maven/remote-openapi/src/main/java/remote/org/openapitools/client/model/CategoryCustom.java"
)));
}

protected CodeGenMojo loadMojo(Path temporaryFolder, String projectRoot, String profile) throws Exception {
return loadMojo(temporaryFolder, projectRoot, profile, "default");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
</dependency>
</dependencies>
</profile>
<profile>
<id>user-templates</id>
<properties>
<inputSpec>${basedir}/src/main/resources/petstore.yaml</inputSpec>
</properties>
</profile>
</profiles>
<build>
<finalName>common-maven</finalName>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
servers:
- url: "http://localhost:8080"
openapi: "3.0.0"
paths:
/pet:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1pet"
/user/{username}:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1user~1{username}"
/pet/findByStatus:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1pet~1findByStatus"
/user/createWithList:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1user~1createWithList"
/pet/{petId}/uploadImage:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1pet~1{petId}~1uploadImage"
/store/inventory:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1store~1inventory"
/user/login:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1user~1login"
/user:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1user"
/user/createWithArray:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1user~1createWithArray"
/pet/findByTags:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1pet~1findByTags"
/store/order:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1store~1order"
/user/logout:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1user~1logout"
/pet/{petId}:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1pet~1{petId}"
/store/order/{orderId}:
$ref: "./src\\main\\resources\\petstore.yaml#/paths/~1store~1order~1{orderId}"
info:
title: "merged spec"
description: "merged spec"
version: "1.0.0"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<version>1</version>
<description>POM was created from install:install-file</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<versioning>
<release>1</release>
<versions>
<version>1</version>
</versions>
<lastUpdated>20210724220722</lastUpdated>
</versioning>
</metadata>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!--
~ Copyright 2020, 2021 OpenAPI-Generator Contributors (https://openapi-generator.tech)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>common.maven</groupId>
<artifactId>common-maven</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>OpenAPI Generator Configuration Test</name>
<url>https://openapi-generator.tech/</url>
<build>
<finalName>common-maven</finalName>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<configuration>
<inputSpec>${basedir}/src/main/resources/petstore.yaml</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<dateLibrary>joda</dateLibrary>
</configOptions>
<apiNameSuffix>Suffix</apiNameSuffix>
<library>jersey2</library>
<output>${basedir}/target/generated-sources/common-maven/remote-openapi</output>
<apiPackage>remote.org.openapitools.client.api</apiPackage>
<modelPackage>remote.org.openapitools.client.model</modelPackage>
<invokerPackage>remote.org.openapitools.client</invokerPackage>
<collapsedSpec>petstore-full-spec</collapsedSpec>
<includeCollapsedSpecInArtifacts>true</includeCollapsedSpecInArtifacts>
<templateDirectory>${basedir}/templates</templateDirectory>
<userDefinedTemplateFiles>
<userDefinedTemplateFile>templateFile=modeltest.mustache,templateType=models,destinationFilename=Custom.java</userDefinedTemplateFile>
</userDefinedTemplateFiles>
</configuration>
<executions>
<execution>
<id>executionId</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<settings>
<activeProfiles>
<activeProfile>default</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>default</id>
<localRepository>${basedir}/local-repo</localRepository>
</profile>
</profiles>
</settings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;

public class Example {
public static void main(String[] args) {
System.out.println("You did it!");
}
}
Loading