From 00349406f8addfe2463fce5901a66cd13495187b Mon Sep 17 00:00:00 2001 From: glederrey Date: Fri, 5 Feb 2016 12:16:27 +0100 Subject: [PATCH] FIX: Fix the flash client library. --- .../codegen/languages/FlashClientCodegen.java | 20 +++--- .../src/main/resources/flash/ApiInvoker.as | 27 +++++++- .../src/main/resources/flash/ApiUrlHelper.as | 2 +- .../src/main/resources/flash/README.txt | 65 +++++++++++++++++++ .../src/main/resources/flash/api.mustache | 2 +- 5 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/flash/README.txt diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlashClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlashClientCodegen.java index 2184e4013fe..02bcbd02b50 100755 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlashClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlashClientCodegen.java @@ -29,7 +29,7 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig protected String packageVersion; protected String invokerPackage = "io.swagger"; - protected String sourceFolder = "src/main/flex"; + protected String sourceFolder = "flash"; public FlashClientCodegen() { super(); @@ -83,7 +83,7 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig .defaultValue("1.0.0")); cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC)); cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, "source folder for generated " + - "code. e.g. src/main/flex")); + "code. e.g. flash")); } @@ -124,8 +124,7 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig //modelPackage = invokerPackage + File.separatorChar + "client" + File.separatorChar + "model"; //apiPackage = invokerPackage + File.separatorChar + "client" + File.separatorChar + "api"; - final String invokerFolder = (sourceFolder + File.separator + invokerPackage + File.separator - + "swagger" + File.separator).replace(".", File.separator).replace('.', File.separatorChar); + final String invokerFolder = (sourceFolder + File.separator + "src/" + invokerPackage + File.separator).replace(".", File.separator).replace('.', File.separatorChar); supportingFiles.add(new SupportingFile("ApiInvoker.as", invokerFolder + "common", "ApiInvoker.as")); supportingFiles.add(new SupportingFile("ApiUrlHelper.as", invokerFolder + "common", "ApiUrlHelper.as")); @@ -133,14 +132,15 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig supportingFiles.add(new SupportingFile("ListWrapper.as", invokerFolder + "common", "ListWrapper.as")); supportingFiles.add(new SupportingFile("SwaggerApi.as", invokerFolder + "common", "SwaggerApi.as")); supportingFiles.add(new SupportingFile("XMLWriter.as", invokerFolder + "common", "XMLWriter.as")); - supportingFiles.add(new SupportingFile("ApiError.as", invokerFolder + "exception", "ApiErrors.as")); + supportingFiles.add(new SupportingFile("ApiError.as", invokerFolder + "exception", "ApiError.as")); supportingFiles.add(new SupportingFile("ApiErrorCodes.as", invokerFolder + "exception", "ApiErrorCodes.as")); supportingFiles.add(new SupportingFile("ApiClientEvent.as", invokerFolder + "event", "ApiClientEvent.as")); supportingFiles.add(new SupportingFile("Response.as", invokerFolder + "event", "Response.as")); supportingFiles.add(new SupportingFile("build.properties", sourceFolder, "build.properties")); supportingFiles.add(new SupportingFile("build.xml", sourceFolder, "build.xml")); - supportingFiles.add(new SupportingFile("AirExecutorApp-app.xml", sourceFolder + File.separatorChar - + "bin", "AirExecutorApp-app.xml")); + supportingFiles.add(new SupportingFile("README.txt", sourceFolder, "README.txt")); + //supportingFiles.add(new SupportingFile("AirExecutorApp-app.xml", sourceFolder + File.separatorChar + // + "bin", "AirExecutorApp-app.xml")); supportingFiles.add(new SupportingFile("ASAXB-0.1.1.swc", sourceFolder + File.separatorChar + "lib", "ASAXB-0.1.1.swc")); supportingFiles.add(new SupportingFile("as3corelib.swc", sourceFolder + File.separatorChar @@ -181,13 +181,13 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig @Override public String apiFileFolder() { - return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar + return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar + "src/" + apiPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar); } @Override public String modelFileFolder() { - return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar + return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar + "src/" + modelPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar); } @@ -253,7 +253,7 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig } else if (p instanceof ArrayProperty) { return "new Array()"; } else { - return "null"; + return "NaN"; } } diff --git a/modules/swagger-codegen/src/main/resources/flash/ApiInvoker.as b/modules/swagger-codegen/src/main/resources/flash/ApiInvoker.as index 98d94052ec4..6d8466390a6 100644 --- a/modules/swagger-codegen/src/main/resources/flash/ApiInvoker.as +++ b/modules/swagger-codegen/src/main/resources/flash/ApiInvoker.as @@ -2,6 +2,19 @@ package io.swagger.common { import io.swagger.event.ApiClientEvent; import io.swagger.event.Response; +import flash.events.EventDispatcher; +import flash.utils.*; +import mx.rpc.AsyncToken; +import mx.rpc.events.ResultEvent; +import mx.rpc.events.FaultEvent; +import mx.utils.ObjectUtil; +import mx.rpc.http.HTTPService; +import mx.messaging.messages.HTTPRequestMessage; +import mx.messaging.ChannelSet; +import mx.messaging.channels.DirectHTTPChannel; +import asaxb.xml.bind.ASAXBContext; +import asaxb.xml.bind.Unmarshaller; + public class ApiInvoker extends EventDispatcher { private static const DELETE_DATA_DUMMY:String = "dummyDataRequiredForDeleteOverride"; @@ -99,7 +112,7 @@ public class ApiInvoker extends EventDispatcher { var qualifiedClassName:String = objDescriptor.@name; qualifiedClassName = qualifiedClassName.replace("::", "."); var className:String = qualifiedClassName.substring(qualifiedClassName.lastIndexOf(".") + 1); - className = className().toLowerCase() + className.substring(1); + className = className.toLowerCase() + className.substring(1); writer.xml.setName(className); for each(property in objDescriptor.elements("variable")) { @@ -178,7 +191,7 @@ public class ApiInvoker extends EventDispatcher { httpService.useProxy = true; var channelSet:ChannelSet = new ChannelSet(); - var httpChannel:HTTPChannel = new HTTPChannel(); + var httpChannel:DirectHTTPChannel = new DirectHTTPChannel(""); httpChannel.uri = ApiUrlHelper.getProxyUrl(_proxyHostName, _proxyPath); channelSet.addChannel(httpChannel); httpService.channelSet = channelSet; @@ -188,6 +201,11 @@ public class ApiInvoker extends EventDispatcher { } private function onApiRequestResult(event:ResultEvent):void { + + // I put this in comments. Here, it's trying to parse XML and it fails if it's not XML. + // Therefore, it's better to have the raw result that we can parse as we want. + // TODO: Create different parser (JSON, XML, etc.) + /* var completionListener:Function = event.token.completionListener; var result:Object = event.result; var resultType:Class = event.token.returnType; @@ -214,6 +232,9 @@ public class ApiInvoker extends EventDispatcher { } var response:Response = new Response(true, resultObject); + */ + + var response:Response = new Response(true, event.result); response.requestId = event.token.requestId; var successEventType:String = event.token.completionEventType != null ? event.token.completionEventType : ApiClientEvent.SUCCESS_EVENT; @@ -252,4 +273,4 @@ public class ApiInvoker extends EventDispatcher { } -} \ No newline at end of file +} diff --git a/modules/swagger-codegen/src/main/resources/flash/ApiUrlHelper.as b/modules/swagger-codegen/src/main/resources/flash/ApiUrlHelper.as index 4333c6c7e4c..8779610cd48 100644 --- a/modules/swagger-codegen/src/main/resources/flash/ApiUrlHelper.as +++ b/modules/swagger-codegen/src/main/resources/flash/ApiUrlHelper.as @@ -31,7 +31,7 @@ internal class ApiUrlHelper { } internal static function getProxyUrl(hostName: String, proxyPath: String): String{ - if (hostName(hostName.length - 1) == "/") //remove trailing slash + if (hostName..charAt(hostName.length) == "/") //remove trailing slash { hostName = hostName.substring(0, hostName.length - 1); } diff --git a/modules/swagger-codegen/src/main/resources/flash/README.txt b/modules/swagger-codegen/src/main/resources/flash/README.txt new file mode 100644 index 00000000000..c9a2a38d6a6 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/flash/README.txt @@ -0,0 +1,65 @@ +README for the Flash application. + +These instructions are given using the version 4.7 of Flash Builder. + +How to use the Flash client library: + +The code which is given to you is to be considered as a Library. Therefore, in Flash Builder, you have to: +1. Create an ActionScript Library Project. (Warning: If you are dealing with Files as parameter, you need to include the Adobe AIR libraries) +2. Copy the src folder from the flash library to the project. +3. Copy the lib folder. +4. Copy the build.properties and build.xml. (You can update the paths in build.properties) +5. Right+Click on the project folder and go to Properties. Then go to the tab 'ActionScript Library Build Path' and add these two SWC: 'as3corelib.swc' and 'ASAXB-0.1.1.swc'. (They are located in the folder lib) +6. Create the Flash Application + +How to use the Flash client library in an application: + +Using the Flash client library (especially getting the response message from the server) is a bit tricky. + +The response message is given through an EventDispatcher. Therefore, you have to create an EventDispatcher and listen to the endpoints that you're calling. Below, you will find a pseudo-code explaining how to do this. + +After creating an application in java, you will have a mxml file. In this mxml file, you need to declare a Script (given below) after the Declarations. + + + + diff --git a/modules/swagger-codegen/src/main/resources/flash/api.mustache b/modules/swagger-codegen/src/main/resources/flash/api.mustache index 7fa8cb03d0a..2f5ccfe0dbb 100644 --- a/modules/swagger-codegen/src/main/resources/flash/api.mustache +++ b/modules/swagger-codegen/src/main/resources/flash/api.mustache @@ -34,7 +34,7 @@ public class {{classname}} extends SwaggerApi { /* * Returns {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} */ - public function {{nickname}} ({{#allParams}}{{paramName}}: {{{dataType}}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}}, {{/hasMore}}{{/allParams}}): String { + public function {{nickname}} ({{#allParams}}{{paramName}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}): String { // create path and map variables var path: String = "{{path}}".replace(/{format}/g,"xml"){{#pathParams}}.replace("{" + "{{paramName}}" + "}", getApiInvoker().escapeString({{{paramName}}})){{/pathParams}};