trailblazer / roar

Parse and render REST API documents using representers.

Home Page:http://www.trailblazer.to/gems/roar

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using JSONAPI

dwhelan opened this issue · comments

I am trying to use Roar::JSON::JSONAPI to accept and return JSONAPI payloads. I think I must be missing something because the json rendered in a get does not seem to conform JSONAPI v1.0.

require_relative 'spec_helper'

class AppleSauce
  attr_reader :id, :taste

  def initialize
    @id = '1'
    @taste = 'tart'
  end
end

class Decorator < Roar::Decorator
  include Roar::JSON::JSONAPI
  include Roar::Hypermedia
  include Grape::Roar::Representer

  type :apple_sauce
  property :id
  property :taste
end

module Representer
  include Roar::JSON::JSONAPI
  include Roar::Hypermedia
  include Grape::Roar::Representer

  type :apple_sauce
  property :id
  property :taste
end

describe 'roar' do
  let(:expected) { { data: { type: 'apple_sauce', id: '1', attributes: { taste: 'tart' } } } }

  it { expect(JSON.parse(Decorator.prepare(AppleSauce.new).to_json)).to eq expected }
  it { expect(JSON.parse(AppleSauce.new.extend(Representer).to_json)).to eq expected }
end
Failures:

  1) roar should eq {:data=>{:type=>"apple_sauce", :id=>"1", :attributes=>{:taste=>"tart"}}}
     Failure/Error: it { expect(JSON.parse(Decorator.prepare(AppleSauce.new).to_json)).to eq expected }

       expected: {:data=>{:type=>"apple_sauce", :id=>"1", :attributes=>{:taste=>"tart"}}}
            got: {"apple_sauce"=>{"id"=>"1", "taste"=>"tart"}}

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -:data => {:type=>"apple_sauce", :id=>"1", :attributes=>{:taste=>"tart"}},
       +"apple_sauce" => {"id"=>"1", "taste"=>"tart"},

     # ./spec/test_spec.rb:35:in `block (2 levels) in <top (required)>'

  2) roar should eq {:data=>{:type=>"apple_sauce", :id=>"1", :attributes=>{:taste=>"tart"}}}
     Failure/Error: it { expect(JSON.parse(AppleSauce.new.extend(Representer).to_json)).to eq expected }

       expected: {:data=>{:type=>"apple_sauce", :id=>"1", :attributes=>{:taste=>"tart"}}}
            got: {"apple_sauce"=>{"id"=>"1", "taste"=>"tart"}}

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -:data => {:type=>"apple_sauce", :id=>"1", :attributes=>{:taste=>"tart"}},
       +"apple_sauce" => {"id"=>"1", "taste"=>"tart"},

     # ./spec/test_spec.rb:36:in `block (2 levels) in <top (required)>'

Finished in 0.01212 seconds (files took 0.47538 seconds to load)
2 examples, 2 failures

Any suggestions would be most appreciated!

Same proplem here. Is it possible that Roar::JSON::JSOAPI used an old definition of the api?

You have to use the roar/master branch. I will try to release Roar 1.1 next week.