|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe '#962 empty entity with custom documentation type' do |
| 4 | + context "when entity has no properties" do |
| 5 | + let(:app) do |
| 6 | + Class.new(Grape::API) do |
| 7 | + namespace :issue962 do |
| 8 | + class Foo < Grape::Entity |
| 9 | + end |
| 10 | + |
| 11 | + class Report < Grape::Entity |
| 12 | + expose :foo, |
| 13 | + as: :bar, |
| 14 | + using: Foo, |
| 15 | + documentation: { |
| 16 | + type: 'Array[object]', |
| 17 | + desc: 'The bar in your report', |
| 18 | + example: { |
| 19 | + 'id' => 'string', |
| 20 | + 'status' => 'string', |
| 21 | + } |
| 22 | + } |
| 23 | + end |
| 24 | + |
| 25 | + desc 'Get a report', success: Report |
| 26 | + get '/' do |
| 27 | + present({ foo: [] }, with: Report) |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + add_swagger_documentation format: :json |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + subject(:swagger_doc) do |
| 36 | + get '/swagger_doc' |
| 37 | + JSON.parse(last_response.body) |
| 38 | + end |
| 39 | + |
| 40 | + specify do |
| 41 | + expect(swagger_doc['definitions']['Report']['properties']['bar']).to eql({ |
| 42 | + 'type' => 'array', |
| 43 | + 'description' => 'The bar in your report', |
| 44 | + 'items' => { |
| 45 | + '$ref' => '#/definitions/Foo' |
| 46 | + }, |
| 47 | + 'example' => { |
| 48 | + 'id' => 'string', |
| 49 | + 'status' => 'string' |
| 50 | + } |
| 51 | + }) |
| 52 | + end |
| 53 | + |
| 54 | + specify do |
| 55 | + expect(swagger_doc['definitions']['Foo']).to eql({ |
| 56 | + 'type' => 'object', |
| 57 | + 'properties' => {}, |
| 58 | + }) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context "when entity has only hidden properties" do |
| 63 | + let(:app) do |
| 64 | + Class.new(Grape::API) do |
| 65 | + namespace :issue962 do |
| 66 | + class Foo < Grape::Entity |
| 67 | + expose :required_prop, documentation: { hidden: true } |
| 68 | + expose :optional_prop, documentation: { hidden: true }, if: ->() { true } |
| 69 | + end |
| 70 | + |
| 71 | + class Report < Grape::Entity |
| 72 | + expose :foo, |
| 73 | + as: :bar, |
| 74 | + using: Foo, |
| 75 | + documentation: { |
| 76 | + type: 'Array[object]', |
| 77 | + desc: 'The bar in your report', |
| 78 | + example: { |
| 79 | + 'id' => 'string', |
| 80 | + 'status' => 'string', |
| 81 | + } |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + desc 'Get a report', success: Report |
| 86 | + get '/' do |
| 87 | + present({ foo: [] }, with: Report) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + add_swagger_documentation format: :json |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + subject(:swagger_doc) do |
| 96 | + get '/swagger_doc' |
| 97 | + JSON.parse(last_response.body) |
| 98 | + end |
| 99 | + |
| 100 | + specify do |
| 101 | + expect(swagger_doc['definitions']['Report']['properties']['bar']).to eql({ |
| 102 | + 'type' => 'array', |
| 103 | + 'description' => 'The bar in your report', |
| 104 | + 'items' => { |
| 105 | + '$ref' => '#/definitions/Foo' |
| 106 | + }, |
| 107 | + 'example' => { |
| 108 | + 'id' => 'string', |
| 109 | + 'status' => 'string' |
| 110 | + } |
| 111 | + }) |
| 112 | + end |
| 113 | + |
| 114 | + it "hides optional properties only" do |
| 115 | + expect(swagger_doc['definitions']['Foo']).to eql({ |
| 116 | + 'type' => 'object', |
| 117 | + 'properties' => {}, |
| 118 | + 'required' => ['required_prop'], |
| 119 | + }) |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments