-
Notifications
You must be signed in to change notification settings - Fork 90
Exclude id in serialized payload when nil. #76
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
Conversation
# and represents a new resource to be created on the server." | ||
# http://jsonapi.org/format/#document-resource-objects | ||
# We'll assume that if the id is blank, it means the resource is to be created. | ||
data['id'] = serializer.id.to_s if !serializer.id.to_s.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simpler: data['id'] = serializer.id.to_s if serializer.id
Instead of casting the nil
to '' and checking empty.
Thanks @doughsay, back to you with some comments. |
# and represents a new resource to be created on the server." | ||
# http://jsonapi.org/format/#document-resource-objects | ||
# We'll assume that if the id is blank, it means the resource is to be created. | ||
data['id'] = serializer.id.to_s if !serializer.id.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still have to do !...empty?
here, because the default serializer id
method coerces to string. The second .to_s
here was originally redundant, so I've just removed that.
There's still a problem here though, if the user overrides the the id
method to sometimes return nil instead of always returning a string, we'll get a NoMethodError... maybe leaving the .to_s
there is better for safety? Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about just make it explicit then: if serializer.id && !serializer.id.empty?
@fotinakis feedback addressed, but still some concerns. |
Back to you @doughsay |
I agree; should be good to go now @fotinakis! |
Released in gem v0.12.0. Thanks! |
As discussed in #75, it would be nice if id was not included in the payload for the use-case of POSTing a new resource to a server.