Skip to content

Commit

Permalink
[Java] add okhttp template test and regenerate sample
Browse files Browse the repository at this point in the history
  • Loading branch information
fanqiewanzi committed Apr 27, 2024
1 parent 5ac583c commit fcc8bc4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> 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()) {");
}
}
61 changes: 61 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_18516.yaml
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit fcc8bc4

Please sign in to comment.