Skip to content

Validate presence of a class in the class argument #110

@thec0keman

Description

@thec0keman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions