Skip to content

Commit 983536a

Browse files
authored
Merge branch 'master' into bug/fix-for-663-remove-constructor-sideeffect
2 parents 5c80cca + 4a1bc8f commit 983536a

File tree

820 files changed

+752
-51090
lines changed

Some content is hidden

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

820 files changed

+752
-51090
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>io.swagger.codegen.v3</groupId>
1414
<artifactId>swagger-codegen-generators</artifactId>
15-
<version>1.0.20-SNAPSHOT</version>
15+
<version>1.0.21-SNAPSHOT</version>
1616
<packaging>jar</packaging>
1717

1818
<build>
@@ -252,8 +252,8 @@
252252
</dependency>
253253
</dependencies>
254254
<properties>
255-
<swagger-codegen-version>3.0.20-SNAPSHOT</swagger-codegen-version>
256-
<swagger-parser-version>2.0.20-SNAPSHOT</swagger-parser-version>
255+
<swagger-codegen-version>3.0.21-SNAPSHOT</swagger-codegen-version>
256+
<swagger-parser-version>2.0.20</swagger-parser-version>
257257
<swagger-core-version>2.1.2</swagger-core-version>
258258
<jackson-version>2.10.3</jackson-version>
259259
<scala-version>2.11.1</scala-version>

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

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@
109109
import static io.swagger.codegen.v3.generators.CodegenHelper.getTypeMappings;
110110
import static io.swagger.codegen.v3.generators.CodegenHelper.initalizeSpecialCharacterMapping;
111111
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
112-
import static io.swagger.codegen.v3.utils.ModelUtils.processCodegenModels;
113112

114113
public abstract class DefaultCodegenConfig implements CodegenConfig {
115114
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultCodegenConfig.class);
@@ -268,11 +267,47 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> processedMod
268267
}
269268
}
270269
if (supportsInheritance) {
271-
processCodegenModels(allModels);
270+
for (String name : allModels.keySet()) {
271+
final CodegenModel codegenModel = allModels.get(name);
272+
fixUpParentAndInterfaces(codegenModel, allModels);
273+
}
272274
}
273275
return processedModels;
274276
}
275277

