forked from loafle/openapi-generator-original
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:
parent
1dbc8a8552
commit
f3659f88a8
@ -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));
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
@ -30,20 +34,16 @@ public class JavaJaxRSCXFServerCodegen extends JavaJaxRSJersey1ServerCodegen
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
@ -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,22 +55,26 @@ 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) ) {
|
||||
@ -79,18 +84,12 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
|
||||
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") ) {
|
||||
setDateLibrary(additionalProperties.get("dateLibrary").toString());
|
||||
@ -98,55 +97,54 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
|
||||
}
|
||||
|
||||
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"));
|
||||
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());
|
||||
// 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 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 preprocessSwagger(Swagger swagger) {
|
||||
public void preprocessSwagger(Swagger swagger)
|
||||
{
|
||||
if ( "/".equals(swagger.getBasePath()) ) {
|
||||
swagger.setBasePath("");
|
||||
}
|
||||
|
||||
String host = swagger.getHost();
|
||||
String port = "8080";
|
||||
String port = "8080"; // Default value for a JEE Server
|
||||
if ( host != null ) {
|
||||
String[] parts = host.split(":");
|
||||
if ( parts.length > 1 ) {
|
||||
@ -154,7 +152,7 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
|
||||
}
|
||||
}
|
||||
this.additionalProperties.put("serverPort", port);
|
||||
if(swagger != null && swagger.getPaths() != null) {
|
||||
if ( swagger.getPaths() != null ) {
|
||||
for ( String pathname : swagger.getPaths().keySet() ) {
|
||||
Path path = swagger.getPath(pathname);
|
||||
if ( path.getOperations() != null ) {
|
||||
@ -183,7 +181,8 @@ 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 ) {
|
||||
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
|
||||
@ -197,7 +196,7 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
|
||||
}
|
||||
}
|
||||
if (operation.returnType == null) {
|
||||
operation.returnType = "Void";
|
||||
operation.returnType = "void";
|
||||
} else if (operation.returnType.startsWith("List")) {
|
||||
String rt = operation.returnType;
|
||||
int end = rt.lastIndexOf(">");
|
||||
@ -226,7 +225,8 @@ public class JavaJaxRSJersey1ServerCodegen extends JavaClientCodegen {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiName(String name) {
|
||||
public String toApiName(String name)
|
||||
{
|
||||
if ( name.length() == 0 ) {
|
||||
return "DefaultApi";
|
||||
}
|
||||
@ -235,19 +235,17 @@ 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") ) {
|
||||
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") ) {
|
||||
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") ) {
|
||||
int ix = result.lastIndexOf('.');
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
|
@ -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}}
|
||||
|
@ -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}}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package {{package}};
|
||||
|
||||
import java.util.Objects;
|
||||
{{#imports}}import{{import}};
|
||||
{{/imports}}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user