Skip to content
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

0.10.stable / master is ignoring (key/root) option in has_many method #882

Closed
bernEsp opened this issue Apr 23, 2015 · 12 comments
Closed

0.10.stable / master is ignoring (key/root) option in has_many method #882

bernEsp opened this issue Apr 23, 2015 · 12 comments

Comments

@bernEsp
Copy link

bernEsp commented Apr 23, 2015

Example

has_many :charts, root: :rows,  serializer: ChartSerializer 

This produce a json with charts collection and ignore the custom name as follow

  { charts: [ ... ] }

When I'm expecting

   { rows: [ ... ] }

also fails using key option

  has_many :charts, key: :rows,  serializer: ChartSerializer
@bf4
Copy link
Member

bf4 commented Apr 24, 2015

I think this is a feature request

@bernEsp
Copy link
Author

bernEsp commented Apr 25, 2015

@bf4 I think is backward compatibility. This functionality is a good option to avoid create a method just for customize the name of relationship

0.9 version
https://github.com/rails-api/active_model_serializers/tree/0-9-2#associations
0.8 version
https://github.com/rails-api/active_model_serializers/tree/0-8-stable#associations

@bf4
Copy link
Member

bf4 commented Apr 26, 2015

Oh, my bad for not looking back. So, it's either a bug or a breaking change.

@maurogeorge
Copy link
Member

Hi @joaomdmoura I try to write a broken test and got no success 😢
I think the #984 closes this issue. Could you please review this and close this issue if make sense?

Thanks

@bf4
Copy link
Member

bf4 commented Aug 28, 2015

@bernardogalindo Does #984 resolve you issue so we can close this?

@bernEsp
Copy link
Author

bernEsp commented Aug 30, 2015

@bf4 I still having the same issues that I mentioned with the current master branch.

Version 0.9.3 allow me to do root=false to get

"{\"name\":\"Test\",\"parent_id\":null,\"subaccount_ids\":null}"

instead of

+"{\"object\":{\"name\":\"Test\",\"parent_id\":null,\"subaccount_ids\":null}

And allow me customize my the object keys as I mentioned

  { charts: [ ... ] }

When I'm expecting

   { rows: [ ... ] }
The current master branch still ignoring the root and key options

@bernEsp
Copy link
Author

bernEsp commented Aug 30, 2015

Here is one of my test result

16) Failure:
TestLowChartsDecoratorSerializer#test_json_serializer_should_return_low_charts_format [/Users/bgalindo/projects/advance_account_managment/test/serializers/low_charts_decorator_serializer_test.rb:27]:
--- expected
+++ actual
@@ -1 +1 @@
-{"p"=>{"date_format"=>"%b %e", "omit_legend"=>true}, "rows"=>[{"c"=>[{"v"=>"2015-08-29", "f"=>"2015-08-29"}, {"v"=>1, "ts"=>"2015-08-29", "te"=>"2015-08-30", "f"=>"", "partial"=>false}]}], "cols"=>[{"label"=>"test", "type"=>"number"}]}

+{"object"=>{"columns"=>[{"name"=>"test", "type"=>"number"}], "charts"=>[{"points"=>[{"value"=>1, "format"=>"", "partial"=>false, "start_time"=>"2015-08-29", "end_time"=>"2015-08-30"}], "legend"=>{"value"=>"2015-08-29"}, "start_time"=>"2015-08-29", "end_time"=>"2015-08-30"}], "options"=>{"date_format"=>"%b %e", "omit_legend"=>true}}, "options"=>{}, "root"=>nil, "meta"=>nil, "meta_key"=>nil, "scope"=>nil}

I'm expecting a false object root and rows as key instead of charts, also that collections use the correct serializer with settings.

File code

  class LowChartsDecoratorSerializer < ActiveModel::Serializer
    self.root = false 
    attributes :p
    has_many :charts, root: :rows,  serializer: ChartSerializer 
    has_many :columns, root: :cols, serializer: ColumnSerializer 

    def p
       object.options  
    end
  end

@bf4
Copy link
Member

bf4 commented Aug 31, 2015

Which commit ref are you on on master? (It would be in your Gemfile.lock)

I think you want self.root=nil depending on what commit you're on.

If you have the FlattenJson adapter available, that might do what you want.

(Also, I'm not sure if this is from your real code, but calling anything p is bug-prone since it's a method on Kernel, so unless you're really careful about the context, you mind end up calling Kernel.p whatever)

@bernEsp
Copy link
Author

bernEsp commented Aug 31, 2015

@bf4 the master branch returns

undefined method root= for Class

p method is just under serializer scope.

@bernEsp
Copy link
Author

bernEsp commented Aug 31, 2015

@bf4

This is the structure that my serializer generates with version 0.9.3

"{\"average\":110,\"difference\":0,\"data\":{\"p\":{\"u\":\"h\"},\"rows\":[{\"c\":[{\"v\":\"Jan\",\"f\":\"Jan\"}] }],\"cols\":[{\"label\":\"ordinal\",\"type\":\"ordinal\"},{\"label\":\"2015\",\"type\":\"ordinal\"}]}}"

The current master seems to be respecting (data)

  • on level of has many root custom names

The current master branch does not respect
*nested has many root custom names as (p(options), rows(charts), cols(columns), c(points))
*it is adding to the response root:null, meta: null, meta_key: null, scope: null

{\"object\":{\"data\":{\"columns\":[{\"name\":\"ordinal\",\"type\":\"ordinal\"}],\"charts\":[{\"points\":[{\"value\":0,\"format\":\"N/A\",\"partial\":false}] }],\"options\":{\"unit\":\"unique users\"} },\"average\":110,\"difference\":0},\"options\":{},\"root\":null,\"meta\":null,\"meta_key\":null,\"scope\":null}

Answers were generated as follow

get :index, ...

decorator = ...
assert_equal response.body, TestSerializer.new(decorator).to_json

@joaomdmoura
Copy link
Member

Hey @bernardogalindo so, there is a bunch of different things in here.

If you are using master, you will have access to different adapters one of them is FlattenJson that basically removes the root for you. You can check more about it on its docs. This is now the way to control the root existence, you can also find out more about it here.

About changing the key on a relationship declaration, AFAIK you should use key option on the relationship, you can check some tests we have around this here using this serializer declaration

@beauby
Copy link
Contributor

beauby commented Sep 24, 2015

Closing this for now as the issue seems to be solved by @joaomdmoura's suggestions. Feel free to reopen if needed.

@beauby beauby closed this as completed Sep 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants