Skip to content

Commit 01059bd

Browse files
authored
Merge branch 'master' into ue4cpp
2 parents 33a3200 + 9a1460a commit 01059bd

File tree

22 files changed

+397
-73
lines changed

22 files changed

+397
-73
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ Here are some companies/projects using Swagger Codegen in production. To add you
928928
- [Mporium](http://mporium.com/)
929929
- [Neverfail](https://neverfail.com/)
930930
- [NexCap](http://www.nexess-solutions.com/fr/plateforme-iot/)
931+
- [Norwegian Air Shuttle](https://www.norwegian.com/)
931932
- [NTT DATA](http://www.nttdata.com/)
932933
- [nViso](http://www.nviso.ch/)
933934
- [Okiok](https://www.okiok.com)

modules/swagger-codegen/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
<artifactId>maven-compiler-plugin</artifactId>
7575
<version>3.5.1</version>
7676
<configuration>
77-
<source>1.7</source>
78-
<target>1.7</target>
77+
<source>1.8</source>
78+
<target>1.8</target>
7979
</configuration>
8080
</plugin>
8181
<plugin>

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

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -190,43 +190,48 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
190190
allModels.put(modelName, cm);
191191
}
192192
}
193-
// Fix up all parent and interface CodegenModel references.
194-
for (CodegenModel cm : allModels.values()) {
195-
if (cm.parent != null) {
196-
cm.parentModel = allModels.get(cm.parent);
197-
}
198-
if (cm.interfaces != null && !cm.interfaces.isEmpty()) {
199-
cm.interfaceModels = new ArrayList<CodegenModel>(cm.interfaces.size());
200-
for (String intf : cm.interfaces) {
201-
CodegenModel intfModel = allModels.get(intf);
202-
if (intfModel != null) {
203-
cm.interfaceModels.add(intfModel);
204-
}
205-
}
206-
}
207-
}
208-
// Let parent know about all its children
193+
209194
for (String name : allModels.keySet()) {
210-
CodegenModel cm = allModels.get(name);
211-
CodegenModel parent = allModels.get(cm.parent);
212-
// if a discriminator exists on the parent, don't add this child to the inheritance hierarchy
213-
// TODO Determine what to do if the parent discriminator name == the grandparent discriminator name
214-
while (parent != null) {
215-
if (parent.children == null) {
216-
parent.children = new ArrayList<CodegenModel>();
217-
}
218-
parent.children.add(cm);
219-
if (parent.discriminator == null) {
220-
parent = allModels.get(parent.parent);
221-
} else {
222-
parent = null;
223-
}
224-
}
195+
CodegenModel codegenModel = allModels.get(name);
196+
fixUpParentAndInterfaces(codegenModel, allModels);
225197
}
226198
}
227199
return objs;
228200
}
229201

202+
/**
203+
* Fix up all parent and interface CodegenModel references.
204+
* @param allModels
205+
*/
206+
protected void fixUpParentAndInterfaces(CodegenModel codegenModel, Map<String, CodegenModel> allModels) {
207+
if (codegenModel.parent != null) {
208+
codegenModel.parentModel = allModels.get(codegenModel.parent);
209+
}
210+
if (codegenModel.interfaces != null && !codegenModel.interfaces.isEmpty()) {
211+
codegenModel.interfaceModels = new ArrayList<CodegenModel>(codegenModel.interfaces.size());
212+
for (String intf : codegenModel.interfaces) {
213+
CodegenModel intfModel = allModels.get(intf);
214+
if (intfModel != null) {
215+
codegenModel.interfaceModels.add(intfModel);
216+
}
217+
}
218+
}
219+
CodegenModel parent = codegenModel.parentModel;
220+
// if a discriminator exists on the parent, don't add this child to the inheritance hierarchy
221+
// TODO Determine what to do if the parent discriminator name == the grandparent discriminator name
222+
while (parent != null) {
223+
if (parent.children == null) {
224+
parent.children = new ArrayList<CodegenModel>();
225+
}
226+
parent.children.add(codegenModel);
227+
if (parent.discriminator == null) {
228+
parent = allModels.get(parent.parent);
229+
} else {
230+
parent = null;
231+
}
232+
}
233+
}
234+
230235
// override with any special post-processing
231236
@SuppressWarnings("static-method")
232237
public Map<String, Object> postProcessModels(Map<String, Object> objs) {

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.regex.Pattern;
1212

13+
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
1314
import org.apache.commons.lang3.BooleanUtils;
1415
import org.apache.commons.lang3.StringUtils;
1516

@@ -43,6 +44,7 @@
4344
import io.swagger.models.properties.Property;
4445
import io.swagger.models.properties.StringProperty;
4546

47+
import static io.swagger.codegen.languages.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;
4648

4749
public abstract class AbstractJavaCodegen extends DefaultCodegen implements CodegenConfig {
4850

@@ -88,6 +90,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
8890
protected String modelDocPath = "docs/";
8991
protected boolean supportJava6= false;
9092
protected boolean disableHtmlEscaping = false;
93+
private NotNullAnnotationFeatures notNullOption;
9194

9295
public AbstractJavaCodegen() {
9396
super();
@@ -161,7 +164,9 @@ public AbstractJavaCodegen() {
161164
cliOptions.add(CliOption.newBoolean(FULL_JAVA_UTIL, "whether to use fully qualified name for classes under java.util. This option only works for Java API client"));
162165
cliOptions.add(new CliOption("hideGenerationTimestamp", "hides the timestamp when files were generated"));
163166
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)"));
164-
167+
if(this instanceof NotNullAnnotationFeatures){
168+
cliOptions.add(CliOption.newBoolean(NOT_NULL_JACKSON_ANNOTATION, "adds @JsonInclude(JsonInclude.Include.NON_NULL) annotation to model classes"));
169+
}
165170
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
166171
Map<String, String> dateOptions = new HashMap<String, String>();
167172
dateOptions.put("java8", "Java 8 native JSR310 (preferred for jdk 1.8+) - note: this also sets \"" + JAVA8_MODE + "\" to true");
@@ -338,6 +343,17 @@ public void processOpts() {
338343
this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
339344
}
340345

346+
if (this instanceof NotNullAnnotationFeatures) {
347+
notNullOption = (NotNullAnnotationFeatures)this;
348+
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
349+
notNullOption.setNotNullJacksonAnnotation(convertPropertyToBoolean(NOT_NULL_JACKSON_ANNOTATION));
350+
writePropertyBack(NOT_NULL_JACKSON_ANNOTATION, notNullOption.isNotNullJacksonAnnotation());
351+
if (notNullOption.isNotNullJacksonAnnotation()) {
352+
importMapping.put("JsonInclude", "com.fasterxml.jackson.annotation.JsonInclude");
353+
}
354+
}
355+
}
356+
341357
if (fullJavaUtil) {
342358
javaUtilPrefix = "java.util.";
343359
}
@@ -907,6 +923,16 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
907923
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
908924
codegenModel = AbstractJavaCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel);
909925
}
926+
if (this instanceof NotNullAnnotationFeatures) {
927+
if (this instanceof NotNullAnnotationFeatures) {
928+
notNullOption = (NotNullAnnotationFeatures)this;
929+
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
930+
if (notNullOption.isNotNullJacksonAnnotation()) {
931+
codegenModel.imports.add("JsonInclude");
932+
}
933+
}
934+
}
935+
}
910936
return codegenModel;
911937
}
912938

@@ -938,6 +964,33 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
938964
}
939965
}
940966

