Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty relationship objects still being made when data is null #278

Open
dhenson02 opened this issue Oct 24, 2024 · 0 comments
Open

Empty relationship objects still being made when data is null #278

dhenson02 opened this issue Oct 24, 2024 · 0 comments

Comments

@dhenson02
Copy link

Returned data in a dataset comes back as null but the relationship object is still being created.

Examples:

// when data exists, this works:
      "relationships": {
        "notes": {
          "data": [
            {
              "type": "notes",
              "id": "01929cc3-37f9-7461-8661-a76256691082"
            }
          ]
        }

// when data is null, this is what is returned:
      "relationships": {
        "notes": { "data": null }
      }

The second one will fail validation and needs to be removed entirely if nothing in the relationship in order to pass.

Suggestions on how to do this with this library are welcome. I am currently just manually removing them with the following:

function fixRelationships ( serializedResponse ) {
    const withoutEmptyRelationships = serializedResponse.data.map(d => {
        if ( "relationships" in d ) {
            const newRel = Object.keys(d.relationships).reduce(( obj, key ) => {
                const rel = d.relationships[ key ];
                if ( rel.data || rel.links || rel.meta ) {
                    obj[ key ] = rel;
                }
                return obj;
            }, {});
            if ( Object.keys(newRel).length > 0 ) {
                d.relationships = newRel;
            }
            else {
                delete d.relationships;
            }
        }
        return d;
    });

    return {
        ...serializedResponse,
        data: withoutEmptyRelationships,
    };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant