diff --git a/conf/as3/structure/src/main/as3/com/wordnik/swagger/common/ApiInvoker.as b/conf/as3/structure/src/main/as3/com/wordnik/swagger/common/ApiInvoker.as index 51a32b196fc1..104814acde93 100644 --- a/conf/as3/structure/src/main/as3/com/wordnik/swagger/common/ApiInvoker.as +++ b/conf/as3/structure/src/main/as3/com/wordnik/swagger/common/ApiInvoker.as @@ -6,7 +6,8 @@ package com.wordnik.swagger.common import com.wordnik.swagger.event.ApiClientEvent; import com.wordnik.swagger.event.Response; import com.wordnik.swagger.common.ApiUserCredentials; - + import com.wordnik.swagger.model.LibraryReferences; + import flash.events.EventDispatcher; import flash.utils.Dictionary; import flash.utils.describeType; @@ -35,7 +36,8 @@ package com.wordnik.swagger.common private var _apiPath: String = "/v4"; public var _apiEventNotifier:EventDispatcher; - + public var _apiLibraryReferences:LibraryReferences; + private static const DELETE_DATA_DUMMY:String = "dummyDataRequiredForDeleteOverride"; private static const X_HTTP_OVERRIDE_KEY:String = "X-HTTP-Method-Override"; private static const CONTENT_TYPE_HEADER_KEY:String = "Content-Type"; diff --git a/conf/as3/templates/ReferencesObject.st b/conf/as3/templates/ReferencesObject.st new file mode 100644 index 000000000000..a9006951c98f --- /dev/null +++ b/conf/as3/templates/ReferencesObject.st @@ -0,0 +1,22 @@ +package $packageName$ { + +$imports:{ import | +import $import$; +}$ + + /** + * This class contains references for any classes that might not be directly referenced by the Api classes. + * The AS3 compiler will exclude any class that is not referenced directly somewhere in your code. + * This is an optimization that the compiler applies by design. + * This convenience class prevents the user from having to iclude an import and variable declaration for such classes. + * + * NOTE: This class is auto generated by the drive code generator program so please do not edit the class manually. + * @author deepak + * + */ + public class LibraryReferences { + + $fields:{ field | + private var $field.name$: $field.paramType$;$\r$}$ + } +} \ No newline at end of file diff --git a/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java b/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java index 10310a05ebaf..1f8a9f2f0daf 100644 --- a/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java +++ b/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java @@ -48,9 +48,9 @@ public class LibraryCodeGenerator { private static final String ENUM_OBJECT_TEMPLATE = "EnumObject"; private static final String WRAPPER_OBJECT_TEMPLATE = "WrapperObject"; - private static final String PACKAGE_NAME = "packageName"; - private ApiConfiguration config = null; - private LanguageConfiguration languageConfig = null; + protected static final String PACKAGE_NAME = "packageName"; + protected ApiConfiguration config = null; + protected LanguageConfiguration languageConfig = null; private SwaggerResourceDocReader apiMarshaller; protected DataTypeMappingProvider dataTypeMappingProvider; @@ -107,14 +107,15 @@ public class LibraryCodeGenerator { } generateModelClasses(resources, aTemplateGroup); generateModelClassesForInput(resources, aTemplateGroup); - if(languageConfig.isGenerateHelperEnums()){ + if(languageConfig.isHelperEnumRequired()){ generateEnumForAllowedValues(resources, aTemplateGroup); } - if(languageConfig.isGenerateOutputWrappers()) { + if(languageConfig.isOutputWrapperRequired()) { generateOutputWrappers(resources, aTemplateGroup); } generateAPIClasses(resources, aTemplateGroup); + generateMiscClasses(resources, aTemplateGroup); } /** @@ -324,8 +325,6 @@ public class LibraryCodeGenerator { } } } - - } /** @@ -408,7 +407,16 @@ public class LibraryCodeGenerator { writeFile(aFile, template.toString(), "Wrapper class for test data file"); } - private void writeFile(File aFile, String content, String classType){ + /** + * Override this method in the derived classes to generate language specific classes + * @param resources + * @param aTemplateGroup + */ + protected void generateMiscClasses(List resources, StringTemplateGroup aTemplateGroup) { + //nothing here in the base class + } + + protected void writeFile(File aFile, String content, String classType){ try{ System.out.println("Writing to the file " + aFile.getAbsolutePath()); FileWriter aWriter = new FileWriter(aFile); @@ -497,5 +505,4 @@ public class LibraryCodeGenerator { return configuration; } - } diff --git a/src/main/java/com/wordnik/swagger/codegen/config/LanguageConfiguration.java b/src/main/java/com/wordnik/swagger/codegen/config/LanguageConfiguration.java index a70dbbb16d07..f4d6a1c91f6c 100644 --- a/src/main/java/com/wordnik/swagger/codegen/config/LanguageConfiguration.java +++ b/src/main/java/com/wordnik/swagger/codegen/config/LanguageConfiguration.java @@ -41,9 +41,9 @@ public class LanguageConfiguration { private String annotationPackageName; - private boolean generateHelperEnums = true; + private boolean isHelperEnumRequired = true; - private boolean generateOutputWrappers = false; + private boolean isOutputWrapperRequired = false; public String getClassFileExtension() { return classFileExtension; @@ -114,19 +114,19 @@ public class LanguageConfiguration { this.libraryHome = libraryHome; } - public void setGenerateHelperEnums(boolean generateHelperEnums) { - this.generateHelperEnums = generateHelperEnums; + public void setHelperEnumRequired(boolean helperEnumRequired) { + this.isHelperEnumRequired = helperEnumRequired; } - public boolean isGenerateHelperEnums() { - return generateHelperEnums; + public boolean isHelperEnumRequired() { + return isHelperEnumRequired; } - public void setGenerateOutputWrappers(boolean generateOutputWrappers) { - this.generateOutputWrappers = generateOutputWrappers; + public void setOutputWrapperRequired(boolean outputWrapperRequired) { + this.isOutputWrapperRequired = outputWrapperRequired; } - public boolean isGenerateOutputWrappers() { - return generateOutputWrappers; + public boolean isOutputWrapperRequired() { + return isOutputWrapperRequired; } } diff --git a/src/main/java/com/wordnik/swagger/codegen/config/as3/As3LibCodeGen.java b/src/main/java/com/wordnik/swagger/codegen/config/as3/As3LibCodeGen.java index 473bb03ff210..504c3f99db9a 100644 --- a/src/main/java/com/wordnik/swagger/codegen/config/as3/As3LibCodeGen.java +++ b/src/main/java/com/wordnik/swagger/codegen/config/as3/As3LibCodeGen.java @@ -2,11 +2,17 @@ package com.wordnik.swagger.codegen.config.as3; import com.wordnik.swagger.codegen.LibraryCodeGenerator; import com.wordnik.swagger.codegen.config.LanguageConfiguration; -import com.wordnik.swagger.codegen.config.common.CamelCaseNamingPolicyProvider; import com.wordnik.swagger.codegen.exception.CodeGenerationException; +import com.wordnik.swagger.codegen.resource.Model; +import com.wordnik.swagger.codegen.resource.ModelField; +import com.wordnik.swagger.codegen.resource.Resource; import com.wordnik.swagger.codegen.util.FileUtil; +import org.antlr.stringtemplate.StringTemplate; +import org.antlr.stringtemplate.StringTemplateGroup; import java.io.File; +import java.util.ArrayList; +import java.util.List; /** * User: deepakmichael @@ -14,6 +20,8 @@ import java.io.File; * Time: 5:02 PM */ public class As3LibCodeGen extends LibraryCodeGenerator{ + protected static final String MANIFEST_OBJECT_TEMPLATE = "ReferencesObject"; + public static void main(String[] args) { if(args.length < 1){ throw new CodeGenerationException("Invalid number of arguments passed: No command line argument was passed to the program for config json"); @@ -68,18 +76,56 @@ public class As3LibCodeGen extends LibraryCodeGenerator{ //create ouput directories FileUtil.createOutputDirectories(as3Configuration.getModelClassLocation(), as3Configuration.getClassFileExtension()); FileUtil.createOutputDirectories(as3Configuration.getResourceClassLocation(), as3Configuration.getClassFileExtension()); -/* - FileUtil.clearFolder(as3Configuration.getLibraryHome() + "/src/main/java/com/wordnik/swagger/runtime"); - FileUtil.createOutputDirectories(as3Configuration.getLibraryHome() + "/src/main/java/com/wordnik/swagger/runtime", "as"); -*/ FileUtil.clearFolder(as3Configuration.getLibraryHome() + "/src/main/as3/com/wordnik/swagger/common"); FileUtil.clearFolder(as3Configuration.getLibraryHome() + "/src/main/as3/com/wordnik/swagger/exception"); FileUtil.clearFolder(as3Configuration.getLibraryHome() + "/src/main/as3/com/wordnik/swagger/event"); FileUtil.copyDirectory(new File(as3Configuration.getStructureLocation()), new File(as3Configuration.getLibraryHome())); - as3Configuration.setGenerateHelperEnums(false); - as3Configuration.setGenerateOutputWrappers(true); + as3Configuration.setHelperEnumRequired(false); + as3Configuration.setOutputWrapperRequired(true); return as3Configuration; } + protected void generateMiscClasses(List resources, StringTemplateGroup aTemplateGroup) { + generateReferencesObject(resources, aTemplateGroup); + } + + private void generateReferencesObject(List resources, StringTemplateGroup templateGroup) { + StringTemplate template = templateGroup.getInstanceOf(MANIFEST_OBJECT_TEMPLATE); + if(template == null){ + System.out.println("WrapperObject template not found to generate output wrappers"); + return; + } + Model referencesModel = new Model(); + List refFields = new ArrayList(); + ModelField refModelField; + for(Resource resource: resources) { + for(Model model : resource.getModels()){ + + for(ModelField modelField : model.getFields()){ + final String collectionItemType = modelField.getFieldDefinition().getCollectionItemType(); + if(collectionItemType != null){ + refModelField = new ModelField(); + refModelField.setName(modelField.getName()); + refModelField.setParamType(collectionItemType); + refFields.add(refModelField); + } + } + } + } + List imports = new ArrayList(); + imports.addAll(this.config.getDefaultModelImports()); + referencesModel.setFields(refFields); + referencesModel.setName("LibraryReferences"); + template = templateGroup.getInstanceOf(MANIFEST_OBJECT_TEMPLATE); + template.setAttribute("model", referencesModel); + template.setAttribute("fields", referencesModel.getFields()); + template.setAttribute("imports", imports); + template.setAttribute("annotationPackageName", languageConfig.getAnnotationPackageName()); + template.setAttribute("extends", config.getDefaultModelBaseClass()); + template.setAttribute("className", referencesModel.getGenratedClassName()); + template.setAttribute(PACKAGE_NAME, config.getModelPackageName()); + File aFile = new File(languageConfig.getModelClassLocation()+referencesModel.getGenratedClassName()+languageConfig.getClassFileExtension()); + writeFile(aFile, template.toString(), "Model class"); + } }