967+
@Override
968+
protected void fixUpParentAndInterfaces(CodegenModel codegenModel, Map<String, CodegenModel> allModels) {
969+
super.fixUpParentAndInterfaces(codegenModel, allModels);
970+
if (codegenModel.vars == null || codegenModel.vars.isEmpty() || codegenModel.parentModel == null) {
971+
return;
972+
}
973+
CodegenModel parentModel = codegenModel.parentModel;
974+
975+
for (CodegenProperty codegenProperty : codegenModel.vars) {
976+
while (parentModel != null) {
977+
if (parentModel.vars == null || parentModel.vars.isEmpty()) {
978+
parentModel = parentModel.parentModel;
979+
continue;
980+
}
981+
boolean hasConflict = parentModel.vars.stream()
982+
.anyMatch(parentProperty -> parentProperty.name.equals(codegenProperty.name) && !parentProperty.datatype.equals(codegenProperty.datatype));
983+
if (hasConflict) {
984+
codegenProperty.name = toVarName(codegenModel.name + "_" + codegenProperty.name);
985+
codegenProperty.getter = toGetter(codegenProperty.name);
986+
codegenProperty.setter = toGetter(codegenProperty.name);
987+
break;
988+
}
989+
parentModel = parentModel.parentModel;
990+
}
991+
}
992+
}
993+
941994
@Override
942995
public void postProcessParameter(CodegenParameter parameter) { }
943996

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.swagger.codegen.*;
77
import io.swagger.codegen.languages.features.BeanValidationFeatures;
88
import io.swagger.codegen.languages.features.GzipFeatures;
9+
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
910
import io.swagger.codegen.languages.features.PerformBeanValidationFeatures;
1011

