Skip to content

Commit 57316e6

Browse files
committed
[Java] add okhttp template test and regenerate sample
1 parent 96f9f2b commit 57316e6

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
@@ -3473,4 +3473,32 @@ void testBuilderJavaClient() throws IOException {
34733473
"hasLegs(getHasLegs())");
34743474
}
34753475

3476+
3477+
@Test
3478+
public void testRequiredAndNullableAreBothTrue() throws IOException {
3479+
File output = Files.createTempDirectory("test").toFile();
3480+
output.deleteOnExit();
3481+
3482+
final CodegenConfigurator configurator = new CodegenConfigurator()
3483+
.setGeneratorName("java")
3484+
.setLibrary(JavaClientCodegen.OKHTTP_GSON)
3485+
.setInputSpec("src/test/resources/bugs/issue_18516.yaml")
3486+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
3487+
3488+
3489+
DefaultGenerator generator = new DefaultGenerator();
3490+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
3491+
files.forEach(File::deleteOnExit);
3492+
3493+
validateJavaSourceFiles(files);
3494+
3495+
Path modelFile = Paths.get(output + "/src/main/java/org/openapitools/client/model/SomeObject.java");
3496+
TestUtils.assertFileContains(
3497+
modelFile,
3498+
"} else if (!jsonObj.get(\"ids\").isJsonArray() && !jsonObj.get(\"ids\").isJsonNull()) {",
3499+
"if (!jsonObj.get(\"users\").isJsonArray() && !jsonObj.get(\"users\").isJsonNull()) {",
3500+
"if (jsonObj.get(\"user\") != null && !jsonObj.get(\"user\").isJsonNull()) {",
3501+
"if (jsonObj.get(\"role\") != null && !jsonObj.get(\"role\").isJsonNull()) {",
3502+
"if (jsonObj.get(\"custom\") != null && !jsonObj.get(\"custom\").isJsonNull()) {");
3503+
}
34763504
}
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)