Polymorphic schema usage
See original GitHub issueHi, I have a question regarding usage of polymorphic schema objects.
Say, I have a base Config class object, with only id, name and type fields:
class Config(Schema):
id = base_fields.Integer(description='Configuration ID', required=True)
name = base_fields.String(description='Configuration name', required=True)
type = base_fields.Integer(description='Configuration type', required=True)
Now, assuming that there will be many schema types that derives from Config but each with specific field(s), like:
class ConfigFile(Schema):
path = base_fields.String(description='Config file path', required=True)
class ConfigDB(Schema):
user = base_fields.String(description='DB username', required=True)
password = base_fields.String(description='DB password', required=True)
When e.g. I pull data from the DB, I will get list of SQLAlchemy objects that are polymorphic, and type is a discriminator:
__mapper_args__ = {'polymorphic_identity': 'ConfigFile'}
...
__mapper_args__ = {'polymorphic_identity': 'ConfigDB'}
Is it possible to use ConfigFile schema when for example, creating a response which can hold list of objects that are sub-classes of it like:
class ConfigResponse(Schema):
response = base_fields.Nested(ConfigFile, description='', many=True, required=True)
Is usage of marshmallow-oneofschema safe or some custom fix is recommended? Additional reason to ask is because we also use flasgger and I am not sure how it will ‘dance’ with it.
One additional question, not related to this topic: what are performance penalties comparing to usage of custom serialize method/class based on plain dictionaries? I like the library, but one of my coworkers keeps undermine usage of it based on suspicions regarding speed and not only that but also of ease of usage. I think second can not be further from the truth, but for speed I am not sure how much is the overhead. Are there some figures already, or to make my own test?
Thank you in advance
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
Apispec uses a “discriminator” to solve the problem of determining which schema an object belongs to. It’s not a perfect solution, but I think it is something that marshmallow/apispec could implement.
Thank you all!