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

Nested Associations and type declarations in Polymorphic serializations 0.10+ (latest) #1717

Closed
cgmckeever opened this issue May 4, 2016 · 3 comments

Comments

@cgmckeever
Copy link
Contributor

Expected behavior vs actual behavior

The polymorphic test of the master branch leads me to a bit of confusion with the expected behavior of the returned serialization. Expected, from the example, I would think that imageable would identify the type and then there would be a lower association of 'employee' with the employee data

         def test_json_serializer
          expected =
            {
              picture: {
                id: 1,
                title: 'headshot-1.jpg',
                imageable: {
                  id: 42,
                  name: 'Zoop Zoopler'
                }
              }
            }

for our current test of 0.8.2, our test expected this behavior, and passed with a and 'asset' key, a 'type' attribute of 'transformed_image' and then a 'transformed_image' key of the subset of data.

  it 'creates media specific JSON for a photo in a typical photo album' do
    album = create(:album, :with_photos)
    media = album.active_medias.first
    parsed_json = ActiveModelSerializers::SerializableResource.new(media, {adapter: :json, include: '**', key_transform: true}).as_json
    expect(parsed_json['id']).to eq media.id
    expect(parsed_json['asset']['type']).to eq 'transformed_image'
    expect(parsed_json['asset']['transformed_image']['id']).to eq media.asset.id
    exp

when running with latest, all the transformed data is rolled up under asset, and really doesnt properly define the data.

   :asset=>
    {:id=>6,
     :parsed_transform_data=>{},
     :width=>4608,
     :height=>4391,

   class MediaSerializer < ApplicationSerializer
      attributes :id,
             :label,
             :position,
             :status,
             :media_container_id

     has_one :asset, polymorphic: true
 end

Switching to json-api does provide a much closer representation


{:asset=>{:data=>{:id=>"6", :type=>"transformed-images"}}}},
 :included=>
  [{:id=>"6",
    :type=>"transformed-images",
    :attributes=>

Is this the expected data representation for polymorphs? There was an older thread/issue stating how an earlier version was improperly handing this, and that eventually got changed. I think that was for a pre 0.10 branch. Ill have to google fu a little more to find that thread. Any help on this would be much appreciated. Thanks!

@cgmckeever
Copy link
Contributor Author

here is one earlier reference to the nested types .. It wasnt the very direct one that had a good conversation around it - still looking for that

   images: [{
   id: 1,
   imageable: {
     id: 1,
     type: 'User'
    },
   url: 'http://test.com/photo.jpg'
 }]

emberjs/data#1574

@cgmckeever
Copy link
Contributor Author

It appears that @marcgarreau has gotten the polymorphic relation to properly give the data (as Im desiring). Hoping this bat signal can bring him out for a little lend-of-hand to help me get my payload like that 👍

{
   picture: {
      id: 1,
      imageable: {
         type: "product",
         product: {
            id: 3,
            username: "Product 3"
         }
      }
   }
}

#1420 (comment)

@cgmckeever
Copy link
Contributor Author

Conversed via @remear via Slack@help on this. It appears that this is the expected/intended behavior, but may not be the long term desire. He is looping back with @beauby on the direction/decisions of polymorphs. Ill dig in and try to make a PR for some type/nested handling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant