Skip to content

Added apiNameSuffix parameter for JaxRsSpec code generator #7160

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

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
36 changes: 36 additions & 0 deletions bin/jaxrs-spec-petstore-server-api-suffix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

SCRIPT="$0"

while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi

executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l jaxrs-spec -o samples/server/petstore/jaxrs-spec-api-suffix
-DhideGenerationTimestamp=true
-DserializableModel=true
--additional-properties=apiNameSuffix=Resource
-DapiPackage=io.swagger.resource
-DinvokerPackage=io.swagger.resource"

java $JAVA_OPTS -jar $executable $ags
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public class Generate implements Runnable {
description = CodegenConstants.API_PACKAGE_DESC)
private String apiPackage;

@Option(name = {"--api-name-suffix"}, title = "api name suffix",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be here at all. If I'm not mistaken, these are the general swagger-codegen wide parameters that all generator should support. I think you only need to add you parameters to your specific codegen. See this for an example:

https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaPlayFrameworkCodegen.java#L22

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I check all your code, it seems you are indeed trying to create an "all generators" parameter... I'm not sure it is really safe to add that kind of parameters. I will add @wing328 and @cbornet to the conversation to have their advices.

description = CodegenConstants.API_NAME_SUFFIX_DESC)
private String apiNameSuffix;

@Option(name = {"--model-package"}, title = "model package",
description = CodegenConstants.MODEL_PACKAGE_DESC)
private String modelPackage;
Expand Down Expand Up @@ -217,6 +221,10 @@ public void run() {
configurator.setApiPackage(apiPackage);
}

if (isNotEmpty(apiNameSuffix)) {
configurator.setApiNameSuffix(apiNameSuffix);
}

if (isNotEmpty(modelPackage)) {
configurator.setModelPackage(modelPackage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "library", required = false)
private String library;

/**
* Sets the suffix for api classes
*/
@Parameter(name = "apiNameSuffix", required = false)
private String apiNameSuffix;

/**
* Sets the prefix for model enums and classes
*/
Expand Down Expand Up @@ -391,6 +397,10 @@ public void execute() throws MojoExecutionException {
configurator.setLibrary(library);
}

if (isNotEmpty(apiNameSuffix)) {
configurator.setApiNameSuffix(apiNameSuffix);
}

if (isNotEmpty(modelNamePrefix)) {
configurator.setModelNamePrefix(modelNamePrefix);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
public static final String ENUM_PROPERTY_NAMING = "enumPropertyNaming";
public static final String ENUM_PROPERTY_NAMING_DESC = "Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'";

public static final String API_NAME_SUFFIX = "apiNameSuffix";
public static final String API_NAME_SUFFIX_DESC = "Suffix that will be appended to all api names. Default is \"Api\".";

public static final String MODEL_NAME_PREFIX = "modelNamePrefix";
public static final String MODEL_NAME_PREFIX_DESC = "Prefix that will be prepended to all model names. Default is the empty string.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class DefaultCodegen {
protected Set<String> languageSpecificPrimitives = new HashSet<String>();
protected Map<String, String> importMapping = new HashMap<String, String>();
protected String modelPackage = "", apiPackage = "", fileSuffix;
protected String apiNameSuffix = "Api";
protected String modelNamePrefix = "", modelNameSuffix = "";
protected String testPackage = "";
protected Map<String, String> apiTemplateFiles = new HashMap<String, String>();
Expand Down Expand Up @@ -143,6 +144,10 @@ public void processOpts() {
.get(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS).toString()));
}

if (additionalProperties.containsKey(CodegenConstants.API_NAME_SUFFIX)) {
this.setApiNameSuffix((String) additionalProperties.get(CodegenConstants.API_NAME_SUFFIX));
}

if(additionalProperties.containsKey(CodegenConstants.MODEL_NAME_PREFIX)){
this.setModelNamePrefix((String) additionalProperties.get(CodegenConstants.MODEL_NAME_PREFIX));
}
Expand Down Expand Up @@ -567,6 +572,10 @@ public void setTemplateDir(String templateDir) {
this.templateDir = templateDir;
}

public void setApiNameSuffix(String apiNameSuffix) {
this.apiNameSuffix = apiNameSuffix;
}

