-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Dont serialize new belongsTo records #5317
Conversation
LGTM 👍 |
Thanks @ryanto. |
How does this impact the embedded records mixin? Or those implementing their own rendition of it? While it does break spec, the idea seems to be the major issue being addressed by version 1.1 of the spec and seems to be the general consensus of how it would be solved (sending not only id/type but also attributes). The same principle applies to hasMany as well. Having said that, my team have broke spec and rely on this behavior. While we're not running this version yet I suspect it will break us badly. If there's any way we could re-introduce the existing behavior via a flag or similar that would be extremely helpful. Thanks! |
@travisghansen Yikes! Sorry to hear this might break your app. I'm happy to help you work out a fix. Do you mind posting what payload your app is sending before this change, and what payload your app is sending with this change? Version 1.1 of the JSON:API spec addresses multiple operation support and will be solved differently from the bug this PR originally addressed. |
@ryanto yeah I'll have a look (currently on 2.11 so I won't be able to upgrade immediately, but I'll make the hack in 2.11 as a start). What I'm describing is more than multiple operation support (I believe). This is the unique situation where the it's a parent/child style relationship but also wanting to be atomic (at the API layer at least). It raises a difficult question where the parent doesn't actually exist so the children can't bind preemptively (ie: an invoice/PO/whatever with line items). Regardless, thanks for the receptive response! As soon as I have a moment I'll apply the same logic to our existing code base and respond with examples. |
@ryanto ok, I've done some testing and this doesn't appear to impact us (we're hijacking serializeBelongsTo). For some reason I was thinking this would impact the |
This change broke functionality in our app too, probably because we have a custom solution for allowing multiple self-referencing records to be created in one atomic request to the backend. We make this work by using UUIDs generated on the frontend combined with async relationships, and sideload all records to be updated/persisted as part of the POST. Because of this we need to serialize the ID for the Thoughts? We'll revert to ED 3.0.1 for now, and maybe look at overriding How far through is the JSON API 1.1 spec? Is it still at an RFC stage? |
@lsg-richard you should absolutely override serializeBelongsTo. The default is just that, a default. It is not expected that it will work for everyone. |
Just jumping in to say this also broke some serious things for us. We ended up fixing it by using |
@fotinakis why wouldn’t you just implement a serializer hook that serializes them if your API supports it? Or were these actually existing records? |
@runspired Was easier to fix the other way. They are simple records that we can assume exist on the backend by ID, so we happened to use |
If the data can be assumed to be canonical then using push is the correct thing to do. 😀 |
Just been looking at how to upgrade from [email protected] in light of this change, and wondering if changing let belongsToIsNotNew = belongsTo && belongsTo.record && !belongsTo.record.get('isNew') to let belongsToIsNotNew = belongsTo && belongsTo.record && !isBlank(record.get('id')) would be considered? The rationale is that if there's an ID set then we're in a position to return valid JSON API, even though yes technically the record may be in the isNew state. |
@lsg-richard good suggestion, I think that will work! I'm happy to make the change, @runspired thoughts? @fotinakis Are you creating records with an |
This suggested change would also apply to let nonNewHasMany = hasMany.filter(item => item.record && !item.record.get('isNew')); would become let nonNewHasMany = hasMany.filter(item => item.record && !isBlank(item.record.get('id')); In this case I wonder if, for API consistency with let nonNewHasMany = hasMany.filter(item => this.shouldSerializeHasManyItem(snapshot, key, relationship, item));
...
shouldSerializeHasManyItem(snapshot, key, relationship, item) {
return item.record && !isBlank(item.record.get('id'))
} The latter may be a step too far for now, as it adds a new public API method, but it does feel consistent with the existing callbacks. The app I work on already adds its own system along these lines for doing custom filtering on hasMany serializations in order to optimize payload size in app-specific ways, which is partly what prompts me to make this suggestion. |
@lsg-richard @ryanto I would support the suggestion: let belongsToIsNotNew = belongsTo && belongsTo.record && !isBlank(record.get('id')) I'm having problems with my |
What is the roadmap here? Should an option ( We are struggling with this too, if I counted correctly as the fifth affected here. |
@phil294 you should override |
Currently when saving a model with an unsaved
belognsTo
a null id is used in the JSON:API document.For example
Generates this post payload:
According to the spec this is an invalid payload:
This PR addresses this by not including the belongsTo that is unsaved in the relationship key.
I took this existing PR and updated it to fit @wecc's comment: #4500. Big thanks to @pangratz