278+
/**
279+
* Fix up all parent and interface CodegenModel references.
280+
* @param allModels
281+
*/
282+
protected void fixUpParentAndInterfaces(CodegenModel codegenModel, Map<String, CodegenModel> allModels) {
283+
if (codegenModel.parent != null) {
284+
codegenModel.parentModel = allModels.get(codegenModel.parent);
285+
}
286+
if (codegenModel.interfaces != null && !codegenModel.interfaces.isEmpty()) {
287+
codegenModel.interfaceModels = new ArrayList<CodegenModel>(codegenModel.interfaces.size());
288+
for (String intf : codegenModel.interfaces) {
289+
CodegenModel intfModel = allModels.get(intf);
290+
if (intfModel != null) {
291+
codegenModel.interfaceModels.add(intfModel);
292+
}
293+
}
294+
}
295+
CodegenModel parent = codegenModel.parentModel;
296+
// if a discriminator exists on the parent, don't add this child to the inheritance hierarchy
297+
// TODO Determine what to do if the parent discriminator name == the grandparent discriminator name
298+
while (parent != null) {
299+
if (parent.children == null) {
300+
parent.children = new ArrayList<CodegenModel>();
301+
}
302+
parent.children.add(codegenModel);
303+
if (parent.discriminator == null) {
304+
parent = allModels.get(parent.parent);
305+
} else {
306+
parent = null;
307+
}
308+
}
309+
}
310+
276311
// override with any special post-processing
277312
@SuppressWarnings("static-method")
278313
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
@@ -2144,31 +2179,6 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
21442179
param = getParameterFromRef(param.get$ref(), openAPI);
21452180
}
21462181
CodegenParameter codegenParameter = fromParameter(param, imports);
2147-
// rename parameters to make sure all of them have unique names
2148-
if (ensureUniqueParams) {
2149-
while (true) {
2150-
boolean exists = false;
2151-
for (CodegenParameter cp : allParams) {
2152-
if (codegenParameter.paramName != null && codegenParameter.paramName.equals(cp.paramName)) {
2153-
exists = true;
2154-
break;
2155-
}
2156-
}
2157-
if (exists) {
2158-
codegenParameter.paramName = generateNextName(codegenParameter.paramName);
2159-
} else {
2160-
break;
2161-
}
2162-
}
2163-
}
2164-
2165-
// set isPrimitiveType and baseType for allParams
2166-
/*if (languageSpecificPrimitives.contains(p.baseType)) {
2167-
p.isPrimitiveType = true;
2168-
p.baseType = getSwaggerType(p);
2169-
}*/
2170-
2171-
21722182
allParams.add(codegenParameter);
21732183
// Issue #2561 (neilotoole) : Moved setting of is<Type>Param flags
21742184
// from here to fromParameter().
@@ -4229,6 +4239,10 @@ protected void configuresParameterForMediaType(CodegenOperation codegenOperation
42294239
}
42304240
this.addCodegenContentParemeters(codegenOperation, codegenContents);
42314241
for (CodegenContent content : codegenContents) {
4242+
if (ensureUniqueParams) {
4243+
ensureUniqueParameters(content.getParameters());
4244+
}
4245+
42324246
Collections.sort(content.getParameters(), (CodegenParameter one, CodegenParameter another) -> {
42334247
if (one.required == another.required){
42344248
return 0;
@@ -4263,6 +4277,20 @@ protected void addCodegenContentParemeters(CodegenOperation codegenOperation, Li
42634277
}
42644278
}
42654279

4280+
protected void ensureUniqueParameters(List<CodegenParameter> codegenParameters) {
4281+
if (codegenParameters == null || codegenParameters.isEmpty()) {
4282+
return;
4283+
}
4284+
for (CodegenParameter codegenParameter : codegenParameters) {
4285+
long count = codegenParameters.stream()
4286+
.filter(codegenParam -> codegenParam.paramName.equals(codegenParameter.paramName))
4287+
.count();
4288+
if (count > 1l) {
4289+
codegenParameter.paramName = generateNextName(codegenParameter.paramName);
4290+
}
4291+
}
4292+
}
4293+
42664294
protected void setParameterNullable(CodegenParameter parameter, CodegenProperty property) {
42674295
parameter.nullable = property.nullable;
42684296
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ protected CodegenModel processArrayItemSchema(CodegenModel codegenModel, ArraySc
124124
final Schema itemsSchema = arraySchema.getItems();
125125
if (itemsSchema instanceof ComposedSchema) {
126126
final CodegenModel composedModel = this.processComposedSchema(codegenModel.name + ARRAY_ITEMS_SUFFIX, (ComposedSchema) itemsSchema, allModels);
127+
if (composedModel == null) {
128+
return null;
129+
}
127130
this.updateArrayModel(codegenModel, composedModel.name, arraySchema);
128131
return composedModel;
129132
}

src/main/java/io/swagger/codegen/v3/generators/dotnet/AspNetCoreServerCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void processOpts() {
149149
supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache", packageFolder + File.separator + "Filters", "BasePathFilter.cs"));
150150
supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs"));
151151
supportingFiles.add(new SupportingFile("Startup.mustache", packageFolder, "Startup.cs"));
152-
} else if (aspNetCoreVersion.equals("2.1")) {
152+
} else if (aspNetCoreVersion.equals("2.1") || aspNetCoreVersion.equals("2.2")) {
153153
apiTemplateFiles.put("2.1/controller.mustache", ".cs");
154154
addInterfaceControllerTemplate();
155155

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.swagger.codegen.v3.generators.features;
2+
3+
public interface NotNullAnnotationFeatures {
4+
5+
// Language supports generating not Null Jackson Annotation
6+
String NOT_NULL_JACKSON_ANNOTATION = "notNullJacksonAnnotation";
7+
8+
void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation);
9+
10+
boolean isNotNullJacksonAnnotation();
11+
}

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

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import io.swagger.codegen.v3.CodegenOperation;
99
import io.swagger.codegen.v3.CodegenParameter;
1010
import io.swagger.codegen.v3.CodegenProperty;
11-
import io.swagger.codegen.v3.generators.handlebars.java.JavaHelper;
1211
import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
12+
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
13+
import io.swagger.codegen.v3.generators.handlebars.java.JavaHelper;
1314
import io.swagger.v3.oas.models.OpenAPI;
1415
import io.swagger.v3.oas.models.Operation;
1516
import io.swagger.v3.oas.models.PathItem;
@@ -40,6 +41,7 @@
4041

4142
import static io.swagger.codegen.v3.CodegenConstants.HAS_ENUMS_EXT_NAME;
4243
import static io.swagger.codegen.v3.CodegenConstants.IS_ENUM_EXT_NAME;
44+
import static io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;
4345
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
4446

4547
public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
@@ -81,6 +83,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
8183
protected String apiDocPath = "docs/";
8284
protected String modelDocPath = "docs/";
8385
protected boolean supportJava6= false;
86+
private NotNullAnnotationFeatures notNullOption;
8487

8588
public AbstractJavaCodegen() {
8689
super();
@@ -150,7 +153,9 @@ public AbstractJavaCodegen() {
150153
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC));
151154
cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)"));
152155
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_OAS2, CodegenConstants.USE_OAS2_DESC));
153-
156+
if(this instanceof NotNullAnnotationFeatures){
157+
cliOptions.add(CliOption.newBoolean(NOT_NULL_JACKSON_ANNOTATION, "adds @JsonInclude(JsonInclude.Include.NON_NULL) annotation to model classes"));
158+
}
154159
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
155160
Map<String, String> dateOptions = new HashMap<String, String>();
156161
dateOptions.put("java8", "Java 8 native JSR310 (preferred for jdk 1.8+) - note: this also sets \"" + JAVA8_MODE + "\" to true");
@@ -328,6 +333,17 @@ public void processOpts() {
328333
this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
329334
}
330335

336+
if (this instanceof NotNullAnnotationFeatures) {
337+
notNullOption = (NotNullAnnotationFeatures)this;
338+
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
339+
notNullOption.setNotNullJacksonAnnotation(convertPropertyToBoolean(NOT_NULL_JACKSON_ANNOTATION));
340+
writePropertyBack(NOT_NULL_JACKSON_ANNOTATION, notNullOption.isNotNullJacksonAnnotation());
341+
if (notNullOption.isNotNullJacksonAnnotation()) {
342+
importMapping.put("JsonInclude", "com.fasterxml.jackson.annotation.JsonInclude");
343+
}
344+
}
345+
}
346+
331347
if (fullJavaUtil) {
332348
javaUtilPrefix = "java.util.";
333349
}
@@ -912,6 +928,16 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
912928
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel, allSchemas);
913929
codegenModel = AbstractJavaCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel);
914930
}
931+
if (this instanceof NotNullAnnotationFeatures) {
932+
if (this instanceof NotNullAnnotationFeatures) {
933+
notNullOption = (NotNullAnnotationFeatures)this;
934+
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
935+
if (notNullOption.isNotNullJacksonAnnotation()) {
936+
codegenModel.imports.add("JsonInclude");
937+
}
938+
}
939+
}
940+
}
915941
return codegenModel;
916942
}
917943

