Skip to content

Commit

Permalink
[Avro] Default value for no required fields to null (#7006)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
patou authored Jul 22, 2020
1 parent 2ab35e0 commit b1dcf65
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
{
"name": "{{baseName}}",
"type": {{^required}}["null", {{/required}}{{>typeProperty}}{{^required}}]{{/required}},
"doc": "{{{description}}}"
"doc": "{{{description}}}"{{^required}},
"default": null{{/required}}
}{{^-last}},{{/-last}}
{{/vars}}
]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ApiResponse.avsc
Category.avsc
InlineObject.avsc
InlineObject1.avsc
Order.avsc
Pet.avsc
Tag.avsc
User.avsc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.0-SNAPSHOT
5.0.0-SNAPSHOT
10 changes: 7 additions & 3 deletions samples/openapi3/schema/petstore/avro-schema/ApiResponse.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]

}
7 changes: 5 additions & 2 deletions samples/openapi3/schema/petstore/avro-schema/Category.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
{
"name": "id",
"type": ["null", "long"],
"doc": ""
"doc": "",
"default": null
},
{
"name": "name",
"type": ["null", "string"],
"doc": ""
"doc": "",
"default": null
}
]

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]

}
19 changes: 13 additions & 6 deletions samples/openapi3/schema/petstore/avro-schema/Order.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -35,12 +39,15 @@
"delivered"
]
}],
"doc": "Order Status"
"doc": "Order Status",
"default": null
},
{
"name": "complete",
"type": ["null", "boolean"],
"doc": ""
"doc": "",
"default": null
}
]

}
13 changes: 9 additions & 4 deletions samples/openapi3/schema/petstore/avro-schema/Pet.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -33,7 +35,8 @@
"type": "array",
"items": "model.Tag"
}],
"doc": ""
"doc": "",
"default": null
},
{
"name": "status",
Expand All @@ -46,7 +49,9 @@
"sold"
]
}],
"doc": "pet status in the store"
"doc": "pet status in the store",
"default": null
}
]

}
7 changes: 5 additions & 2 deletions samples/openapi3/schema/petstore/avro-schema/Tag.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
{
"name": "id",
"type": ["null", "long"],
"doc": ""
"doc": "",
"default": null
},
{
"name": "name",
"type": ["null", "string"],
"doc": ""
"doc": "",
"default": null
}
]

}
25 changes: 17 additions & 8 deletions samples/openapi3/schema/petstore/avro-schema/User.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]

}

0 comments on commit b1dcf65

Please sign in to comment.