mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 14:10:56 +00:00
Swagr code-gen: Split config options into gen related config and env related
This commit is contained in:
parent
5ec7c8b40e
commit
643971c011
@ -4,6 +4,7 @@ import com.sun.jersey.api.client.Client;
|
|||||||
import com.sun.jersey.api.client.ClientResponse;
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
import com.sun.jersey.api.client.WebResource;
|
import com.sun.jersey.api.client.WebResource;
|
||||||
import com.wordnik.codegen.config.CodeGenConfig;
|
import com.wordnik.codegen.config.CodeGenConfig;
|
||||||
|
import com.wordnik.codegen.config.GenerationEnvironmentConfig;
|
||||||
import com.wordnik.codegen.resource.*;
|
import com.wordnik.codegen.resource.*;
|
||||||
import com.wordnik.exception.CodeGenerationException;
|
import com.wordnik.exception.CodeGenerationException;
|
||||||
|
|
||||||
@ -37,6 +38,7 @@ public class DriverCodeGenerator {
|
|||||||
private static final String API_LISTING_URL = "apiListResource";
|
private static final String API_LISTING_URL = "apiListResource";
|
||||||
|
|
||||||
private CodeGenConfig config = null;
|
private CodeGenConfig config = null;
|
||||||
|
private GenerationEnvironmentConfig envConfig = null;
|
||||||
private String baseUrl;
|
private String baseUrl;
|
||||||
private String apiKey;
|
private String apiKey;
|
||||||
private String apiListResource;
|
private String apiListResource;
|
||||||
@ -49,6 +51,14 @@ public class DriverCodeGenerator {
|
|||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GenerationEnvironmentConfig getEnvConfig() {
|
||||||
|
return envConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnvConfig(GenerationEnvironmentConfig config) {
|
||||||
|
this.envConfig = config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate classes needed for the model and API invocation
|
* Generate classes needed for the model and API invocation
|
||||||
*/
|
*/
|
||||||
@ -56,7 +66,7 @@ public class DriverCodeGenerator {
|
|||||||
readApiConfig();
|
readApiConfig();
|
||||||
//read resources and get their documentation
|
//read resources and get their documentation
|
||||||
List<Resource> resources = this.readResourceDocumentation(baseUrl);
|
List<Resource> resources = this.readResourceDocumentation(baseUrl);
|
||||||
StringTemplateGroup aTemplateGroup = new StringTemplateGroup("templates",config.getTemplateLocation());
|
StringTemplateGroup aTemplateGroup = new StringTemplateGroup("templates",envConfig.getTemplateLocation());
|
||||||
if(resources.size() > 0) {
|
if(resources.size() > 0) {
|
||||||
generateVersionHelper(resources.get(0).getApiVersion(), aTemplateGroup);
|
generateVersionHelper(resources.get(0).getApiVersion(), aTemplateGroup);
|
||||||
}
|
}
|
||||||
@ -208,7 +218,7 @@ public class DriverCodeGenerator {
|
|||||||
private void generateVersionHelper(String version, StringTemplateGroup templateGroup) {
|
private void generateVersionHelper(String version, StringTemplateGroup templateGroup) {
|
||||||
StringTemplate template = templateGroup.getInstanceOf(VERSION_OBJECT_TEMPLATE);
|
StringTemplate template = templateGroup.getInstanceOf(VERSION_OBJECT_TEMPLATE);
|
||||||
template.setAttribute("apiVersion", version);
|
template.setAttribute("apiVersion", version);
|
||||||
File aFile = new File(config.getResourceClassLocation() + config.getNameGenerator().getVersionCheckerClassName()
|
File aFile = new File(envConfig.getResourceClassLocation() + config.getNameGenerator().getVersionCheckerClassName()
|
||||||
+ config.getClassFileExtension());
|
+ config.getClassFileExtension());
|
||||||
writeFile(aFile, template.toString(), "Version checker class");
|
writeFile(aFile, template.toString(), "Version checker class");
|
||||||
}
|
}
|
||||||
@ -236,7 +246,7 @@ public class DriverCodeGenerator {
|
|||||||
template.setAttribute("imports", imports);
|
template.setAttribute("imports", imports);
|
||||||
template.setAttribute("extends", config.getCodeGenOverridingRules().getModelExtendingClass());
|
template.setAttribute("extends", config.getCodeGenOverridingRules().getModelExtendingClass());
|
||||||
template.setAttribute("className", model.getGenratedClassName());
|
template.setAttribute("className", model.getGenratedClassName());
|
||||||
File aFile = new File(config.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
|
File aFile = new File(envConfig.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
|
||||||
writeFile(aFile, template.toString(), "Model class");
|
writeFile(aFile, template.toString(), "Model class");
|
||||||
generatedClassNames.add(model.getName());
|
generatedClassNames.add(model.getName());
|
||||||
}
|
}
|
||||||
@ -278,7 +288,7 @@ public class DriverCodeGenerator {
|
|||||||
template.setAttribute("imports", imports);
|
template.setAttribute("imports", imports);
|
||||||
template.setAttribute("extends", config.getCodeGenOverridingRules().getModelExtendingClass());
|
template.setAttribute("extends", config.getCodeGenOverridingRules().getModelExtendingClass());
|
||||||
template.setAttribute("className", model.getGenratedClassName());
|
template.setAttribute("className", model.getGenratedClassName());
|
||||||
File aFile = new File(config.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
|
File aFile = new File(envConfig.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
|
||||||
writeFile(aFile, template.toString(), "Input model class");
|
writeFile(aFile, template.toString(), "Input model class");
|
||||||
generatedClasses.add(model.getName());
|
generatedClasses.add(model.getName());
|
||||||
}
|
}
|
||||||
@ -316,7 +326,7 @@ public class DriverCodeGenerator {
|
|||||||
template.setAttribute("methods", filteredMethods);
|
template.setAttribute("methods", filteredMethods);
|
||||||
template.setAttribute("extends", config.getCodeGenOverridingRules().getServiceExtendingClass(className));
|
template.setAttribute("extends", config.getCodeGenOverridingRules().getServiceExtendingClass(className));
|
||||||
|
|
||||||
File aFile = new File(config.getResourceClassLocation()+ resource.generateClassName(config) +config.getClassFileExtension());
|
File aFile = new File(envConfig.getResourceClassLocation()+ resource.generateClassName(config) +config.getClassFileExtension());
|
||||||
writeFile(aFile, template.toString(), "API CLasses");
|
writeFile(aFile, template.toString(), "API CLasses");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -359,7 +369,7 @@ public class DriverCodeGenerator {
|
|||||||
template.setAttribute("imports", imports);
|
template.setAttribute("imports", imports);
|
||||||
template.setAttribute("extends", config.getCodeGenOverridingRules().getModelExtendingClass());
|
template.setAttribute("extends", config.getCodeGenOverridingRules().getModelExtendingClass());
|
||||||
template.setAttribute("className", model.getGenratedClassName());
|
template.setAttribute("className", model.getGenratedClassName());
|
||||||
File aFile = new File(config.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
|
File aFile = new File(envConfig.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
|
||||||
writeFile(aFile, template.toString(), "Wrapper class for test data file");
|
writeFile(aFile, template.toString(), "Wrapper class for test data file");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,41 +11,28 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class CodeGenConfig {
|
public class CodeGenConfig {
|
||||||
|
|
||||||
private String templateLocation; //lang config
|
|
||||||
|
|
||||||
private String classFileExtension; //lang config
|
private String classFileExtension;
|
||||||
|
|
||||||
private String modelClassLocation; //output config
|
|
||||||
|
|
||||||
private String resourceClassLocation; //output config
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default model imports that we need to include in all service classes. This is needed because some times,
|
* Default model imports that we need to include in all service classes. This is needed because some times,
|
||||||
* we may need to write custom classes and those classes will not be known to code generation. To import those
|
* we may need to write custom classes and those classes will not be known to code generation. To import those
|
||||||
* classes in service classes we use this property
|
* classes in service classes we use this property
|
||||||
*/
|
*/
|
||||||
private List<String> defaultModelImports; //code gen helper config
|
private List<String> defaultModelImports;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default service imports that we need to include in all service classes. This is needed because some times,
|
* Default service imports that we need to include in all service classes. This is needed because some times,
|
||||||
* we may need to write custom classes ans those classes will not be known to code generation. To import those
|
* we may need to write custom classes ans those classes will not be known to code generation. To import those
|
||||||
* classes in service classes we use this property
|
* classes in service classes we use this property
|
||||||
*/
|
*/
|
||||||
private List<String> defaultServiceImports; //code gen helper config
|
private List<String> defaultServiceImports;
|
||||||
|
|
||||||
private CodeGenOverridingRules codeGenOverridingRules; //code gen helper config
|
private CodeGenOverridingRules codeGenOverridingRules;
|
||||||
|
|
||||||
private DataTypeMapper dataTypeMapper; //code gen helper
|
private DataTypeMapper dataTypeMapper;
|
||||||
|
|
||||||
private ServiceAndMethodNameGenerator nameGenerator; //code gen helper
|
private ServiceAndMethodNameGenerator nameGenerator;
|
||||||
|
|
||||||
public String getTemplateLocation() {
|
|
||||||
return templateLocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTemplateLocation(String templateLocation) {
|
|
||||||
this.templateLocation = templateLocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getClassFileExtension() {
|
public String getClassFileExtension() {
|
||||||
return classFileExtension;
|
return classFileExtension;
|
||||||
@ -55,22 +42,6 @@ public class CodeGenConfig {
|
|||||||
this.classFileExtension = classFileExtension;
|
this.classFileExtension = classFileExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getModelClassLocation() {
|
|
||||||
return modelClassLocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setModelClassLocation(String modelClassLocation) {
|
|
||||||
this.modelClassLocation = modelClassLocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getResourceClassLocation() {
|
|
||||||
return resourceClassLocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setResourceClassLocation(String resourceClassLocation) {
|
|
||||||
this.resourceClassLocation = resourceClassLocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getDefaultModelImports() {
|
public List<String> getDefaultModelImports() {
|
||||||
return defaultModelImports;
|
return defaultModelImports;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.wordnik.codegen.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: deepakmichael
|
||||||
|
* Date: 23/07/11
|
||||||
|
* Time: 8:01 AM
|
||||||
|
*/
|
||||||
|
public class GenerationEnvironmentConfig {
|
||||||
|
|
||||||
|
private String templateLocation; //lang config
|
||||||
|
|
||||||
|
private String modelClassLocation; //output config
|
||||||
|
|
||||||
|
private String resourceClassLocation; //output config
|
||||||
|
|
||||||
|
public String getTemplateLocation() {
|
||||||
|
return templateLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTemplateLocation(String templateLocation) {
|
||||||
|
this.templateLocation = templateLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModelClassLocation() {
|
||||||
|
return modelClassLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModelClassLocation(String modelClassLocation) {
|
||||||
|
this.modelClassLocation = modelClassLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getResourceClassLocation() {
|
||||||
|
return resourceClassLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResourceClassLocation(String resourceClassLocation) {
|
||||||
|
this.resourceClassLocation = resourceClassLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -14,9 +14,6 @@ public class JavaCodeGenConfig extends CodeGenConfig {
|
|||||||
|
|
||||||
public JavaCodeGenConfig() {
|
public JavaCodeGenConfig() {
|
||||||
this.setClassFileExtension(".java");
|
this.setClassFileExtension(".java");
|
||||||
this.setModelClassLocation("../java/src/main/java/com/wordnik/model/");
|
|
||||||
this.setResourceClassLocation("../java/src/main/java/com/wordnik/api/");
|
|
||||||
this.setTemplateLocation("conf/templates/java");
|
|
||||||
List<String> defaultModelImports = new ArrayList<String>();
|
List<String> defaultModelImports = new ArrayList<String>();
|
||||||
defaultModelImports.add("com.wordnik.common.WordListType");
|
defaultModelImports.add("com.wordnik.common.WordListType");
|
||||||
defaultModelImports.add("com.wordnik.common.StringValue");
|
defaultModelImports.add("com.wordnik.common.StringValue");
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.wordnik.codegen.java;
|
||||||
|
|
||||||
|
import com.wordnik.codegen.config.GenerationEnvironmentConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: deepakmichael
|
||||||
|
* Date: 23/07/11
|
||||||
|
* Time: 8:09 AM
|
||||||
|
*/
|
||||||
|
public class JavaGenEnvConfig extends GenerationEnvironmentConfig{
|
||||||
|
|
||||||
|
public JavaGenEnvConfig() {
|
||||||
|
this.setModelClassLocation("../java/src/main/java/com/wordnik/model/");
|
||||||
|
this.setResourceClassLocation("../java/src/main/java/com/wordnik/api/");
|
||||||
|
this.setTemplateLocation("conf/templates/java");
|
||||||
|
}
|
||||||
|
}
|
@ -16,5 +16,6 @@ public class JavaLibCodeGen extends DriverCodeGenerator {
|
|||||||
|
|
||||||
public JavaLibCodeGen(){
|
public JavaLibCodeGen(){
|
||||||
this.setConfig(new JavaCodeGenConfig());
|
this.setConfig(new JavaCodeGenConfig());
|
||||||
|
this.setEnvConfig(new JavaGenEnvConfig());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user