-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
JSON adapter with empty array does not maintain root key #1153
Comments
Thing is, when you have an empty array, there is no real way for AMS to infer the type of resources you would have had in it (at least not based on the resources themselves since, well there aren't any). Two possible workarounds:
|
AMS could attempt to safe_constantize the model part of |
From a design standpoint, is there ever really an instance where you wouldn't have an array of homogenous object types? At least, for the JSON adapter? I don't see why AMS couldn't deduce the root key based on the serializer. The workarounds are fine, but it was definitely a surprise to me how it reacted in this instance. Doesn't seem like a good expected result.Ryan LeFevre (@meltingice)
From: Lucas Hosseini notifications@github.com Thing is, when you have an empty array, there is no real way for AMS to infer the type of resources you would have had in it (at least not based on the resources themselves since, well there aren't any). Two possible workarounds: explicitly specify the root option make a custom collection serializer that inherits from ArraySerializer that sets the root for you. — |
@beauby @joaomdmoura how do you feel about inferring the root name in this scenario? Can you think of any down sides? |
I think we should do it but only if it cannot be inferred from the resources themselves (mainly because there are none), otherwise it will bring pain when serializing with a custom serializer (like |
yeah, that's what I was thinking. |
Imma get a PR for this |
Addressed in #1156 |
@meltingice are we discussing If The option for manually specifying the serializer is
Without class ArticleController < ApplicationController
def index
@articles = []
render json: @articles, adapter: :json #=> { articles: [] }, where the resource root comes from the current controller.
end
end If |
Sorry, thought I replied to this, but must have forgotten to send the email. In this case it actually is |
I've noticed it also doesn't respect the |
For root false, the attributes adapter would cover that |
@meltingice As I wrote in #1153 (comment) there's no serializer for an Array, so AMS won't do anything and you'll get
Is that related to this issue? |
Closing as stale. Please re-open if I closed this in error. |
If you attempt to serialize an empty array with the JSON adapter and ArraySerializer, the root key becomes an empty string. This issue is present in 0.10.0.rc2 and the latest master branch commit at this time (
17c353826056bc3eac3e6d6a2fd081af175bb6ad
).Example Code
Expected
{ "articles": [] }
Actual
{ "": [] }
The text was updated successfully, but these errors were encountered: