Skip to content

Commit fcc8bc4

Browse files
committed
[Java] add okhttp template test and regenerate sample
1 parent 5ac583c commit fcc8bc4

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3178,4 +3178,32 @@ public void testQueryParamsExploded_whenQueryParamIsNull() throws IOException {
31783178
Path petApi = Paths.get(output + "/src/main/java/xyz/abcdef/api/DepartmentApi.java");
31793179
TestUtils.assertFileContains(petApi, "if (filter != null) {");
31803180
}
3181+
3182+
@Test
3183+
public void testRequiredAndNullableAreBothTrue() throws IOException {
3184+
File output = Files.createTempDirectory("test").toFile();
3185+
output.deleteOnExit();
3186+
3187+
final CodegenConfigurator configurator = new CodegenConfigurator()
3188+
.setGeneratorName("java")
3189+
.setLibrary(JavaClientCodegen.OKHTTP_GSON)
3190+
.setInputSpec("src/test/resources/bugs/issue_18516.yaml")
3191+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
3192+
3193+
3194+
DefaultGenerator generator = new DefaultGenerator();
3195+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
3196+
files.forEach(File::deleteOnExit);
3197+
3198+
validateJavaSourceFiles(files);
3199+
3200+
Path modelFile = Paths.get(output + "/src/main/java/org/openapitools/client/model/SomeObject.java");
3201+
TestUtils.assertFileContains(
3202+
modelFile,
3203+
"} else if (!jsonObj.get(\"ids\").isJsonArray() && !jsonObj.get(\"ids\").isJsonNull()) {",
3204+
"if (!jsonObj.get(\"users\").isJsonArray() && !jsonObj.get(\"users\").isJsonNull()) {",
3205+
"if (jsonObj.get(\"user\") != null && !jsonObj.get(\"user\").isJsonNull()) {",
3206+
"if (jsonObj.get(\"role\") != null && !jsonObj.get(\"role\").isJsonNull()) {",
3207+
"if (jsonObj.get(\"custom\") != null && !jsonObj.get(\"custom\").isJsonNull()) {");
3208+
}
31813209
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
description: Test API
5+
version: 1.0.1
6+
7+
paths:
8+
/test:
9+
get:
10+
responses:
11+
200:
12+
description: Valid response
13+
content:
14+
application/json:
15+
schema:
16+
$ref: "#/components/schemas/SomeObject"
17+
18+
components:
19+
schemas:
20+
SomeObject:
21+
type: object
22+
required:
23+
- ids
24+
- users
25+
- user
26+
- role
27+
- custom
28+
properties:
29+
ids:
30+
type: array
31+
nullable: true
32+
items:
33+
type: integer
34+
users:
35+
type: array
36+
nullable: true
37+
items:
38+
type: object
39+
properties:
40+
id:
41+
type: string
42+
user:
43+
type: object
44+
nullable: true
45+
properties:
46+
id:
47+
type: string
48+
role:
49+
type: string
50+
nullable: true
51+
enum:
52+
- admin
53+
- tenant
54+
custom:
55+
$ref: "#/components/schemas/customEnum"
56+
customEnum:
57+
type: string
58+
nullable: true
59+
enum:
60+
- custom
61+

samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/model/PetWithRequiredNullableCases1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
427427
// ensure the required json array is present
428428
if (jsonObj.get("photoUrls") == null) {
429429
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
430-
} else if (!jsonObj.get("photoUrls").isJsonArray()) {
430+
} else if (!jsonObj.get("photoUrls").isJsonArray() && !jsonObj.get("photoUrls").isJsonNull()) {
431431
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
432432
}
433433
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {

0 commit comments

Comments
 (0)