1112
import org.apache.commons.lang3.BooleanUtils;
@@ -19,7 +20,7 @@
1920

2021
public class JavaClientCodegen extends AbstractJavaCodegen
2122
implements BeanValidationFeatures, PerformBeanValidationFeatures,
22-
GzipFeatures
23+
GzipFeatures, NotNullAnnotationFeatures
2324
{
2425
static final String MEDIA_TYPE = "mediaType";
2526

@@ -52,7 +53,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
5253
protected boolean performBeanValidation = false;
5354
protected boolean useGzipFeature = false;
5455
protected boolean useRuntimeException = false;
55-
56+
private boolean notNullJacksonAnnotation;
5657

5758
public JavaClientCodegen() {
5859
super();
@@ -611,4 +612,13 @@ static boolean isJsonVendorMimeType(String mime) {
611612
return mime != null && JSON_VENDOR_MIME_PATTERN.matcher(mime).matches();
612613
}
613614

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

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.samskivert.mustache.Template;
55
import io.swagger.codegen.*;
66
import io.swagger.codegen.languages.features.BeanValidationFeatures;
7+
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
78
import io.swagger.codegen.languages.features.OptionalFeatures;
89
import io.swagger.models.Operation;
910
import io.swagger.models.Path;
@@ -17,7 +18,7 @@
1718

1819

1920
public class SpringCodegen extends AbstractJavaCodegen
20-
implements BeanValidationFeatures, OptionalFeatures {
21+
implements BeanValidationFeatures, OptionalFeatures, NotNullAnnotationFeatures {
2122
public static final String DEFAULT_LIBRARY = "spring-boot";
2223
public static final String TITLE = "title";
2324
public static final String CONFIG_PACKAGE = "configPackage";
@@ -53,6 +54,7 @@ public class SpringCodegen extends AbstractJavaCodegen
5354
protected boolean useOptional = false;
5455
protected boolean openFeign = false;
5556
protected boolean defaultInterfaces = true;
57+
private boolean notNullJacksonAnnotation;
5658

5759
public SpringCodegen() {
5860
super();
@@ -499,6 +501,16 @@ public void setReturnContainer(final String returnContainer) {
499501
return objs;
500502
}
501503

504+
@Override
505+
public void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation) {
506+
this.notNullJacksonAnnotation = notNullJacksonAnnotation;
507+
}
508+
509+
@Override
510+
public boolean isNotNullJacksonAnnotation() {
511+
return notNullJacksonAnnotation;
512+
}
513+
502514
private interface DataTypeAssigner {
503515
void setReturnType(String returnType);
504516
void setReturnContainer(String returnContainer);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.swagger.codegen.languages.features;
2+
3+
public interface NotNullAnnotationFeatures {
4+
// Language supports generating not Null Jackson Annotation
5+
String NOT_NULL_JACKSON_ANNOTATION = "notNullJacksonAnnotation";
6+
7+
void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation);
8+
9+
boolean isNotNullJacksonAnnotation();
10+
}

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@
236236
<scope>provided</scope>
237237
</dependency>
238238
{{/useBeanValidation}}
239+
{{#notNullJacksonAnnotation}}
240+
<dependency>
241+
<groupId>com.fasterxml.jackson.core</groupId>
242+
<artifactId>jackson-annotations</artifactId>
243+
<version>2.10.1</version>
244+
</dependency>
245+
{{/notNullJacksonAnnotation}}
239246
{{#performBeanValidation}}
240247
<!-- Bean Validation Impl. used to perform BeanValidation -->
241248
<dependency>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
*/{{#description}}
44
@ApiModel(description = "{{{description}}}"){{/description}}
55
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
6+
7+
{{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}}
8+
69
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcelableModel}}implements Parcelable {{#serializableModel}}, Serializable {{/serializableModel}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable {{/serializableModel}}{{/parcelableModel}}{
710
{{#serializableModel}}
811
private static final long serialVersionUID = 1L;

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-boot/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@
9595
<artifactId>validation-api</artifactId>
9696
</dependency>
9797
{{/useBeanValidation}}
98+
{{#notNullJacksonAnnotation}}
99+
<dependency>
100+
<groupId>com.fasterxml.jackson.core</groupId>
101+
<artifactId>jackson-annotations</artifactId>
102+
<version>2.10.1</version>
103+
</dependency>
104+
{{/notNullJacksonAnnotation}}
98105
<dependency>
99106
<groupId>org.springframework.boot</groupId>
100107
<artifactId>spring-boot-starter-test</artifactId>

0 commit comments

Comments
 (0)