Skip to content
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

Cannot add field default value if it contains a collection of records #592

Closed
ex-ratt opened this issue Jun 5, 2024 · 0 comments · Fixed by #605
Closed

Cannot add field default value if it contains a collection of records #592

ex-ratt opened this issue Jun 5, 2024 · 0 comments · Fixed by #605

Comments

@ex-ratt
Copy link
Contributor

ex-ratt commented Jun 5, 2024

When manually creating a record codec, then it is possible to define default values for fields. This works most of the time, but it will not work if the value is a collection or has a nested collection that contains records. See the following example to reproduce the issue:

import vulcan.Codec
import vulcan.generic.*

case class Element(value: String)
case class Collection(elements: List[Element])

implicit val elementCodec: Codec[Element] = Codec.derive
implicit val collectionCodec: Codec[Collection] = Codec.record(
  name = "Collection",
  namespace = "whatever",
)(field => field(name = "elements", access = _.elements, default = Some(List(Element("a")))).map(Collection.apply))

println(collectionCodec.schema) // Left(AvroError(org.apache.avro.AvroRuntimeException: Unknown datum class: class org.apache.avro.generic.GenericData$Record))

The bug is caused by a missing case in vulcan.internal.schema.adaptForSchema(Any). There is a recursion for IndexedRecord, but it would also need to recurse on the elements of java.util.Collection or java.util.List (the actual type of encoded is java.util.ArrayList according to the debugger).

If the field elements in the example above is Map[String, Element] instead of a List[Element], then the schema could also not be generated, because encoded would be a JavaCollectionWrappers.MapWrapper, which is also not handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant