From 7851c605bb1f67271cc65e277d627dafe0ca0472 Mon Sep 17 00:00:00 2001 From: Julian Vennen Date: Tue, 27 Jan 2026 11:57:12 +0100 Subject: [PATCH 1/2] [php][php-nextgen] Fix nullability when multiple response types are possible --- .../languages/PhpNextgenClientCodegen.java | 6 +++- .../php/PhpNextgenClientCodegenTest.java | 30 +++++++++++++++++++ .../src/test/resources/bugs/issue_22817.yaml | 24 +++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/resources/bugs/issue_22817.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java index 259916c0ebae..447e2b346666 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java @@ -214,7 +214,11 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List 1) { + phpReturnType = phpReturnType + "|null"; + } else { + phpReturnType = "?" + phpReturnType; + } docReturnType = docReturnType + "|null"; } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpNextgenClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpNextgenClientCodegenTest.java index 68e9dc11ed1f..9dcc6d279252 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpNextgenClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpNextgenClientCodegenTest.java @@ -29,6 +29,7 @@ import org.testng.annotations.Test; import java.io.File; +import java.io.IOException; import java.nio.file.Files; import java.util.List; import java.util.Map; @@ -146,4 +147,33 @@ public void testEnumUnknownDefaultCaseDeserializationDisabled() throws Exception Assert.assertListNotContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), ""); Assert.assertListContains(modelContent, a -> a.equalsIgnoreCase("\"Invalid value '%s' for 'color', must be one of '%s'\","), ""); } + + @Test + public void testDifferentResponseSchemasWithEmpty() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + + OpenAPI openAPI = new OpenAPIParser() + .readLocation("src/test/resources/bugs/issue_22817.yaml", null, new ParseOptions()) + .getOpenAPI(); + + + codegen.setOutputDir(output.getAbsolutePath()); + ClientOptInput input = new ClientOptInput() + .openAPI(openAPI) + .config(codegen); + + DefaultGenerator generator = new DefaultGenerator(); + Map files = generator.opts(input).generate().stream() + .collect(Collectors.toMap(File::getName, Function.identity())); + + List modelContent = Files + .readAllLines(files.get("DefaultApi.php").toPath()) + .stream() + .map(String::trim) + .collect(Collectors.toList()); + + Assert.assertListContains(modelContent, a -> a.equals("): integer|string|null"), "Expected to find nullable return type declaration."); + Assert.assertListNotContains(modelContent, a -> a.equals("): ?integer|string"), "Expected to not find invalid union type with '?'."); + } } diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_22817.yaml b/modules/openapi-generator/src/test/resources/bugs/issue_22817.yaml new file mode 100644 index 000000000000..d3711323344e --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_22817.yaml @@ -0,0 +1,24 @@ +openapi: 3.0.4 +info: + title: "Different response schemas including an empty one" + version: "1.0.0" +paths: + /example: + get: + operationId: exampleGet + responses: + 200: + description: "A successful response with data" + content: + application/json: + schema: + type: integer + 400: + description: "A bad request with a message" + content: + application/json: + schema: + type: string + 500: + description: "An internal server error with no content" + content: { } From 56d373d8078c35d16e42a41726211a76ab05527a Mon Sep 17 00:00:00 2001 From: Julian Vennen Date: Tue, 27 Jan 2026 12:18:01 +0100 Subject: [PATCH 2/2] [php][php-nextgen] Fix test --- .../openapitools/codegen/php/PhpNextgenClientCodegenTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpNextgenClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpNextgenClientCodegenTest.java index 9dcc6d279252..02f58f48f64e 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpNextgenClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpNextgenClientCodegenTest.java @@ -173,7 +173,7 @@ public void testDifferentResponseSchemasWithEmpty() throws IOException { .map(String::trim) .collect(Collectors.toList()); - Assert.assertListContains(modelContent, a -> a.equals("): integer|string|null"), "Expected to find nullable return type declaration."); - Assert.assertListNotContains(modelContent, a -> a.equals("): ?integer|string"), "Expected to not find invalid union type with '?'."); + Assert.assertListContains(modelContent, a -> a.equals("): int|string|null"), "Expected to find nullable return type declaration."); + Assert.assertListNotContains(modelContent, a -> a.equals("): ?int|string"), "Expected to not find invalid union type with '?'."); } }