marcosschroh / dataclasses-avroschema

Generate avro schemas from python classes. Code generation from avro schemas. Serialize/Deserialize python instances with avro schemas
https://marcosschroh.github.io/dataclasses-avroschema/
MIT License
213 stars 64 forks source link

Add option to use `original_schema` in AvroModel.serialize() #712

Open kamilglod opened 1 month ago

kamilglod commented 1 month ago

Is your feature request related to a problem? Please describe. I generate dataclass models from AVRO schema that other team provides, and I want to be sure that I'm generating AVRO data using exact same schema. So I'm building python instance and now I want to call .serialize() but it uses only auto-generated schema from model, there is no option to use original_schema

Describe the solution you'd like It would be nice to have either separate method or flag as an argument of the .serialize() method, something like use_original_schema: bool = False (for me True should be default but it would break backward compatibility)

Describe alternatives you've considered Creating helper function in my code that would be used alywas to serialize model:

def serialize(model: AvroModel) -> bytes:
    return serialization.serialize(
        model.asdict(standardize_factory=utils.standardize_custom_type),
        model.original_schema,
        serialization_type="avro",
    )
marcosschroh commented 2 weeks ago

Hi @kamilglod

I think adding a flag called use_original_schema: bool = False to serialize method is a good idea.

If the original_schema was included in class Meta and the flag was set to True then the original_schema will be used, any other combination will be the fallback schema.

Also, this new flag might have to be added to the deserialize, which requires a bit more of refinement because we can use already 2 schemas to deserialize (contained schema and writer schema)