Skip to content

Commit

Permalink
Fix OpenAPITools#13385 spring json nullable array (OpenAPITools#13537)
Browse files Browse the repository at this point in the history
* Fixing to use equalsNullable when nullable set in config for SpringCodeGen

* Adding additional test case file

* removed print statement from SpringCodeGen

* Updated model object

* Corrected indentation and removed import

* Fixed broken test

* Updating sample
  • Loading branch information
DeaneOC authored and JayAtOC committed Oct 6, 2022
1 parent ba0c810 commit 6b530cd
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2149,4 +2149,24 @@ public List<VendorExtension> getSupportedVendorExtensions() {
extensions.add(VendorExtension.X_FIELD_EXTRA_ANNOTATION);
return extensions;
}

public boolean isAddNullableImports(CodegenModel cm, boolean addImports, CodegenProperty var) {
if (this.openApiNullable) {
boolean isOptionalNullable = Boolean.FALSE.equals(var.required) && Boolean.TRUE.equals(var.isNullable);
// only add JsonNullable and related imports to optional and nullable values
addImports |= isOptionalNullable;
var.getVendorExtensions().put("x-is-jackson-optional-nullable", isOptionalNullable);
findByName(var.name, cm.readOnlyVars)
.ifPresent(p -> p.getVendorExtensions().put("x-is-jackson-optional-nullable", isOptionalNullable));
}
return addImports;
}
public static void addImports(List<Map<String, String>> imports, CodegenModel cm, Map<String, String> imports2Classnames) {
for (Map.Entry<String, String> entry : imports2Classnames.entrySet()) {
cm.imports.add(entry.getKey());
Map<String, String> importsItem = new HashMap<>();
importsItem.put("import", entry.getValue());
imports.add(importsItem);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -918,18 +918,10 @@ public ModelsMap postProcessModels(ModelsMap objs) {
List<Map<String, String>> imports = objs.getImports();
for (ModelMap mo : models) {
CodegenModel cm = mo.getModel();
boolean addImports = false;
boolean addNullableImports = false;

for (CodegenProperty var : cm.vars) {
if (this.openApiNullable) {
boolean isOptionalNullable = Boolean.FALSE.equals(var.required) && Boolean.TRUE.equals(var.isNullable);
// only add JsonNullable and related imports to optional and nullable values
addImports |= isOptionalNullable;
var.getVendorExtensions().put("x-is-jackson-optional-nullable", isOptionalNullable);
findByName(var.name, cm.readOnlyVars)
.ifPresent(p -> p.getVendorExtensions().put("x-is-jackson-optional-nullable", isOptionalNullable));
}

addNullableImports = isAddNullableImports(cm, addNullableImports, var);
if (Boolean.TRUE.equals(var.getVendorExtensions().get("x-enum-as-string"))) {
// treat enum string as just string
var.datatypeWithEnum = var.dataType;
Expand All @@ -956,17 +948,12 @@ public ModelsMap postProcessModels(ModelsMap objs) {

}

if (addImports) {
if (addNullableImports) {
Map<String, String> imports2Classnames = new HashMap<>();
imports2Classnames.put("JsonNullable", "org.openapitools.jackson.nullable.JsonNullable");
imports2Classnames.put("NoSuchElementException", "java.util.NoSuchElementException");
imports2Classnames.put("JsonIgnore", "com.fasterxml.jackson.annotation.JsonIgnore");
for (Map.Entry<String, String> entry : imports2Classnames.entrySet()) {
cm.imports.add(entry.getKey());
Map<String, String> importsItem = new HashMap<>();
importsItem.put("import", entry.getValue());
imports.add(importsItem);
}
addImports(imports, cm, imports2Classnames);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,13 +1036,21 @@ public ModelsMap postProcessModelsEnum(ModelsMap objs) {
final List<Map<String, String>> imports = objs.getImports();
for (ModelMap mo : objs.getModels()) {
CodegenModel cm = mo.getModel();
// for enum model
boolean addNullableImports = false;
for (CodegenProperty var : cm.vars) {
addNullableImports = isAddNullableImports(cm, addNullableImports, var);
}
if (Boolean.TRUE.equals(cm.isEnum) && cm.allowableValues != null) {
cm.imports.add(importMapping.get("JsonValue"));
final Map<String, String> item = new HashMap<>();
item.put("import", importMapping.get("JsonValue"));
imports.add(item);
}
if (addNullableImports) {
Map<String, String> imports2Classnames = new HashMap<>();
imports2Classnames.put("NoSuchElementException", "java.util.NoSuchElementException");
addImports(imports, cm, imports2Classnames);
}
}

return objs;
Expand Down
23 changes: 23 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_13385.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
openapi: 3.0.3
info:
version: 1.0.0
description: Specification to reproduce nullable issue with Array
title: ArrayNullableTest Api
paths:
/arrayNullable:
get:
summary: dummy
operationId: dummy
responses:
'200':
description: OK

components:
schemas:
TestObject:
type: object
properties:
picture:
type: string
format: byte
nullable: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
openapi: 3.0.3
info:
version: 1.0.0
description: Specification to reproduce nullable issue with Array
title: ArrayNullableTest Api
paths:
/arrayNullable:
get:
summary: dummy
operationId: dummy
responses:
'200':
description: OK

components:
schemas:
TestObject:
type: object
properties:
picture:
type: string
format: byte
nullable: false
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Set;
import org.openapitools.jackson.nullable.JsonNullable;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.NoSuchElementException;
import org.openapitools.jackson.nullable.JsonNullable;
import java.time.OffsetDateTime;
import javax.validation.Valid;
Expand Down Expand Up @@ -210,9 +211,9 @@ public boolean equals(Object o) {
return false;
}
ObjectWithUniqueItems objectWithUniqueItems = (ObjectWithUniqueItems) o;
return Objects.equals(this.nullSet, objectWithUniqueItems.nullSet) &&
return equalsNullable(this.nullSet, objectWithUniqueItems.nullSet) &&
Objects.equals(this.notNullSet, objectWithUniqueItems.notNullSet) &&
Objects.equals(this.nullList, objectWithUniqueItems.nullList) &&
equalsNullable(this.nullList, objectWithUniqueItems.nullList) &&
Objects.equals(this.notNullList, objectWithUniqueItems.notNullList) &&
Objects.equals(this.notNullDateField, objectWithUniqueItems.notNullDateField) &&
Objects.equals(this.nullDateField, objectWithUniqueItems.nullDateField);
Expand All @@ -224,7 +225,7 @@ private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b)

@Override
public int hashCode() {
return Objects.hash(nullSet, notNullSet, nullList, notNullList, notNullDateField, nullDateField);
return Objects.hash(hashCodeNullable(nullSet), notNullSet, hashCodeNullable(nullList), notNullList, notNullDateField, nullDateField);
}

private static <T> int hashCodeNullable(JsonNullable<T> a) {
Expand Down

0 comments on commit 6b530cd

Please sign in to comment.