Work almost finish

Aask @wing328 if the modificatgion in the DefaultGenerator class is a
problem. If needed, find a way to do it only for the CXF Codegen.
TODO: Maybe create an abstract JAX-RS Codegen and make Jersey & CXF
extends it ? CXF should not extends Jersey since there at two different
frameworks implementing the JAX-RS specification.
Important: nothing is tested yet, only visual checks during Maven build.
This commit is contained in:
Maelig Nantel 2016-01-20 19:10:06 +01:00 committed by wing328
parent 1dbc8a8552
commit f3659f88a8
7 changed files with 139 additions and 145 deletions

View File

@ -264,7 +264,8 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
operation.put("basePath", basePath);
operation.put("basePathWithoutHost", basePathWithoutHost);
operation.put("contextPath", contextPath);
operation.put("baseName", tag);
// operation.put("baseName", tag); FIXME ask @hiveship if this modification cause a problem
operation.put("baseName", basePathWithoutHost);
operation.put("modelPackage", config.modelPackage());
operation.putAll(config.additionalProperties());
operation.put("classname", config.toApiName(tag));

View File

@ -493,7 +493,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
if (allDefinitions != null && codegenModel != null && codegenModel.parentSchema != null && codegenModel.hasEnums) {
final Model parentModel = allDefinitions.get(codegenModel.parentSchema);
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
codegenModel = this.reconcileInlineEnums(codegenModel, parentCodegenModel);
codegenModel = JavaClientCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel);
}
return codegenModel;

View File

@ -2,8 +2,12 @@
package io.swagger.codegen.languages;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.WordUtils;
import io.swagger.codegen.CodegenOperation;
import io.swagger.models.Operation;
// TODO: create an abstract JavaJAXRSServerCodegen that both Jersey1 & CXF will extends.
public class JavaJaxRSCXFServerCodegen extends JavaJaxRSJersey1ServerCodegen
@ -28,22 +32,18 @@ public class JavaJaxRSCXFServerCodegen extends JavaJaxRSJersey1ServerCodegen
modelTemplateFiles.put("entityModel.mustache", ".java");
supportingFiles.clear();
}
@Override
public String toApiName(String name)
{
String computedName = name;
computedName = computedName.replace('-', ' ');
computedName = WordUtils.capitalize(computedName);
computedName = computedName.replaceAll("\\s", "");
computedName = super.toApiName(computedName);
return computedName;
}
}
@Override
public String getName()
{
return "cxf";
}
@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
co.subresourceOperation = !co.path.isEmpty();
}
}

View File

