The beginning of support for multiple content types. You can now do
this:
employees = EmployeeResource.all(params)
employees.to_json # simple plain json
employees.to_xml # plain json in xml format
employees.to_jsonapi # json in jsonapi format
All serialization, sparse fields and "sideloads" will be respected. When
rendering JSON, the top-level keys are employees (or whatever the
jsonapi "type" is) and meta. This way we can continue to support total
count and other metadata.
With some minor additions to a Rails app + responders gem, which we will
add to our generator, that means this:
def index
employees = EmployeeResource.all(params)
respond_with(employees)
end
Will render the approprate response based on the request format
(.json, .xml, .jsonapi), or content type (application/json,
application/xml, application/vnd.api+json').
Note there are no custom xml or json renderers registered in the
Railtie! Our proxy object correctly responds to to_xml and to_json,
so it works out-of-the-box, even alongside existing APIs already
rendering vanilla json.
Some exceptions:
Write payloads are still jsonapi-only. Would not be particularly hard
to support other formats, but not worth even the minor effort at this
point.
This means we could accept different query payloads as well (again,
just not worth it right now).
Errors are still always in jsonapi format. Again, I'd like to have a
real-life use case before supporting others.
Other notes:
Ensure sideloads resolve with unique, non-nil parent ids by the new
#ids_for_parents method, which will be passed to .scope or used
within #load_params.
The beginning of support for multiple content types. You can now do this:
All serialization, sparse fields and "sideloads" will be respected. When rendering JSON, the top-level keys are
employees
(or whatever the jsonapi "type" is) andmeta
. This way we can continue to support total count and other metadata.With some minor additions to a Rails app + responders gem, which we will add to our generator, that means this:
Will render the approprate response based on the request format (
.json
,.xml
,.jsonapi
), or content type (application/json
,application/xml
,application/vnd.api+json'
).Note there are no custom
xml
orjson
renderers registered in the Railtie! Our proxy object correctly responds toto_xml
andto_json
, so it works out-of-the-box, even alongside existing APIs already rendering vanilla json.Some exceptions:
Other notes:
#ids_for_parents
method, which will be passed to.scope
or used within#load_params
.