Skip to content

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

Merged
merged 4 commits into from
May 20, 2016

Conversation

doughsay
Copy link
Contributor

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.

# 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?
Copy link
Owner

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.

@fotinakis
Copy link
Owner

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?
Copy link
Contributor Author

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?

Copy link
Owner

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?

@doughsay
Copy link
Contributor Author

@fotinakis feedback addressed, but still some concerns.

@fotinakis
Copy link
Owner

Back to you @doughsay

@doughsay
Copy link
Contributor Author

I agree; should be good to go now @fotinakis!

@fotinakis fotinakis merged commit b61fbe1 into fotinakis:master May 20, 2016
@fotinakis
Copy link
Owner

Released in gem v0.12.0. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants