Description
OneOfSchema
is the go-to answer for questions about polymorphism.
I would like to see it on-course to have the following two things happen:
- Avoid overriding
load
anddump
, so that hooks work
As I have proposed elsewhere, I think it should hook in at _deserialize
and _serialize
. It's hard to make the case for extending marshmallow
to support this use-case right now.
- Built-in support in apispec
This is possible without bringing it into marshmallow
, but harder to justify.
The current marshmallow-oneofschema code is quite small in size, only a little over 100 LLOC.
Plus, I think we can make it even smaller and simpler.
We could boil the whole thing down to something like this:
class OneOfSchema(Schema):
oneof: Dict[str, Type[Schema]] = {}
discriminator: Optional[str] = None
def discriminate_on_load(self, data):
return self.oneof[self.discriminator]
def discriminate_on_dump(self, data):
return self.oneof[data["discriminator"]]
def _serialize(...): ...
def _deserialize(...): ...
I also think moving it into marshmallow
would result in more active maintenance.
@sloria, @lafrech, @deckar01, has this been discussed in the past? I wasn't able to find any useful clues other than some comments about not wanting to do this in the past. I understand not wanting to grow the public surface of a package and take on the related doc, bugfix, Q&A, etc workload, but in this case I think it's counterproductive.
I'd be happy to work on this if it stood a chance at being accepted.