cerebris / jsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.
http://jsonapi-resources.com
MIT License
2.32k stars 532 forks source link

After creation a resource is not rendered if its id is nil #1025

Open hidde-jan opened 7 years ago

hidde-jan commented 7 years ago

In 0.8.3, I could 'create' a PORO object, and let the id of the object be nil, and everything would render well, i.e.:

class Poro
  def id
    nil
  end

  def save
    # some logic
  end
end

class PoroResource < JSONAPI::Resource; end
# POST /poros
{ "data": { "attributes": {}, "type": "poros" } }

# Response
{ "data": { "attributes": {}, "type": "poros", "id": null, "..etc..": "..etc.." } }

However, in 0.9.0, the data is just null:

# POST /poros
{ "data": { "attributes": {}, "type": "poros" } }

# Response
{ "data": null }

This is due to https://github.com/cerebris/jsonapi-resources/blob/release-0-9/lib/jsonapi/resource_serializer.rb#L69

Maybe a short line in the docs somewhere would let people know this happens.

KevinColemanInc commented 5 years ago

I don't know when this changed, but you can't do idis nil now because then content[:data] is nil here.

https://github.com/cerebris/jsonapi-resources/blob/release-0-9/lib/jsonapi/acts_as_resource_controller.rb#L237