public void setModelPackage(String modelPackage) {
this.modelPackage = modelPackage;
}
Expand Down Expand Up @@ -1266,7 +1275,7 @@ public String toApiName(String name) {
if (name.length() == 0) {
return "DefaultApi";
}
return initialCaps(name) + "Api";
return initialCaps(name) + apiNameSuffix;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class CodegenConfigurator implements Serializable {
private String templateDir;
private String auth;
private String apiPackage;
private String apiNameSuffix;
private String modelPackage;
private String invokerPackage;
private String modelNamePrefix;
Expand Down Expand Up @@ -99,6 +100,15 @@ public CodegenConfigurator setOutputDir(String outputDir) {
return this;
}

public String getApiNameSuffix() {
return apiNameSuffix;
}

public CodegenConfigurator setApiNameSuffix(String apiNameSuffix) {
this.apiNameSuffix = apiNameSuffix;
return this;
}

public String getModelPackage() {
return modelPackage;
}
Expand Down Expand Up @@ -402,6 +412,7 @@ public ClientOptInput toClientOptInput() {
config.reservedWordsMappings().putAll(reservedWordMappings);

checkAndSetAdditionalProperty(apiPackage, CodegenConstants.API_PACKAGE);
checkAndSetAdditionalProperty(apiNameSuffix, CodegenConstants.API_NAME_SUFFIX);
checkAndSetAdditionalProperty(modelPackage, CodegenConstants.MODEL_PACKAGE);
checkAndSetAdditionalProperty(invokerPackage, CodegenConstants.INVOKER_PACKAGE);
checkAndSetAdditionalProperty(groupId, CodegenConstants.GROUP_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public String toApiName(String name) {
if (name.length() == 0) {
return "DefaultApi";
}
return camelize(name) + "Api";
return camelize(name) + apiNameSuffix;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public String toApiName(final String name) {
return "DefaultApi";
}
computed = sanitizeName(computed);
return camelize(computed) + "Api";
return camelize(computed) + apiNameSuffix;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Swagger Codegen Ignore
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0-SNAPSHOT
83 changes: 83 additions & 0 deletions samples/server/petstore/jaxrs-spec-api-suffix/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs-server</artifactId>
<packaging>war</packaging>
<name>swagger-jaxrs-server</name>
<version>1.0.0</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<scope>provided</scope>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
<exclusion>
<artifactId>snakeyaml</artifactId>
<groupId>org.yaml</groupId>
</exclusion>
<exclusion>
<artifactId>bsh</artifactId>
<groupId>org.beanshell</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- Bean Validation API support -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<junit-version>4.8.1</junit-version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package io.swagger.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.Serializable;
import javax.validation.constraints.*;
import javax.validation.Valid;


import io.swagger.annotations.*;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;


public class AdditionalPropertiesClass implements Serializable {

private @Valid Map<String, String> mapProperty = new HashMap<String, String>();
private @Valid Map<String, Map<String, String>> mapOfMapProperty = new HashMap<String, Map<String, String>>();

/**
**/
public AdditionalPropertiesClass mapProperty(Map<String, String> mapProperty) {
this.mapProperty = mapProperty;
return this;
}


@ApiModelProperty(value = "")
@JsonProperty("map_property")
public Map<String, String> getMapProperty() {
return mapProperty;
}
public void setMapProperty(Map<String, String> mapProperty) {
this.mapProperty = mapProperty;
}

/**
**/
public AdditionalPropertiesClass mapOfMapProperty(Map<String, Map<String, String>> mapOfMapProperty) {
this.mapOfMapProperty = mapOfMapProperty;
return this;
}


@ApiModelProperty(value = "")
@JsonProperty("map_of_map_property")
public Map<String, Map<String, String>> getMapOfMapProperty() {
return mapOfMapProperty;
}
public void setMapOfMapProperty(Map<String, Map<String, String>> mapOfMapProperty) {
this.mapOfMapProperty = mapOfMapProperty;
}


@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AdditionalPropertiesClass additionalPropertiesClass = (AdditionalPropertiesClass) o;
return Objects.equals(mapProperty, additionalPropertiesClass.mapProperty) &&
Objects.equals(mapOfMapProperty, additionalPropertiesClass.mapOfMapProperty);
}

@Override
public int hashCode() {
return Objects.hash(mapProperty, mapOfMapProperty);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class AdditionalPropertiesClass {\n");

sb.append(" mapProperty: ").append(toIndentedString(mapProperty)).append("\n");
sb.append(" mapOfMapProperty: ").append(toIndentedString(mapOfMapProperty)).append("\n");
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}

Loading