Skip to content

Commit 775149d

Browse files
authored
Merge branch 'master' into pr_453_regresion_fix
2 parents c290272 + f2410e9 commit 775149d

File tree

22 files changed

+175
-77
lines changed

22 files changed

+175
-77
lines changed

src/main/java/io/swagger/codegen/v3/generators/java/Spring2Codegen.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class Spring2Codegen extends AbstractJavaCodegen implements BeanValidatio
4242
public static final String SPRING_CLOUD_LIBRARY = "spring-cloud";
4343
public static final String IMPLICIT_HEADERS = "implicitHeaders";
4444
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig";
45+
public static final String TARGET_OPENFEIGN = "generateForOpenFeign";
4546

4647
protected String title = "swagger-petstore";
4748
protected String configPackage = "io.swagger.configuration";
@@ -58,6 +59,7 @@ public class Spring2Codegen extends AbstractJavaCodegen implements BeanValidatio
5859
protected boolean implicitHeaders = false;
5960
protected boolean swaggerDocketConfig = false;
6061
protected boolean useOptional = false;
62+
protected boolean openFeign = false;
6163

6264
public Spring2Codegen() {
6365
super();
@@ -88,6 +90,7 @@ public Spring2Codegen() {
8890
cliOptions.add(CliOption.newBoolean(SWAGGER_DOCKET_CONFIG, "Generate Spring Swagger Docket configuration class."));
8991
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL,
9092
"Use Optional container for optional parameters"));
93+
cliOptions.add(CliOption.newBoolean(TARGET_OPENFEIGN,"Generate for usage with OpenFeign (instead of feign)"));
9194

9295
supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
9396
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
@@ -200,6 +203,10 @@ public void processOpts() {
200203
this.setUseOptional(convertPropertyToBoolean(USE_OPTIONAL));
201204
}
202205

206+
if (additionalProperties.containsKey(TARGET_OPENFEIGN)) {
207+
this.setOpenFeign(convertPropertyToBoolean(TARGET_OPENFEIGN));
208+
}
209+
203210
if (useBeanValidation) {
204211
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
205212
}
@@ -316,6 +323,10 @@ public void processOpts() {
316323
additionalProperties.put(RESPONSE_WRAPPER, "Callable");
317324
}
318325

