swagger-blocks icon indicating copy to clipboard operation
swagger-blocks copied to clipboard

Can't find a way to follow jsonapi spec for the 'included' field

Open pldavid2 opened this issue 5 years ago • 1 comments

Hello thanks for this awesome gem!

I'm having an issue when trying to define the schema following jsonapi spec, particularly with the "included" array where someone can specify objects related to the primary data, an array that can have different model/schema types.

I currently have a schema defined for an Article and then a schema for the Author and the Comments. If i try to relate this two last schemas (Author and Comments) on the 'included' array of the Article block it doesn't work. I tried with bot these two forms

      property :included, type: :array do
        items do
          key :'$ref', :Comment
        end          
        items do
          key :'$ref', :Author
        end          
      end

And with

      property :included, type: :array do
        items do
          key :'$ref', :Comment
          key :'$ref', :Author
        end          
      end

The full Article, Comment and Author schemas are something like these

module Swagger::ArticleSchema
  extend ActiveSupport::Concern
  include Swagger::Blocks

  included do
    swagger_schema :Article do
      property :id do
        key :type, :integer
      end
      property :type do
        key :type, :string
      end
      property :attributes, type: :object do
        property :text do
          key :type, :string
        end
      end

      property :included, type: :array do
        items do
          key :'$ref', :Comment
        end          
        items do
          key :'$ref', :Author
        end          
      end #end included
    end
  end
end

module Swagger::CommentSchema
  extend ActiveSupport::Concern
  include Swagger::Blocks

  included do
    swagger_schema :Article do
      property :id do
        key :type, :integer
      end
      property :type do
        key :type, :string
      end
      property :attributes, type: :object do
        property :text do
          key :type, :string
        end
      end
    end
  end
end

module Swagger::AuthorSchema
  extend ActiveSupport::Concern
  include Swagger::Blocks

  included do
    swagger_schema :Article do
      property :id do
        key :type, :integer
      end
      property :type do
        key :type, :string
      end
      property :attributes, type: :object do
        property :full_name do
          key :type, :string
        end
      end
    end
  end
end

Is there any way to get them working with $ref? Or i need to manually specify all the 'included' relations manually?

Thanks!

pldavid2 avatar Dec 29 '19 12:12 pldavid2

Same issue here: https://github.com/fotinakis/swagger-blocks/issues/122

faizaankhan avatar Feb 04 '20 12:02 faizaankhan