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

JSON API Serialize deep relationships #2

Closed
myabc opened this issue Dec 10, 2016 · 6 comments
Closed

JSON API Serialize deep relationships #2

myabc opened this issue Dec 10, 2016 · 6 comments
Assignees

Comments

@myabc
Copy link
Collaborator

myabc commented Dec 10, 2016

From @wuarmin on September 27, 2016 17:49

I know JSON API module is not finished yet, but maybe this can be accomplished with less effort. In my opinion the serialized compound doc, generated from following code, should contain the ressource object (type: organisations, id: 8) in the included-payload.

require 'roar/json'
require 'roar/decorator'
require 'roar/json/json_api'

class ArticleDecorator < Roar::Decorator
  include Roar::JSON::JSONAPI
  type :articles

  property :id
  property :title


  has_one :author, class: Authors do
    type :authors

    property :id
    property :email

    has_one :organisation, class: Organisations do
      type :organisations

      property :id
      property :name
    end
  end

end

organisation = Organisations.new(id: 8, name: 'Articles23')
author = Authors.new(id: 4711, email: 'author@test.com', organisation: organisation)
article = Articles.new(id: 1, author: author, title: 'Programming')

puts ArticleDecorator.new(article).to_json

The result json contains the relationship, but the ressource-object is missing:

{
  "data": {
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "Programming"
    },
    "relationships": {
      "author": {
        "data": {
          "type": "authors",
          "id": "4711"
        }
      }
    }
  },
  "included": [
    {
      "type": "authors",
      "id": "4711",
      "attributes": {
        "email": "author@test.com"
      },
      "relationships": {
        "organisation": {
          "data": {
            "type": "organisations",
            "id": "8"
          }
        }
      }
    }
  ]
}

best regards

Copied from original issue: trailblazer/roar#200

@myabc
Copy link
Collaborator Author

myabc commented Jan 9, 2017

@franzliedke not sure if this issue is still relevant for you, but if it is, I'd appreciate feedback on this fix.

@franzliedke
Copy link

@myabc Oh, sorry, I was still planning to get back to you on #12...

I am currently not using this library anymore (although it's good to know that progress is being made in this area), but from what I can tell (by glancing at the tests), this looks nice. :)

@myabc
Copy link
Collaborator Author

myabc commented Jan 9, 2017

@franzliedke thanks for getting back! (and apologies, I just tagged you on the wrong issue)

@myabc
Copy link
Collaborator Author

myabc commented Jan 9, 2017

@wuarmin not sure if this issue is still relevant for you, but if it is, I'd appreciate feedback on this fix.

@wuarmin
Copy link

wuarmin commented Jan 10, 2017

@myabc Thank you. It looks nice. I'll test it on my side.

@wuarmin
Copy link

wuarmin commented Jan 31, 2017

@myabc It works as expected. We'll discuss internally to switch to roar-jsonapi. I don't see a reason why not.

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

3 participants