Skip to content

Commit 4d25d26

Browse files
committed
Merge remote-tracking branch 'origin/develop_2.0' into java-auth
Conflicts: modules/swagger-codegen/src/main/resources/Java/apiInvoker.mustache samples/client/petstore/java/src/main/java/io/swagger/client/ApiInvoker.java
2 parents 43cfc7d + 1657f2e commit 4d25d26

File tree

90 files changed

+2884
-924
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+2884
-924
lines changed

bin/swift-petstore.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/swift -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l swift -o samples/client/petstore/swift"
30+
31+
java -DappName=PetstoreClient $JAVA_OPTS -jar $executable $ags

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java

Lines changed: 99 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.File;
44
import java.util.ArrayList;
55
import java.util.Arrays;
6+
import java.util.Collection;
7+
import java.util.Collections;
68
import java.util.HashMap;
79
import java.util.HashSet;
810
import java.util.Iterator;
@@ -12,12 +14,17 @@
1214
import java.util.regex.Matcher;
1315
import java.util.regex.Pattern;
1416

17+
import javax.annotation.Nullable;
18+
1519
import org.apache.commons.lang.StringUtils;
1620
import org.slf4j.Logger;
1721
import org.slf4j.LoggerFactory;
1822

23+
import com.google.common.base.Function;
24+
import com.google.common.collect.Lists;
1925
import com.wordnik.swagger.codegen.examples.ExampleGenerator;
2026
import com.wordnik.swagger.models.ArrayModel;
27+
import com.wordnik.swagger.models.ComposedModel;
2128
import com.wordnik.swagger.models.Model;
2229
import com.wordnik.swagger.models.ModelImpl;
2330
import com.wordnik.swagger.models.Operation;
@@ -53,8 +60,9 @@
5360
import com.wordnik.swagger.models.properties.StringProperty;
5461
import com.wordnik.swagger.util.Json;
5562

63+
5664
public class DefaultCodegen {
57-
Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class);
65+
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class);
5866

5967
protected String outputFolder = "";
6068
protected Set<String> defaultIncludes = new HashSet<String>();
@@ -192,6 +200,7 @@ public String toVarName(String name) {
192200
}
193201

194202
public String toParamName(String name) {
203+
name = removeNonNameElementToCamelCase(name);
195204
if(reservedWords.contains(name)) {
196205
return escapeReservedWord(name);
197206
}
@@ -466,100 +475,28 @@ public CodegenModel fromModel(String name, Model model) {
466475
m.classVarName = toVarName(name);
467476
m.modelJson = Json.pretty(model);
468477
m.externalDocs = model.getExternalDocs();
469-
int count = 0;
470478
if(model instanceof ArrayModel) {
471479
ArrayModel am = (ArrayModel) model;
472480
ArrayProperty arrayProperty = new ArrayProperty(am.getItems());
473-
CodegenProperty cp = fromProperty(name, arrayProperty);
474-
if(cp.complexType != null && !defaultIncludes.contains(cp.complexType))
475-
m.imports.add(cp.complexType);
476-
m.parent = toInstantiationType(arrayProperty);
477-
String containerType = cp.containerType;
478-
if(instantiationTypes.containsKey(containerType))
479-
m.imports.add(instantiationTypes.get(containerType));
480-
if(typeMapping.containsKey(containerType)) {
481-
containerType = typeMapping.get(containerType);
482-
cp.containerType = containerType;
483-
m.imports.add(containerType);
484-
}
481+
addParentContainer(m, name, arrayProperty);
485482
}
486483
else if (model instanceof RefModel) {
487484
// TODO
488-
}
489-
else {
485+
} else if (model instanceof ComposedModel) {
486+
final ComposedModel composed = (ComposedModel) model;
487+
final RefModel parent = (RefModel) composed.getParent();
488+
final String parentModel = toModelName(parent.getSimpleRef());
489+
m.parent = parentModel;
490+
addImport(m, parentModel);
491+
final ModelImpl child = (ModelImpl) composed.getChild();
492+
addVars(m, child.getProperties(), child.getRequired());
493+
} else {
490494
ModelImpl impl = (ModelImpl) model;
491495
if(impl.getAdditionalProperties() != null) {
492496
MapProperty mapProperty = new MapProperty(impl.getAdditionalProperties());
493-
CodegenProperty cp = fromProperty(name, mapProperty);
494-
if(cp.complexType != null && !defaultIncludes.contains(cp.complexType))
495-
m.imports.add(cp.complexType);
496-
m.parent = toInstantiationType(mapProperty);
497-
String containerType = cp.containerType;
498-
if(instantiationTypes.containsKey(containerType))
499-
m.imports.add(instantiationTypes.get(containerType));
500-
if(typeMapping.containsKey(containerType)) {
501-
containerType = typeMapping.get(containerType);
502-
cp.containerType = containerType;
503-
m.imports.add(containerType);
504-
}
505-
}
506-
if(impl.getProperties() != null && impl.getProperties().size() > 0) {
507-
m.hasVars = true;
508-
m.hasEnums = false;
509-
for(String key: impl.getProperties().keySet()) {
510-
Property prop = impl.getProperties().get(key);
511-
512-
if(prop == null) {
513-
LOGGER.warn("null property for " + key);
514-
}
515-
else {
516-
CodegenProperty cp;
517-
try{
518-
cp = fromProperty(key, prop);
519-
}
520-
catch(Exception e) {
521-
System.out.println("failed to process model " + name);
522-
throw new RuntimeException(e);
523-
}
524-
cp.required = null;
525-
if(impl.getRequired() != null) {
526-
for(String req : impl.getRequired()) {
527-
if(key.equals(req))
528-
cp.required = true;
529-
}
530-
}
531-
if(cp.complexType != null && !defaultIncludes.contains(cp.complexType)) {
532-
m.imports.add(cp.complexType);
533-
}
534-
m.vars.add(cp);
535-
count += 1;
536-
if (cp.isEnum)
537-
m.hasEnums = true;
538-
if(count != impl.getProperties().keySet().size())
539-
cp.hasMore = new Boolean(true);
540-
if(cp.isContainer != null) {
541-
String arrayImport = typeMapping.get("array");
542-
if(arrayImport != null &&
543-
!languageSpecificPrimitives.contains(arrayImport) &&
544-
!defaultIncludes.contains(arrayImport))
545-
m.imports.add(arrayImport);
546-
}
547-
548-
if(cp.complexType != null &&
549-
!languageSpecificPrimitives.contains(cp.complexType) &&
550-
!defaultIncludes.contains(cp.complexType))
551-
m.imports.add(cp.complexType);
552-
553-
if(cp.baseType != null &&
554-
!languageSpecificPrimitives.contains(cp.baseType) &&
555-
!defaultIncludes.contains(cp.baseType))
556-
m.imports.add(cp.baseType);
557-
}
558-
}
559-
}
560-
else {
561-
m.emptyVars = true;
497+
addParentContainer(m, name, mapProperty);
562498
}
499+
addVars(m, impl.getProperties(), impl.getRequired());
563500
}
564501
return m;
565502
}
@@ -718,6 +655,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
718655
operationId = builder.toString();
719656
LOGGER.warn("generated operationId " + operationId);
720657
}
658+
operationId = removeNonNameElementToCamelCase(operationId);
721659
op.path = path;
722660
op.operationId = toOperationId(operationId);
723661
op.summary = escapeText(operation.getSummary());
@@ -1141,6 +1079,62 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
11411079
co.baseName = tag;
11421080
}
11431081

