Swagr codegen: Moving configurations to json file at apiConfiguration.json

This commit is contained in:
Deepak Michael 2011-08-02 22:24:09 +05:30
parent fe4a29ca7a
commit ed6e09c52f
2 changed files with 27 additions and 25 deletions

View File

@ -27,8 +27,8 @@
<!-- generates the classes -->
<target name="generate-java" depends="compile" description="generates APIs and model classes for java language">
<fail unless="outputPath.set">
Must specify the parameters: outputPath and apiServerRootDir eg. -DoutputDir=../../api-server-lib/java/src/main/java/com/wordnik/swagger
-DapiServerRootDir=../../api-server-lib/java/
Must specify the parameters: outputPath and apiServerRootDir
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)
</fail>
<mkdir dir="${outputDir}/api"/>
@ -49,9 +49,7 @@
<include name="**/*.jar"/>
</fileset>
</classpath>
<arg value="${outputDir}"/>
<arg value="${apiServerRootDir}/config/apiConfiguration.json"/>
<arg value="${apiServerRootDir}/config/rulesProvider.json"/>
</java>
<copy todir="${apiServerRootDir}" overwrite="true">
<fileset dir="conf/java/structure"/>

View File

@ -5,12 +5,11 @@ import com.wordnik.swagger.codegen.config.ApiConfiguration;
import com.wordnik.swagger.codegen.config.LanguageConfiguration;
import com.wordnik.swagger.codegen.config.common.CamelCaseNamingPolicyProvider;
import com.wordnik.swagger.exception.CodeGenerationException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* User: ramesh
@ -20,57 +19,62 @@ import java.util.List;
public class JavaLibCodeGen extends LibraryCodeGenerator {
public static void main(String[] args) {
if(args.length < 3){
throw new CodeGenerationException("Invalid number of arguments passed: No command line argument was passed to the program for output path");
if(args.length < 1){
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[1];
String rulesConfigPath = args[2];
JavaLibCodeGen codeGenerator = new JavaLibCodeGen(outputPath, configPath, rulesConfigPath);
String configPath = args[0];
JavaLibCodeGen codeGenerator = new JavaLibCodeGen(configPath);
codeGenerator.generateCode();
}
public JavaLibCodeGen(String outputPath, String configPath, String rulesConfigPath){
public JavaLibCodeGen(String configPath){
this.setApiConfig(readApiConfiguration(configPath));
this.setCodeGenRulesProvider(readRulesProviderConfig(rulesConfigPath));
this.setLanguageConfig(initializeLangConfig(outputPath));
final ObjectMapper mapper = new ObjectMapper();
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.setNameGenerator(new CamelCaseNamingPolicyProvider());
}
private JavaCodeGenRulesProvider readRulesProviderConfig(String rulesProviderLocation) {
ObjectMapper mapper = new ObjectMapper();
private JavaCodeGenRulesProvider readRulesProviderConfig(String rulesProviderLocation, ObjectMapper mapper) {
JavaCodeGenRulesProvider javaCodeGenRules = null;
try {
javaCodeGenRules = mapper.readValue(new File(rulesProviderLocation), JavaCodeGenRulesProvider.class);
} catch (IOException e) {
e.printStackTrace();
throw new CodeGenerationException("Java codegen rules configuration could not be read from the location : " + rulesProviderLocation);
}
return javaCodeGenRules;
}
private ApiConfiguration readApiConfiguration(String apiConfigLocation) {
ObjectMapper mapper = new ObjectMapper();
private ApiConfiguration readApiConfiguration(String apiConfigLocation, ObjectMapper mapper) {
ApiConfiguration configuration = null;
try {
configuration = mapper.readValue(new File(apiConfigLocation), ApiConfiguration.class);
} catch (IOException e) {
e.printStackTrace();
throw new CodeGenerationException("Api configuration could not be read from the location : " + apiConfigLocation);
}
return configuration;
}
private LanguageConfiguration initializeLangConfig(String outputPath) {
LanguageConfiguration javaConfiguration = new LanguageConfiguration();
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.setOutputDirectory(outputPath);
javaConfiguration.setTemplateLocation("conf/java/templates");
javaConfiguration.setExceptionPackageName("com.wordnik.swagger.exception");
javaConfiguration.setAnnotationPackageName("com.wordnik.swagger.annotations");