Skip to content

Commit e9c850f

Browse files
authored
Adds isNull to codegenX java core classes that represent schemas (#8462)
* Adds isNull to all schema classes * Adds null model and property samples, adds models test of isNull * Adds isNull tests for parameter and response also * Reverts version file
1 parent cd2a0db commit e9c850f

File tree

8 files changed

+165
-4
lines changed

8 files changed

+165
-4
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
9595
public boolean isArray;
9696
public boolean hasChildren;
9797
public boolean isMap;
98+
public boolean isNull;
9899
/**
99100
* Indicates the OAS schema specifies "deprecated: true".
100101
*/
@@ -702,6 +703,16 @@ public void setXmlPrefix(String xmlPrefix) {
702703
this.xmlPrefix = xmlPrefix;
703704
}
704705

706+
@Override
707+
public boolean getIsNull() {
708+
return isNull;
709+
}
710+
711+
@Override
712+
public void setIsNull(boolean isNull) {
713+
this.isNull = isNull;
714+
}
715+
705716
@Override
706717
public boolean equals(Object o) {
707718
if (this == o) return true;
@@ -730,6 +741,7 @@ public boolean equals(Object o) {
730741
isMap == that.isMap &&
731742
isDeprecated == that.isDeprecated &&
732743
hasOnlyReadOnly == that.hasOnlyReadOnly &&
744+
isNull == that.isNull &&
733745
getUniqueItems() == that.getUniqueItems() &&
734746
getExclusiveMinimum() == that.getExclusiveMinimum() &&
735747
getExclusiveMaximum() == that.getExclusiveMaximum() &&
@@ -794,7 +806,7 @@ public int hashCode() {
794806
getDescription(), getClassVarName(), getModelJson(), getDataType(), getXmlPrefix(), getXmlNamespace(),
795807
getXmlName(), getClassFilename(), getUnescapedDescription(), getDiscriminator(), getDefaultValue(),
796808
getArrayModelType(), isAlias, isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble,
797-
isDate, isDateTime,
809+
isDate, isDateTime, isNull,
798810
getVars(), getAllVars(), getRequiredVars(), getOptionalVars(), getReadOnlyVars(), getReadWriteVars(),
799811
getParentVars(), getAllowableValues(), getMandatory(), getAllMandatory(), getImports(), hasVars,
800812
isEmptyVars(), hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArray,
@@ -885,6 +897,7 @@ public String toString() {
885897
sb.append(", items='").append(items).append('\'');
886898
sb.append(", additionalProperties='").append(additionalProperties).append('\'');
887899
sb.append(", isModel='").append(isModel).append('\'');
900+
sb.append(", isNull='").append(isNull);
888901
sb.append('}');
889902
return sb.toString();
890903
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
101101
public Number multipleOf;
102102
private Integer maxProperties;
103103
private Integer minProperties;
104+
public boolean isNull;
104105

105106
public CodegenParameter copy() {
106107
CodegenParameter output = new CodegenParameter();
@@ -147,6 +148,7 @@ public CodegenParameter copy() {
147148
output.minimum = this.minimum;
148149
output.pattern = this.pattern;
149150
output.additionalProperties = this.additionalProperties;
151+
output.isNull = this.isNull;
150152

151153
if (this._enum != null) {
152154
output._enum = new ArrayList<String>(this._enum);
@@ -200,7 +202,7 @@ public CodegenParameter copy() {
200202

201203
@Override
202204
public int hashCode() {
203-
return Objects.hash(isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isContainer, isCollectionFormatMulti, isPrimitiveType, isModel, isExplode, baseName, paramName, dataType, datatypeWithEnum, dataFormat, collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName, style, example, jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject, isAnyType, isArray, isMap, isFile, isEnum, _enum, allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation, getMaxProperties(), getMinProperties(), isNullable, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf);
205+
return Objects.hash(isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isContainer, isCollectionFormatMulti, isPrimitiveType, isModel, isExplode, baseName, paramName, dataType, datatypeWithEnum, dataFormat, collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName, style, example, jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject, isAnyType, isArray, isMap, isFile, isEnum, _enum, allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation, getMaxProperties(), getMinProperties(), isNullable, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf, isNull);
204206
}
205207

206208
@Override
@@ -244,6 +246,7 @@ public boolean equals(Object o) {
244246
hasValidation == that.hasValidation &&
245247
isNullable == that.isNullable &&
246248
required == that.required &&
249+
isNull == that.isNull &&
247250
getExclusiveMaximum() == that.getExclusiveMaximum() &&
248251
getExclusiveMinimum() == that.getExclusiveMinimum() &&
249252
getUniqueItems() == that.getUniqueItems() &&
@@ -357,6 +360,7 @@ public String toString() {
357360
sb.append(", uniqueItems=").append(uniqueItems);
358361
sb.append(", contentType=").append(contentType);
359362
sb.append(", multipleOf=").append(multipleOf);
363+
sb.append(", isNull=").append(isNull);
360364
sb.append('}');
361365
return sb.toString();
362366
}
@@ -568,5 +572,15 @@ public List<CodegenProperty> getRequiredVars() {
568572
public void setRequiredVars(List<CodegenProperty> requiredVars) {
569573
this.requiredVars = requiredVars;
570574
}
575+
576+
@Override
577+
public boolean getIsNull() {
578+
return isNull;
579+
}
580+
581+
@Override
582+
public void setIsNull(boolean isNull) {
583+
this.isNull = isNull;
584+
}
571585
}
572586

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
132132
public boolean isUuid;
133133
public boolean isUri;
134134
public boolean isEmail;
135+
public boolean isNull;
135136
/**
136137
* The type is a free-form object, i.e. it is a map of string to values with no declared properties.
137138
* A OAS free-form schema may include the 'additionalProperties' attribute, which puts a constraint
@@ -676,6 +677,16 @@ public void setRequiredVars(List<CodegenProperty> requiredVars) {
676677
this.requiredVars = requiredVars;
677678
}
678679

680+
@Override
681+
public boolean getIsNull() {
682+
return isNull;
683+
}
684+
685+
@Override
686+
public void setIsNull(boolean isNull) {
687+
this.isNull = isNull;
688+
}
689+
679690
@Override
680691
public String toString() {
681692
final StringBuilder sb = new StringBuilder("CodegenProperty{");
@@ -764,6 +775,7 @@ public String toString() {
764775
sb.append(", xmlName='").append(xmlName).append('\'');
765776
sb.append(", xmlNamespace='").append(xmlNamespace).append('\'');
766777
sb.append(", isXmlWrapped=").append(isXmlWrapped);
778+
sb.append(", isNull=").append(isNull);
767779
sb.append('}');
768780
return sb.toString();
769781
}
@@ -812,6 +824,7 @@ public boolean equals(Object o) {
812824
isInherited == that.isInherited &&
813825
isXmlAttribute == that.isXmlAttribute &&
814826
isXmlWrapped == that.isXmlWrapped &&
827+
isNull == that.isNull &&
815828
Objects.equals(openApiType, that.openApiType) &&
816829
Objects.equals(baseName, that.baseName) &&
817830
Objects.equals(complexType, that.complexType) &&
@@ -873,6 +886,6 @@ public int hashCode() {
873886
items, mostInnerItems, additionalProperties, vars, requiredVars,
874887
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
875888
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
876-
xmlNamespace, isXmlWrapped);
889+
xmlNamespace, isXmlWrapped, isNull);
877890
}
878891
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenResponse.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
5757
public boolean isArray;
5858
public boolean isBinary = false;
5959
public boolean isFile = false;
60+
public boolean isNull;
6061
public Object schema;
6162
public String jsonSchema;
6263
public Map<String, Object> vendorExtensions = new HashMap<String, Object>();
@@ -84,7 +85,7 @@ public int hashCode() {
8485
isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBoolean, isDate,
8586
isDateTime, isUuid, isEmail, isModel, isFreeFormObject, isAnyType, isDefault, simpleType, primitiveType,
8687
isMap, isArray, isBinary, isFile, schema, jsonSchema, vendorExtensions, items, additionalProperties,
87-
vars, requiredVars,
88+
vars, requiredVars, isNull,
8889
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
8990
getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern(),
9091
is1xx, is2xx, is3xx, is4xx, is5xx);
@@ -122,6 +123,7 @@ public boolean equals(Object o) {
122123
isFile == that.isFile &&
123124
items == that.items &&
124125
additionalProperties == that.additionalProperties &&
126+
isNull == that.isNull &&
125127
is1xx == that.is1xx &&
126128
is2xx == that.is2xx &&
127129
is3xx == that.is3xx &&
@@ -423,6 +425,7 @@ public String toString() {
423425
sb.append(", additionalProperties='").append(additionalProperties).append('\'');
424426
sb.append(", vars='").append(vars).append('\'');
425427
sb.append(", requiredVars='").append(requiredVars).append('\'');
428+
sb.append(", isNull='").append(isNull);
426429
sb.append('}');
427430
return sb.toString();
428431
}
@@ -443,4 +446,14 @@ public boolean isRange() {
443446
return true;
444447
return false;
445448
}
449+
450+
@Override
451+
public boolean getIsNull() {
452+
return isNull;
453+
}
454+
455+
@Override
456+
public void setIsNull(boolean isNull) {
457+
this.isNull = isNull;
458+
}
446459
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,8 @@ public CodegenModel fromModel(String name, Schema schema) {
23062306
m.arrayModelType = arrayProperty.complexType;
23072307
addParentContainer(m, name, schema);
23082308
ModelUtils.syncValidationProperties(schema, m);
2309+
} else if (ModelUtils.isNullType(schema)) {
2310+
m.isNull = true;
23092311
} else if (schema instanceof ComposedSchema) {
23102312
final ComposedSchema composed = (ComposedSchema) schema;
23112313
Map<String, Schema> properties = new LinkedHashMap<String, Schema>();
@@ -3279,6 +3281,8 @@ public CodegenProperty fromProperty(String name, Schema p) {
32793281
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
32803282
p.setAdditionalProperties(innerSchema);
32813283
}
3284+
} else if (ModelUtils.isNullType(p)) {
3285+
property.isNull = true;
32823286
}
32833287

32843288
//Inline enum case:
@@ -4044,6 +4048,7 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
40444048
if (r.schema != null) {
40454049
Map<String, Schema> allSchemas = null;
40464050
CodegenProperty cp = fromProperty("response", responseSchema);
4051+
r.isNull = cp.isNull;
40474052

40484053
if (ModelUtils.isArraySchema(responseSchema)) {
40494054
ArraySchema as = (ArraySchema) responseSchema;
@@ -4317,6 +4322,8 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
43174322
imports.add(codegenProperty.baseType);
43184323
codegenProperty = codegenProperty.items;
43194324
}
4325+
} else if (ModelUtils.isNullType(parameterSchema)) {
4326+
codegenParameter.isNull = true;
43204327
}
43214328
/* TODO revise the logic below
43224329
} else {
@@ -6146,6 +6153,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
61466153
} else {
61476154
codegenParameter.baseName = bodyParameterName;
61486155
}
6156+
codegenParameter.isNull = codegenProperty.isNull;
61496157
codegenParameter.isPrimitiveType = true;
61506158
codegenParameter.baseType = codegenProperty.baseType;
61516159
codegenParameter.dataType = codegenProperty.dataType;

modules/openapi-generator/src/main/java/org/openapitools/codegen/IJsonSchemaValidationProperties.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,8 @@ public interface IJsonSchemaValidationProperties {
9090
List<CodegenProperty> getRequiredVars();
9191

9292
void setRequiredVars(List<CodegenProperty> requiredVars);
93+
94+
boolean getIsNull();
95+
96+
void setIsNull(boolean isNull);
9397
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,23 @@ public void testIsXPresence() {
25572557
assertEquals(cm.isString, false);
25582558
assertEquals(cm.isDate, true);
25592559

2560+
modelName = "NullModel";
2561+
sc = openAPI.getComponents().getSchemas().get(modelName);
2562+
cm = codegen.fromModel(modelName, sc);
2563+
assertEquals(cm.isNull, true);
2564+
2565+
modelName = "ObjectWithTypeNullProperties";
2566+
sc = openAPI.getComponents().getSchemas().get(modelName);
2567+
cm = codegen.fromModel(modelName, sc);
2568+
assertEquals(cm.getVars().get(0).isNull, true);
2569+
assertEquals(cm.getVars().get(1).getItems().isNull, true);
2570+
assertEquals(cm.getAdditionalProperties().isNull, true);
2571+
2572+
modelName = "ArrayOfNulls";
2573+
sc = openAPI.getComponents().getSchemas().get(modelName);
2574+
cm = codegen.fromModel(modelName, sc);
2575+
assertEquals(cm.getItems().isNull, true);
2576+
25602577
modelName = "ObjectWithDateWithValidation";
25612578
sc = openAPI.getComponents().getSchemas().get(modelName);
25622579
cm = codegen.fromModel(modelName, sc);
@@ -2618,6 +2635,20 @@ public void testIsXPresence() {
26182635
assertEquals(co.bodyParams.get(0).isDateTime, true);
26192636
assertEquals(co.responses.get(0).isString, false);
26202637
assertEquals(co.responses.get(0).isDateTime, true);
2638+
2639+
path = "/null/{param}";
2640+
operation = openAPI.getPaths().get(path).getPost();
2641+
co = codegen.fromOperation(path, "POST", operation, null);
2642+
assertEquals(co.pathParams.get(0).isNull, true);
2643+
assertEquals(co.bodyParams.get(0).isNull, true);
2644+
assertEquals(co.responses.get(0).isNull, true);
2645+
2646+
path = "/ref_null/{param}";
2647+
operation = openAPI.getPaths().get(path).getPost();
2648+
co = codegen.fromOperation(path, "POST", operation, null);
2649+
assertEquals(co.pathParams.get(0).isNull, true);
2650+
assertEquals(co.bodyParams.get(0).isNull, true);
2651+
assertEquals(co.responses.get(0).isNull, true);
26212652
}
26222653

26232654
@Test

modules/openapi-generator/src/test/resources/3_0/issue_7651.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,73 @@ paths:
120120
type: string
121121
format: date-time
122122
pattern: '^2020.*'
123+
/null/{param}:
124+
post:
125+
tags:
126+
- isX
127+
operationId: null
128+
parameters:
129+
- name: param
130+
in: path
131+
required: true
132+
schema:
133+
type: 'null'
134+
requestBody:
135+
content:
136+
application/json:
137+
schema:
138+
type: 'null'
139+
required: true
140+
responses:
141+
200:
142+
description: success
143+
content:
144+
application/json:
145+
schema:
146+
type: 'null'
147+
/ref_null/{param}:
148+
post:
149+
tags:
150+
- isX
151+
operationId: null
152+
parameters:
153+
- name: param
154+
in: path
155+
required: true
156+
schema:
157+
$ref: '#/components/schemas/NullModel'
158+
requestBody:
159+
content:
160+
application/json:
161+
schema:
162+
$ref: '#/components/schemas/NullModel'
163+
required: true
164+
responses:
165+
200:
166+
description: success
167+
content:
168+
application/json:
169+
schema:
170+
$ref: '#/components/schemas/NullModel'
123171
components:
124172
schemas:
173+
NullModel:
174+
type: 'null'
175+
ObjectWithTypeNullProperties:
176+
type: object
177+
properties:
178+
nullProp:
179+
type: 'null'
180+
listOfNulls:
181+
type: array
182+
items:
183+
type: 'null'
184+
additionalProperties:
185+
type: 'null'
186+
ArrayOfNulls:
187+
type: array
188+
items:
189+
type: 'null'
125190
DateWithValidation:
126191
type: string
127192
format: date

0 commit comments

Comments
 (0)