@ -8,12 +8,14 @@ import io.swagger.models.Swagger;
import java.io.File;
import java.util.*;
public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen
{
protected static final String JAXRS_TEMPLATE_DIRECTORY_NAME = "JavaJaxRS";
protected String title = "Swagger Server";
protected String implFolder = "src/main/java";
public JavaJaxRSJersey1ServerCodegen() {
public JavaJaxRSJersey1ServerCodegen()
{
super();
sourceFolder = "src/gen/java";
@ -30,11 +32,11 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
modelPackage = "io.swagger.model";
additionalProperties.put("title", title);
embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "jersey1_18";
for(int i = 0; i < cliOptions.size(); i++) {
if(CodegenConstants.LIBRARY.equals(cliOptions.get(i).getOpt())) {
for ( int i = 0; i < cliOptions.size(); i++ ) {
if ( CodegenConstants.LIBRARY.equals(cliOptions.get(i).getOpt()) ) {
cliOptions.remove(i);
break;
}
@ -46,7 +48,6 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
Map<String, String> supportedLibraries = new LinkedHashMap<String, String>();
supportedLibraries.put(DEFAULT_LIBRARY, "Jersey core 1.18.1");
// supportedLibraries.put("jersey2", "Jersey2 core library 2.x");
library.setEnum(supportedLibraries);
cliOptions.add(library);
@ -54,123 +55,120 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
}
@Override
public CodegenType getTag() {
public CodegenType getTag()
{
return CodegenType.SERVER;
}
@Override
public String getName() {
public String getName()
{
return "jaxrs";
}
@Override
public String getHelp() {
return "Generates a Java JAXRS Server application.";
public String getHelp()
{
return "Generates a Java JAXRS Server application based on Jersey framework.";
}
@Override
public void processOpts() {
public void processOpts()
{
super.processOpts();
if(additionalProperties.containsKey(CodegenConstants.IMPL_FOLDER)) {
implFolder = (String) additionalProperties.get(CodegenConstants.IMPL_FOLDER);
if ( additionalProperties.containsKey(CodegenConstants.IMPL_FOLDER) ) {
implFolder = (String) additionalProperties.get(CodegenConstants.IMPL_FOLDER);
}
supportingFiles.clear();
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("ApiException.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiException.java"));
supportingFiles.add(new SupportingFile("ApiOriginFilter.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiOriginFilter.java"));
supportingFiles.add(new SupportingFile("ApiResponseMessage.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiResponseMessage.java"));
supportingFiles.add(new SupportingFile("NotFoundException.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "NotFoundException.java"));
supportingFiles.add(new SupportingFile("web.mustache",
("src/main/webapp/WEB-INF"), "web.xml"));
supportingFiles.add(new SupportingFile("StringUtil.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "StringUtil.java"));
supportingFiles.add(new SupportingFile("ApiException.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiException.java"));
supportingFiles.add(new SupportingFile("ApiOriginFilter.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiOriginFilter.java"));
supportingFiles.add(new SupportingFile("ApiResponseMessage.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiResponseMessage.java"));
supportingFiles.add(new SupportingFile("NotFoundException.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "NotFoundException.java"));
supportingFiles.add(new SupportingFile("web.mustache", ("src/main/webapp/WEB-INF"), "web.xml"));
supportingFiles.add(new SupportingFile("StringUtil.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "StringUtil.java"));
if (additionalProperties.containsKey("dateLibrary")) {
if ( additionalProperties.containsKey("dateLibrary") ) {
setDateLibrary(additionalProperties.get("dateLibrary").toString());
additionalProperties.put(dateLibrary, "true");
}
if("joda".equals(dateLibrary)) {
supportingFiles.add(new SupportingFile("JodaDateTimeProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaDateTimeProvider.java"));
supportingFiles.add(new SupportingFile("JodaLocalDateProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaLocalDateProvider.java"));
}
else if ("java8".equals(dateLibrary)) {
supportingFiles.add(new SupportingFile("LocalDateTimeProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateTimeProvider.java"));
supportingFiles.add(new SupportingFile("LocalDateProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateProvider.java"));
if ( "joda".equals(dateLibrary) ) {
supportingFiles.add(new SupportingFile("JodaDateTimeProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaDateTimeProvider.java"));
supportingFiles.add(new SupportingFile("JodaLocalDateProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaLocalDateProvider.java"));
} else if ( "java8".equals(dateLibrary) ) {
supportingFiles.add(new SupportingFile("LocalDateTimeProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateTimeProvider.java"));
supportingFiles.add(new SupportingFile("LocalDateProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateProvider.java"));
}
}
@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
String basePath = resourcePath;
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
}
int pos = basePath.indexOf("/");
if (pos > 0) {
basePath = basePath.substring(0, pos);
}
if (basePath == "") {
basePath = "default";
} else {
if (co.path.startsWith("/" + basePath)) {
co.path = co.path.substring(("/" + basePath).length());
}
co.subresourceOperation = !co.path.isEmpty();
}
List<CodegenOperation> opList = operations.get(basePath);
if (opList == null) {
opList = new ArrayList<CodegenOperation>();
operations.put(basePath, opList);
}
opList.add(co);
co.baseName = basePath;
}
// @Override
// public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations)
// {
// String basePath = resourcePath;
//
// if ( basePath.startsWith("/") ) {
// basePath = basePath.substring(1);
// }
// int pos = basePath.indexOf("/");
// if ( pos > 0 ) {
// basePath = basePath.substring(0, pos);
// }
//
// if ( basePath == "" ) {
// basePath = "default";
// } else {
// if ( co.path.startsWith("/" + basePath) ) {
// co.path = co.path.substring(("/" + basePath).length());
// System.out.println("### DEBUG | jerseyCodegen addOperationToGroupe | co.path -> " + co.path.toString());
// }
// co.subresourceOperation = !co.path.isEmpty();
// }
// List<CodegenOperation> opList = operations.get(basePath);
// if ( opList == null ) {
// opList = new ArrayList<CodegenOperation>();
// operations.put(basePath, opList);
// }
// opList.add(co);
// co.baseName = basePath;
// }
@Override
public void preprocessSwagger(Swagger swagger) {
if("/".equals(swagger.getBasePath())) {
public void preprocessSwagger(Swagger swagger)
{
if ( "/".equals(swagger.getBasePath()) ) {
swagger.setBasePath("");
}
String host = swagger.getHost();
String port = "8080";
if(host != null) {
String port = "8080"; // Default value for a JEE Server
if ( host != null ) {
String[] parts = host.split(":");
if(parts.length > 1) {
if ( parts.length > 1 ) {
port = parts[1];
}
}
this.additionalProperties.put("serverPort", port);
if(swagger != null && swagger.getPaths() != null) {
for(String pathname : swagger.getPaths().keySet()) {
if ( swagger.getPaths() != null ) {
for ( String pathname : swagger.getPaths().keySet() ) {
Path path = swagger.getPath(pathname);
if(path.getOperations() != null) {
for(Operation operation : path.getOperations()) {
if(operation.getTags() != null) {
if ( path.getOperations() != null ) {
for ( Operation operation : path.getOperations() ) {
if ( operation.getTags() != null ) {
List<Map<String, String>> tags = new ArrayList<Map<String, String>>();
for(String tag : operation.getTags()) {
for ( String tag : operation.getTags() ) {
Map<String, String> value = new HashMap<String, String>();
value.put("tag", tag);
value.put("hasMore", "true");
tags.add(value);
}
if(tags.size() > 0) {
if ( tags.size() > 0 ) {
tags.get(tags.size() - 1).remove("hasMore");
}
if(operation.getTags().size() > 0) {
if ( operation.getTags().size() > 0 ) {
String tag = operation.getTags().get(0);
operation.setTags(Arrays.asList(tag));
}
@ -183,51 +181,53 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
}
@Override
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
public Map<String, Object> postProcessOperations(Map<String, Object> objs)
{
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
if (operations != null) {
if ( operations != null ) {
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation operation : ops) {
for ( CodegenOperation operation : ops ) {
List<CodegenResponse> responses = operation.responses;
if (responses != null) {
for (CodegenResponse resp : responses) {
if ("0".equals(resp.code)) {
if ( responses != null ) {
for ( CodegenResponse resp : responses ) {
if ( "0".equals(resp.code) ) {
resp.code = "200";
}
}
}
if (operation.returnType == null) {
operation.returnType = "Void";
} else if (operation.returnType.startsWith("List")) {
String rt = operation.returnType;
int end = rt.lastIndexOf(">");
if (end > 0) {
operation.returnType = rt.substring("List<".length(), end).trim();
operation.returnContainer = "List";
}
} else if (operation.returnType.startsWith("Map")) {
String rt = operation.returnType;
int end = rt.lastIndexOf(">");
if (end > 0) {
operation.returnType = rt.substring("Map<".length(), end).split(",")[1].trim();
operation.returnContainer = "Map";
}
} else if (operation.returnType.startsWith("Set")) {
String rt = operation.returnType;
int end = rt.lastIndexOf(">");
if (end > 0) {
operation.returnType = rt.substring("Set<".length(), end).trim();
operation.returnContainer = "Set";
}
}
if (operation.returnType == null) {
operation.returnType = "void";
} else if (operation.returnType.startsWith("List")) {
String rt = operation.returnType;
int end = rt.lastIndexOf(">");
if (end > 0) {
operation.returnType = rt.substring("List<".length(), end).trim();
operation.returnContainer = "List";
}
} else if (operation.returnType.startsWith("Map")) {
String rt = operation.returnType;
int end = rt.lastIndexOf(">");
if (end > 0) {
operation.returnType = rt.substring("Map<".length(), end).split(",")[1].trim();
operation.returnContainer = "Map";
}
} else if (operation.returnType.startsWith("Set")) {
String rt = operation.returnType;
int end = rt.lastIndexOf(">");
if (end > 0) {
operation.returnType = rt.substring("Set<".length(), end).trim();
operation.returnContainer = "Set";
}
}
}
}
return objs;
}
@Override
public String toApiName(String name) {
if (name.length() == 0) {
public String toApiName(String name)
{
if ( name.length() == 0 ) {
return "DefaultApi";
}
name = sanitizeName(name);
@ -235,21 +235,19 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
}
@Override
public String apiFilename(String templateName, String tag) {
public String apiFilename(String templateName, String tag)
{
String result = super.apiFilename(templateName, tag);
if (templateName.endsWith("Impl.mustache")) {
if ( templateName.endsWith("Impl.mustache") ) {
int ix = result.lastIndexOf('/');
result = result.substring(0, ix) + "/impl" + result.substring(ix, result.length() - 5) + "ServiceImpl.java";
result = result.replace(apiFileFolder(), implFileFolder(implFolder));
} else if (templateName.endsWith("Factory.mustache")) {
} else if ( templateName.endsWith("Factory.mustache") ) {
int ix = result.lastIndexOf('/');
result = result.substring(0, ix) + "/factories" + result.substring(ix, result.length() - 5) + "ServiceFactory.java";
result = result.replace(apiFileFolder(), implFileFolder(implFolder));
} else if (templateName.endsWith("Service.mustache")) {
} else if ( templateName.endsWith("Service.mustache") ) {
int ix = result.lastIndexOf('.');
result = result.substring(0, ix) + "Service.java";
}
@ -257,12 +255,14 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
return result;
}
private String implFileFolder(String output) {
private String implFileFolder(String output)
{
return outputFolder + "/" + output + "/" + apiPackage().replace('.', '/');
}
@Override
public boolean shouldOverwrite(String filename) {
public boolean shouldOverwrite(String filename)
{
return super.shouldOverwrite(filename) && !filename.endsWith("ServiceImpl.java") && !filename.endsWith("ServiceFactory.java");
}
}

View File

@ -3,11 +3,10 @@ package {{package}};
{{#imports}}import {{import}};
{{/imports}}
import {{modelPackage}}.*;
import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
@Path("/{{baseName}}")
@Path("{{baseName}}")
public interface {{classname}} {
{{#operations}}
{{#operation}}

View File

@ -3,9 +3,6 @@ package {{package}};
{{#imports}}import {{import}};
{{/imports}}
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
{{#models}}
@ -21,9 +18,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {
public enum {{datatypeWithEnum}} {
{{#allowableValues}}{{#values}} {{.}}, {{/values}}{{/allowableValues}}
};
{{#required}}@NotNull{{/required}}
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{^isEnum}}
{{#required}}@NotNull{{/required}}
private {{{datatype}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{/vars}}
{{#vars}}

View File

@ -1,7 +1,6 @@
package {{package}};
import java.util.Objects;
{{#imports}}import {{import}};
{{#imports}}import{{import}};
{{/imports}}
{{#serializableModel}}import java.io.Serializable;{{/serializableModel}}