{"id":13935777,"url":"https://github.com/marshmallow-code/marshmallow-oneofschema","last_synced_at":"2025-04-13T04:00:02.325Z","repository":{"id":5780354,"uuid":"53915395","full_name":"marshmallow-code/marshmallow-oneofschema","owner":"marshmallow-code","description":"Marshmallow library extension that allows schema (de)multiplexing","archived":false,"fork":false,"pushed_at":"2025-04-07T18:39:00.000Z","size":151,"stargazers_count":139,"open_issues_count":27,"forks_count":43,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-13T03:59:57.376Z","etag":null,"topics":["hacktoberfest","marshmallow","polymorphic"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marshmallow-code.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"marshmallow","tidelift":"pypi/marshmallow"}},"created_at":"2016-03-15T04:46:28.000Z","updated_at":"2025-03-05T03:42:46.000Z","dependencies_parsed_at":"2023-02-14T00:45:14.662Z","dependency_job_id":"87c352bb-6a4a-4248-b353-82cc06aefb05","html_url":"https://github.com/marshmallow-code/marshmallow-oneofschema","commit_stats":{"total_commits":175,"total_committers":23,"mean_commits":7.608695652173913,"dds":0.7657142857142857,"last_synced_commit":"57acc2f91f3c93ef1fcc58d3032cb6bc5df7ef73"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marshmallow-code%2Fmarshmallow-oneofschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marshmallow-code%2Fmarshmallow-oneofschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marshmallow-code%2Fmarshmallow-oneofschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marshmallow-code%2Fmarshmallow-oneofschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marshmallow-code","download_url":"https://codeload.github.com/marshmallow-code/marshmallow-oneofschema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248661706,"owners_count":21141450,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","marshmallow","polymorphic"],"created_at":"2024-08-07T23:02:05.375Z","updated_at":"2025-04-13T04:00:02.297Z","avatar_url":"https://github.com/marshmallow-code.png","language":"Python","readme":"=======================\nmarshmallow-oneofschema\n=======================\n\n|pypi| |build-status| |marshmallow3|\n\n.. |pypi| image:: https://badgen.net/pypi/v/marshmallow-oneofschema\n    :target: https://pypi.org/project/marshmallow-oneofschema/\n    :alt: PyPI package\n\n.. |build-status| image:: https://github.com/marshmallow-code/marshmallow-oneofschema/actions/workflows/build-release.yml/badge.svg\n    :target: https://github.com/marshmallow-code/flask-marshmallow/actions/workflows/build-release.yml\n    :alt: Build status\n\n.. |marshmallow-support| image:: https://badgen.net/badge/marshmallow/3,4?list=1\n    :target: https://marshmallow.readthedocs.io/en/latest/upgrading.html\n    :alt: marshmallow 3|4 compatible\n\nAn extension to marshmallow to support schema (de)multiplexing.\n\nmarshmallow is a fantastic library for serialization and deserialization of data.\nFor more on that project see its `GitHub \u003chttps://github.com/marshmallow-code/marshmallow\u003e`_\npage or its `Documentation \u003chttp://marshmallow.readthedocs.org/en/latest/\u003e`_.\n\nThis library adds a special kind of schema that actually multiplexes other schemas\nbased on object type. When serializing values, it uses get_obj_type() method\nto get object type name. Then it uses ``type_schemas`` name-to-Schema mapping\nto get schema for that particular object type, serializes object using that\nschema and adds an extra field with name of object type. Deserialization is reverse.\n\nInstalling\n----------\n\n::\n\n    $ pip install marshmallow-oneofschema\n\nExample\n-------\n\nThe code below demonstrates how to set up a polymorphic schema. For the full context check out the tests.\nOnce setup the schema should act like any other schema. If it does not then please file an Issue.\n\n.. code:: python\n\n    import marshmallow\n    import marshmallow.fields\n    from marshmallow_oneofschema import OneOfSchema\n\n\n    class Foo:\n        def __init__(self, foo):\n            self.foo = foo\n\n\n    class Bar:\n        def __init__(self, bar):\n            self.bar = bar\n\n\n    class FooSchema(marshmallow.Schema):\n        foo = marshmallow.fields.String(required=True)\n\n        @marshmallow.post_load\n        def make_foo(self, data, **kwargs):\n            return Foo(**data)\n\n\n    class BarSchema(marshmallow.Schema):\n        bar = marshmallow.fields.Integer(required=True)\n\n        @marshmallow.post_load\n        def make_bar(self, data, **kwargs):\n            return Bar(**data)\n\n\n    class MyUberSchema(OneOfSchema):\n        type_schemas = {\"foo\": FooSchema, \"bar\": BarSchema}\n\n        def get_obj_type(self, obj):\n            if isinstance(obj, Foo):\n                return \"foo\"\n            elif isinstance(obj, Bar):\n                return \"bar\"\n            else:\n                raise Exception(\"Unknown object type: {}\".format(obj.__class__.__name__))\n\n\n    MyUberSchema().dump([Foo(foo=\"hello\"), Bar(bar=123)], many=True)\n    # =\u003e [{'type': 'foo', 'foo': 'hello'}, {'type': 'bar', 'bar': 123}]\n\n    MyUberSchema().load(\n        [{\"type\": \"foo\", \"foo\": \"hello\"}, {\"type\": \"bar\", \"bar\": 123}], many=True\n    )\n    # =\u003e [Foo('hello'), Bar(123)]\n\nBy default get_obj_type() returns obj.__class__.__name__, so you can just reuse that\nto save some typing:\n\n.. code:: python\n\n    class MyUberSchema(OneOfSchema):\n        type_schemas = {\"Foo\": FooSchema, \"Bar\": BarSchema}\n\nYou can customize type field with `type_field` class property:\n\n.. code:: python\n\n    class MyUberSchema(OneOfSchema):\n        type_field = \"object_type\"\n        type_schemas = {\"Foo\": FooSchema, \"Bar\": BarSchema}\n\n\n    MyUberSchema().dump([Foo(foo=\"hello\"), Bar(bar=123)], many=True)\n    # =\u003e [{'object_type': 'Foo', 'foo': 'hello'}, {'object_type': 'Bar', 'bar': 123}]\n\nYou can use resulting schema everywhere marshmallow.Schema can be used, e.g.\n\n.. code:: python\n\n    import marshmallow as m\n    import marshmallow.fields as f\n\n\n    class MyOtherSchema(m.Schema):\n        items = f.List(f.Nested(MyUberSchema))\n\nLicense\n-------\n\nMIT licensed. See the bundled `LICENSE \u003chttps://github.com/marshmallow-code/marshmallow-oneofschema/blob/master/LICENSE\u003e`_ file for more details.\n","funding_links":["https://opencollective.com/marshmallow","https://tidelift.com/funding/github/pypi/marshmallow"],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarshmallow-code%2Fmarshmallow-oneofschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarshmallow-code%2Fmarshmallow-oneofschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarshmallow-code%2Fmarshmallow-oneofschema/lists"}