woohoolabs / yang

The efficient and elegant, PSR-7 compliant JSON:API 1.1 client library for PHP
MIT License
168 stars 13 forks source link

Fixing hydrator issue when relationship is not included #36

Open BahaaAlhagar opened 2 years ago

BahaaAlhagar commented 2 years ago

I was trying to use DocumentHydrator to hydrate incoming request

I found out an issue on the Hydrator relationship is not included it gets ignored

consider this example

{
    "data": {
        "id": "",
        "type": "used_car",
        "attributes": {
            "year": 1972,
            "status": "draft"
        },
        "relationships": {
            "brand": {
                "data": {
                    "id": "9",
                    "type": "brand"
                }
            },
            "carBodyShape": {
                "data": {
                    "id": "2",
                    "type": "car_body_shape"
                }
            },
            "carModel": {
                "data": {
                    "id": "3",
                    "type": "car_model"
                }
            },
            "carGrade": {
                "data": {
                    "id": "4",
                    "type": "car_grade"
                }
            },
            "country": {
                "data": {
                    "id": "4",
                    "type": "country"
                }
            },
            "city": {
                "data": {
                    "id": "4",
                    "type": "city"
                }
            },
            "area": {
                "data": {
                    "id": "4",
                    "type": "area"
                }
            },
            "more_licenses": {
                "data": [
                    {
                        "id": "bb4c1dba-3d9d-4df1-85d0-7cb28wec4e135",
                        "type": "used_car_license"
                    },
                    {
                        "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ewc4e136",
                        "type": "used_car_license"
                    }
                ]
            },
            "licenses": {
                "data": [
                    {
                        "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e135",
                        "type": "used_car_license"
                    },
                    {
                        "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e136",
                        "type": "used_car_license"
                    }
                ]
            },
            "internalImages": {
                "data": [
                    {
                        "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e135",
                        "type": "used_car_image"
                    },
                    {
                        "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e136",
                        "type": "used_car_image"
                    }
                ]
            },
            "externalImages": {
                "data": [
                    {
                        "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e137",
                        "type": "used_car_image"
                    },
                    {
                        "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e138",
                        "type": "used_car_image"
                    }
                ]
            }
        }
    },
    "included": [
        {
            "id": "9",
            "type": "brand",
            "attributes": {
                "title": "repellat"
            }

        },
        {
            "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e136",
            "type": "used_car_license",
            "attributes": {
                "file": 1,
                "image_type": "front"
            }

        },
        {
            "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e135",
            "type": "used_car_license",
            "attributes": {
                "file": 1,
                "image_type": "back"
            }
        },
        {
            "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e136",
            "type": "used_car_image",
            "attributes": {
                "file": 1,
                "image_type": "internal"
            }

        },
        {
            "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e135",
            "type": "used_car_image",
            "attributes": {
                "file": 1,
                "image_type": "internal"
            }
        },
        {
            "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e137",
            "type": "used_car_image",
            "attributes": {
                "file": 1,
                "image_type": "external"
            }

        },
        {
            "id": "bb4c1dba-3d9d-4df1-85d0-7cb28ec4e138",
            "type": "used_car_image",
            "attributes": {
                "file": 1,
                "image_type": "external"
            }
        }
    ]
}

On the last case all relationships that does not have included data gets removed from serialized to JSON object carModel, carBodyShape, carGrade will get ignored because it does not included

This updates add an object for each not included relationship while hydrating data The object will have only id & type