Merge branch 'master' of github.com:wordnik/swagger-codegen

This commit is contained in:
rpidikiti 2011-08-02 11:45:32 -07:00
commit d498f6f996
4 changed files with 88 additions and 48 deletions

View File

@ -11,10 +11,12 @@
<mkdir dir="lib/ext"/> <mkdir dir="lib/ext"/>
<mkdir dir="build"/> <mkdir dir="build"/>
<mkdir dir="src/main/scala"/> <mkdir dir="src/main/scala"/>
<mkdir dir="src/test/java"/>
<mkdir dir="src/test/scala"/>
<condition property="outputPath.set"> <condition property="outputPath.set">
<and> <and>
<isset property="outputPath"/> <isset property="outputDir"/>
<isset property="apiServerRootDir"/> <isset property="apiServerRootDir"/>
</and> </and>
</condition> </condition>
@ -129,20 +131,20 @@
<!-- generates the classes --> <!-- generates the classes -->
<target name="generate-java" depends="compile" description="generates APIs and model classes for java language"> <target name="generate-java" depends="compile" description="generates APIs and model classes for java language">
<fail unless="outputPath.set"> <fail unless="outputPath.set">
Must specify the parameters: outputPath and apiServerRootDir eg. -DoutputPath=../../api-server-lib/java/src/main/java/com/wordnik/ Must specify the parameters: outputPath and apiServerRootDir
-DapiServerRootDir=../../api-server-lib/java/ eg. -DoutputDir=../api-server-lib/java/src/main/java/com/wordnik/swagger -DapiServerRootDir=../api-server-lib/java/
These are the output path of the Apis and the root directory that will be created for the api server (directory where structure is created) These are the output path of the Apis and the root directory that will be created for the api server (directory where structure is created)
</fail> </fail>
<mkdir dir="${outputPath}/api"/> <mkdir dir="${outputDir}/api"/>
<mkdir dir="${outputPath}/model"/> <mkdir dir="${outputDir}/model"/>
<echo> <echo>
outputPath for Api = ${outputPath} outputDir for Api = ${outputDir}
</echo> </echo>
<echo> <echo>
apiServerRootDir = ${apiServerRootDir} apiServerRootDir = ${apiServerRootDir}
</echo> </echo>
<delete> <delete>
<fileset dir="${outputPath}" includes="*.java"/> <fileset dir="${outputDir}" includes="*.java"/>
</delete> </delete>
<java classname="com.wordnik.swagger.codegen.config.java.JavaLibCodeGen"> <java classname="com.wordnik.swagger.codegen.config.java.JavaLibCodeGen">
<classpath> <classpath>
@ -151,7 +153,7 @@
<include name="**/*.jar"/> <include name="**/*.jar"/>
</fileset> </fileset>
</classpath> </classpath>
<arg value="${outputPath}"/> <arg value="${apiServerRootDir}/config/apiConfiguration.json"/>
</java> </java>
<copy todir="${apiServerRootDir}" overwrite="true"> <copy todir="${apiServerRootDir}" overwrite="true">
<fileset dir="conf/java/structure"/> <fileset dir="conf/java/structure"/>

View File

@ -41,6 +41,24 @@ public class ApiConfiguration {
} }
public String getDefaultServiceBaseClass() {
return defaultServiceBaseClass;
}
public void setDefaultServiceBaseClass(String defaultServiceBaseClass) {
this.defaultServiceBaseClass = defaultServiceBaseClass;
}
public Map<String, String> getBaseClassNames() {
return baseClassNames;
}
public void setBaseClassNames(Map<String, String> baseClassNames) {
this.baseClassNames = baseClassNames;
}
public void setServiceBaseClass(String defaultServiceBaseClass) { public void setServiceBaseClass(String defaultServiceBaseClass) {
this.defaultServiceBaseClass = defaultServiceBaseClass; this.defaultServiceBaseClass = defaultServiceBaseClass;
} }

View File

@ -30,4 +30,20 @@ public class JavaCodeGenRulesProvider implements RulesProvider {
return ignoreModels.contains(modelName); return ignoreModels.contains(modelName);
} }
public List<String> getIgnoreMethods() {
return ignoreMethods;
}
public void setIgnoreMethods(List<String> ignoreMethods) {
this.ignoreMethods = ignoreMethods;
}
public List<String> getIgnoreModels() {
return ignoreModels;
}
public void setIgnoreModels(List<String> ignoreModels) {
this.ignoreModels = ignoreModels;
}
} }

View File

