-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set explicit default null values for nullable fields in generated schemas #294
Set explicit default null values for nullable fields in generated schemas #294
Conversation
Need this PR merged, please! :) |
Sorry for the slow response - I'll try to take a look this evening. |
I agree this behaviour should be available but I also think it should be optional - so keep the existing |
@bplommer thanks for looking into this. |
I guess it's also possible to use magnolia's param default value extractor and have more generic annotation: case class ABC(
@AvroDefaultValue a: Int = 5,
b: Option[String],
@AvroDefaultValue c: Option[Long] = None
) =>
With implementation like: ...
param.annotations.collectFirst {
case adv: AvroDefaultValue =>
param.default match {
case Some(defaultValue) =>
param.typeclass.encode(defaultValue) match {
case Left(err) => throw err.throwable
case Right(value) =>
// TODO: Handle other unions except for [null, ???]
if (schema.isNullable && value == null)
Schema.Field.NULL_DEFAULT_VALUE
else if (schema.isNullable)
throw new Exception(
s"For optional fields only supported default value is None, field ${param.label} has default value $defaultValue."
)
else value
}
case None =>
throw new Exception(
s"Missing default value for a field ${param.label} annotated with $adv."
)
}
}.orNull
... Which kinda works, but because in avro default values for union fields have to correspond to the first schema in the union would have to either:
|
This looks great, thanks!
I think it's best to leave this as is to avoid too much complexity. @vlovgr any thoughts on this before I merge? |
Agreed, let's keep the derivation module as simple as possible. |
Some schema consumers require explicitly set
"default": null
for the "nullable union field".Related: AVRO-1803