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

Validate presence of a class in the class argument #110

Open
thec0keman opened this issue Dec 13, 2018 · 3 comments
Open

Validate presence of a class in the class argument #110

thec0keman opened this issue Dec 13, 2018 · 3 comments

Comments

@thec0keman
Copy link

When rendering custom class / serializer mappings:

render_jsonapi records, class: {
  :'User' => ::FrontEnd::SerializableUser,
  :'Comment' => ::FrontEnd::OnDemand::SerializableComment
}

If the left-hand key isn't correct, you will get a cryptic error

undefined method `new' for nil:NilClass

I think a solution would be to update _build in renderer to something like:

def _build(object, exposures, klass)
  raise "#{ object.class.name.to_sym } not found in the class option passed to jsonapi_render" if klass[object.class.name.to_sym].blank?

  klass[object.class.name.to_sym].new(exposures.merge(object: object))
end

This way, if you've forgotten to map something there will be a helpful error message.

@beauby
Copy link
Member

beauby commented Apr 5, 2019

I agree that the error message is not very helpful. However, checking for blank? is not enough, as it would still allow non-existing classes. Probably something along the lines of raise ... unless klass[...].is_a? JSONAPI::Serializable::Resource. @dawidof What do you think?

@dawidof
Copy link
Member

dawidof commented Apr 5, 2019

I think its a good idea. I' also spent some time in past to figure out from where this error is coming.

@tlconnor
Copy link

tlconnor commented Apr 9, 2019

Looks like this was resolved by 914ccde

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

4 participants