326+
if(this.openFeign){
327+
additionalProperties.put("isOpenFeign", "true");
328+
}
329+
319330
// Some well-known Spring or Spring-Cloud response wrappers
320331
switch (this.responseWrapper) {
321332
case "Future":
@@ -745,4 +756,8 @@ public void setUseBeanValidation(boolean useBeanValidation) {
745756
public void setUseOptional(boolean useOptional) {
746757
this.useOptional = useOptional;
747758
}
759+
760+
public void setOpenFeign(boolean openFeign) {
761+
this.openFeign = openFeign;
762+
}
748763
}

src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
6161
public static final String SPRING_CLOUD_LIBRARY = "spring-cloud";
6262
public static final String IMPLICIT_HEADERS = "implicitHeaders";
6363
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig";
64+
public static final String TARGET_OPENFEIGN = "generateForOpenFeign";
6465

6566
protected String title = "swagger-petstore";
6667
protected String configPackage = "io.swagger.configuration";
@@ -77,6 +78,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
7778
protected boolean implicitHeaders = false;
7879
protected boolean swaggerDocketConfig = false;
7980
protected boolean useOptional = false;
81+
protected boolean openFeign = false;
8082

8183
public SpringCodegen() {
8284
super();
@@ -107,6 +109,7 @@ public SpringCodegen() {
107109
cliOptions.add(CliOption.newBoolean(SWAGGER_DOCKET_CONFIG, "Generate Spring Swagger Docket configuration class."));
108110
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL,
109111
"Use Optional container for optional parameters"));
112+
cliOptions.add(CliOption.newBoolean(TARGET_OPENFEIGN,"Generate for usage with OpenFeign (instead of feign)"));
110113

111114
supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
112115
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
@@ -219,6 +222,10 @@ public void processOpts() {
219222
this.setUseOptional(convertPropertyToBoolean(USE_OPTIONAL));
220223
}
221224

225+
if (additionalProperties.containsKey(TARGET_OPENFEIGN)) {
226+
this.setOpenFeign(convertPropertyToBoolean(TARGET_OPENFEIGN));
227+
}
228+
222229
if (useBeanValidation) {
223230
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
224231
}
@@ -335,6 +342,10 @@ public void processOpts() {
335342
additionalProperties.put(RESPONSE_WRAPPER, "Callable");
336343
}
337344

345+
if(this.openFeign){
346+
additionalProperties.put("isOpenFeign", "true");
347+
}
348+
338349
// Some well-known Spring or Spring-Cloud response wrappers
339350
switch (this.responseWrapper) {
340351
case "Future":
@@ -764,4 +775,8 @@ public void setUseBeanValidation(boolean useBeanValidation) {
764775
public void setUseOptional(boolean useOptional) {
765776
this.useOptional = useOptional;
766777
}
778+
779+
public void setOpenFeign(boolean openFeign) {
780+
this.openFeign = openFeign;
781+
}
767782
}

src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
import java.util.Map;
1818
import java.util.Set;
1919

20-
public abstract class AbstractKotlinCodegen extends DefaultCodegenConfig {
20+
public abstract class AbstractKotlinCodegen extends DefaultCodegenConfig {
2121

2222
private static Logger LOGGER = LoggerFactory.getLogger(AbstractKotlinCodegen.class);
2323

2424
private Set<String> instantiationLibraryFunction;
2525

26-
27-
2826
protected String artifactId;
2927
protected String artifactVersion = "1.0.0";
3028
protected String groupId = "io.swagger";
@@ -41,7 +39,7 @@ public AbstractKotlinCodegen() {
4139
super();
4240
supportsInheritance = true;
4341

44-
languageSpecificPrimitives = new HashSet<String>(Arrays.asList(
42+
languageSpecificPrimitives = new HashSet<>(Arrays.asList(
4543
"kotlin.Any",
4644
"kotlin.Byte",
4745
"kotlin.Short",
@@ -60,8 +58,9 @@ public AbstractKotlinCodegen() {
6058

6159
// this includes hard reserved words defined by https://github.com/JetBrains/kotlin/blob/master/core/descriptors/src/org/jetbrains/kotlin/renderer/KeywordStringsGenerated.java
6260
// as well as keywords from https://kotlinlang.org/docs/reference/keyword-reference.html
63-
reservedWords = new HashSet<String>(Arrays.asList(
61+
reservedWords = new HashSet<>(Arrays.asList(
6462
"abstract",
63+
"actual",
6564
"annotation",
6665
"as",
6766
"break",
@@ -78,6 +77,7 @@ public AbstractKotlinCodegen() {
7877
"do",
7978
"else",
8079
"enum",
80+
"expect",
8181
"external",
8282
"false",
8383
"final",
@@ -126,7 +126,7 @@ public AbstractKotlinCodegen() {
126126
"while"
127127
));
128128

129-
defaultIncludes = new HashSet<String>(Arrays.asList(
129+
defaultIncludes = new HashSet<>(Arrays.asList(
130130
"kotlin.Byte",
131131
"kotlin.Short",
132132
"kotlin.Int",
@@ -141,12 +141,12 @@ public AbstractKotlinCodegen() {
141141
"kotlin.collections.Map"
142142
));
143143

144-
instantiationLibraryFunction = new HashSet<String>(Arrays.asList(
144+
instantiationLibraryFunction = new HashSet<>(Arrays.asList(
145145
"arrayOf",
146146
"mapOf"
147147
));
148148

149-
typeMapping = new HashMap<String, String>();
149+
typeMapping = new HashMap<>();
150150
typeMapping.put("string", "kotlin.String");
151151
typeMapping.put("boolean", "kotlin.Boolean");
152152
typeMapping.put("integer", "kotlin.Int");
@@ -169,7 +169,7 @@ public AbstractKotlinCodegen() {
169169
instantiationTypes.put("list", "arrayOf");
170170
instantiationTypes.put("map", "mapOf");
171171

172-
importMapping = new HashMap<String, String>();
172+
importMapping = new HashMap<>();
173173
importMapping.put("BigDecimal", "java.math.BigDecimal");
174174
importMapping.put("UUID", "java.util.UUID");
175175
importMapping.put("File", "java.io.File");
@@ -272,7 +272,7 @@ public void setEnumPropertyNaming(final String enumPropertyNamingType) {
272272
public String getTypeDeclaration(Schema propertySchema) {
273273
if (propertySchema instanceof ArraySchema) {
274274
return getArrayTypeDeclaration((ArraySchema) propertySchema);
275-
} else if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) {
275+
} else if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) {
276276
Schema inner = (Schema) propertySchema.getAdditionalProperties();
277277
if (inner == null) {
278278
LOGGER.warn(propertySchema.getName() + "(map property) does not have a proper inner type defined");
@@ -444,7 +444,7 @@ public String toEnumVarName(String value, String datatype) {
444444
break;
445445
}
446446

447-
if (reservedWords.contains(modified)) {
447+
if (isReservedWord(modified)) {
448448
return escapeReservedWord(modified);
449449
}
450450

@@ -498,7 +498,7 @@ public String toModelName(final String name) {
498498
String modifiedName = name.replaceAll("\\.", "");
499499
modifiedName = sanitizeKotlinSpecificNames(modifiedName);
500500

501-
if (reservedWords.contains(modifiedName)) {
501+
if (isReservedWord(modifiedName)) {
502502
modifiedName = escapeReservedWord(modifiedName);
503503
}
504504

@@ -578,11 +578,11 @@ protected boolean isReservedWord(String word) {
578578
protected boolean needToImport(String type) {
579579
// provides extra protection against improperly trying to import language primitives and java types
580580
boolean imports =
581-
!type.startsWith("kotlin.") &&
582-
!type.startsWith("java.") &&
583-
!defaultIncludes.contains(type) &&
584-
!languageSpecificPrimitives.contains(type) &&
585-
!instantiationLibraryFunction.contains(type);
581+
!type.startsWith("kotlin.") &&
582+
!type.startsWith("java.") &&
583+
!defaultIncludes.contains(type) &&
584+
!languageSpecificPrimitives.contains(type) &&
585+
!instantiationLibraryFunction.contains(type);
586586

587587
return imports;
588588
}

src/main/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegen.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package io.swagger.codegen.v3.generators.kotlin;
22

3+
import com.github.jknack.handlebars.Handlebars;
4+
import com.github.jknack.handlebars.helper.ConditionalHelpers;
35
import io.swagger.codegen.v3.CliOption;
46
import io.swagger.codegen.v3.CodegenType;
57
import io.swagger.codegen.v3.SupportingFile;
8+
import org.apache.commons.lang3.StringUtils;
69
import org.slf4j.Logger;
710
import org.slf4j.LoggerFactory;
811

912
import java.io.File;
1013
import java.util.HashMap;
1114
import java.util.Map;
1215

13-
import org.apache.commons.lang3.StringUtils;
14-
1516
public class KotlinClientCodegen extends AbstractKotlinCodegen {
1617

17-
public static final String DATE_LIBRARY = "dateLibrary";
18+
public static final String DATE_LIBRARY = "dateLibrary";
1819
private static Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class);
1920

2021
protected String dateLibrary = DateLibrary.JAVA8.value;
@@ -57,6 +58,12 @@ public KotlinClientCodegen() {
5758
cliOptions.add(dateLibrary);
5859
}
5960

61+
@Override
62+
public void addHandlebarHelpers(Handlebars handlebars) {
63+
super.addHandlebarHelpers(handlebars);
64+
handlebars.registerHelpers(ConditionalHelpers.class);
65+
}
66+
6067
@Override
6168
public String getDefaultTemplateDir() {
6269
return "kotlin-client";
@@ -80,7 +87,7 @@ public void setDateLibrary(String library) {
8087

8188
@Override
8289
public void processOpts() {
83-
super.processOpts();
90+
super.processOpts();
8491

8592
if (StringUtils.isBlank(templateDir)) {
8693
embeddedTemplateDir = templateDir = getTemplateDir();
@@ -90,7 +97,7 @@ public void processOpts() {
9097
setDateLibrary(additionalProperties.get(DATE_LIBRARY).toString());
9198
}
9299

93-
if (DateLibrary.THREETENBP.value.equals(dateLibrary)) {
100+
if (DateLibrary.THREETENBP.value.equals(dateLibrary)) {
94101
additionalProperties.put(DateLibrary.THREETENBP.value, true);
95102
typeMapping.put("date", "LocalDate");
96103
typeMapping.put("DateTime", "LocalDateTime");
@@ -123,4 +130,4 @@ public void processOpts() {
123130
supportingFiles.add(new SupportingFile("infrastructure/Serializer.kt.mustache", infrastructureFolder, "Serializer.kt"));
124131
supportingFiles.add(new SupportingFile("infrastructure/Errors.kt.mustache", infrastructureFolder, "Errors.kt"));
125132
}
126-
}
133+
}

src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAngularClientCodegen.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
3939
public static final String SNAPSHOT = "snapshot";
4040
public static final String WITH_INTERFACES = "withInterfaces";
4141
public static final String NG_VERSION = "ngVersion";
42+
public static final String NG_PACKAGR = "useNgPackagr";
4243

4344
protected String npmName = null;
4445
protected String npmVersion = "1.0.0";
@@ -102,19 +103,9 @@ public void processOpts() {
102103
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
103104
supportingFiles.add(new SupportingFile("encoder.mustache", getIndexDirectory(), "encoder.ts"));
104105
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
106+
supportingFiles.add(new SupportingFile("npmignore", "", ".npmignore"));
105107
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
106108

107-
if (additionalProperties.containsKey(NPM_NAME)) {
108-
addNpmPackageGeneration();
109-
}
110-
111-
if (additionalProperties.containsKey(WITH_INTERFACES)) {
112-
boolean withInterfaces = Boolean.parseBoolean(additionalProperties.get(WITH_INTERFACES).toString());
113-
if (withInterfaces) {
114-
apiTemplateFiles.put("apiInterface.mustache", "Interface.ts");
115-
}
116-
}
117-
118109
// determine NG version
119110
SemVer ngVersion;
120111
if (additionalProperties.containsKey(NG_VERSION)) {
@@ -125,13 +116,27 @@ public void processOpts() {
125116
LOGGER.info(" (you can select the angular version by setting the additionalProperty ngVersion)");
126117
}
127118
additionalProperties.put(NG_VERSION, ngVersion);
119+
additionalProperties.put(NG_PACKAGR, ngVersion.atLeast("8.0.0"));
128120
additionalProperties.put("useRxJS6", ngVersion.atLeast("6.0.0"));
129121
additionalProperties.put("injectionToken", ngVersion.atLeast("4.0.0") ? "InjectionToken" : "OpaqueToken");
130122
additionalProperties.put("injectionTokenTyped", ngVersion.atLeast("4.0.0"));
131123
additionalProperties.put("useHttpClient", ngVersion.atLeast("4.3.0"));
124+
additionalProperties.put("useHttpClientPackage", ngVersion.atLeast("4.3.0") && !ngVersion.atLeast("8.0.0"));
132125
if (!ngVersion.atLeast("4.3.0")) {
133126
supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts"));
134127
}
128+
129+
if (additionalProperties.containsKey(NPM_NAME)) {
130+
addNpmPackageGeneration();
131+
}
132+
133+
if (additionalProperties.containsKey(WITH_INTERFACES)) {
134+
boolean withInterfaces = Boolean.parseBoolean(additionalProperties.get(WITH_INTERFACES).toString());
135+
if (withInterfaces) {
136+
apiTemplateFiles.put("apiInterface.mustache", "Interface.ts");
137+
}
138+
}
139+
135140
}
136141

137142
private void addNpmPackageGeneration() {
@@ -158,6 +163,10 @@ private void addNpmPackageGeneration() {
158163
supportingFiles.add(new SupportingFile("package.mustache", getIndexDirectory(), "package.json"));
159164
supportingFiles.add(new SupportingFile("typings.mustache", getIndexDirectory(), "typings.json"));
160165
supportingFiles.add(new SupportingFile("tsconfig.mustache", getIndexDirectory(), "tsconfig.json"));
166+
if (additionalProperties.containsKey(NG_PACKAGR)
167+
&& Boolean.valueOf(additionalProperties.get(NG_PACKAGR).toString())) {
168+
supportingFiles.add(new SupportingFile("ng-package.mustache", getIndexDirectory(), "ng-package.json"));
169+
}
161170
}
162171

163172
private String getIndexDirectory() {

src/main/resources/handlebars/Java/pojo.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{#description}}{{#useOas2}}@ApiModel{{/useOas2}}{{^useOas2}}@Schema{{/useOas2}}(description = "{{{description}}}"){{/description}}
55
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
66

7-
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcelableModel}}implements Parcelable {{#serializableModel}}, Serializable {{/serializableModel}}{{#interfaceModels}}{{#@first}}, {{/@first}}{{name}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable{{#interfaceModels}}, {{name}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/serializableModel}}{{^serializableModel}}{{#interfaceModels}}{{#@first}}implements {{/@first}}{{name}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/serializableModel}}{{/parcelableModel}}{
7+
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcelableModel}}implements Parcelable {{#serializableModel}}, Serializable {{/serializableModel}}{{#interfaceModels}}{{#@first}}, {{/@first}}{{classname}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable{{#interfaceModels}}, {{classname}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/serializableModel}}{{^serializableModel}}{{#interfaceModels}}{{#@first}}implements {{/@first}}{{classname}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/serializableModel}}{{/parcelableModel}}{
88
{{#serializableModel}}
99
private static final long serialVersionUID = 1L;
1010
{{/serializableModel}}

src/main/resources/handlebars/JavaSpring/libraries/spring-cloud/Application.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ package io.swagger;
33
import feign.Logger;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.boot.builder.SpringApplicationBuilder;
6-
import org.springframework.cloud.netflix.feign.EnableFeignClients;
6+
{{^isOpenFeign}}
7+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
8+
{{/isOpenFeign}}
9+
{{#isOpenFeign}}
10+
import org.springframework.cloud.openfeign.EnableFeignClients;
11+
{{/isOpenFeign}}
712
import org.springframework.context.annotation.Bean;
813

914
@SpringBootApplication

0 commit comments

Comments
 (0)