From 7ac9bc9407da9b4e4a4b0d8d65bc3f449b50bc1e Mon Sep 17 00:00:00 2001 From: Eugene Lim Date: Thu, 17 Nov 2022 00:04:08 +0800 Subject: [PATCH] Don't document non-schema object examples --- CHANGELOG.md | 1 + lib/grape-swagger/doc_methods/parse_params.rb | 2 +- ..._dont_document_non_schema_examples_spec.rb | 49 +++++++++++++++++++ spec/swagger_v2/params_example_spec.rb | 12 ++--- 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 spec/issues/884_dont_document_non_schema_examples_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index c84d381b..c6dbbe6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/grape-swagger/doc_methods/parse_params.rb b/lib/grape-swagger/doc_methods/parse_params.rb index 90fe229b..8a7b684f 100644 --- a/lib/grape-swagger/doc_methods/parse_params.rb +++ b/lib/grape-swagger/doc_methods/parse_params.rb @@ -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 diff --git a/spec/issues/884_dont_document_non_schema_examples_spec.rb b/spec/issues/884_dont_document_non_schema_examples_spec.rb new file mode 100644 index 00000000..2dee287d --- /dev/null +++ b/spec/issues/884_dont_document_non_schema_examples_spec.rb @@ -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 diff --git a/spec/swagger_v2/params_example_spec.rb b/spec/swagger_v2/params_example_spec.rb index 86ee3d99..911d46ce 100644 --- a/spec/swagger_v2/params_example_spec.rb +++ b/spec/swagger_v2/params_example_spec.rb @@ -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 @@ -27,13 +27,11 @@ 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 } - ] + expect(parameters).to eql( + { 'id' => { 'type' => 'integer', 'format' => 'int32', 'example' => 123 }, 'name' => { 'type' => 'string', 'example' => 'Person' }, 'obj' => { 'type' => 'Object', 'example' => { 'foo' => 'bar' } } } ) end end