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
2 changes: 1 addition & 1 deletion bin/utils/test_file_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/ClientTest.java"
sha256: 325fdd5d7e2c97790c0fb44f712ab7b2ba022d7e1a5b0056f47b07f342682b6d
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java"
sha256: 67941355a0a27ed9ff9318b1caa103e78b81b9aff61b594b18be5cd2bb9f6591
sha256: b1b1d31e0df17f0b68cf2747a4a53879f12acb1bf2860e45385c679c1efe9894
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/api/PetApiTest.java"
sha256: 8b1b8f2a2ad00ccb090873a94a5f73e328b98317d2ec715f53bd7a1accb2a023
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/PetTest.java"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,16 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
{{#isArray}}
{{#items.isModel}}
{{#required}}
// ensure the json data is an array
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
if (jsonObj.get("{{{baseName}}}") != null{{#isNullable}} && !jsonObj.get("{{{baseName}}}").isJsonNull(){{/isNullable}}) {
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
}
JsonArray jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}");
// validate the required field `{{{baseName}}}` (array)
for (int i = 0; i < jsonArray{{name}}.size(); i++) {
{{{items.dataType}}}.validateJsonElement(jsonArray{{name}}.get(i));
}
}

JsonArray jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}");
// validate the required field `{{{baseName}}}` (array)
for (int i = 0; i < jsonArray{{name}}.size(); i++) {
{{{items.dataType}}}.validateJsonElement(jsonArray{{name}}.get(i));
};
{{/required}}
{{^required}}
if (jsonObj.get("{{{baseName}}}") != null && !jsonObj.get("{{{baseName}}}").isJsonNull()) {
Expand Down Expand Up @@ -424,7 +424,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
// ensure the required json array is present
if (jsonObj.get("{{{baseName}}}") == null) {
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
} else if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
} else if (!jsonObj.get("{{{baseName}}}").isJsonArray(){{#isNullable}} && !jsonObj.get("{{baseName}}").isJsonNull(){{/isNullable}}) {
throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
}
{{/required}}
Expand All @@ -438,8 +438,14 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
{{/isString}}
{{#isModel}}
{{#required}}
{{#isNullable}}
if (jsonObj.get("{{{baseName}}}") != null && !jsonObj.get("{{{baseName}}}").isJsonNull()) {
{{/isNullable}}
// validate the required field `{{{baseName}}}`
{{{dataType}}}.validateJsonElement(jsonObj.get("{{{baseName}}}"));
{{#isNullable}}
}
{{/isNullable}}
{{/required}}
{{^required}}
// validate the optional field `{{{baseName}}}`
Expand All @@ -450,8 +456,14 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
{{/isModel}}
{{#isEnum}}
{{#required}}
{{#isNullable}}
if (jsonObj.get("{{{baseName}}}") != null && !jsonObj.get("{{{baseName}}}").isJsonNull()) {
{{/isNullable}}
// validate the required field `{{{baseName}}}`
{{{datatypeWithEnum}}}.validateJsonElement(jsonObj.get("{{{baseName}}}"));
{{#isNullable}}
}
{{/isNullable}}
{{/required}}
{{^required}}
// validate the optional field `{{{baseName}}}`
Expand All @@ -462,8 +474,14 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
{{/isEnum}}
{{#isEnumRef}}
{{#required}}
{{#isNullable}}
if (jsonObj.get("{{{baseName}}}") != null && !jsonObj.get("{{{baseName}}}").isJsonNull()) {
{{/isNullable}}
// validate the required field `{{{baseName}}}`
{{{dataType}}}.validateJsonElement(jsonObj.get("{{{baseName}}}"));
{{#isNullable}}
}
{{/isNullable}}
{{/required}}
{{^required}}
// validate the optional field `{{{baseName}}}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3769,6 +3769,35 @@ public void testClassesAreValidJavaOkHttpGson() {
);
}

@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\") != null && !jsonObj.get(\"users\").isJsonNull()) {",
"if (!jsonObj.get(\"users\").isJsonArray()) {",
"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()) {");
}

@Test(description = "Issue #21051")
public void givenComplexObjectHasDefaultValueWhenGenerateThenDefaultAssignmentsAreValid() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,19 @@ paths:
$ref: '#/components/schemas/Variable'
'400':
description: Invalid Value
/fake/required-nullable-body:
get:
tags:
- fake
summary: fields in the response body, required and nullable are both true
description: ''
responses:
'200':
description: success
content:
application/json:
schema:
$ref: '#/components/schemas/RequiredNullableBody'
servers:
- url: 'http://{server}.swagger.io:{port}/v2'
description: petstore server
Expand Down Expand Up @@ -2768,6 +2781,38 @@ components:
$ref: '#/components/schemas/ArrayOneOf'
anyof_prop:
$ref: '#/components/schemas/ArrayAnyOf'
NullableEnum:
type: string
nullable: true
enum:
- custom
RequiredNullableBody:
allOf:
- $ref: '#/components/schemas/NullableClass'
- type: object
required:
- custom_ref_enum
- custom_enum
- integer_prop
- number_prop
- boolean_prop
- string_prop
- date_prop
- datetime_prop
- array_nullable_prop
- array_and_items_nullable_prop
- array_items_nullable
- object_nullable_prop
- object_and_items_nullable_prop
- object_items_nullable
properties:
custom_ref_enum:
$ref: "#/components/schemas/NullableEnum"
custom_enum:
type: string
nullable: true
enum:
- custom
NestedArrayWithDefaultValues:
type: object
properties:
Expand Down
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 @@ -424,7 +424,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(java.util.Locale.ROOT, "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
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ docs/NewPet.md
docs/NewPetCategoryInlineAllof.md
docs/NewPetCategoryInlineAllofAllOfCategoryTag.md
docs/NullableClass.md
docs/NullableEnum.md
docs/NullableShape.md
docs/NumberOnly.md
docs/ObjectWithDeprecatedFields.md
Expand All @@ -100,6 +101,7 @@ docs/PropertyNameCollision.md
docs/Quadrilateral.md
docs/QuadrilateralInterface.md
docs/ReadOnlyFirst.md
docs/RequiredNullableBody.md
docs/Scalar.md
docs/ScalarAnyOf.md
docs/ScaleneTriangle.md
Expand Down Expand Up @@ -228,6 +230,7 @@ src/main/java/org/openapitools/client/model/NewPet.java
src/main/java/org/openapitools/client/model/NewPetCategoryInlineAllof.java
src/main/java/org/openapitools/client/model/NewPetCategoryInlineAllofAllOfCategoryTag.java
src/main/java/org/openapitools/client/model/NullableClass.java
src/main/java/org/openapitools/client/model/NullableEnum.java
src/main/java/org/openapitools/client/model/NullableShape.java
src/main/java/org/openapitools/client/model/NumberOnly.java
src/main/java/org/openapitools/client/model/ObjectWithDeprecatedFields.java
Expand All @@ -248,6 +251,7 @@ src/main/java/org/openapitools/client/model/PropertyNameCollision.java
src/main/java/org/openapitools/client/model/Quadrilateral.java
src/main/java/org/openapitools/client/model/QuadrilateralInterface.java
src/main/java/org/openapitools/client/model/ReadOnlyFirst.java
src/main/java/org/openapitools/client/model/RequiredNullableBody.java
src/main/java/org/openapitools/client/model/Scalar.java
src/main/java/org/openapitools/client/model/ScalarAnyOf.java
src/main/java/org/openapitools/client/model/ScaleneTriangle.java
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/java/okhttp-gson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Class | Method | HTTP request | Description
*FakeApi* | [**fakeOuterNumberSerialize**](docs/FakeApi.md#fakeOuterNumberSerialize) | **POST** /fake/outer/number |
*FakeApi* | [**fakeOuterStringSerialize**](docs/FakeApi.md#fakeOuterStringSerialize) | **POST** /fake/outer/string |
*FakeApi* | [**fakeRefParameter**](docs/FakeApi.md#fakeRefParameter) | **POST** /fake/pet/{petId}/reference/parameter | fake reference parameter
*FakeApi* | [**fakeRequiredNullableBodyGet**](docs/FakeApi.md#fakeRequiredNullableBodyGet) | **GET** /fake/required-nullable-body | fields in the response body, required and nullable are both true
*FakeApi* | [**fakeUploadRefRequestBodies**](docs/FakeApi.md#fakeUploadRefRequestBodies) | **POST** /fake/pet/{petId}/uploadImage | fake reference parameter
*FakeApi* | [**getFakeArrayofenums**](docs/FakeApi.md#getFakeArrayofenums) | **GET** /fake/array-of-enums | Array of Enums
*FakeApi* | [**getFakeHealth**](docs/FakeApi.md#getFakeHealth) | **GET** /fake/health | Health check endpoint
Expand Down Expand Up @@ -240,6 +241,7 @@ Class | Method | HTTP request | Description
- [NewPetCategoryInlineAllof](docs/NewPetCategoryInlineAllof.md)
- [NewPetCategoryInlineAllofAllOfCategoryTag](docs/NewPetCategoryInlineAllofAllOfCategoryTag.md)
- [NullableClass](docs/NullableClass.md)
- [NullableEnum](docs/NullableEnum.md)
- [NullableShape](docs/NullableShape.md)
- [NumberOnly](docs/NumberOnly.md)
- [ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md)
Expand All @@ -260,6 +262,7 @@ Class | Method | HTTP request | Description
- [Quadrilateral](docs/Quadrilateral.md)
- [QuadrilateralInterface](docs/QuadrilateralInterface.md)
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [RequiredNullableBody](docs/RequiredNullableBody.md)
- [Scalar](docs/Scalar.md)
- [ScalarAnyOf](docs/ScalarAnyOf.md)
- [ScaleneTriangle](docs/ScaleneTriangle.md)
Expand Down
71 changes: 71 additions & 0 deletions samples/client/petstore/java/okhttp-gson/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,21 @@ paths:
x-internal: true
x-accepts:
- application/json
/fake/required-nullable-body:
get:
description: ""
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/RequiredNullableBody"
description: success
summary: "fields in the response body, required and nullable are both true"
tags:
- fake
x-accepts:
- application/json
components:
parameters:
pet_id:
Expand Down Expand Up @@ -2870,6 +2885,62 @@ components:
anyof_prop:
$ref: "#/components/schemas/ArrayAnyOf"
type: object
NullableEnum:
enum:
- custom
nullable: true
type: string
RequiredNullableBody:
allOf:
- $ref: "#/components/schemas/NullableClass"
- properties:
custom_ref_enum:
$ref: "#/components/schemas/NullableEnum"
custom_enum:
enum:
- custom
nullable: true
type: string
required:
- array_and_items_nullable_prop
- array_items_nullable
- array_nullable_prop
- boolean_prop
- custom_enum
- custom_ref_enum
- date_prop
- datetime_prop
- integer_prop
- number_prop
- object_and_items_nullable_prop
- object_items_nullable
- object_nullable_prop
- string_prop
type: object
example:
number_prop: 6.027456183070403
datetime_prop: 2000-01-23T04:56:07.000+00:00
custom_ref_enum: custom
boolean_prop: true
string_prop: string_prop
array_nullable_prop:
- "{}"
- "{}"
custom_enum: custom
integer_prop: 0
array_and_items_nullable_prop:
- "{}"
- "{}"
object_items_nullable:
key: "{}"
object_nullable_prop:
key: "{}"
object_and_items_nullable_prop:
key: "{}"
date_prop: 2000-01-23
array_items_nullable:
- "{}"
- "{}"
NestedArrayWithDefaultValues:
properties:
nestedArray:
Expand Down
Loading
Loading