[BUG] [java] non-required arrays fail validation
Created by: ashatch
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Optional array properties in JSON responses fail validation when an optional array property is null:
// ensure the json data is an array
if (!jsonObj.get("anotherThing").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `linkedContent` to be an array in the JSON string but got `%s`", jsonObj.get("linkedContent").toString()));
}
A null check is required.
java.lang.NullPointerException: Cannot invoke "com.google.gson.JsonElement.isJsonArray()" because the return value of "com.google.gson.JsonObject.get(String)" is null
Expected to check null and validate successfully.
openapi-generator version
Current master
(0d56ebc
).
OpenAPI declaration file content or url
Declaration of an array property:
components:
schemas:
SomeObject:
properties:
something:
$ref: '#/components/schemas/Something'
anotherThing:
items:
$ref: '#/components/schemas/OtherThingItem'
type: array
Suggest a fix
Looks like it was introduced in 3ae37e27, pojo.moustache
line 500. I'll work on a PR.