From b1dcf65c5b3754a7daa8fc6768d28efb5e989e3d Mon Sep 17 00:00:00 2001 From: Patrice De Saint Steban Date: Wed, 22 Jul 2020 07:13:59 +0200 Subject: [PATCH] [Avro] Default value for no required fields to null (#7006) * Default value for the non required fields to null When a field is not required, the avro schema has no default value. Has described in an [avro issues](https://issues.apache.org/jira/browse/AVRO-1803), the property default: null must be set. If we don't do this in the avro file, when we generate java code from generated avro schema, the builder fail if the data isn't set in the avro data class. * Update generated avro sample --- .../main/resources/avro-schema/pojo.mustache | 5 ++-- .../avro-schema/.openapi-generator/FILES | 8 ++++++ .../avro-schema/.openapi-generator/VERSION | 2 +- .../petstore/avro-schema/ApiResponse.avsc | 10 +++++--- .../schema/petstore/avro-schema/Category.avsc | 7 ++++-- .../petstore/avro-schema/InlineObject.avsc | 7 ++++-- .../petstore/avro-schema/InlineObject1.avsc | 7 ++++-- .../schema/petstore/avro-schema/Order.avsc | 19 +++++++++----- .../schema/petstore/avro-schema/Pet.avsc | 13 +++++++--- .../schema/petstore/avro-schema/Tag.avsc | 7 ++++-- .../schema/petstore/avro-schema/User.avsc | 25 +++++++++++++------ 11 files changed, 78 insertions(+), 32 deletions(-) create mode 100644 samples/openapi3/schema/petstore/avro-schema/.openapi-generator/FILES diff --git a/modules/openapi-generator/src/main/resources/avro-schema/pojo.mustache b/modules/openapi-generator/src/main/resources/avro-schema/pojo.mustache index 0472804f293..e6967d322a3 100644 --- a/modules/openapi-generator/src/main/resources/avro-schema/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/avro-schema/pojo.mustache @@ -3,7 +3,8 @@ { "name": "{{baseName}}", "type": {{^required}}["null", {{/required}}{{>typeProperty}}{{^required}}]{{/required}}, - "doc": "{{{description}}}" + "doc": "{{{description}}}"{{^required}}, + "default": null{{/required}} }{{^-last}},{{/-last}} {{/vars}} - ] \ No newline at end of file + ] diff --git a/samples/openapi3/schema/petstore/avro-schema/.openapi-generator/FILES b/samples/openapi3/schema/petstore/avro-schema/.openapi-generator/FILES new file mode 100644 index 00000000000..894d1ff1db0 --- /dev/null +++ b/samples/openapi3/schema/petstore/avro-schema/.openapi-generator/FILES @@ -0,0 +1,8 @@ +ApiResponse.avsc +Category.avsc +InlineObject.avsc +InlineObject1.avsc +Order.avsc +Pet.avsc +Tag.avsc +User.avsc diff --git a/samples/openapi3/schema/petstore/avro-schema/.openapi-generator/VERSION b/samples/openapi3/schema/petstore/avro-schema/.openapi-generator/VERSION index c3a2c7076fa..d99e7162d01 100644 --- a/samples/openapi3/schema/petstore/avro-schema/.openapi-generator/VERSION +++ b/samples/openapi3/schema/petstore/avro-schema/.openapi-generator/VERSION @@ -1 +1 @@ -4.2.0-SNAPSHOT \ No newline at end of file +5.0.0-SNAPSHOT \ No newline at end of file diff --git a/samples/openapi3/schema/petstore/avro-schema/ApiResponse.avsc b/samples/openapi3/schema/petstore/avro-schema/ApiResponse.avsc index d3ce5cc6aea..f105b1688d3 100644 --- a/samples/openapi3/schema/petstore/avro-schema/ApiResponse.avsc +++ b/samples/openapi3/schema/petstore/avro-schema/ApiResponse.avsc @@ -7,17 +7,21 @@ { "name": "code", "type": ["null", "int"], - "doc": "" + "doc": "", + "default": null }, { "name": "type", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null }, { "name": "message", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null } ] + } diff --git a/samples/openapi3/schema/petstore/avro-schema/Category.avsc b/samples/openapi3/schema/petstore/avro-schema/Category.avsc index 527f2d389cd..1ff49df61c0 100644 --- a/samples/openapi3/schema/petstore/avro-schema/Category.avsc +++ b/samples/openapi3/schema/petstore/avro-schema/Category.avsc @@ -7,12 +7,15 @@ { "name": "id", "type": ["null", "long"], - "doc": "" + "doc": "", + "default": null }, { "name": "name", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null } ] + } diff --git a/samples/openapi3/schema/petstore/avro-schema/InlineObject.avsc b/samples/openapi3/schema/petstore/avro-schema/InlineObject.avsc index 70af82014c9..57b4895da79 100644 --- a/samples/openapi3/schema/petstore/avro-schema/InlineObject.avsc +++ b/samples/openapi3/schema/petstore/avro-schema/InlineObject.avsc @@ -7,12 +7,15 @@ { "name": "name", "type": ["null", "string"], - "doc": "Updated name of the pet" + "doc": "Updated name of the pet", + "default": null }, { "name": "status", "type": ["null", "string"], - "doc": "Updated status of the pet" + "doc": "Updated status of the pet", + "default": null } ] + } diff --git a/samples/openapi3/schema/petstore/avro-schema/InlineObject1.avsc b/samples/openapi3/schema/petstore/avro-schema/InlineObject1.avsc index 322dae3751e..6ef90935203 100644 --- a/samples/openapi3/schema/petstore/avro-schema/InlineObject1.avsc +++ b/samples/openapi3/schema/petstore/avro-schema/InlineObject1.avsc @@ -7,12 +7,15 @@ { "name": "additionalMetadata", "type": ["null", "string"], - "doc": "Additional data to pass to server" + "doc": "Additional data to pass to server", + "default": null }, { "name": "file", "type": ["null", "model.File"], - "doc": "file to upload" + "doc": "file to upload", + "default": null } ] + } diff --git a/samples/openapi3/schema/petstore/avro-schema/Order.avsc b/samples/openapi3/schema/petstore/avro-schema/Order.avsc index 945f42d579e..c1d5e6c0e6e 100644 --- a/samples/openapi3/schema/petstore/avro-schema/Order.avsc +++ b/samples/openapi3/schema/petstore/avro-schema/Order.avsc @@ -7,22 +7,26 @@ { "name": "id", "type": ["null", "long"], - "doc": "" + "doc": "", + "default": null }, { "name": "petId", "type": ["null", "long"], - "doc": "" + "doc": "", + "default": null }, { "name": "quantity", "type": ["null", "int"], - "doc": "" + "doc": "", + "default": null }, { "name": "shipDate", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null }, { "name": "status", @@ -35,12 +39,15 @@ "delivered" ] }], - "doc": "Order Status" + "doc": "Order Status", + "default": null }, { "name": "complete", "type": ["null", "boolean"], - "doc": "" + "doc": "", + "default": null } ] + } diff --git a/samples/openapi3/schema/petstore/avro-schema/Pet.avsc b/samples/openapi3/schema/petstore/avro-schema/Pet.avsc index 46ccf5f3d66..b2925056c20 100644 --- a/samples/openapi3/schema/petstore/avro-schema/Pet.avsc +++ b/samples/openapi3/schema/petstore/avro-schema/Pet.avsc @@ -7,12 +7,14 @@ { "name": "id", "type": ["null", "long"], - "doc": "" + "doc": "", + "default": null }, { "name": "category", "type": ["null", "model.Category"], - "doc": "" + "doc": "", + "default": null }, { "name": "name", @@ -33,7 +35,8 @@ "type": "array", "items": "model.Tag" }], - "doc": "" + "doc": "", + "default": null }, { "name": "status", @@ -46,7 +49,9 @@ "sold" ] }], - "doc": "pet status in the store" + "doc": "pet status in the store", + "default": null } ] + } diff --git a/samples/openapi3/schema/petstore/avro-schema/Tag.avsc b/samples/openapi3/schema/petstore/avro-schema/Tag.avsc index ec31d8fb17a..ad5b34e5d4a 100644 --- a/samples/openapi3/schema/petstore/avro-schema/Tag.avsc +++ b/samples/openapi3/schema/petstore/avro-schema/Tag.avsc @@ -7,12 +7,15 @@ { "name": "id", "type": ["null", "long"], - "doc": "" + "doc": "", + "default": null }, { "name": "name", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null } ] + } diff --git a/samples/openapi3/schema/petstore/avro-schema/User.avsc b/samples/openapi3/schema/petstore/avro-schema/User.avsc index 2654a817946..53f1f9d36e5 100644 --- a/samples/openapi3/schema/petstore/avro-schema/User.avsc +++ b/samples/openapi3/schema/petstore/avro-schema/User.avsc @@ -7,42 +7,51 @@ { "name": "id", "type": ["null", "long"], - "doc": "" + "doc": "", + "default": null }, { "name": "username", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null }, { "name": "firstName", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null }, { "name": "lastName", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null }, { "name": "email", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null }, { "name": "password", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null }, { "name": "phone", "type": ["null", "string"], - "doc": "" + "doc": "", + "default": null }, { "name": "userStatus", "type": ["null", "int"], - "doc": "User Status" + "doc": "User Status", + "default": null } ] + }