@@ -954,6 +980,33 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
954980
}
955981
}
956982

983+
@Override
984+
protected void fixUpParentAndInterfaces(CodegenModel codegenModel, Map<String, CodegenModel> allModels) {
985+
super.fixUpParentAndInterfaces(codegenModel, allModels);
986+
if (codegenModel.vars == null || codegenModel.vars.isEmpty() || codegenModel.parentModel == null) {
987+
return;
988+
}
989+
CodegenModel parentModel = codegenModel.parentModel;
990+
991+
for (CodegenProperty codegenProperty : codegenModel.vars) {
992+
while (parentModel != null) {
993+
if (parentModel.vars == null || parentModel.vars.isEmpty()) {
994+
parentModel = parentModel.parentModel;
995+
continue;
996+
}
997+
boolean hasConflict = parentModel.vars.stream()
998+
.anyMatch(parentProperty -> parentProperty.name.equals(codegenProperty.name) && !parentProperty.datatype.equals(codegenProperty.datatype));
999+
if (hasConflict) {
1000+
codegenProperty.name = toVarName(codegenModel.name + "_" + codegenProperty.name);
1001+
codegenProperty.getter = toGetter(codegenProperty.name);
1002+
codegenProperty.setter = toGetter(codegenProperty.name);
1003+
break;
1004+
}
1005+
parentModel = parentModel.parentModel;
1006+
}
1007+
}
1008+
}
1009+
9571010
@Override
9581011
public void postProcessParameter(CodegenParameter parameter) { }
9591012

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.swagger.codegen.v3.SupportingFile;
1111
import io.swagger.codegen.v3.generators.features.BeanValidationFeatures;
1212
import io.swagger.codegen.v3.generators.features.GzipFeatures;
13+
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
1314
import io.swagger.codegen.v3.generators.features.PerformBeanValidationFeatures;
1415
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
1516
import org.apache.commons.lang3.BooleanUtils;
@@ -32,7 +33,7 @@
3233
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
3334
import static java.util.Collections.sort;
3435