@ -5,9 +5,11 @@ import com.wordnik.swagger.codegen.config.ApiConfiguration;
import com.wordnik.swagger.codegen.config.LanguageConfiguration; import com.wordnik.swagger.codegen.config.LanguageConfiguration;
import com.wordnik.swagger.codegen.config.common.CamelCaseNamingPolicyProvider; import com.wordnik.swagger.codegen.config.common.CamelCaseNamingPolicyProvider;
import com.wordnik.swagger.exception.CodeGenerationException; import com.wordnik.swagger.exception.CodeGenerationException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import java.util.ArrayList; import java.io.File;
import java.util.List; import java.io.IOException;
/** /**
* User: ramesh * User: ramesh
@ -18,59 +20,61 @@ public class JavaLibCodeGen extends LibraryCodeGenerator {
public static void main(String[] args) { public static void main(String[] args) {
if(args.length < 1){ if(args.length < 1){
throw new CodeGenerationException("Invalid number of arguments passed: No command line argument was passed to the program for output path"); throw new CodeGenerationException("Invalid number of arguments passed: No command line argument was passed to the program for config json");
} }
String outputPath = args[0]; String configPath = args[0];
JavaLibCodeGen codeGenerator = new JavaLibCodeGen(outputPath); JavaLibCodeGen codeGenerator = new JavaLibCodeGen(configPath);
codeGenerator.generateCode(); codeGenerator.generateCode();
} }
public JavaLibCodeGen(String outputPath){ public JavaLibCodeGen(String configPath){
this.setApiConfig(initializeApiConfig()); final ObjectMapper mapper = new ObjectMapper();
this.setLanguageConfig(initializeLangConfig(outputPath)); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.setApiConfig(readApiConfiguration(configPath, mapper));
this.setCodeGenRulesProvider(readRulesProviderConfig(configPath, mapper));
this.setLanguageConfig( initializeLangConfig(readLanguageConfiguration(configPath, mapper)) );
this.setDataTypeMappingProvider(new JavaDataTypeMappingProvider()); this.setDataTypeMappingProvider(new JavaDataTypeMappingProvider());
this.setCodeGenRulesProvider(new JavaCodeGenRulesProvider());
this.setNameGenerator(new CamelCaseNamingPolicyProvider()); this.setNameGenerator(new CamelCaseNamingPolicyProvider());
} }
private ApiConfiguration initializeApiConfig() { private JavaCodeGenRulesProvider readRulesProviderConfig(String rulesProviderLocation, ObjectMapper mapper) {
ApiConfiguration apiConfiguration = new ApiConfiguration(); JavaCodeGenRulesProvider javaCodeGenRules = null;
apiConfiguration.setServiceBaseClass("WordAPI","AbstractWordAPI"); try {
//default base class for services if not specified for a service javaCodeGenRules = mapper.readValue(new File(rulesProviderLocation), JavaCodeGenRulesProvider.class);
apiConfiguration.setServiceBaseClass("WordnikAPI"); } catch (IOException e) {
apiConfiguration.setModelBaseClass("WordnikObject"); throw new CodeGenerationException("Java codegen rules configuration could not be read from the location : " + rulesProviderLocation);
}
List<String> defaultModelImports = new ArrayList<String>(); return javaCodeGenRules;
defaultModelImports.add("com.wordnik.swagger.common.WordListType");
defaultModelImports.add("com.wordnik.swagger.common.StringValue");
defaultModelImports.add("com.wordnik.swagger.common.Size");
defaultModelImports.add("com.wordnik.swagger.common.WordnikObject");
List<String> defaultServiceImports = new ArrayList<String>();
defaultServiceImports.add("com.wordnik.swagger.model.Long");
defaultServiceImports.add("com.wordnik.swagger.common.*");
defaultServiceImports.add("com.wordnik.swagger.common.ext.*");
apiConfiguration.setDefaultModelImports(defaultModelImports);
apiConfiguration.setDefaultServiceImports(defaultServiceImports);
apiConfiguration.setModelPackageName("com.wordnik.swagger.model");
apiConfiguration.setApiPackageName("com.wordnik.swagger.api");
apiConfiguration.setApiKey("myKey");
apiConfiguration.setApiUrl("http://swagr.api.wordnik.com/v4/");
apiConfiguration.setApiListResource("/list");
return apiConfiguration;
} }
private LanguageConfiguration initializeLangConfig(String outputPath) { private ApiConfiguration readApiConfiguration(String apiConfigLocation, ObjectMapper mapper) {
LanguageConfiguration javaConfiguration = new LanguageConfiguration(); ApiConfiguration configuration = null;
try {
configuration = mapper.readValue(new File(apiConfigLocation), ApiConfiguration.class);
} catch (IOException e) {
throw new CodeGenerationException("Api configuration could not be read from the location : " + apiConfigLocation);
}
return configuration;
}
private LanguageConfiguration readLanguageConfiguration(String langConfigLocation, ObjectMapper mapper) {
LanguageConfiguration langConfig = null;
try {
langConfig = mapper.readValue(new File(langConfigLocation), LanguageConfiguration.class);
} catch (IOException e) {
throw new CodeGenerationException("Language configuration value could not be read from the location : " + langConfigLocation);
}
return langConfig;
}
private LanguageConfiguration initializeLangConfig(LanguageConfiguration javaConfiguration) {
javaConfiguration.setClassFileExtension(".java"); javaConfiguration.setClassFileExtension(".java");
javaConfiguration.setOutputDirectory(outputPath);
javaConfiguration.setTemplateLocation("conf/java/templates"); javaConfiguration.setTemplateLocation("conf/java/templates");
javaConfiguration.setExceptionPackageName("com.wordnik.swagger.exception"); javaConfiguration.setExceptionPackageName("com.wordnik.swagger.exception");
javaConfiguration.setAnnotationPackageName("com.wordnik.swagger.annotations"); javaConfiguration.setAnnotationPackageName("com.wordnik.swagger.annotations");