-
Notifications
You must be signed in to change notification settings - Fork 6k
Add usePromise parameter to JS client #2072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7e9a5f0
Add usePromise parameter to JS client
delenius 153faeb
Modify JS PetStore tests for prototypes change
delenius 802f9e4
revert to `Object` when model missing
fehguy e2d61ab
update parser version
fehguy e99e322
adde Date mapping, escape operationId
fehguy f539fdc
Merge pull request #2074 from swagger-api/issue-2073
fehguy cabe003
Merge pull request #2071 from delenius/prototype-tests
wing328 e825b95
Add usePromise parameter to JS client
delenius bf71e6b
Merge branch 'promises' of https://github.com/delenius/swagger-codege…
delenius 7301618
Add petstore tests for usePromises version
delenius aab96ec
Remove unused callback param when using promises
delenius dd29cf9
Use defaultValue for the usePromise CliOption
delenius d56d626
Remove wildcards from imports
delenius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/bin/sh | ||
|
||
SCRIPT="$0" | ||
|
||
while [ -h "$SCRIPT" ] ; do | ||
ls=`ls -ld "$SCRIPT"` | ||
link=`expr "$ls" : '.*-> \(.*\)$'` | ||
if expr "$link" : '/.*' > /dev/null; then | ||
SCRIPT="$link" | ||
else | ||
SCRIPT=`dirname "$SCRIPT"`/"$link" | ||
fi | ||
done | ||
|
||
if [ ! -d "${APP_DIR}" ]; then | ||
APP_DIR=`dirname "$SCRIPT"`/.. | ||
APP_DIR=`cd "${APP_DIR}"; pwd` | ||
fi | ||
|
||
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" | ||
|
||
if [ ! -f "$executable" ] | ||
then | ||
mvn clean package | ||
fi | ||
|
||
# if you've executed sbt assembly previously it will use that instead. | ||
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" | ||
ags="$@ generate -t modules/swagger-codegen/src/main/resources/javascript \ | ||
-i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l javascript \ | ||
-o samples/client/petstore/javascript-promise \ | ||
--additional-properties usePromises=true" | ||
|
||
java -DappName=PetstoreClient $JAVA_OPTS -jar $executable $ags |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,35 +1,19 @@ | ||||
package io.swagger.codegen.languages; | ||||
|
||||
import com.google.common.base.Strings; | ||||
|
||||
import io.swagger.codegen.CliOption; | ||||
import io.swagger.codegen.CodegenConfig; | ||||
import io.swagger.codegen.CodegenConstants; | ||||
import io.swagger.codegen.CodegenModel; | ||||
import io.swagger.codegen.CodegenOperation; | ||||
import io.swagger.codegen.CodegenProperty; | ||||
import io.swagger.codegen.CodegenType; | ||||
import io.swagger.codegen.DefaultCodegen; | ||||
import io.swagger.codegen.SupportingFile; | ||||
import io.swagger.codegen.*; | ||||
import io.swagger.models.*; | ||||
import io.swagger.models.properties.ArrayProperty; | ||||
import io.swagger.models.properties.MapProperty; | ||||
import io.swagger.models.properties.Property; | ||||
import io.swagger.models.properties.RefProperty; | ||||
|
||||
import java.io.File; | ||||
import java.util.ArrayList; | ||||
import java.util.Arrays; | ||||
import java.util.HashMap; | ||||
import java.util.HashSet; | ||||
import java.util.Iterator; | ||||
import java.util.List; | ||||
import java.util.Map; | ||||
|
||||
import org.apache.commons.lang.StringUtils; | ||||
import org.slf4j.Logger; | ||||
import org.slf4j.LoggerFactory; | ||||
|
||||
import java.io.File; | ||||
import java.util.*; | ||||
|
||||
public class JavascriptClientCodegen extends DefaultCodegen implements CodegenConfig { | ||||
@SuppressWarnings("hiding") | ||||
private static final Logger LOGGER = LoggerFactory.getLogger(JavascriptClientCodegen.class); | ||||
|
@@ -39,6 +23,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo | |||
private static final String PROJECT_DESCRIPTION = "projectDescription"; | ||||
private static final String PROJECT_VERSION = "projectVersion"; | ||||
private static final String PROJECT_LICENSE_NAME = "projectLicenseName"; | ||||
private static final String USE_PROMISES = "usePromises"; | ||||
|
||||
protected String projectName; | ||||
protected String moduleName; | ||||
|
@@ -47,7 +32,8 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo | |||
|
||||
protected String sourceFolder = "src"; | ||||
protected String localVariablePrefix = ""; | ||||
|
||||
protected boolean usePromises = false; | ||||
|
||||
public JavascriptClientCodegen() { | ||||
super(); | ||||
outputFolder = "generated-code/js"; | ||||
|
@@ -56,7 +42,7 @@ public JavascriptClientCodegen() { | |||
templateDir = "Javascript"; | ||||
apiPackage = "api"; | ||||
modelPackage = "model"; | ||||
|
||||
// reference: http://www.w3schools.com/js/js_reserved.asp | ||||
reservedWords = new HashSet<String>( | ||||
Arrays.asList( | ||||
|
@@ -96,6 +82,8 @@ public JavascriptClientCodegen() { | |||
"version of the project (Default: using info.version or \"1.0.0\")")); | ||||
cliOptions.add(new CliOption(PROJECT_LICENSE_NAME, | ||||
"name of the license the project uses (Default: using info.license.name)")); | ||||
cliOptions.add(new CliOption(USE_PROMISES, | ||||
"use Promises as return values from the client API, instead of superagent callbacks (Default: false)")); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of putting the default value in the description, you can use Line 79 in ee8547f
|
||||
} | ||||
|
||||
@Override | ||||
|
@@ -133,6 +121,7 @@ public void processOpts() { | |||
typeMapping.put("double", "Number"); | ||||
typeMapping.put("number", "Number"); | ||||
typeMapping.put("DateTime", "Date"); | ||||
typeMapping.put("Date", "Date"); | ||||
// binary not supported in JavaScript client right now, using String as a workaround | ||||
typeMapping.put("binary", "String"); | ||||
|
||||
|
@@ -161,7 +150,10 @@ public void preprocessSwagger(Swagger swagger) { | |||
if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) { | ||||
sourceFolder = (String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER); | ||||
} | ||||
|
||||
if (additionalProperties.containsKey(USE_PROMISES)) { | ||||
usePromises = Boolean.parseBoolean((String)additionalProperties.get(USE_PROMISES)); | ||||
} | ||||
|
||||
if (swagger.getInfo() != null) { | ||||
Info info = swagger.getInfo(); | ||||
if (StringUtils.isBlank(projectName) && info.getTitle() != null) { | ||||
|
@@ -204,6 +196,7 @@ public void preprocessSwagger(Swagger swagger) { | |||
additionalProperties.put(PROJECT_VERSION, projectVersion); | ||||
additionalProperties.put(CodegenConstants.LOCAL_VARIABLE_PREFIX, localVariablePrefix); | ||||
additionalProperties.put(CodegenConstants.SOURCE_FOLDER, sourceFolder); | ||||
additionalProperties.put(USE_PROMISES, usePromises); | ||||
|
||||
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json")); | ||||
supportingFiles.add(new SupportingFile("index.mustache", sourceFolder, "index.js")); | ||||
|
@@ -358,7 +351,7 @@ public String toOperationId(String operationId) { | |||
|
||||
// method name cannot use reserved keyword, e.g. return | ||||
if (reservedWords.contains(operationId)) { | ||||
throw new RuntimeException(operationId + " (reserved word) cannot be used as method name"); | ||||
operationId = escapeReservedWord(operationId); | ||||
} | ||||
|
||||
return camelize(sanitizeName(operationId), true); | ||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "swagger-petstore", | ||
"version": "1.0.0", | ||
"description": "This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters", | ||
"license": "Apache 2.0", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha --recursive" | ||
}, | ||
"dependencies": { | ||
"superagent": "1.7.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~2.3.4", | ||
"expect.js": "~0.3.1" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>io.swagger</groupId> | ||
<artifactId>swagger-petstore-javascript-promise</artifactId> | ||
<packaging>pom</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>Swagger Petstore - Javascript Client (Promise version)</name> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.2.1</version> | ||
<executions> | ||
<execution> | ||
<id>npm-install</id> | ||
<phase>pre-integration-test</phase> | ||
<goals> | ||
<goal>exec</goal> | ||
</goals> | ||
<configuration> | ||
<executable>npm</executable> | ||
<arguments> | ||
<argument>install</argument> | ||
</arguments> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>mocha</id> | ||
<phase>integration-test</phase> | ||
<goals> | ||
<goal>exec</goal> | ||
</goals> | ||
<configuration> | ||
<executable>npm</executable> | ||
<arguments> | ||
<argument>test</argument> | ||
</arguments> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefer not using wildcard import: https://google.github.io/styleguide/javaguide.html#s3.3.1-wildcard-imports
Ref: https://github.com/swagger-api/swagger-codegen/blob/master/CONTRIBUTING.md#style-guide