Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
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";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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 '?'.");
}
}
24 changes: 24 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_22817.yaml
Original file line number Diff line number Diff line change
@@ -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: { }
Loading