1082+
private void addParentContainer(CodegenModel m, String name, Property property) {
1083+
final CodegenProperty tmp = fromProperty(name, property);
1084+
addImport(m, tmp.complexType);
1085+
m.parent = toInstantiationType(property);
1086+
final String containerType = tmp.containerType;
1087+
final String instantiationType = instantiationTypes.get(containerType);
1088+
if (instantiationType != null) {
1089+
addImport(m, instantiationType);
1090+
}
1091+
final String mappedType = typeMapping.get(containerType);
1092+
if (mappedType != null) {
1093+
addImport(m, mappedType);
1094+
}
1095+
}
1096+
1097+
private void addImport(CodegenModel m, String type) {
1098+
if (type != null && !languageSpecificPrimitives.contains(type) && !defaultIncludes.contains(type)) {
1099+
m.imports.add(type);
1100+
}
1101+
}
1102+
1103+
private void addVars(CodegenModel m, Map<String, Property> properties, Collection<String> required) {
1104+
if (properties != null && properties.size() > 0) {
1105+
m.hasVars = true;
1106+
m.hasEnums = false;
1107+
final int totalCount = properties.size();
1108+
final Set<String> mandatory = required == null ? Collections.<String> emptySet() : new HashSet<String>(required);
1109+
int count = 0;
1110+
for (Map.Entry<String, Property> entry : properties.entrySet()) {
1111+
final String key = entry.getKey();
1112+
final Property prop = entry.getValue();
1113+
1114+
if (prop == null) {
1115+
LOGGER.warn("null property for " + key);
1116+
} else {
1117+
final CodegenProperty cp = fromProperty(key, prop);
1118+
cp.required = mandatory.contains(key) ? true : null;
1119+
if (cp.isEnum) {
1120+
m.hasEnums = true;
1121+
}
1122+
count += 1;
1123+
if (count != totalCount)
1124+
cp.hasMore = true;
1125+
if (cp.isContainer != null) {
1126+
addImport(m, typeMapping.get("array"));
1127+
}
1128+
addImport(m, cp.baseType);
1129+
addImport(m, cp.complexType);
1130+
m.vars.add(cp);
1131+
}
1132+
}
1133+
} else {
1134+
m.emptyVars = true;
1135+
}
1136+
}
1137+
11441138
/* underscore and camelize are copied from Twitter elephant bird
11451139
* https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
11461140
*/
@@ -1166,6 +1160,26 @@ public static String underscore(String word) {
11661160
return word;
11671161
}
11681162

1163+
/**
1164+
* Remove characters not suitable for variable or method name from the input and camelize it
1165+
* @param name
1166+
* @return
1167+
*/
1168+
public String removeNonNameElementToCamelCase(String name) {
1169+
String nonNameElementPattern = "[-_:;#]";
1170+
name = StringUtils.join(Lists.transform(Lists.newArrayList(name.split(nonNameElementPattern)), new Function<String, String>() {
1171+
@Nullable
1172+
@Override
1173+
public String apply(String input) {
1174+
return StringUtils.capitalize(input);
1175+
}
1176+
}), "");
1177+
if (name.length() > 0) {
1178+
name = name.substring(0, 1).toLowerCase() + name.substring(1);
1179+
}
1180+
return name;
1181+
}
1182+
11691183
public static String camelize(String word) {
11701184
return camelize(word, false);
11711185
}

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/CSharpClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public CSharpClientCodegen() {
8383
typeMapping.put("file", "string"); // path to file
8484
typeMapping.put("array", "List");
8585
typeMapping.put("map", "Dictionary");
86+
typeMapping.put("object", "Object");
8687

8788
}
8889

0 commit comments

Comments
 (0)