Skip to content

Rienafairefr's templating branch updated to current master #2657

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

Merged
merged 26 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
652358b
openapi-generator-api
Aug 17, 2018
119b662
templating engine adapters
Aug 17, 2018
f2e9184
-e templating engine CLI option
Aug 17, 2018
4699c64
adapt Generator to process Template with the passed TemplatingEngineA…
Aug 17, 2018
30cb6c4
add a MustacheEngineAdapter to the codegen in the unit tests
Aug 19, 2018
1d0a1af
force default MustacheEngineAdapter
Aug 19, 2018
30fbd73
copy api module in the root Dockerfile
Aug 19, 2018
585ec3b
rename the interfaces module to openapi-generator-core
Aug 20, 2018
148f043
rename getFullTemplate to getFullTemplateContents
Aug 20, 2018
7da8cc8
reinstate the processCompiler step in case of a MustacheEngineAdapter
Aug 20, 2018
3c1f18c
add processTemplatingEngine to CodegenConfig, to be overriden by Code…
Aug 20, 2018
387ba9d
multiple file extensions per templating engine adapter
Aug 20, 2018
8093822
docs touchup
Aug 22, 2018
1732281
Rename template engine method to compileTemplate
jimschubert Mar 9, 2019
96dbb43
Initialize config-customized mustache compiler only once
jimschubert Mar 9, 2019
765a539
Extend handlebars experimental adapter with more explicit contextual …
jimschubert Mar 17, 2019
da803dc
Merge branch 'master' into rienafairefr-templating
jimschubert Apr 2, 2019
f95715f
Use Locale.ROOT in String.format
jimschubert Apr 2, 2019
fb2346c
Merge branch 'master' into rienafairefr-templating
jimschubert Apr 13, 2019
9f6be28
Add new openapi-generator-core/pom.xml to release_version_update.sh
jimschubert Apr 13, 2019
d32f683
Log a message on missing handlebars helper
jimschubert Apr 17, 2019
2b901ad
Add clarification around template default and beta options
jimschubert Apr 17, 2019
b5f2c2a
Move mustache package under new templating package
jimschubert Apr 17, 2019
9351529
Include built-in handlebars helpers which require explicit registrati…
jimschubert Apr 17, 2019
edb4dce
Locale.ROOT in toLowerCase
jimschubert Apr 17, 2019
553f4ff
Merge branch 'master' into rienafairefr-templating
jimschubert Apr 25, 2019
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
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ COPY ./modules/openapi-generator-gradle-plugin ${GEN_DIR}/modules/openapi-genera
COPY ./modules/openapi-generator-maven-plugin ${GEN_DIR}/modules/openapi-generator-maven-plugin
COPY ./modules/openapi-generator-online ${GEN_DIR}/modules/openapi-generator-online
COPY ./modules/openapi-generator-cli ${GEN_DIR}/modules/openapi-generator-cli
COPY ./modules/openapi-generator-core ${GEN_DIR}/modules/openapi-generator-core
COPY ./modules/openapi-generator ${GEN_DIR}/modules/openapi-generator
COPY ./pom.xml ${GEN_DIR}

Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,12 @@ SYNOPSIS
[--artifact-version <artifact version>]
[(-c <configuration file> | --config <configuration file>)]
[-D <system properties>...]
[(-e <templating engine> | --engine <templating engine>)]
[--enable-post-process-file]
[(-g <generator name> | --generator-name <generator name>)]
[--git-repo-id <git repo id>] [--git-user-id <git user id>]
[--group-id <group id>] [--http-user-agent <http user agent>]
[--generate-alias-as-model] [--git-repo-id <git repo id>]
[--git-user-id <git user id>] [--group-id <group id>]
[--http-user-agent <http user agent>]
(-i <spec file> | --input-spec <spec file>)
[--ignore-file-override <ignore file override location>]
[--import-mappings <import mappings>...]
Expand Down Expand Up @@ -584,6 +587,9 @@ OpenAPI Generator core team members are contributors who have been making signif
:heart: = Link to support the contributor directly

