jsonapi-rb / jsonapi-renderer

Efficiently render JSON API documents.
http://jsonapi-rb.org
MIT License
27 stars 11 forks source link

Collection of POROs renders only first element #34

Closed wizardone closed 5 years ago

wizardone commented 5 years ago

Am I doing something wrong? I cannot seem to properly render a collection of objects. Looked across the documentation but could not find anything.. It looks really basic and this is what gets me confused..

results = [AddressFinder::AddressSearch::Result.new(a: "Some address", pxid: "qwe1", v: 1), AddressFinder::AddressSearch::Result.new(a: "Some other address", pxid: "qwe2", v: 1)]
render json: JSONAPI::Serializable::Renderer.new.render(
  results,
  class: { 'AddressFinder::AddressSearch::Result': AddressFinderSerializer }
)
class AddressFinderSerializer < JSONAPI::Serializable::Resource
  type 'AddressFinder'

  attribute :address do
    @object.a
  end
end

End Result:

{
  'data' => [
    {
      'id' => '',
      'type'=> 'AddressFinder',
      'attributes' => {
        'address' => 'Some address'
      }
    }
  ]
}

Expected result:

{
  'data' => [
    {
      'id' => '',
      'type'=> 'AddressFinder',
      'attributes' => {
        'address' => 'Some address'
      }
    },
    {
       'id' => '',
       'type'=> 'AddressFinder',
       'attributes' => {
         'address' => 'Some other address'
       }
     }
   ]
}
wizardone commented 5 years ago

Aaand it did not take me long to solve it. After some debugging I realized I needed to supply an id attribute to the serializable resource so they are unique. Although rather self explanatory I think it will be helpful to add this to the documentation.