Skip to content

Commit

Permalink
Don't document non-schema object examples
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceraccoon committed Nov 16, 2022
1 parent 1eb9fe6 commit 92e9ed8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#885](https://github.com/ruby-grape/grape-swagger/pull/885): Don't document non-schema object examples - [@spaceraccoon](https://github.com/spaceraccoon)
* Your contribution here.

#### Fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def call(param, settings, path, route, definitions)
document_required(settings)
document_additional_properties(definitions, settings) unless value_type[:is_array]
document_add_extensions(settings)
document_example(settings)
document_example(settings) if @parsed_param[:in] == 'body'

@parsed_param
end
Expand Down
49 changes: 49 additions & 0 deletions spec/issues/884_dont_document_non_schema_examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec::Matchers.define_negated_matcher :exclude, :include

describe '#884 dont document non-schema examples' do
let(:app) do
Class.new(Grape::API) do
namespace :issue_884 do
params do
requires :id, type: Integer, documentation: { example: 123 }
optional :name, type: String, documentation: { example: 'Buddy Guy' }
end

post 'document_example' do
present params
end

desc 'do not document this' do
consumes ['application/x-www-form-urlencoded']
end
params do
requires :id, type: Integer, documentation: { example: 123 }
optional :name, type: String, documentation: { example: 'Buddy Guy' }
end

post 'dont_document_example' do
present params
end
end

add_swagger_documentation format: :json
end
end

subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end

let(:parameters_document_example) { subject['definitions']['postIssue884DocumentExample']['properties'] }
let(:parameters_dont_document_example) { subject['paths']['/issue_884/dont_document_example']['post']['parameters'] }

specify do
expect(parameters_document_example.values).to all(include('example'))
expect(parameters_dont_document_example).to all(exclude('example'))
end
end
14 changes: 7 additions & 7 deletions spec/swagger_v2/params_example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def app
optional :obj, type: 'Object', documentation: { example: { 'foo' => 'bar' } }
end

get '/endpoint_with_examples' do
post '/endpoint_with_examples' do
{ 'declared_params' => declared(params) }
end

Expand All @@ -27,13 +27,13 @@ def app
JSON.parse(last_response.body)
end

let(:parameters) { subject['definitions']['postEndpointWithExamples']['properties'] }

specify do
expect(subject['paths']['/endpoint_with_examples']['get']['parameters']).to eql(
[
{ 'in' => 'query', 'name' => 'id', 'type' => 'integer', 'example' => 123, 'format' => 'int32', 'required' => true },
{ 'in' => 'query', 'name' => 'name', 'type' => 'string', 'example' => 'Person', 'required' => false },
{ 'in' => 'query', 'name' => 'obj', 'type' => 'Object', 'example' => { 'foo' => 'bar' }, 'required' => false }
]
puts subject['definitions']
puts parameters
expect(parameters).to eql(
{ 'id' => { 'type' => 'integer', 'format' => 'int32', 'example' => 123 }, 'name' => { 'type' => 'string', 'example' => 'Person' }, 'obj' => { 'type' => 'Object', 'example' => { 'foo' => 'bar' } } }
)
end
end
Expand Down

0 comments on commit 92e9ed8

Please sign in to comment.