35-
public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, PerformBeanValidationFeatures, GzipFeatures {
36+
public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, PerformBeanValidationFeatures, GzipFeatures, NotNullAnnotationFeatures {
3637
static final String MEDIA_TYPE = "mediaType";
3738

3839
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
@@ -62,6 +63,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValida
6263
protected boolean performBeanValidation = false;
6364
protected boolean useGzipFeature = false;
6465
protected boolean useRuntimeException = false;
66+
private boolean notNullJacksonAnnotation = false;
6567

6668

6769
public JavaClientCodegen() {
@@ -611,4 +613,13 @@ static boolean isJsonVendorMimeType(String mime) {
611613
return mime != null && JSON_VENDOR_MIME_PATTERN.matcher(mime).matches();
612614
}
613615

616+
@Override
617+
public void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation) {
618+
this.notNullJacksonAnnotation = notNullJacksonAnnotation;
619+
}
620+
621+
@Override
622+
public boolean isNotNullJacksonAnnotation() {
623+
return notNullJacksonAnnotation;
624+
}
614625
}

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class MicronautCodegen extends AbstractJavaCodegen implements BeanValidat
3535
private static final String BASE_PACKAGE = "basePackage";
3636
private static final String USE_TAGS = "useTags";
3737
private static final String IMPLICIT_HEADERS = "implicitHeaders";
38+
private static final String SKIP_SUPPORT_FILES = "skipSupportFiles";
3839

3940
private String title = "swagger-petstore";
4041
private String configPackage = "io.swagger.configuration";
@@ -66,6 +67,7 @@ private void init() {
6667
cliOptions.add(new CliOption(TITLE, "server title name or client service name"));
6768
cliOptions.add(new CliOption(CONFIG_PACKAGE, "configuration package for generated code"));
6869
cliOptions.add(new CliOption(BASE_PACKAGE, "base package (invokerPackage) for generated code"));
70+
cliOptions.add(new CliOption(SKIP_SUPPORT_FILES, "skip support files such as pom.xml, mvnw, etc from code generation."));
6971
cliOptions.add(CliOption.newBoolean(USE_TAGS, "use tags for creating interface and controller classnames"));
7072
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
7173
cliOptions.add(CliOption.newBoolean(IMPLICIT_HEADERS, "Use of @ApiImplicitParams for headers."));
@@ -152,6 +154,11 @@ public void processOpts() {
152154
this.setUseOptional(convertPropertyToBoolean(USE_OPTIONAL));
153155
}
154156

157+
boolean skipSupportFiles = false;
158+
if (additionalProperties.containsKey(SKIP_SUPPORT_FILES)) {
159+
skipSupportFiles = Boolean.valueOf(additionalProperties.get(SKIP_SUPPORT_FILES).toString());
160+
}
161+
155162
if (useBeanValidation) {
156163
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
157164
}
@@ -164,14 +171,15 @@ public void processOpts() {
164171
writePropertyBack(USE_OPTIONAL, useOptional);
165172
}
166173

167-
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
168-
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
169-
supportingFiles.add(new SupportingFile("mvnw", "", "mvnw"));
170-
supportingFiles.add(new SupportingFile("mvnw.cmd", "", "mvnw.cmd"));
171-
supportingFiles.add(new SupportingFile("unsupportedOperationExceptionHandler.mustache",
174+
if (!skipSupportFiles) {
175+
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
176+
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
177+
supportingFiles.add(new SupportingFile("mvnw", "", "mvnw"));
178+
supportingFiles.add(new SupportingFile("mvnw.cmd", "", "mvnw.cmd"));
179+
supportingFiles.add(new SupportingFile("unsupportedOperationExceptionHandler.mustache",
172180
(sourceFolder + File.separator + configPackage).replace(".", File.separator), "UnsupportedOperationExceptionHandler.java"));
173-
supportingFiles.add(new SupportingFile("mainApplication.mustache", (sourceFolder + File.separator).replace(".", File.separator), "MainApplication.java"));
174-
181+
supportingFiles.add(new SupportingFile("mainApplication.mustache", (sourceFolder + File.separator).replace(".", File.separator), "MainApplication.java"));
182+
}
175183
addHandlebarsLambdas(additionalProperties);
176184
}
177185

0 commit comments

Comments
 (0)