Skip to content

Commit 84928ab

Browse files
committed
Merge branch 'master' into java-global-security
2 parents 1c21730 + 860b551 commit 84928ab

File tree

44 files changed

+1056
-289
lines changed

Some content is hidden

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

44 files changed

+1056
-289
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
10441044
}
10451045
}
10461046
for (String i : imports) {
1047-
if (!defaultIncludes.contains(i) && !languageSpecificPrimitives.contains(i)) {
1047+
if (needToImport(i)) {
10481048
op.imports.add(i);
10491049
}
10501050
}
@@ -1317,6 +1317,12 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
13171317
return secs;
13181318
}
13191319

1320+
protected boolean needToImport(String type) {
1321+
return !defaultIncludes.contains(type)
1322+
&& !languageSpecificPrimitives.contains(type)
1323+
&& type.indexOf(".") < 0;
1324+
}
1325+
13201326
protected List<Map<String, Object>> toExamples(Map<String, Object> examples) {
13211327
if (examples == null) {
13221328
return null;
@@ -1420,7 +1426,7 @@ public static String underscore(String word) {
14201426
}
14211427

14221428
private void addImport(CodegenModel m, String type) {
1423-
if (type != null && !languageSpecificPrimitives.contains(type) && !defaultIncludes.contains(type)) {
1429+
if (type != null && needToImport(type)) {
14241430
m.imports.add(type);
14251431
}
14261432
}
@@ -1599,8 +1605,8 @@ protected CliOption buildLibraryCliOption(Map<String, String> supportedLibraries
15991605
* @return sanitized string
16001606
*/
16011607
public String sanitizeName(String name) {
1602-
// NOTE: performance wise, we should have written with 2 replaceAll to replace desired
1603-
// character with _ or empty character. Below aims to spell out different cases we've
1608+
// NOTE: performance wise, we should have written with 2 replaceAll to replace desired
1609+
// character with _ or empty character. Below aims to spell out different cases we've
16041610
// encountered so far and hopefully make it easier for others to add more special
16051611
// cases in the future.
16061612

@@ -1623,7 +1629,7 @@ public String sanitizeName(String name) {
16231629

16241630
// input name and age => input_name_and_age
16251631
name = name.replaceAll(" ", "_");
1626-
1632+
16271633
// remove everything else other than word, number and _
16281634
// $php_variable => php_variable
16291635
return name.replaceAll("[^a-zA-Z0-9_]", "");

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
3737
protected String artifactVersion = "1.0.0";
3838
protected String sourceFolder = "src/main/java";
3939
protected String localVariablePrefix = "";
40+
protected boolean fullJavaUtil = false;
41+
protected String javaUtilPrefix = "";
4042
protected Boolean serializableModel = false;
4143

4244
public JavaClientCodegen() {
@@ -81,6 +83,7 @@ public JavaClientCodegen() {
8183
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC));
8284
cliOptions.add(new CliOption(CodegenConstants.LOCAL_VARIABLE_PREFIX, CodegenConstants.LOCAL_VARIABLE_PREFIX_DESC));
8385
cliOptions.add(new CliOption(CodegenConstants.SERIALIZABLE_MODEL, CodegenConstants.SERIALIZABLE_MODEL_DESC));
86+
cliOptions.add(new CliOption("fullJavaUtil", "whether to use fully qualified name for classes under java.util (default to false)"));
8487

8588
supportedLibraries.put("<default>", "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2");
8689
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6");
@@ -107,7 +110,7 @@ public String getHelp() {
107110
@Override
108111
public void processOpts() {
109112
super.processOpts();
110-
113+
111114
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
112115
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
113116
} else {
@@ -152,13 +155,42 @@ public void processOpts() {
152155
// need to put back serializableModel (boolean) into additionalProperties as value in additionalProperties is string
153156
additionalProperties.put(CodegenConstants.SERIALIZABLE_MODEL, serializableModel);
154157

158+
if (additionalProperties.containsKey("fullJavaUtil")) {
159+
fullJavaUtil = Boolean.valueOf(additionalProperties.get("fullJavaUtil").toString());
160+
}
161+
if (fullJavaUtil) {
162+
javaUtilPrefix = "java.util.";
163+
}
164+
additionalProperties.put("fullJavaUtil", fullJavaUtil);
165+
additionalProperties.put("javaUtilPrefix", javaUtilPrefix);
166+
167+
if (fullJavaUtil) {
168+
typeMapping.put("array", "java.util.List");
169+
typeMapping.put("map", "java.util.Map");
170+
typeMapping.put("DateTime", "java.util.Date");
171+
typeMapping.remove("List");
172+
importMapping.remove("Date");
173+
importMapping.remove("Map");
174+
importMapping.remove("HashMap");
175+
importMapping.remove("Array");
176+
importMapping.remove("ArrayList");
177+
importMapping.remove("List");
178+
importMapping.remove("Set");
179+
importMapping.remove("DateTime");
180+
instantiationTypes.put("array", "java.util.ArrayList");
181+
instantiationTypes.put("map", "java.util.HashMap");
182+
}
183+
155184
this.sanitizeConfig();
156185

157186
final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator);
158187
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
188+
supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle"));
189+
supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle"));
190+
supportingFiles.add(new SupportingFile("gradle.properties.mustache", "", "gradle.properties"));
159191
supportingFiles.add(new SupportingFile("ApiClient.mustache", invokerFolder, "ApiClient.java"));
160192
supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java"));
161-
193+
162194
final String authFolder = (sourceFolder + File.separator + invokerPackage + ".auth").replace(".", File.separator);
163195
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java"));
164196
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
@@ -172,13 +204,11 @@ public void processOpts() {
172204
supportingFiles.add(new SupportingFile("Pair.mustache", invokerFolder, "Pair.java"));
173205
supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java"));
174206
}
175-
207+
176208
// library-specific files
177209
if ("okhttp-gson".equals(getLibrary())) {
178210
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
179211
supportingFiles.add(new SupportingFile("ApiCallback.mustache", invokerFolder, "ApiCallback.java"));
180-
// "build.gradle" is for development with Gradle
181-
supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle"));
182212
// "build.sbt" is for development with SBT
183213
supportingFiles.add(new SupportingFile("build.sbt.mustache", "", "build.sbt"));
184214
} else if ("retrofit".equals(getLibrary())) {
@@ -189,25 +219,25 @@ public void processOpts() {
189219
}
190220

191221
private void sanitizeConfig() {
192-
// Sanitize any config options here. We also have to update the additionalProperties because
222+
// Sanitize any config options here. We also have to update the additionalProperties because
193223
// the whole additionalProperties object is injected into the main object passed to the mustache layer
194-
224+
195225
this.setApiPackage(sanitizePackageName(apiPackage));
196226
if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
197227
this.additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);
198228
}
199-
229+
200230
this.setModelPackage(sanitizePackageName(modelPackage));
201231
if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
202232
this.additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
203233
}
204-
234+
205235
this.setInvokerPackage(sanitizePackageName(invokerPackage));
206236
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
207237
this.additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
208238
}
209239
}
210-
240+
211241
@Override
212242
public String escapeReservedWord(String name) {
213243
return "_" + name;
@@ -294,10 +324,22 @@ public String getTypeDeclaration(Property p) {
294324
public String toDefaultValue(Property p) {
295325
if (p instanceof ArrayProperty) {
296326
final ArrayProperty ap = (ArrayProperty) p;
297-
return String.format("new ArrayList<%s>()", getTypeDeclaration(ap.getItems()));
327+
final String pattern;
328+
if (fullJavaUtil) {
329+
pattern = "new java.util.ArrayList<%s>()";
330+
} else {
331+
pattern = "new ArrayList<%s>()";
332+
}
333+
return String.format(pattern, getTypeDeclaration(ap.getItems()));
298334
} else if (p instanceof MapProperty) {
299335
final MapProperty ap = (MapProperty) p;
300-
return String.format("new HashMap<String, %s>()", getTypeDeclaration(ap.getAdditionalProperties()));
336+
final String pattern;
337+
if (fullJavaUtil) {
338+
pattern = "new java.util.HashMap<String, %s>()";
339+
} else {
340+
pattern = "new HashMap<String, %s>()";
341+
}
342+
return String.format(pattern, getTypeDeclaration(ap.getAdditionalProperties()));
301343
}
302344
return super.toDefaultValue(p);
303345
}
@@ -308,7 +350,7 @@ public String getSwaggerType(Property p) {
308350
String type = null;
309351
if (typeMapping.containsKey(swaggerType)) {
310352
type = typeMapping.get(swaggerType);
311-
if (languageSpecificPrimitives.contains(type)) {
353+
if (languageSpecificPrimitives.contains(type) || type.indexOf(".") >= 0) {
312354
return type;
313355
}
314356
} else {
@@ -394,7 +436,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
394436
}
395437
return objs;
396438
}
397-
439+
398440
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
399441
if("retrofit".equals(getLibrary())) {
400442
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
@@ -418,6 +460,10 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
418460
return objs;
419461
}
420462

463+
protected boolean needToImport(String type) {
464+
return super.needToImport(type) && type.indexOf(".") < 0;
465+
}
466+
421467
private String findCommonPrefixOfVars(List<String> vars) {
422468
String prefix = StringUtils.getCommonPrefix(vars.toArray(new String[vars.size()]));
423469
// exclude trailing characters that should be part of a valid variable
@@ -426,7 +472,12 @@ private String findCommonPrefixOfVars(List<String> vars) {
426472
}
427473

428474
private String toEnumVarName(String value) {
429-
return value.replaceAll("\\W+", "_").toUpperCase();
475+
String var = value.replaceAll("\\W+", "_").toUpperCase();
476+
if (var.matches("\\d.*")) {
477+
return "_" + var;
478+
} else {
479+
return var;
480+
}
430481
}
431482

432483
private CodegenModel reconcileInlineEnums(CodegenModel codegenModel, CodegenModel parentCodegenModel) {

modules/swagger-codegen/src/main/resources/Java/api.mustache

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ import {{invokerPackage}}.Configuration;
66
import {{invokerPackage}}.Pair;
77
import {{invokerPackage}}.TypeRef;
88

9-
import {{modelPackage}}.*;
10-
11-
import java.util.*;
12-
139
{{#imports}}import {{import}};
1410
{{/imports}}
1511

16-
import java.io.File;
17-
import java.util.Map;
18-
import java.util.HashMap;
12+
{{^fullJavaUtil}}
13+
import java.util.*;
14+
{{/fullJavaUtil}}
1915

2016
{{>generatedAnnotation}}
2117
{{#operations}}
@@ -59,9 +55,9 @@ public class {{classname}} {
5955
.replaceAll("\\{" + "{{baseName}}" + "\\}", {{localVariablePrefix}}apiClient.escapeString({{{paramName}}}.toString())){{/pathParams}};
6056

6157
// query params
62-
List<Pair> {{localVariablePrefix}}queryParams = new ArrayList<Pair>();
63-
Map<String, String> {{localVariablePrefix}}headerParams = new HashMap<String, String>();
64-
Map<String, Object> {{localVariablePrefix}}formParams = new HashMap<String, Object>();
58+
{{javaUtilPrefix}}List<Pair> {{localVariablePrefix}}queryParams = new {{javaUtilPrefix}}ArrayList<Pair>();
59+
{{javaUtilPrefix}}Map<String, String> {{localVariablePrefix}}headerParams = new {{javaUtilPrefix}}HashMap<String, String>();
60+
{{javaUtilPrefix}}Map<String, Object> {{localVariablePrefix}}formParams = new {{javaUtilPrefix}}HashMap<String, Object>();
6561

6662
{{#queryParams}}
6763
{{localVariablePrefix}}queryParams.addAll({{localVariablePrefix}}apiClient.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
group = '{{groupId}}'
2+
version = '{{artifactVersion}}'
3+
4+
buildscript {
5+
repositories {
6+
jcenter()
7+
}
8+
dependencies {
9+
classpath 'com.android.tools.build:gradle:1.2.2'
10+
classpath 'com.github.dcendents:android-maven-plugin:1.2'
11+
}
12+
}
13+
14+
repositories {
15+
jcenter()
16+
}
17+
18+
19+
if(hasProperty('target') && target == 'android') {
20+
21+
apply plugin: 'com.android.library'
22+
apply plugin: 'com.github.dcendents.android-maven'
23+
24+
android {
25+
compileSdkVersion 22
26+
buildToolsVersion '22.0.0'
27+
defaultConfig {
28+
minSdkVersion 14
29+
targetSdkVersion 22
30+
}
31+
compileOptions {
32+
sourceCompatibility JavaVersion.VERSION_1_7
33+
targetCompatibility JavaVersion.VERSION_1_7
34+
}
35+
36+
// Rename the aar correctly
37+
libraryVariants.all { variant ->
38+
variant.outputs.each { output ->
39+
def outputFile = output.outputFile
40+
if (outputFile != null && outputFile.name.endsWith('.aar')) {
41+
def fileName = "${project.name}-${variant.baseName}-${version}.aar"
42+
output.outputFile = new File(outputFile.parent, fileName)
43+
}
44+
}
45+
}
46+
}
47+
48+
afterEvaluate {
49+
android.libraryVariants.all { variant ->
50+
def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
51+
task.description = "Create jar artifact for ${variant.name}"
52+
task.dependsOn variant.javaCompile
53+
task.from variant.javaCompile.destinationDir
54+
task.destinationDir = project.file("${project.buildDir}/outputs/jar")
55+
task.archiveName = "${project.name}-${variant.baseName}-${version}.jar"
56+
artifacts.add('archives', task);
57+
}
58+
}
59+
60+
task sourcesJar(type: Jar) {
61+
from android.sourceSets.main.java.srcDirs
62+
classifier = 'sources'
63+
}
64+
65+
artifacts {
66+
archives sourcesJar
67+
}
68+
69+
} else {
70+
71+
apply plugin: 'java'
72+
apply plugin: 'maven'
73+
74+
sourceCompatibility = JavaVersion.VERSION_1_7
75+
targetCompatibility = JavaVersion.VERSION_1_7
76+
77+
install {
78+
repositories.mavenInstaller {
79+
pom.artifactId = '{{artifactId}}'
80+
}
81+
}
82+
83+
task execute(type:JavaExec) {
84+
main = System.getProperty('mainClass')
85+
classpath = sourceSets.main.runtimeClasspath
86+
}
87+
}
88+
89+
ext {
90+
swagger_annotations_version = "1.5.0"
91+
jackson_version = "2.4.2"
92+
jersey_version = "1.18"
93+
jodatime_version = "2.3"
94+
junit_version = "4.8.1"
95+
}
96+
97+
dependencies {
98+
compile "io.swagger:swagger-annotations:$swagger_annotations_version"
99+
compile "com.sun.jersey:jersey-client:$jersey_version"
100+
compile "com.sun.jersey.contribs:jersey-multipart:$jersey_version"
101+
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
102+
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
103+
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
104+
compile "com.fasterxml.jackson.datatype:jackson-datatype-joda:2.1.5"
105+
compile "joda-time:joda-time:$jodatime_version"
106+
testCompile "junit:junit:$junit_version"
107+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Uncomment to build for Android
2+
#target = android

0 commit comments

Comments
 (0)