[php][php-nextgen] Fix nullability when multiple response types are possible (#22827)

* [php][php-nextgen] Fix nullability when multiple response types are possible

* [php][php-nextgen] Fix test
This commit is contained in:
Julian Vennen
2026-01-27 17:49:20 +01:00
committed by GitHub
parent 11e06d1e77
commit 6022e4ec7d
3 changed files with 59 additions and 1 deletions

View File

@@ -214,7 +214,11 @@ public class PhpNextgenClientCodegen extends AbstractPhpCodegen {
String phpReturnType = String.join("|", phpReturnTypeOptions);
String docReturnType = String.join("|", docReturnTypeOptions);
if (hasEmptyResponse) {
phpReturnType = "?" + phpReturnType;
if (phpReturnTypeOptions.size() > 1) {
phpReturnType = phpReturnType + "|null";
} else {
phpReturnType = "?" + phpReturnType;
}
docReturnType = docReturnType + "|null";
}

View File

@@ -29,6 +29,7 @@ import org.testng.annotations.BeforeMethod;
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 class PhpNextgenClientCodegenTest {
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<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));
List<String> modelContent = Files
.readAllLines(files.get("DefaultApi.php").toPath())
.stream()
.map(String::trim)
.collect(Collectors.toList());
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 '?'.");
}
}

View File

@@ -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: { }