#### Template Creator

**NOTE**: Embedded templates are only supported in _Mustache_ format. Support for all other formats is experimental and subject to change at any time.

Here is a list of template creators:
* API Clients:
* Ada: @stcarrez
Expand Down
1 change: 1 addition & 0 deletions bin/utils/release_version_update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ echo "Release preparation: replacing $FROM with $TO in different files"
declare -a files=("modules/openapi-generator-cli/pom.xml"
"modules/openapi-generator-gradle-plugin/gradle.properties"
"modules/openapi-generator-gradle-plugin/pom.xml"
"modules/openapi-generator-core/pom.xml"
"modules/openapi-generator-maven-plugin/pom.xml"
"modules/openapi-generator-online/pom.xml"
"modules/openapi-generator/pom.xml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public class Generate implements Runnable {
description = "folder containing the template files")
private String templateDir;

@Option(name = {"-e", "--engine"}, title = "templating engine",
description = "templating engine: \"mustache\" (default) or \"handlebars\" (beta)")
private String templatingEngine;

@Option(
name = {"-a", "--auth"},
title = "authorization",
Expand Down Expand Up @@ -284,6 +288,10 @@ public void run() {
configurator.setTemplateDir(templateDir);
}

if (isNotEmpty(templatingEngine)) {
configurator.setTemplatingEngineName(templatingEngine);
}

if (isNotEmpty(apiPackage)) {
configurator.setApiPackage(apiPackage);
}
Expand Down
16 changes: 16 additions & 0 deletions modules/openapi-generator-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>openapi-generator-project</artifactId>
<groupId>org.openapitools</groupId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>openapi-generator-core</artifactId>
<name>openapi-generator-core</name>
<url>https://github.com/openapitools/openapi-generator</url>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.openapitools.codegen.api;

import java.util.Locale;

/**
* Provides abstractions around the template engine adapter interface, for reuse by implementers.
*/
public abstract class AbstractTemplatingEngineAdapter implements TemplatingEngineAdapter {

/**
* Gets all possible template paths for a given location.
*
* @param location The full location of the template.
*
* @return A new array of locations, modified according to the extensions or other adapter rules.
*/
protected String[] getModifiedFileLocation(String location) {
String[] extensions = getFileExtensions();
String[] result = new String[extensions.length];
for (int i = 0; i < extensions.length; i++) {
String extension = extensions[i];
result[i] = String.format(Locale.ROOT, "%s.%s", getPathWithoutExtension(location), extension);
}
return result;
}

/**
* Returns the path without an extension for an input location.
*
* @param location The location of the file, with original file extension intact.
*
* @return The full path, without extension (e.g. /path/to/file.txt => /path/to/file)
*/
private String getPathWithoutExtension(String location) {
if (location == null) return null;
int idx = location.lastIndexOf('.');
if (idx == -1) return location;
return location.substring(0, idx);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.openapitools.codegen.api;

import java.io.IOException;
import java.util.Map;

/**
* Each templating engine is called by an Adapter, selected at runtime
*/
public interface TemplatingEngineAdapter{

/**
* Compiles a template into a string
*
* @param generator From where we can fetch the templates content (e.g. an instance of DefaultGenerator)
* @param bundle The map of values to pass to the template
* @param templateFile The name of the template (e.g. model.mustache )
* @return the processed template result
* @throws IOException an error ocurred in the template processing
*/
String compileTemplate(TemplatingGenerator generator, Map<String, Object> bundle,
String templateFile) throws IOException;

/**
* During generation, if a supporting file has a file extension that is
* inside that array, then it is considered a templated supporting file
* and we use the templating engine adapter to generate it
* @return string array of the valid file extensions for this templating engine
*/
String[] getFileExtensions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.openapitools.codegen.api;

/**
* interface to the full template content
* implementers might take into account the -t cli option,
* look in the resources for a language specific template, etc
*/
public interface TemplatingGenerator {

/**
* returns the template content by name
* @param name the template name (e.g. model.mustache)
* @return the contents of that template
*/
String getFullTemplateContents(String name);

}
15 changes: 15 additions & 0 deletions modules/openapi-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@
<artifactId>jmustache</artifactId>
<version>${jmustache-version}</version>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>${handlebars.java-version}</version>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars-jackson2</artifactId>
<version>${handlebars.java-version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down Expand Up @@ -300,6 +310,11 @@
<artifactId>jackson-datatype-threetenbp</artifactId>
<version>${jackson-threetenbp-version}</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-core</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.api.TemplatingGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,7 +31,7 @@
import java.util.Scanner;
import java.util.regex.Pattern;

public abstract class AbstractGenerator {
public abstract class AbstractGenerator implements TemplatingGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGenerator.class);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;
import org.openapitools.codegen.api.TemplatingEngineAdapter;

import java.io.File;
import java.util.List;
Expand Down Expand Up @@ -149,6 +150,8 @@ public interface CodegenConfig {

Compiler processCompiler(Compiler compiler);

TemplatingEngineAdapter processTemplatingEngine(TemplatingEngineAdapter templatingEngine);

String sanitizeTag(String tag);

String toApiFilename(String name);
Expand Down Expand Up @@ -260,8 +263,11 @@ public interface CodegenConfig {
*/
void setOpenAPI(OpenAPI openAPI);

void setTemplatingEngine(TemplatingEngineAdapter s);

TemplatingEngineAdapter getTemplatingEngine();

public boolean isEnableMinimalUpdate();

public void setEnableMinimalUpdate(boolean isEnableMinimalUpdate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public class CodegenConstants {
public static final String DOTNET_FRAMEWORK = "targetFramework";
public static final String DOTNET_FRAMEWORK_DESC = "The target .NET framework version.";

public static final String TEMPLATING_ENGINE = "templatingEngine";
public static final String TEMPLATING_ENGINE_DESC = "The templating engine plugin to use: \"mustache\" (default) or \"handlebars\" (beta)";

public static enum MODEL_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case, original}

public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case, original, UPPERCASE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.openapitools.codegen.CodegenDiscriminator.MappedModel;
import org.openapitools.codegen.api.TemplatingEngineAdapter;
import org.openapitools.codegen.config.GeneratorProperties;
import org.openapitools.codegen.examples.ExampleGenerator;
import org.openapitools.codegen.serializer.SerializerUtils;
import org.openapitools.codegen.templating.MustacheEngineAdapter;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -111,6 +113,8 @@ public class DefaultCodegen implements CodegenConfig {
protected String ignoreFilePathOverride;
// flag to indicate whether to use environment variable to post process file
protected boolean enablePostProcessFile = false;
private TemplatingEngineAdapter templatingEngine = new MustacheEngineAdapter();

// flag to indicate whether to only update files whose contents have changed
protected boolean enableMinimalUpdate = false;

Expand Down Expand Up @@ -462,6 +466,12 @@ public Compiler processCompiler(Compiler compiler) {
return compiler;
}

// override with any special handling for the templating engine
@SuppressWarnings("unused")
public TemplatingEngineAdapter processTemplatingEngine(TemplatingEngineAdapter templatingEngine) {
return templatingEngine;
}

// override with any special text escaping logic
@SuppressWarnings("static-method")
public String escapeText(String input) {
Expand Down Expand Up @@ -3828,6 +3838,16 @@ public String sanitizeName(String name) {
return sanitizeName(name, "\\W");
}

@Override
public void setTemplatingEngine(TemplatingEngineAdapter templatingEngine) {
this.templatingEngine = templatingEngine;
}

@Override
public TemplatingEngineAdapter getTemplatingEngine() {
return this.templatingEngine;
}

/**
* Sanitize name (parameter, property, method, etc)
*
Expand Down
Loading