Skip to content

Commit 06b5c47

Browse files
fanqiewanziMarcel Jacek
authored andcommitted
[Java] add okhttp template test and regenerate sample
1 parent 40551fb commit 06b5c47

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
@@ -3769,6 +3769,34 @@ public void testClassesAreValidJavaOkHttpGson() {
37693769
);
37703770
}
37713771

3772+
@Test
3773+
public void testRequiredAndNullableAreBothTrue() throws IOException {
3774+
File output = Files.createTempDirectory("test").toFile();
3775+
output.deleteOnExit();
3776+
3777+
final CodegenConfigurator configurator = new CodegenConfigurator()
3778+
.setGeneratorName("java")
3779+
.setLibrary(JavaClientCodegen.OKHTTP_GSON)
3780+
.setInputSpec("src/test/resources/bugs/issue_18516.yaml")
3781+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
3782+
3783+
3784+
DefaultGenerator generator = new DefaultGenerator();
3785+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
3786+
files.forEach(File::deleteOnExit);
3787+
3788+
validateJavaSourceFiles(files);
3789+
3790+
Path modelFile = Paths.get(output + "/src/main/java/org/openapitools/client/model/SomeObject.java");
3791+
TestUtils.assertFileContains(
3792+
modelFile,
3793+
"} else if (!jsonObj.get(\"ids\").isJsonArray() && !jsonObj.get(\"ids\").isJsonNull()) {",
3794+
"if (!jsonObj.get(\"users\").isJsonArray() && !jsonObj.get(\"users\").isJsonNull()) {",
3795+
"if (jsonObj.get(\"user\") != null && !jsonObj.get(\"user\").isJsonNull()) {",
3796+
"if (jsonObj.get(\"role\") != null && !jsonObj.get(\"role\").isJsonNull()) {",
3797+
"if (jsonObj.get(\"custom\") != null && !jsonObj.get(\"custom\").isJsonNull()) {");
3798+
}
3799+
37723800
@Test(description = "Issue #21051")
37733801
public void givenComplexObjectHasDefaultValueWhenGenerateThenDefaultAssignmentsAreValid() throws Exception {
37743802
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
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
@@ -424,7 +424,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
424424
// ensure the required json array is present
425425
if (jsonObj.get("photoUrls") == null) {
426426
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
427-
} else if (!jsonObj.get("photoUrls").isJsonArray()) {
427+
} else if (!jsonObj.get("photoUrls").isJsonArray() && !jsonObj.get("photoUrls").isJsonNull()) {
428428
throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
429429
}
430430
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {

0 commit comments

Comments
 (0)