test/unit/serializer/serializer_test.rb in jsonapi-resources-0.3.3 vs test/unit/serializer/serializer_test.rb in jsonapi-resources-0.4.0

- old
+ new

@@ -17,46 +17,62 @@ JSONAPI.configuration.json_key_format = :underscored_key end def test_serializer + serialized = JSONAPI::ResourceSerializer.new( + PostResource, + base_url: 'http://example.com').serialize_to_hash(PostResource.new(@post) + ) + assert_hash_equals( { data: { type: 'posts', id: '1', - title: 'New post', - body: 'A body!!!', - subject: 'New post', links: { self: 'http://example.com/posts/1', + }, + attributes: { + title: 'New post', + body: 'A body!!!', + subject: 'New post' + }, + relationships: { section: { - self: 'http://example.com/posts/1/links/section', - related: 'http://example.com/posts/1/section', - linkage: nil + links: { + self: 'http://example.com/posts/1/relationships/section', + related: 'http://example.com/posts/1/section' + }, + data: nil }, author: { - self: 'http://example.com/posts/1/links/author', - related: 'http://example.com/posts/1/author', - linkage: { + links: { + self: 'http://example.com/posts/1/relationships/author', + related: 'http://example.com/posts/1/author' + }, + data: { type: 'people', id: '1' } }, tags: { - self: 'http://example.com/posts/1/links/tags', - related: 'http://example.com/posts/1/tags' + links: { + self: 'http://example.com/posts/1/relationships/tags', + related: 'http://example.com/posts/1/tags' + } }, comments: { - self: 'http://example.com/posts/1/links/comments', - related: 'http://example.com/posts/1/comments' + links: { + self: 'http://example.com/posts/1/relationships/comments', + related: 'http://example.com/posts/1/comments' + } } } } }, - JSONAPI::ResourceSerializer.new(PostResource, - base_url: 'http://example.com').serialize_to_hash(PostResource.new(@post)) + serialized ) end def test_serializer_nil_handling assert_hash_equals( @@ -71,31 +87,41 @@ assert_hash_equals( { data: { type: 'posts', id: '1', - title: 'New post', - body: 'A body!!!', - subject: 'New post', links: { - self: 'http://example.com/api/v1/posts/1', + self: 'http://example.com/api/v1/posts/1' + }, + attributes: { + title: 'New post', + body: 'A body!!!', + subject: 'New post' + }, + relationships: { section: { - self: 'http://example.com/api/v1/posts/1/links/section', - related: 'http://example.com/api/v1/posts/1/section', - linkage: nil + links:{ + self: 'http://example.com/api/v1/posts/1/relationships/section', + related: 'http://example.com/api/v1/posts/1/section' + }, + data: nil }, writer: { - self: 'http://example.com/api/v1/posts/1/links/writer', - related: 'http://example.com/api/v1/posts/1/writer', - linkage: { + links:{ + self: 'http://example.com/api/v1/posts/1/relationships/writer', + related: 'http://example.com/api/v1/posts/1/writer' + }, + data: { type: 'writers', id: '1' } }, comments: { - self: 'http://example.com/api/v1/posts/1/links/comments', - related: 'http://example.com/api/v1/posts/1/comments' + links:{ + self: 'http://example.com/api/v1/posts/1/relationships/comments', + related: 'http://example.com/api/v1/posts/1/comments' + } } } } }, JSONAPI::ResourceSerializer.new(Api::V1::PostResource, @@ -109,17 +135,23 @@ assert_hash_equals( { data: { type: 'posts', id: '1', - title: 'New post', links: { - self: '/posts/1', + self: '/posts/1' + }, + attributes: { + title: 'New post' + }, + relationships: { author: { - self: '/posts/1/links/author', - related: '/posts/1/author', - linkage: { + links: { + self: '/posts/1/relationships/author', + related: '/posts/1/author' + }, + data: { type: 'people', id: '1' } } } @@ -131,75 +163,99 @@ end def test_serializer_include serialized = JSONAPI::ResourceSerializer.new( PostResource, - include: [:author] + include: ['author'] ).serialize_to_hash(PostResource.new(@post)) assert_hash_equals( { data: { type: 'posts', id: '1', - title: 'New post', - body: 'A body!!!', - subject: 'New post', links: { - self: '/posts/1', + self: '/posts/1' + }, + attributes: { + title: 'New post', + body: 'A body!!!', + subject: 'New post' + }, + relationships: { section: { - self: '/posts/1/links/section', - related: '/posts/1/section', - linkage: nil + links: { + self: '/posts/1/relationships/section', + related: '/posts/1/section' + }, + data: nil }, author: { - self: '/posts/1/links/author', - related: '/posts/1/author', - linkage: { + links: { + self: '/posts/1/relationships/author', + related: '/posts/1/author' + }, + data: { type: 'people', id: '1' } }, tags: { - self: '/posts/1/links/tags', - related: '/posts/1/tags' + links: { + self: '/posts/1/relationships/tags', + related: '/posts/1/tags' + } }, comments: { - self: '/posts/1/links/comments', - related: '/posts/1/comments' + links: { + self: '/posts/1/relationships/comments', + related: '/posts/1/comments' + } } } }, included: [ { type: 'people', id: '1', - name: 'Joe Author', - email: 'joe@xyz.fake', - dateJoined: '2013-08-07 16:25:00 -0400', + attributes: { + name: 'Joe Author', + email: 'joe@xyz.fake', + dateJoined: '2013-08-07 16:25:00 -0400' + }, links: { - self: '/people/1', + self: '/people/1' + }, + relationships: { comments: { - self: '/people/1/links/comments', - related: '/people/1/comments' + links: { + self: '/people/1/relationships/comments', + related: '/people/1/comments' + } }, posts: { - self: '/people/1/links/posts', - related: '/people/1/posts' + links: { + self: '/people/1/relationships/posts', + related: '/people/1/posts' + } }, preferences: { - self: '/people/1/links/preferences', - related: '/people/1/preferences', - linkage: { + links: { + self: '/people/1/relationships/preferences', + related: '/people/1/preferences' + }, + data: { type: 'preferences', id: '1' } }, hairCut: { - self: "/people/1/links/hairCut", - related: "/people/1/hairCut", - linkage: nil + links: { + self: "/people/1/relationships/hairCut", + related: "/people/1/hairCut" + }, + data: nil } } } ] }, @@ -208,76 +264,100 @@ end def test_serializer_key_format serialized = JSONAPI::ResourceSerializer.new( PostResource, - include: [:author], + include: ['author'], key_formatter: UnderscoredKeyFormatter ).serialize_to_hash(PostResource.new(@post)) assert_hash_equals( { data: { type: 'posts', id: '1', - title: 'New post', - body: 'A body!!!', - subject: 'New post', + attributes: { + title: 'New post', + body: 'A body!!!', + subject: 'New post' + }, links: { - self: '/posts/1', + self: '/posts/1' + }, + relationships: { section: { - self: '/posts/1/links/section', - related: '/posts/1/section', - linkage: nil + links: { + self: '/posts/1/relationships/section', + related: '/posts/1/section' + }, + data: nil }, author: { - self: '/posts/1/links/author', - related: '/posts/1/author', - linkage: { + links: { + self: '/posts/1/relationships/author', + related: '/posts/1/author' + }, + data: { type: 'people', id: '1' } }, tags: { - self: '/posts/1/links/tags', - related: '/posts/1/tags' + links: { + self: '/posts/1/relationships/tags', + related: '/posts/1/tags' + } }, comments: { - self: '/posts/1/links/comments', - related: '/posts/1/comments' + links: { + self: '/posts/1/relationships/comments', + related: '/posts/1/comments' + } } } }, included: [ { type: 'people', id: '1', - name: 'Joe Author', - email: 'joe@xyz.fake', - date_joined: '2013-08-07 16:25:00 -0400', + attributes: { + name: 'Joe Author', + email: 'joe@xyz.fake', + date_joined: '2013-08-07 16:25:00 -0400' + }, links: { - self: '/people/1', + self: '/people/1' + }, + relationships: { comments: { - self: '/people/1/links/comments', - related: '/people/1/comments' + links: { + self: '/people/1/relationships/comments', + related: '/people/1/comments' + } }, posts: { - self: '/people/1/links/posts', - related: '/people/1/posts' + links: { + self: '/people/1/relationships/posts', + related: '/people/1/posts' + } }, preferences: { - self: '/people/1/links/preferences', - related: '/people/1/preferences', - linkage: { + links: { + self: '/people/1/relationships/preferences', + related: '/people/1/preferences' + }, + data: { type: 'preferences', id: '1' } }, hair_cut: { - self: '/people/1/links/hairCut', - related: '/people/1/hairCut', - linkage: nil + links: { + self: '/people/1/relationships/hairCut', + related: '/people/1/hairCut' + }, + data: nil } } } ] }, @@ -290,299 +370,200 @@ assert_hash_equals( { data: { type: 'posts', id: '1', - title: 'New post', - body: 'A body!!!', - subject: 'New post', + attributes: { + title: 'New post', + body: 'A body!!!', + subject: 'New post' + }, links: { - self: '/posts/1', + self: '/posts/1' + }, + relationships: { section: { - self: '/posts/1/links/section', - related: '/posts/1/section', - linkage: nil + links: { + self: '/posts/1/relationships/section', + related: '/posts/1/section' + }, + data: nil }, author: { - self: '/posts/1/links/author', - related: '/posts/1/author', - linkage: { + links: { + self: '/posts/1/relationships/author', + related: '/posts/1/author' + }, + data: { type: 'people', id: '1' } }, tags: { - self: '/posts/1/links/tags', - related: '/posts/1/tags' + links: { + self: '/posts/1/relationships/tags', + related: '/posts/1/tags' + } }, comments: { - self: '/posts/1/links/comments', - related: '/posts/1/comments', - linkage: [ + links: { + self: '/posts/1/relationships/comments', + related: '/posts/1/comments' + }, + data: [ {type: 'comments', id: '1'}, {type: 'comments', id: '2'} ] } } }, included: [ { type: 'tags', id: '1', - name: 'short', + attributes: { + name: 'short' + }, links: { - self: '/tags/1', + self: '/tags/1' + }, + relationships: { posts: { - self: '/tags/1/links/posts', - related: '/tags/1/posts' + links: { + self: '/tags/1/relationships/posts', + related: '/tags/1/posts' + } } } }, { type: 'tags', id: '2', - name: 'whiny', + attributes: { + name: 'whiny' + }, links: { - self: '/tags/2', + self: '/tags/2' + }, + relationships: { posts: { - self: '/tags/2/links/posts', - related: '/tags/2/posts' + links: { + self: '/tags/2/relationships/posts', + related: '/tags/2/posts' + } } } }, { type: 'tags', id: '4', - name: 'happy', + attributes: { + name: 'happy' + }, links: { - self: '/tags/4', + self: '/tags/4' + }, + relationships: { posts: { - self: '/tags/4/links/posts', - related: '/tags/4/posts', + links: { + self: '/tags/4/relationships/posts', + related: '/tags/4/posts' + }, } } }, { type: 'comments', id: '1', - body: 'what a dumb post', + attributes: { + body: 'what a dumb post' + }, links: { - self: '/comments/1', + self: '/comments/1' + }, + relationships: { author: { - self: '/comments/1/links/author', - related: '/comments/1/author', - linkage: { + links: { + self: '/comments/1/relationships/author', + related: '/comments/1/author' + }, + data: { type: 'people', id: '1' } }, post: { - self: '/comments/1/links/post', - related: '/comments/1/post', - linkage: { + links: { + self: '/comments/1/relationships/post', + related: '/comments/1/post' + }, + data: { type: 'posts', id: '1' } }, tags: { - self: '/comments/1/links/tags', - related: '/comments/1/tags', - linkage: [ + links: { + self: '/comments/1/relationships/tags', + related: '/comments/1/tags' + }, + data: [ {type: 'tags', id: '1'}, {type: 'tags', id: '2'} ] } } }, { type: 'comments', id: '2', - body: 'i liked it', + attributes: { + body: 'i liked it' + }, links: { - self: '/comments/2', + self: '/comments/2' + }, + relationships: { author: { - self: '/comments/2/links/author', - related: '/comments/2/author', - linkage: { + links: { + self: '/comments/2/relationships/author', + related: '/comments/2/author' + }, + data: { type: 'people', id: '2' } }, post: { - self: '/comments/2/links/post', - related: '/comments/2/post', - linkage: { + links: { + self: '/comments/2/relationships/post', + related: '/comments/2/post' + }, + data: { type: 'posts', id: '1' } }, tags: { - self: '/comments/2/links/tags', - related: '/comments/2/tags', - linkage: [ + links: { + self: '/comments/2/relationships/tags', + related: '/comments/2/tags' + }, + data: [ {type: 'tags', id: '1'}, {type: 'tags', id: '4'} ] } } } ] }, JSONAPI::ResourceSerializer.new(PostResource, - include: [:comments, 'comments.tags']).serialize_to_hash(PostResource.new(@post)) + include: ['comments', 'comments.tags']).serialize_to_hash(PostResource.new(@post)) ) end - def test_serializer_include_has_many_sub_objects_only - - assert_hash_equals( - { - data: { - type: 'posts', - id: '1', - title: 'New post', - body: 'A body!!!', - subject: 'New post', - links: { - self: '/posts/1', - section: { - self: '/posts/1/links/section', - related: '/posts/1/section', - linkage: nil - }, - author: { - self: '/posts/1/links/author', - related: '/posts/1/author', - linkage: { - type: 'people', - id: '1' - } - }, - tags: { - self: '/posts/1/links/tags', - related: '/posts/1/tags' - }, - comments: { - self: '/posts/1/links/comments', - related: '/posts/1/comments' - } - } - }, - included: [ - { - type: 'tags', - id: '1', - name: 'short', - links: { - self: '/tags/1', - posts: { - self: '/tags/1/links/posts', - related: '/tags/1/posts' - } - } - }, - { - type: 'tags', - id: '2', - name: 'whiny', - links: { - self: '/tags/2', - posts: { - self: '/tags/2/links/posts', - related: '/tags/2/posts' - } - } - }, - { - type: 'tags', - id: '4', - name: 'happy', - links: { - self: '/tags/4', - posts: { - self: '/tags/4/links/posts', - related: '/tags/4/posts', - } - } - } - ] - }, - JSONAPI::ResourceSerializer.new(PostResource, include: ['comments.tags']).serialize_to_hash(PostResource.new(@post)) - ) - end - - def test_serializer_include_has_one_sub_objects_only - - assert_hash_equals( - { - data: { - type: 'posts', - id: '1', - title: 'New post', - body: 'A body!!!', - subject: 'New post', - links: { - self: '/posts/1', - section: { - self: '/posts/1/links/section', - related: '/posts/1/section', - linkage: nil - }, - author: { - self: '/posts/1/links/author', - related: '/posts/1/author', - linkage: { - type: 'people', - id: '1' - } - }, - tags: { - self: '/posts/1/links/tags', - related: '/posts/1/tags' - }, - comments: { - self: '/posts/1/links/comments', - related: '/posts/1/comments' - } - } - }, - included: [ - { - type: 'comments', - id: '1', - body: 'what a dumb post', - links: { - self: '/comments/1', - author: { - self: '/comments/1/links/author', - related: '/comments/1/author', - linkage: { - type: 'people', - id: '1' - } - }, - post: { - self: '/comments/1/links/post', - related: '/comments/1/post', - linkage: { - type: 'posts', - id: '1' - } - }, - tags: { - self: '/comments/1/links/tags', - related: '/comments/1/tags' - } - } - } - ] - }, - JSONAPI::ResourceSerializer.new(PostResource, - include: ['author.comments']).serialize_to_hash(PostResource.new(@post)) - ) - end - def test_serializer_different_foreign_key serialized = JSONAPI::ResourceSerializer.new( PersonResource, include: ['comments'] ).serialize_to_hash(PersonResource.new(@fred)) @@ -590,93 +571,125 @@ assert_hash_equals( { data: { type: 'people', id: '2', - name: 'Fred Reader', - email: 'fred@xyz.fake', - dateJoined: '2013-10-31 16:25:00 -0400', + attributes: { + name: 'Fred Reader', + email: 'fred@xyz.fake', + dateJoined: '2013-10-31 16:25:00 -0400' + }, links: { - self: '/people/2', + self: '/people/2' + }, + relationships: { posts: { - self: '/people/2/links/posts', - related: '/people/2/posts' + links: { + self: '/people/2/relationships/posts', + related: '/people/2/posts' + } }, comments: { - self: '/people/2/links/comments', - related: '/people/2/comments', - linkage: [ + links: { + self: '/people/2/relationships/comments', + related: '/people/2/comments' + }, + data: [ {type: 'comments', id: '2'}, {type: 'comments', id: '3'} ] }, preferences: { - self: "/people/2/links/preferences", - related: "/people/2/preferences", - linkage: nil + links: { + self: "/people/2/relationships/preferences", + related: "/people/2/preferences" + }, + data: nil }, hairCut: { - self: "/people/2/links/hairCut", - related: "/people/2/hairCut", - linkage: nil + links: { + self: "/people/2/relationships/hairCut", + related: "/people/2/hairCut" + }, + data: nil } } }, included: [ { type: 'comments', id: '2', - body: 'i liked it', + attributes: { + body: 'i liked it' + }, links: { - self: '/comments/2', + self: '/comments/2' + }, + relationships: { author: { - self: '/comments/2/links/author', - related: '/comments/2/author', - linkage: { + links: { + self: '/comments/2/relationships/author', + related: '/comments/2/author' + }, + data: { type: 'people', id: '2' } }, post: { - self: '/comments/2/links/post', - related: '/comments/2/post', - linkage: { + links: { + self: '/comments/2/relationships/post', + related: '/comments/2/post' + }, + data: { type: 'posts', id: '1' } }, tags: { - self: '/comments/2/links/tags', - related: '/comments/2/tags' + links: { + self: '/comments/2/relationships/tags', + related: '/comments/2/tags' + } } } }, { type: 'comments', id: '3', - body: 'Thanks man. Great post. But what is JR?', + attributes: { + body: 'Thanks man. Great post. But what is JR?' + }, links: { - self: '/comments/3', + self: '/comments/3' + }, + relationships: { author: { - self: '/comments/3/links/author', - related: '/comments/3/author', - linkage: { + links: { + self: '/comments/3/relationships/author', + related: '/comments/3/author' + }, + data: { type: 'people', id: '2' } }, post: { - self: '/comments/3/links/post', - related: '/comments/3/post', - linkage: { + links: { + self: '/comments/3/relationships/post', + related: '/comments/3/post' + }, + data: { type: 'posts', id: '2' } }, tags: { - self: '/comments/3/links/tags', - related: '/comments/3/tags' + links: { + self: '/comments/3/relationships/tags', + related: '/comments/3/tags' + } } } } ] }, @@ -695,219 +708,297 @@ { data: [ { type: 'posts', id: '1', - title: 'New post', - body: 'A body!!!', - subject: 'New post', + attributes: { + title: 'New post', + body: 'A body!!!', + subject: 'New post' + }, links: { - self: '/posts/1', + self: '/posts/1' + }, + relationships: { section: { - self: '/posts/1/links/section', - related: '/posts/1/section', - linkage: nil + links: { + self: '/posts/1/relationships/section', + related: '/posts/1/section' + }, + data: nil }, author: { - self: '/posts/1/links/author', - related: '/posts/1/author', - linkage: { + links: { + self: '/posts/1/relationships/author', + related: '/posts/1/author' + }, + data: { type: 'people', id: '1' } }, tags: { - self: '/posts/1/links/tags', - related: '/posts/1/tags' + links: { + self: '/posts/1/relationships/tags', + related: '/posts/1/tags' + } }, comments: { - self: '/posts/1/links/comments', - related: '/posts/1/comments', - linkage: [ + links: { + self: '/posts/1/relationships/comments', + related: '/posts/1/comments' + }, + data: [ {type: 'comments', id: '1'}, {type: 'comments', id: '2'} ] } } }, { type: 'posts', id: '2', - title: 'JR Solves your serialization woes!', - body: 'Use JR', - subject: 'JR Solves your serialization woes!', + attributes: { + title: 'JR Solves your serialization woes!', + body: 'Use JR', + subject: 'JR Solves your serialization woes!' + }, links: { - self: '/posts/2', + self: '/posts/2' + }, + relationships: { section: { - self: '/posts/2/links/section', - related: '/posts/2/section', - linkage: { + links: { + self: '/posts/2/relationships/section', + related: '/posts/2/section' + }, + data: { type: 'sections', id: '2' } }, author: { - self: '/posts/2/links/author', - related: '/posts/2/author', - linkage: { + links: { + self: '/posts/2/relationships/author', + related: '/posts/2/author' + }, + data: { type: 'people', id: '1' } }, tags: { - self: '/posts/2/links/tags', - related: '/posts/2/tags' + links: { + self: '/posts/2/relationships/tags', + related: '/posts/2/tags' + } }, comments: { - self: '/posts/2/links/comments', - related: '/posts/2/comments', - linkage: [ + links: { + self: '/posts/2/relationships/comments', + related: '/posts/2/comments' + }, + data: [ {type: 'comments', id: '3'} ] } } } ], included: [ { type: 'tags', id: '1', - name: 'short', + attributes: { + name: 'short' + }, links: { - self: '/tags/1', + self: '/tags/1' + }, + relationships: { posts: { - self: '/tags/1/links/posts', - related: '/tags/1/posts' + links: { + self: '/tags/1/relationships/posts', + related: '/tags/1/posts' + } } } }, { type: 'tags', id: '2', - name: 'whiny', + attributes: { + name: 'whiny' + }, links: { - self: '/tags/2', + self: '/tags/2' + }, + relationships: { posts: { - self: '/tags/2/links/posts', - related: '/tags/2/posts' + links: { + self: '/tags/2/relationships/posts', + related: '/tags/2/posts' + } } } }, { type: 'tags', id: '4', - name: 'happy', + attributes: { + name: 'happy' + }, links: { - self: '/tags/4', + self: '/tags/4' + }, + relationships: { posts: { - self: '/tags/4/links/posts', - related: '/tags/4/posts', + links: { + self: '/tags/4/relationships/posts', + related: '/tags/4/posts' + } } } }, { type: 'tags', id: '5', - name: 'JR', + attributes: { + name: 'JR' + }, links: { - self: '/tags/5', + self: '/tags/5' + }, + relationships: { posts: { - self: '/tags/5/links/posts', - related: '/tags/5/posts', + links: { + self: '/tags/5/relationships/posts', + related: '/tags/5/posts' + } } } }, { type: 'comments', id: '1', - body: 'what a dumb post', + attributes: { + body: 'what a dumb post' + }, links: { - self: '/comments/1', + self: '/comments/1' + }, + relationships: { author: { - self: '/comments/1/links/author', - related: '/comments/1/author', - linkage: { + links: { + self: '/comments/1/relationships/author', + related: '/comments/1/author' + }, + data: { type: 'people', id: '1' } }, post: { - self: '/comments/1/links/post', - related: '/comments/1/post', - linkage: { + links: { + self: '/comments/1/relationships/post', + related: '/comments/1/post' + }, + data: { type: 'posts', id: '1' } }, tags: { - self: '/comments/1/links/tags', - related: '/comments/1/tags', - linkage: [ + links: { + self: '/comments/1/relationships/tags', + related: '/comments/1/tags' + }, + data: [ {type: 'tags', id: '1'}, {type: 'tags', id: '2'} ] } } }, { type: 'comments', id: '2', - body: 'i liked it', + attributes: { + body: 'i liked it' + }, links: { - self: '/comments/2', + self: '/comments/2' + }, + relationships: { author: { - self: '/comments/2/links/author', - related: '/comments/2/author', - linkage: { + links: { + self: '/comments/2/relationships/author', + related: '/comments/2/author' + }, + data: { type: 'people', id: '2' } }, post: { - self: '/comments/2/links/post', - related: '/comments/2/post', - linkage: { + links: { + self: '/comments/2/relationships/post', + related: '/comments/2/post' + }, + data: { type: 'posts', id: '1' } }, tags: { - self: '/comments/2/links/tags', - related: '/comments/2/tags', - linkage: [ + links: { + self: '/comments/2/relationships/tags', + related: '/comments/2/tags' + }, + data: [ {type: 'tags', id: '4'}, {type: 'tags', id: '1'} ] } } }, { type: 'comments', id: '3', - body: 'Thanks man. Great post. But what is JR?', + attributes: { + body: 'Thanks man. Great post. But what is JR?' + }, links: { - self: '/comments/3', + self: '/comments/3' + }, + relationships: { author: { - self: '/comments/3/links/author', - related: '/comments/3/author', - linkage: { + links: { + self: '/comments/3/relationships/author', + related: '/comments/3/author' + }, + data: { type: 'people', id: '2' } }, post: { - self: '/comments/3/links/post', - related: '/comments/3/post', - linkage: { + links: { + self: '/comments/3/relationships/post', + related: '/comments/3/post' + }, + data: { type: 'posts', id: '2' } }, tags: { - self: '/comments/3/links/tags', - related: '/comments/3/tags', - linkage: [ + links: { + self: '/comments/3/relationships/tags', + related: '/comments/3/tags' + }, + data: [ {type: 'tags', id: '5'} ] } } } @@ -929,143 +1020,157 @@ { data: [ { type: 'posts', id: '1', - title: 'New post', + attributes: { + title: 'New post' + }, links: { - self: '/posts/1', - author: { - self: '/posts/1/links/author', - related: '/posts/1/author', - linkage: { - type: 'people', - id: '1' - } - } + self: '/posts/1' } }, { type: 'posts', id: '2', - title: 'JR Solves your serialization woes!', + attributes: { + title: 'JR Solves your serialization woes!' + }, links: { - self: '/posts/2', - author: { - self: '/posts/2/links/author', - related: '/posts/2/author', - linkage: { - type: 'people', - id: '1' - } - } + self: '/posts/2' } } ], included: [ { type: 'posts', id: '11', - title: 'JR How To', + attributes: { + title: 'JR How To' + }, links: { - self: '/posts/11', - author: { - self: '/posts/11/links/author', - related: '/posts/11/author', - linkage: { - type: 'people', - id: '1' - } - } + self: '/posts/11' } }, { type: 'people', id: '1', - email: 'joe@xyz.fake', + attributes: { + email: 'joe@xyz.fake' + }, links: { - self: '/people/1', + self: '/people/1' + }, + relationships: { comments: { - self: '/people/1/links/comments', - related: '/people/1/comments' + links: { + self: '/people/1/relationships/comments', + related: '/people/1/comments' + } } } }, { id: '1', type: 'tags', - name: 'short', + attributes: { + name: 'short' + }, links: { self: '/tags/1' } }, { id: '2', type: 'tags', - name: 'whiny', + attributes: { + name: 'whiny' + }, links: { self: '/tags/2' } }, { id: '4', type: 'tags', - name: 'happy', + attributes: { + name: 'happy' + }, links: { self: '/tags/4' } }, { id: '5', type: 'tags', - name: 'JR', + attributes: { + name: 'JR' + }, links: { self: '/tags/5' } }, { type: 'comments', id: '1', - body: 'what a dumb post', + attributes: { + body: 'what a dumb post' + }, links: { - self: '/comments/1', + self: '/comments/1' + }, + relationships: { post: { - self: '/comments/1/links/post', - related: '/comments/1/post', - linkage: { + links: { + self: '/comments/1/relationships/post', + related: '/comments/1/post' + }, + data: { type: 'posts', id: '1' } } } }, { type: 'comments', id: '2', - body: 'i liked it', + attributes: { + body: 'i liked it' + }, links: { - self: '/comments/2', + self: '/comments/2' + }, + relationships: { post: { - self: '/comments/2/links/post', - related: '/comments/2/post', - linkage: { + links: { + self: '/comments/2/relationships/post', + related: '/comments/2/post' + }, + data: { type: 'posts', id: '1' } } } }, { type: 'comments', id: '3', - body: 'Thanks man. Great post. But what is JR?', + attributes: { + body: 'Thanks man. Great post. But what is JR?' + }, links: { - self: '/comments/3', + self: '/comments/3' + }, + relationships: { post: { - self: '/comments/3/links/post', - related: '/comments/3/post', - linkage: { + links: { + self: '/comments/3/relationships/post', + related: '/comments/3/post' + }, + data: { type: 'posts', id: '2' } } } @@ -1074,11 +1179,11 @@ }, JSONAPI::ResourceSerializer.new(PostResource, include: ['comments', 'author', 'comments.tags', 'author.posts'], fields: { people: [:id, :email, :comments], - posts: [:id, :title, :author], + posts: [:id, :title], tags: [:name], comments: [:id, :body, :post] }).serialize_to_hash(posts) ) end @@ -1090,57 +1195,69 @@ assert_hash_equals( { data: { type: 'expenseEntries', id: '1', - transactionDate: '04/15/2014', - cost: 12.05, + attributes: { + transactionDate: '04/15/2014', + cost: 12.05 + }, links: { - self: '/expenseEntries/1', + self: '/expenseEntries/1' + }, + relationships: { isoCurrency: { - self: '/expenseEntries/1/links/isoCurrency', - related: '/expenseEntries/1/isoCurrency', - linkage: { + links: { + self: '/expenseEntries/1/relationships/isoCurrency', + related: '/expenseEntries/1/isoCurrency' + }, + data: { type: 'isoCurrencies', id: 'USD' } }, employee: { - self: '/expenseEntries/1/links/employee', - related: '/expenseEntries/1/employee', - linkage: { + links: { + self: '/expenseEntries/1/relationships/employee', + related: '/expenseEntries/1/employee' + }, + data: { type: 'people', id: '3' } } } }, included: [ { type: 'isoCurrencies', id: 'USD', - countryName: 'United States', - name: 'United States Dollar', - minorUnit: 'cent', + attributes: { + countryName: 'United States', + name: 'United States Dollar', + minorUnit: 'cent' + }, links: { self: '/isoCurrencies/USD' } }, { type: 'people', id: '3', - email: 'lazy@xyz.fake', - name: 'Lazy Author', - dateJoined: '2013-10-31 17:25:00 -0400', + attributes: { + email: 'lazy@xyz.fake', + name: 'Lazy Author', + dateJoined: '2013-10-31 17:25:00 -0400' + }, links: { self: '/people/3', } } ] }, JSONAPI::ResourceSerializer.new(ExpenseEntryResource, - include: ['isoCurrency', 'employee'], + include: ['iso_currency', 'employee'], fields: {people: [:id, :name, :email, :date_joined]}).serialize_to_hash( ExpenseEntryResource.new(@expense_entry)) ) end @@ -1151,26 +1268,36 @@ assert_hash_equals( { data: { type: 'planets', id: '8', - name: 'Beta W', - description: 'Newly discovered Planet W', + attributes: { + name: 'Beta W', + description: 'Newly discovered Planet W' + }, links: { - self: '/planets/8', + self: '/planets/8' + }, + relationships: { planetType: { - self: '/planets/8/links/planetType', - related: '/planets/8/planetType', - linkage: nil + links: { + self: '/planets/8/relationships/planetType', + related: '/planets/8/planetType' + }, + data: nil }, tags: { - self: '/planets/8/links/tags', - related: '/planets/8/tags' + links: { + self: '/planets/8/relationships/tags', + related: '/planets/8/tags' + } }, moons: { - self: '/planets/8/links/moons', - related: '/planets/8/moons' + links: { + self: '/planets/8/relationships/moons', + related: '/planets/8/moons' + } } } } }, planet_hash) end @@ -1188,60 +1315,82 @@ assert_hash_equals( { data: [{ type: 'planets', id: '7', - name: 'Beta X', - description: 'Newly discovered Planet Z', + attributes: { + name: 'Beta X', + description: 'Newly discovered Planet Z' + }, links: { - self: '/planets/7', + self: '/planets/7' + }, + relationships: { planetType: { - self: '/planets/7/links/planetType', - related: '/planets/7/planetType', - linkage: { + links: { + self: '/planets/7/relationships/planetType', + related: '/planets/7/planetType' + }, + data: { type: 'planetTypes', id: '5' } }, tags: { - self: '/planets/7/links/tags', - related: '/planets/7/tags' + links: { + self: '/planets/7/relationships/tags', + related: '/planets/7/tags' + } }, moons: { - self: '/planets/7/links/moons', - related: '/planets/7/moons' + links: { + self: '/planets/7/relationships/moons', + related: '/planets/7/moons' + } } } }, { type: 'planets', id: '8', - name: 'Beta W', - description: 'Newly discovered Planet W', + attributes: { + name: 'Beta W', + description: 'Newly discovered Planet W' + }, links: { - self: '/planets/8', + self: '/planets/8' + }, + relationships: { planetType: { - self: '/planets/8/links/planetType', - related: '/planets/8/planetType', - linkage: nil + links: { + self: '/planets/8/relationships/planetType', + related: '/planets/8/planetType' + }, + data: nil }, tags: { - self: '/planets/8/links/tags', - related: '/planets/8/tags' + links: { + self: '/planets/8/relationships/tags', + related: '/planets/8/tags' + } }, moons: { - self: '/planets/8/links/moons', - related: '/planets/8/moons' + links: { + self: '/planets/8/relationships/moons', + related: '/planets/8/moons' + } } } } ], included: [ { type: 'planetTypes', id: '5', - name: 'unknown', + attributes: { + name: 'unknown' + }, links: { self: '/planetTypes/5' } } ] @@ -1256,21 +1405,29 @@ assert_hash_equals( { data: { type: 'preferences', id: '1', - advanced_mode: false, + attributes: { + advanced_mode: false + }, links: { - self: '/preferences/1', + self: '/preferences/1' + }, + relationships: { author: { - self: '/preferences/1/links/author', - related: '/preferences/1/author', - linkage: nil + links: { + self: '/preferences/1/relationships/author', + related: '/preferences/1/author' + }, + data: nil }, friends: { - self: '/preferences/1/links/friends', - related: '/preferences/1/friends' + links: { + self: '/preferences/1/relationships/friends', + related: '/preferences/1/friends' + } } } } }, JSONAPI::ResourceSerializer.new(PreferencesResource).serialize_to_hash(preferences) @@ -1285,18 +1442,20 @@ assert_hash_equals( { data: { type: 'facts', id: '1', - spouse_name: 'Jane Author', - bio: 'First man to run across Antartica.', - quality_rating: 23.89/45.6, - salary: BigDecimal('47000.56', 30), - date_time_joined: DateTime.parse('2013-08-07 20:25:00 UTC +00:00'), - birthday: Date.parse('1965-06-30'), - bedtime: Time.parse('2000-01-01 20:00:00 UTC +00:00'), #DB seems to set the date to 2001-01-01 for time types - photo: "abc", - cool: false, + attributes: { + spouse_name: 'Jane Author', + bio: 'First man to run across Antartica.', + quality_rating: 23.89/45.6, + salary: BigDecimal('47000.56', 30), + date_time_joined: DateTime.parse('2013-08-07 20:25:00 UTC +00:00'), + birthday: Date.parse('1965-06-30'), + bedtime: Time.parse('2000-01-01 20:00:00 UTC +00:00'), #DB seems to set the date to 2001-01-01 for time types + photo: "abc", + cool: false + }, links: { self: '/facts/1' } } },