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 --> <!-- 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. -DoutputDir=../../api-server-lib/java/src/main/java/com/wordnik/swagger 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="${outputDir}/api"/> <mkdir dir="${outputDir}/api"/>
@ -49,9 +49,7 @@
<include name="**/*.jar"/> <include name="**/*.jar"/>
</fileset> </fileset>
</classpath> </classpath>
<arg value="${outputDir}"/>
<arg value="${apiServerRootDir}/config/apiConfiguration.json"/> <arg value="${apiServerRootDir}/config/apiConfiguration.json"/>
<arg value="${apiServerRootDir}/config/rulesProvider.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

@ -5,12 +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 org.codehaus.jackson.map.ObjectMapper;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/** /**
* User: ramesh * User: ramesh
@ -20,57 +19,62 @@ import java.util.List;
public class JavaLibCodeGen extends LibraryCodeGenerator { public class JavaLibCodeGen extends LibraryCodeGenerator {
public static void main(String[] args) { public static void main(String[] args) {
if(args.length < 3){ 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];
String configPath = args[1]; JavaLibCodeGen codeGenerator = new JavaLibCodeGen(configPath);
String rulesConfigPath = args[2];
JavaLibCodeGen codeGenerator = new JavaLibCodeGen(outputPath, configPath, rulesConfigPath);
codeGenerator.generateCode(); codeGenerator.generateCode();
} }
public JavaLibCodeGen(String outputPath, String configPath, String rulesConfigPath){ public JavaLibCodeGen(String configPath){
this.setApiConfig(readApiConfiguration(configPath)); final ObjectMapper mapper = new ObjectMapper();
this.setCodeGenRulesProvider(readRulesProviderConfig(rulesConfigPath)); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.setLanguageConfig(initializeLangConfig(outputPath)); 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.setNameGenerator(new CamelCaseNamingPolicyProvider()); this.setNameGenerator(new CamelCaseNamingPolicyProvider());
} }
private JavaCodeGenRulesProvider readRulesProviderConfig(String rulesProviderLocation) { private JavaCodeGenRulesProvider readRulesProviderConfig(String rulesProviderLocation, ObjectMapper mapper) {
ObjectMapper mapper = new ObjectMapper();
JavaCodeGenRulesProvider javaCodeGenRules = null; JavaCodeGenRulesProvider javaCodeGenRules = null;
try { try {
javaCodeGenRules = mapper.readValue(new File(rulesProviderLocation), JavaCodeGenRulesProvider.class); javaCodeGenRules = mapper.readValue(new File(rulesProviderLocation), JavaCodeGenRulesProvider.class);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
throw new CodeGenerationException("Java codegen rules configuration could not be read from the location : " + rulesProviderLocation); throw new CodeGenerationException("Java codegen rules configuration could not be read from the location : " + rulesProviderLocation);
} }
return javaCodeGenRules; return javaCodeGenRules;
} }
private ApiConfiguration readApiConfiguration(String apiConfigLocation) { private ApiConfiguration readApiConfiguration(String apiConfigLocation, ObjectMapper mapper) {
ObjectMapper mapper = new ObjectMapper();
ApiConfiguration configuration = null; ApiConfiguration configuration = null;
try { try {
configuration = mapper.readValue(new File(apiConfigLocation), ApiConfiguration.class); configuration = mapper.readValue(new File(apiConfigLocation), ApiConfiguration.class);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
throw new CodeGenerationException("Api configuration could not be read from the location : " + apiConfigLocation); throw new CodeGenerationException("Api configuration could not be read from the location : " + apiConfigLocation);
} }
return configuration; return configuration;
} }
private LanguageConfiguration initializeLangConfig(String outputPath) { private LanguageConfiguration readLanguageConfiguration(String langConfigLocation, ObjectMapper mapper) {
LanguageConfiguration javaConfiguration = new LanguageConfiguration(); 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");