diff --git a/build.xml b/build.xml index efd5a883dd8..51719f1cebe 100644 --- a/build.xml +++ b/build.xml @@ -27,8 +27,8 @@ - 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) @@ -49,9 +49,7 @@ - - diff --git a/src/main/java/com/wordnik/swagger/codegen/config/java/JavaLibCodeGen.java b/src/main/java/com/wordnik/swagger/codegen/config/java/JavaLibCodeGen.java index 59282f52b99..016f752994c 100644 --- a/src/main/java/com/wordnik/swagger/codegen/config/java/JavaLibCodeGen.java +++ b/src/main/java/com/wordnik/swagger/codegen/config/java/JavaLibCodeGen.java @@ -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");