You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I define an Avromatic class, and instantiate it, I expect all the nested records to also be instantiated. Instead, only the top-most record is instantiated, and any sub-records are nil.
Sorry. We don't have a fix yet, mainly because we've moved to creating immutable models by default for performance reasons and this defaulting is likely to result in a model tree that's invalid and can't be made valid. I definitely see how it could be useful when dealing with mutable models though. In the meantime, you could workaround it via something like:
moduleModelNestedRecordDefaultingEMPTY_HASH={}.freezedefinitialize(...)superattribute_definitions.eachdo |attribute_name,attribute_definition|
ifattribute_definition.type.is_a?(Avromatic::Model::Types::RecordType) && send(attribute_name).nil?send(attribute_definition.setter_name,EMPTY_HASH)endendendend# Include it in specific modelsAvromatic::Model.model(schema: schema).include(ModelNestedRecordDefaulting)# Alternatively patch it in globally for all modelsmoduleBuilderInclusionsPatchdefinclusionssuper + [ModelNestedRecordDefaulting]endendAvromatic::Model::Builder.prepend(BuilderInclusionsPatch)
When I define an Avromatic class, and instantiate it, I expect all the nested records to also be instantiated. Instead, only the top-most record is instantiated, and any sub-records are nil.
Here's a simple reproduction:
Avro Schema:
Then we load the schema and create a class:
Actual Result:
Expected Result:
Obviously, it's possible to manually instantiate the entire class hierarchy, but this quickly becomes difficult as the level of nesting increases.
The text was updated successfully, but these errors were encountered: