You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 54fe232
Browse filesBrowse the repository at this point in the historyBrowse files
[BUG] [JAVA] validateJsonElement fails for required nullable fields (#22912)
* [Java] add missing nullable judgement when required property is true
* [Java] add okhttp template test and regenerate sample
* [Java] add tests when field is nullable and required
* [Java] regenerate samples to fix pipeline error
* [Java] add JSONTest fro RequiredNullableBody class
* run generate-samples after rebase
* review feedback
* review feedback
* fix test
* update hash of test file
---------
Co-authored-by: weirdo0314 <2019215183@stu.cqupt.edu.cn>
Copy file name to clipboardExpand all lines: modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/pojo.mustache
+28-10Lines changed: 28 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -385,16 +385,16 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
385
385
{{#isArray}}
386
386
{{#items.isModel}}
387
387
{{#required}}
388
-
// ensure the json data is an array
389
-
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
390
-
throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
388
+
if (jsonObj.get("{{{baseName}}}") != null{{#isNullable}} && !jsonObj.get("{{{baseName}}}").isJsonNull(){{/isNullable}}) {
389
+
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
390
+
throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
if (jsonObj.get("{{{baseName}}}") != null && !jsonObj.get("{{{baseName}}}").isJsonNull()) {
@@ -424,7 +424,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
424
424
// ensure the required json array is present
425
425
if (jsonObj.get("{{{baseName}}}") == null) {
426
426
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
427
-
} else if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
427
+
} else if (!jsonObj.get("{{{baseName}}}").isJsonArray(){{#isNullable}} && !jsonObj.get("{{baseName}}").isJsonNull(){{/isNullable}}) {
428
428
throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
429
429
}
430
430
{{/required}}
@@ -438,8 +438,14 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
438
438
{{/isString}}
439
439
{{#isModel}}
440
440
{{#required}}
441
+
{{#isNullable}}
442
+
if (jsonObj.get("{{{baseName}}}") != null && !jsonObj.get("{{{baseName}}}").isJsonNull()) {
Copy file name to clipboardExpand all lines: modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml
+45Lines changed: 45 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1369,6 +1369,19 @@ paths:
1369
1369
$ref: '#/components/schemas/Variable'
1370
1370
'400':
1371
1371
description: Invalid Value
1372
+
/fake/required-nullable-body:
1373
+
get:
1374
+
tags:
1375
+
- fake
1376
+
summary: fields in the response body, required and nullable are both true
Copy file name to clipboardExpand all lines: samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/model/PetWithRequiredNullableCases1.java
thrownewIllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
429
429
}
430
430
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
*FakeApi* | [**fakeRequiredNullableBodyGet**](docs/FakeApi.md#fakeRequiredNullableBodyGet) | **GET** /fake/required-nullable-body | fields in the response body, required and nullable are both true
0 commit comments