Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Schema serialization and deserialization is not symmetric? #419

Open
jeff-bonevich opened this issue Sep 21, 2021 · 1 comment
Open

Schema serialization and deserialization is not symmetric? #419

jeff-bonevich opened this issue Sep 21, 2021 · 1 comment

Comments

@jeff-bonevich
Copy link

jeff-bonevich commented Sep 21, 2021

If I try to dump a an object with a relationship, then deserialize the output, i do not get the same object - the relationship is now represented as the id of the related schema.

from marshmallow_jsonapi import fields, Schema
from pprint import pprint


class TeamSchema(Schema):
    id = fields.Int()
    name = fields.Str(required=True)

    class Meta:
        type_ = 'team'


class AssetSchema(Schema):
    id = fields.Int()
    name = fields.Str(required=True)

    team = fields.Relationship(
        include_resource_linkage=True,
        type_='team',
        schema='TeamSchema',
        id_field='id'
    )

    class Meta:
        type_ = 'asset'


data = {'name': 'Asset#1', 'team': {'id': 456}}
print('Input data:')
pprint(data)

asset_serialized = AssetSchema().dump(data)
print('*****DUMP (SERIALIZED) *****')
pprint(asset_serialized)

asset_deserialized = AssetSchema(include_data=('team',)).load(asset_serialized)
print('*****LOAD (DESERIALIZED) *****')
print('Deserialized vs original data:')
pprint(asset_deserialized)
pprint(data)

Output is:

Input data:
{'name': 'Asset#1', 'team': {'id': 456}}
*****DUMP (SERIALIZED) *****
{'data': {'attributes': {'name': 'Asset#1'},
          'relationships': {'team': {'data': {'id': '456', 'type': 'team'}}},
          'type': 'asset'}}
*****LOAD (DESERIALIZED) *****
Deserialized vs original data:
{'name': 'Asset#1', 'team': 456}
{'name': 'Asset#1', 'team': {'id': 456}}
@jeff-bonevich
Copy link
Author

Hello! Is this project dead? Or is there a better place to post this, such as stackoverflow or else where? Happy to jump in and submit patches, but would like to understand context etc first.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant