spec/serializer_spec.rb in jsonapi-serializers-0.15.0 vs spec/serializer_spec.rb in jsonapi-serializers-0.16.0
- old
+ new
@@ -439,16 +439,16 @@
# The members data and errors MUST NOT coexist in the same document.
describe 'JSONAPI::Serializer.serialize_errors' do
it 'can include a top level errors node' do
errors = [
{
- 'source' => { 'pointer' => '/data/attributes/first-name' },
+ 'source' => {'pointer' => '/data/attributes/first-name'},
'title' => 'Invalid Attribute',
'detail' => 'First name must contain at least three characters.'
},
{
- 'source' => { 'pointer' => '/data/attributes/first-name' },
+ 'source' => {'pointer' => '/data/attributes/first-name'},
'title' => 'Invalid Attribute',
'detail' => 'First name must contain an emoji.'
}
]
expect(JSONAPI::Serializer.serialize_errors(errors)).to eq({'errors' => errors})
@@ -475,19 +475,19 @@
end
end
user = DummyUser.new
jsonapi_errors = [
{
- 'source' => { 'pointer' => '/data/attributes/email' },
+ 'source' => {'pointer' => '/data/attributes/email'},
'detail' => 'Email is invalid'
},
{
- 'source' => { 'pointer' => '/data/attributes/email' },
+ 'source' => {'pointer' => '/data/attributes/email'},
'detail' => "Email can't be blank"
},
{
- 'source' => { 'pointer' => '/data/attributes/first-name' },
+ 'source' => {'pointer' => '/data/attributes/first-name'},
'detail' => "First name can't be blank"
}
]
expect(JSONAPI::Serializer.serialize_errors(user.errors)).to eq({
'errors' => jsonapi_errors,
@@ -849,9 +849,116 @@
'data' => expected_primary_data,
'included' => [
serialize_primary(long_comments.first, {serializer: MyApp::LongCommentSerializer}),
serialize_primary(long_comments.last, {serializer: MyApp::LongCommentSerializer}),
],
+ })
+ end
+ end
+
+ context 'sparse fieldsets' do
+ it 'allows to limit fields(attributes) for serialized resource' do
+ first_user = create(:user)
+ second_user = create(:user)
+ first_comment = create(:long_comment, user: first_user)
+ second_comment = create(:long_comment, user: second_user)
+ long_comments = [first_comment, second_comment]
+ post = create(:post, :with_author, long_comments: long_comments)
+
+ serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: :title})
+ expect(serialized_data).to eq ({
+ 'data' => {
+ 'type' => 'posts',
+ 'id' => post.id.to_s,
+ 'attributes' => {
+ 'title' => post.title,
+ },
+ 'links' => {
+ 'self' => '/posts/1'
+ }
+ }
+ })
+ end
+ it 'allows to limit fields(relationships) for serialized resource' do
+ first_user = create(:user)
+ second_user = create(:user)
+ first_comment = create(:long_comment, user: first_user)
+ second_comment = create(:long_comment, user: second_user)
+ long_comments = [first_comment, second_comment]
+ post = create(:post, :with_author, long_comments: long_comments)
+
+ fields = {posts: 'title,author,long_comments'}
+ serialized_data = JSONAPI::Serializer.serialize(post, fields: fields)
+ expect(serialized_data['data']['relationships']).to eq ({
+ 'author' => {
+ 'links' => {
+ 'self' => '/posts/1/relationships/author',
+ 'related' => '/posts/1/author'
+ }
+ },
+ 'long-comments' => {
+ 'links' => {
+ 'self' => '/posts/1/relationships/long-comments',
+ 'related' => '/posts/1/long-comments'
+ }
+ }
+ })
+ end
+ it "allows also to pass specific fields as array instead of comma-separates values" do
+ first_user = create(:user)
+ second_user = create(:user)
+ first_comment = create(:long_comment, user: first_user)
+ second_comment = create(:long_comment, user: second_user)
+ long_comments = [first_comment, second_comment]
+ post = create(:post, :with_author, long_comments: long_comments)
+
+ serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: ['title', 'author']})
+ expect(serialized_data['data']['attributes']).to eq ({
+ 'title' => post.title
+ })
+ expect(serialized_data['data']['relationships']).to eq ({
+ 'author' => {
+ 'links' => {
+ 'self' => '/posts/1/relationships/author',
+ 'related' => '/posts/1/author'
+ }
+ }
+ })
+ end
+ it 'allows to limit fields(attributes and relationships) for included resources' do
+ first_user = create(:user)
+ second_user = create(:user)
+ first_comment = create(:long_comment, user: first_user)
+ second_comment = create(:long_comment, user: second_user)
+ long_comments = [first_comment, second_comment]
+ post = create(:post, :with_author, long_comments: long_comments)
+
+ expected_primary_data = serialize_primary(post, {
+ serializer: MyApp::PostSerializer,
+ include_linkages: ['author'],
+ fields: {'posts' => [:title, :author] }
+ })
+
+ fields = {posts: 'title,author', users: ''}
+ serialized_data = JSONAPI::Serializer.serialize(post, fields: fields, include: 'author')
+ expect(serialized_data).to eq ({
+ 'data' => expected_primary_data,
+ 'included' => [
+ serialize_primary(
+ post.author,
+ serializer: MyAppOtherNamespace::UserSerializer,
+ fields: {'users' => []},
+ )
+ ]
+ })
+
+ fields = {posts: 'title,author'}
+ serialized_data = JSONAPI::Serializer.serialize(post, fields: fields, include: 'author')
+ expect(serialized_data).to eq ({
+ 'data' => expected_primary_data,
+ 'included' => [
+ serialize_primary(post.author, serializer: MyAppOtherNamespace::UserSerializer)
+ ]
})
end
end
end