Skip to content

Commit 6022e4e

Browse files
authored
[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
1 parent 11e06d1 commit 6022e4e

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
214214
String phpReturnType = String.join("|", phpReturnTypeOptions);
215215
String docReturnType = String.join("|", docReturnTypeOptions);
216216
if (hasEmptyResponse) {
217-
phpReturnType = "?" + phpReturnType;
217+
if (phpReturnTypeOptions.size() > 1) {
218+
phpReturnType = phpReturnType + "|null";
219+
} else {
220+
phpReturnType = "?" + phpReturnType;
221+
}
218222
docReturnType = docReturnType + "|null";
219223
}
220224

modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpNextgenClientCodegenTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.testng.annotations.Test;
3030

3131
import java.io.File;
32+
import java.io.IOException;
3233
import java.nio.file.Files;
3334
import java.util.List;
3435
import java.util.Map;
@@ -146,4 +147,33 @@ public void testEnumUnknownDefaultCaseDeserializationDisabled() throws Exception
146147
Assert.assertListNotContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), "");
147148
Assert.assertListContains(modelContent, a -> a.equalsIgnoreCase("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
148149
}
150+
151+
@Test
152+
public void testDifferentResponseSchemasWithEmpty() throws IOException {
153+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
154+
output.deleteOnExit();
155+
156+
OpenAPI openAPI = new OpenAPIParser()
157+
.readLocation("src/test/resources/bugs/issue_22817.yaml", null, new ParseOptions())
158+
.getOpenAPI();
159+
160+
161+
codegen.setOutputDir(output.getAbsolutePath());
162+
ClientOptInput input = new ClientOptInput()
163+
.openAPI(openAPI)
164+
.config(codegen);
165+
166+
DefaultGenerator generator = new DefaultGenerator();
167+
Map<String, File> files = generator.opts(input).generate().stream()
168+
.collect(Collectors.toMap(File::getName, Function.identity()));
169+
170+
List<String> modelContent = Files
171+
.readAllLines(files.get("DefaultApi.php").toPath())
172+
.stream()
173+
.map(String::trim)
174+
.collect(Collectors.toList());
175+
176+
Assert.assertListContains(modelContent, a -> a.equals("): int|string|null"), "Expected to find nullable return type declaration.");
177+
Assert.assertListNotContains(modelContent, a -> a.equals("): ?int|string"), "Expected to not find invalid union type with '?'.");
178+
}
149179
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
openapi: 3.0.4
2+
info:
3+
title: "Different response schemas including an empty one"
4+
version: "1.0.0"
5+
paths:
6+
/example:
7+
get:
8+
operationId: exampleGet
9+
responses:
10+
200:
11+
description: "A successful response with data"
12+
content:
13+
application/json:
14+
schema:
15+
type: integer
16+
400:
17+
description: "A bad request with a message"
18+
content:
19+
application/json:
20+
schema:
21+
type: string
22+
500:
23+
description: "An internal server error with no content"
24+
content: { }

0 commit comments

Comments
 (0)