trailblazer / roar-jsonapi

JSON API support for Roar.
http://trailblazer.to/gems/roar/jsonapi.html
MIT License
42 stars 18 forks source link

Using JSONAPI #7

Closed myabc closed 7 years ago

myabc commented 7 years ago

From @dwhelan on May 14, 2016 0:30

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!

Copied from original issue: trailblazer/roar#191

myabc commented 7 years ago

From @choallin on June 13, 2016 6:38

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

myabc commented 7 years ago

From @apotonick on June 15, 2016 9:10

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

myabc commented 7 years ago

From @dwhelan on June 16, 2016 5:34

Thanks @apotonick!

myabc commented 7 years ago

@dwhelan I haven't been able to reproduce your issue when using Decorators, so I'm going to assume that the issue was solved. Please feel free to reopen though if you're still encountering this problem.