diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java index c07c74b8f1c9b..53910b3be6b08 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java @@ -3178,4 +3178,32 @@ public void testQueryParamsExploded_whenQueryParamIsNull() throws IOException { Path petApi = Paths.get(output + "/src/main/java/xyz/abcdef/api/DepartmentApi.java"); TestUtils.assertFileContains(petApi, "if (filter != null) {"); } + + @Test + public void testRequiredAndNullableAreBothTrue() throws IOException { + File output = Files.createTempDirectory("test").toFile(); + output.deleteOnExit(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("java") + .setLibrary(JavaClientCodegen.OKHTTP_GSON) + .setInputSpec("src/test/resources/bugs/issue_18516.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + + DefaultGenerator generator = new DefaultGenerator(); + List files = generator.opts(configurator.toClientOptInput()).generate(); + files.forEach(File::deleteOnExit); + + validateJavaSourceFiles(files); + + Path modelFile = Paths.get(output + "/src/main/java/org/openapitools/client/model/SomeObject.java"); + TestUtils.assertFileContains( + modelFile, + "} else if (!jsonObj.get(\"ids\").isJsonArray() && !jsonObj.get(\"ids\").isJsonNull()) {", + "if (!jsonObj.get(\"users\").isJsonArray() && !jsonObj.get(\"users\").isJsonNull()) {", + "if (jsonObj.get(\"user\") != null && !jsonObj.get(\"user\").isJsonNull()) {", + "if (jsonObj.get(\"role\") != null && !jsonObj.get(\"role\").isJsonNull()) {", + "if (jsonObj.get(\"custom\") != null && !jsonObj.get(\"custom\").isJsonNull()) {"); + } } diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_18516.yaml b/modules/openapi-generator/src/test/resources/bugs/issue_18516.yaml new file mode 100644 index 0000000000000..fcefeb6b521f1 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_18516.yaml @@ -0,0 +1,61 @@ +openapi: 3.0.3 +info: + title: test + description: Test API + version: 1.0.1 + +paths: + /test: + get: + responses: + 200: + description: Valid response + content: + application/json: + schema: + $ref: "#/components/schemas/SomeObject" + +components: + schemas: + SomeObject: + type: object + required: + - ids + - users + - user + - role + - custom + properties: + ids: + type: array + nullable: true + items: + type: integer + users: + type: array + nullable: true + items: + type: object + properties: + id: + type: string + user: + type: object + nullable: true + properties: + id: + type: string + role: + type: string + nullable: true + enum: + - admin + - tenant + custom: + $ref: "#/components/schemas/customEnum" + customEnum: + type: string + nullable: true + enum: + - custom + \ No newline at end of file diff --git a/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/model/PetWithRequiredNullableCases1.java b/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/model/PetWithRequiredNullableCases1.java index d2199df3eea77..ed950f185cbb6 100644 --- a/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/model/PetWithRequiredNullableCases1.java +++ b/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/model/PetWithRequiredNullableCases1.java @@ -427,7 +427,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti // ensure the required json array is present if (jsonObj.get("photoUrls") == null) { throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`"); - } else if (!jsonObj.get("photoUrls").isJsonArray()) { + } else if (!jsonObj.get("photoUrls").isJsonArray() && !jsonObj.get("photoUrls").isJsonNull()) { throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString())); } if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {