From df490ddd32ef280c60ac1a69ae7bc8cc3746ac4c Mon Sep 17 00:00:00 2001 From: Eugene Lim Date: Fri, 11 Nov 2022 11:13:55 +0800 Subject: [PATCH 1/5] Set default parameter location based on consumes --- CHANGELOG.md | 1 + lib/grape-swagger/doc_methods/parse_params.rb | 8 +-- lib/grape-swagger/endpoint.rb | 6 +- spec/issues/532_allow_custom_format_spec.rb | 4 ++ .../553_align_array_put_post_params_spec.rb | 16 +++-- .../579_align_put_post_parameters_spec.rb | 30 +++++---- spec/issues/650_params_array_spec.rb | 6 ++ ...rameter_location_based_on_consumes_spec.rb | 62 +++++++++++++++++++ spec/issues/784_extensions_on_params_spec.rb | 4 ++ .../832_array_hash_float_decimal_spec.rb | 3 + spec/support/model_parsers/entity_parser.rb | 4 +- spec/support/model_parsers/mock_parser.rb | 4 +- .../model_parsers/representable_parser.rb | 4 +- .../api_swagger_v2_hide_param_spec.rb | 12 +++- .../swagger_v2/api_swagger_v2_mounted_spec.rb | 26 +++++--- .../api_swagger_v2_param_type_spec.rb | 24 ++++--- .../api_swagger_v2_request_params_fix_spec.rb | 16 +++-- .../api_swagger_v2_response_spec.rb | 30 +++++---- spec/swagger_v2/api_swagger_v2_spec.rb | 26 +++++--- .../api_swagger_v2_type-format_spec.rb | 3 +- spec/swagger_v2/boolean_params_spec.rb | 3 + spec/swagger_v2/float_api_spec.rb | 3 + spec/swagger_v2/form_params_spec.rb | 9 +++ spec/swagger_v2/param_multi_type_spec.rb | 7 ++- spec/swagger_v2/param_type_spec.rb | 10 ++- spec/swagger_v2/param_values_spec.rb | 18 ++++++ spec/swagger_v2/params_array_spec.rb | 22 +++++++ spec/swagger_v2/params_hash_spec.rb | 6 ++ spec/swagger_v2/params_nested_spec.rb | 6 ++ spec/swagger_v2/simple_mounted_api_spec.rb | 44 +++++++------ 30 files changed, 320 insertions(+), 97 deletions(-) create mode 100644 spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 09dbbbf8c..ce05360b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### Features +* [#880](https://github.com/ruby-grape/grape-swagger/pull/880): Set default parameter location based on consumes - [@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 cad75083d..4e75d6d79 100644 --- a/lib/grape-swagger/doc_methods/parse_params.rb +++ b/lib/grape-swagger/doc_methods/parse_params.rb @@ -4,7 +4,7 @@ module GrapeSwagger module DocMethods class ParseParams class << self - def call(param, settings, path, route, definitions) + def call(param, settings, path, route, definitions, consumes) method = route.request_method additional_documentation = settings.fetch(:documentation, {}) settings.merge!(additional_documentation) @@ -14,7 +14,7 @@ def call(param, settings, path, route, definitions) # required properties @parsed_param = { - in: param_type(value_type), + in: param_type(value_type, consumes), name: settings[:full_name] || param } @@ -148,14 +148,14 @@ def document_example(settings) @parsed_param[:example] = example if example end - def param_type(value_type) + def param_type(value_type, consumes) param_type = value_type[:param_type] || value_type[:in] if value_type[:path].include?("{#{value_type[:param_name]}}") 'path' elsif param_type param_type elsif %w[POST PUT PATCH].include?(value_type[:method]) - DataType.request_primitive?(value_type[:data_type]) ? 'formData' : 'body' + consumes.include?('application/x-www-form-urlencoded') || consumes.include?('multipart/form-data') ? 'formData' : 'body' else 'query' end diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index 89afafea3..dd93f8644 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -119,7 +119,7 @@ def method_object(route, options, path) method[:description] = description_object(route) method[:produces] = produces_object(route, options[:produces] || options[:format]) method[:consumes] = consumes_object(route, options[:consumes] || options[:format]) - method[:parameters] = params_object(route, options, path) + method[:parameters] = params_object(route, options, path, method[:consumes]) method[:security] = security_object(route) method[:responses] = response_object(route, options) method[:tags] = route.options.fetch(:tags, tag_object(route, path)) @@ -175,7 +175,7 @@ def consumes_object(route, format) GrapeSwagger::DocMethods::ProducesConsumes.call(route.settings.dig(:description, :consumes) || format) end - def params_object(route, options, path) + def params_object(route, options, path, consumes) parameters = build_request_params(route, options).each_with_object([]) do |(param, value), memo| next if hidden_parameter?(value) @@ -187,7 +187,7 @@ def params_object(route, options, path) elsif value[:type] expose_params(value[:type]) end - memo << GrapeSwagger::DocMethods::ParseParams.call(param, value, path, route, @definitions) + memo << GrapeSwagger::DocMethods::ParseParams.call(param, value, path, route, @definitions, consumes) end if GrapeSwagger::DocMethods::MoveParams.can_be_moved?(route.request_method, parameters) diff --git a/spec/issues/532_allow_custom_format_spec.rb b/spec/issues/532_allow_custom_format_spec.rb index 907b232d6..842c23f97 100644 --- a/spec/issues/532_allow_custom_format_spec.rb +++ b/spec/issues/532_allow_custom_format_spec.rb @@ -6,6 +6,10 @@ let(:app) do Class.new(Grape::API) do namespace :issue_532 do + desc 'issue_532' do + consumes ['application/x-www-form-urlencoded'] + end + params do requires :logs, type: String, documentation: { format: 'log' } optional :phone_number, type: Integer, documentation: { format: 'phone_number' } diff --git a/spec/issues/553_align_array_put_post_params_spec.rb b/spec/issues/553_align_array_put_post_params_spec.rb index 1f62c7e22..bdf829608 100644 --- a/spec/issues/553_align_array_put_post_params_spec.rb +++ b/spec/issues/553_align_array_put_post_params_spec.rb @@ -6,7 +6,9 @@ let(:app) do Class.new(Grape::API) do namespace :in_form_data do - desc 'create foo' + desc 'create foo' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :guid, type: Array[String] end @@ -14,7 +16,9 @@ # your code goes here end - desc 'put specific foo' + desc 'put specific foo' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :id requires :guid, type: Array[String] @@ -25,7 +29,9 @@ end namespace :in_body do - desc 'create foo' + desc 'create foo' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :guid, type: Array[String], documentation: { param_type: 'body' } end @@ -33,7 +39,9 @@ # your code goes here end - desc 'put specific foo' + desc 'put specific foo' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :id requires :guid, type: Array[String], documentation: { param_type: 'body' } diff --git a/spec/issues/579_align_put_post_parameters_spec.rb b/spec/issues/579_align_put_post_parameters_spec.rb index efd10813d..65464bb3b 100644 --- a/spec/issues/579_align_put_post_parameters_spec.rb +++ b/spec/issues/579_align_put_post_parameters_spec.rb @@ -21,8 +21,9 @@ class Spec < Grape::Entity namespace :implicit do namespace :body_parameter do desc 'update spec', - success: BodySpec, - params: BodySpec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: BodySpec, + params: BodySpec.documentation put ':guid' do # your code goes here end @@ -30,8 +31,9 @@ class Spec < Grape::Entity namespace :form_parameter do desc 'update spec', - success: Spec, - params: Spec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: Spec, + params: Spec.documentation put ':guid' do # your code goes here end @@ -41,8 +43,9 @@ class Spec < Grape::Entity namespace :explicit do namespace :body_parameter do desc 'update spec', - success: BodySpec, - params: BodySpec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: BodySpec, + params: BodySpec.documentation params do requires :guid end @@ -53,8 +56,9 @@ class Spec < Grape::Entity namespace :form_parameter do desc 'update spec', - success: Spec, - params: Spec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: Spec, + params: Spec.documentation params do requires :guid end @@ -68,8 +72,9 @@ class Spec < Grape::Entity route_param :guid do namespace :body_parameter do desc 'update spec', - success: BodySpec, - params: BodySpec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: BodySpec, + params: BodySpec.documentation put do # your code goes here end @@ -77,8 +82,9 @@ class Spec < Grape::Entity namespace :form_parameter do desc 'update spec', - success: Spec, - params: Spec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: Spec, + params: Spec.documentation put do # your code goes here end diff --git a/spec/issues/650_params_array_spec.rb b/spec/issues/650_params_array_spec.rb index 9d36fce2a..3bcbb2db5 100644 --- a/spec/issues/650_params_array_spec.rb +++ b/spec/issues/650_params_array_spec.rb @@ -5,6 +5,9 @@ describe '#605 Group Params as Array' do let(:app) do Class.new(Grape::API) do + desc 'array_of_range' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :array_of_range_string, type: [String], values: %w[a b c] requires :array_of_range_integer, type: [Integer], values: [1, 2, 3] @@ -13,6 +16,9 @@ { 'declared_params' => declared(params) } end + desc 'array_with_default' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :array_with_default_string, type: [String], default: 'abc' requires :array_with_default_integer, type: Array[Integer], default: 123 diff --git a/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb b/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb new file mode 100644 index 000000000..a42b9408a --- /dev/null +++ b/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe '#721 set default parameter location based on consumes' do + let(:app) do + Class.new(Grape::API) do + namespace :issue_721 do + desc 'create item' do + consumes ['application/json'] + end + + params do + requires :logs, type: String + optional :phone_number, type: Integer + end + + post do + present params + end + + desc 'modify item' do + consumes ['application/x-www-form-urlencoded'] + end + + params do + requires :id, type: Integer + requires :logs, type: String + optional :phone_number, type: Integer + end + + put ':id' do + present params + end + end + + add_swagger_documentation + end + end + + subject do + get '/swagger_doc' + JSON.parse(last_response.body) + end + + let(:post_parameters) { subject['paths']['/issue_721']['post']['parameters'] } + let(:post_schema) { subject['definitions']['postIssue721'] } + let(:put_parameters) { subject['paths']['/issue_721/{id}']['put']['parameters'] } + + specify do + expect(post_parameters).to eql( + [{'in'=>'body', 'name'=>'postIssue721', 'required'=>true, 'schema'=>{'$ref'=>'#/definitions/postIssue721'}}] + ) + expect(post_schema).to eql( + {'description'=>'create item', 'properties'=>{'logs'=>{'type'=>'string'}, 'phone_number'=>{'format'=>'int32', 'type'=>'integer'}}, 'required'=>['logs'], 'type'=>'object'} + ) + puts put_parameters + expect(put_parameters).to eql( + [{'in'=>'path', 'name'=>'id', 'type'=>'integer', 'format'=>'int32', 'required'=>true}, {'in'=>'formData', 'name'=>'logs', 'type'=>'string', 'required'=>true}, {'in'=>'formData', 'name'=>'phone_number', 'type'=>'integer', 'format'=>'int32', 'required'=>false}] + ) + end +end diff --git a/spec/issues/784_extensions_on_params_spec.rb b/spec/issues/784_extensions_on_params_spec.rb index 7a1c2168b..9a89ec47a 100644 --- a/spec/issues/784_extensions_on_params_spec.rb +++ b/spec/issues/784_extensions_on_params_spec.rb @@ -6,6 +6,10 @@ let(:app) do Class.new(Grape::API) do namespace :issue_784 do + desc 'issue_784' do + consumes ['application/x-www-form-urlencoded'] + end + params do requires :logs, type: String, documentation: { format: 'log', x: { name: 'Log' } } optional :phone_number, type: Integer, documentation: { format: 'phone_number', x: { name: 'PhoneNumber' } } diff --git a/spec/issues/832_array_hash_float_decimal_spec.rb b/spec/issues/832_array_hash_float_decimal_spec.rb index 5c0bc9303..1e7ab2c8c 100644 --- a/spec/issues/832_array_hash_float_decimal_spec.rb +++ b/spec/issues/832_array_hash_float_decimal_spec.rb @@ -6,6 +6,9 @@ let(:app) do Class.new(Grape::API) do resource :issue_832 do + desc 'issue_832' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :array_param, type: Array do requires :float_param, type: Float diff --git a/spec/support/model_parsers/entity_parser.rb b/spec/support/model_parsers/entity_parser.rb index db19ead01..f05d18362 100644 --- a/spec/support/model_parsers/entity_parser.rb +++ b/spec/support/model_parsers/entity_parser.rb @@ -234,7 +234,7 @@ class DocumentedHashAndArrayModel < Grape::Entity 'post' => { 'description' => 'This creates Thing.', 'produces' => ['application/json'], - 'consumes' => ['application/json'], + 'consumes' => ['application/x-www-form-urlencoded'], 'parameters' => [ { 'in' => 'formData', 'name' => 'text', 'description' => 'Content of something.', 'type' => 'string', 'required' => true }, { 'in' => 'formData', 'name' => 'links', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true } @@ -256,7 +256,7 @@ class DocumentedHashAndArrayModel < Grape::Entity 'put' => { 'description' => 'This updates Thing.', 'produces' => ['application/json'], - 'consumes' => ['application/json'], + 'consumes' => ['application/x-www-form-urlencoded'], 'parameters' => [ { 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }, { 'in' => 'formData', 'name' => 'text', 'description' => 'Content of something.', 'type' => 'string', 'required' => false }, diff --git a/spec/support/model_parsers/mock_parser.rb b/spec/support/model_parsers/mock_parser.rb index 46eae30fd..ddbb3573e 100644 --- a/spec/support/model_parsers/mock_parser.rb +++ b/spec/support/model_parsers/mock_parser.rb @@ -242,7 +242,7 @@ class ApiResponse < OpenStruct; end 'post' => { 'description' => 'This creates Thing.', 'produces' => ['application/json'], - 'consumes' => ['application/json'], + 'consumes' => ['application/x-www-form-urlencoded'], 'parameters' => [ { 'in' => 'formData', 'name' => 'text', 'description' => 'Content of something.', 'type' => 'string', 'required' => true }, { 'in' => 'formData', 'name' => 'links', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true } @@ -264,7 +264,7 @@ class ApiResponse < OpenStruct; end 'put' => { 'description' => 'This updates Thing.', 'produces' => ['application/json'], - 'consumes' => ['application/json'], + 'consumes' => ['application/x-www-form-urlencoded'], 'parameters' => [ { 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }, { 'in' => 'formData', 'name' => 'text', 'description' => 'Content of something.', 'type' => 'string', 'required' => false }, diff --git a/spec/support/model_parsers/representable_parser.rb b/spec/support/model_parsers/representable_parser.rb index 123dc9d15..42bf7d88f 100644 --- a/spec/support/model_parsers/representable_parser.rb +++ b/spec/support/model_parsers/representable_parser.rb @@ -306,7 +306,7 @@ class DocumentedHashAndArrayModel < Representable::Decorator 'post' => { 'description' => 'This creates Thing.', 'produces' => ['application/json'], - 'consumes' => ['application/json'], + 'consumes' => ['application/x-www-form-urlencoded'], 'parameters' => [ { 'in' => 'formData', 'name' => 'text', 'description' => 'Content of something.', 'type' => 'string', 'required' => true }, { 'in' => 'formData', 'name' => 'links', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true } @@ -328,7 +328,7 @@ class DocumentedHashAndArrayModel < Representable::Decorator 'put' => { 'description' => 'This updates Thing.', 'produces' => ['application/json'], - 'consumes' => ['application/json'], + 'consumes' => ['application/x-www-form-urlencoded'], 'parameters' => [ { 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }, { 'in' => 'formData', 'name' => 'text', 'description' => 'Content of something.', 'type' => 'string', 'required' => false }, diff --git a/spec/swagger_v2/api_swagger_v2_hide_param_spec.rb b/spec/swagger_v2/api_swagger_v2_hide_param_spec.rb index 1ba91c70b..cf96bfb5d 100644 --- a/spec/swagger_v2/api_swagger_v2_hide_param_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_hide_param_spec.rb @@ -14,7 +14,9 @@ def resource_owner end namespace :flat_params_endpoint do - desc 'This is a endpoint with a flat parameter hierarchy' + desc 'This is a endpoint with a flat parameter hierarchy' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :name, type: String, documentation: { desc: 'name' } optional :favourite_color, type: String, documentation: { desc: 'I should not be anywhere', hidden: true } @@ -28,7 +30,9 @@ def resource_owner end namespace :nested_params_endpoint do - desc 'This is a endpoint with a nested parameter hierarchy' + desc 'This is a endpoint with a nested parameter hierarchy' do + consumes ['application/x-www-form-urlencoded'] + end params do optional :name, type: String, documentation: { desc: 'name' } optional :hidden_attribute, type: Hash do @@ -47,7 +51,9 @@ def resource_owner end namespace :required_param_endpoint do - desc 'This endpoint has hidden defined for a required parameter' + desc 'This endpoint has hidden defined for a required parameter' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :name, type: String, documentation: { desc: 'name', hidden: true } end diff --git a/spec/swagger_v2/api_swagger_v2_mounted_spec.rb b/spec/swagger_v2/api_swagger_v2_mounted_spec.rb index 7ed75eed2..700e55d16 100644 --- a/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_mounted_spec.rb @@ -12,6 +12,7 @@ def app # Thing stuff desc 'This gets Things.' do + consumes ['application/x-www-form-urlencoded'] params Entities::Something.documentation http_codes [{ code: 401, message: 'Unauthorized', model: Entities::ApiError }] end @@ -21,6 +22,7 @@ def app end desc 'This gets Things.' do + consumes ['application/x-www-form-urlencoded'] http_codes [ { code: 200, message: 'get Horses', model: Entities::Something }, { code: 401, message: 'HorsesOutError', model: Entities::ApiError } @@ -32,6 +34,7 @@ def app end desc 'This gets Thing.' do + consumes ['application/x-www-form-urlencoded'] http_codes [{ code: 200, message: 'getting a single thing' }, { code: 401, message: 'Unauthorized' }] end params do @@ -43,7 +46,8 @@ def app end desc 'This creates Thing.', - success: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + success: Entities::Something params do requires :text, type: String, documentation: { type: 'string', desc: 'Content of something.' } requires :links, type: Array, documentation: { type: 'link', is_array: true } @@ -54,7 +58,8 @@ def app end desc 'This updates Thing.', - success: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + success: Entities::Something params do requires :id, type: Integer optional :text, type: String, desc: 'Content of something.' @@ -66,7 +71,8 @@ def app end desc 'This deletes Thing.', - entity: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::Something params do requires :id, type: Integer end @@ -76,7 +82,8 @@ def app end desc 'dummy route.', - failure: [{ code: 401, message: 'Unauthorized' }] + consumes: ['application/x-www-form-urlencoded'], + failure: [{ code: 401, message: 'Unauthorized' }] params do requires :id, type: Integer end @@ -86,11 +93,12 @@ def app namespace :other_thing do desc 'nested route inside namespace', - entity: Entities::QueryInput, - x: { - 'amazon-apigateway-auth' => { type: 'none' }, - 'amazon-apigateway-integration' => { type: 'aws', uri: 'foo_bar_uri', httpMethod: 'get' } - } + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::QueryInput, + x: { + 'amazon-apigateway-auth' => { type: 'none' }, + 'amazon-apigateway-integration' => { type: 'aws', uri: 'foo_bar_uri', httpMethod: 'get' } + } params do requires :elements, documentation: { diff --git a/spec/swagger_v2/api_swagger_v2_param_type_spec.rb b/spec/swagger_v2/api_swagger_v2_param_type_spec.rb index 315264e0e..4e6d5d2a3 100644 --- a/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_param_type_spec.rb @@ -10,7 +10,8 @@ module TheApi class ParamTypeApi < Grape::API # using `:param_type` desc 'full set of request param types', - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do optional :in_query, type: String, documentation: { param_type: 'query' } optional :in_header, type: String, documentation: { param_type: 'header' } @@ -21,7 +22,8 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types', - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do requires :in_path, type: Integer optional :in_query, type: String, documentation: { param_type: 'query' } @@ -33,7 +35,8 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types', - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do optional :in_path, type: Integer optional :in_query, type: String, documentation: { param_type: 'query' } @@ -46,7 +49,8 @@ class ParamTypeApi < Grape::API # using `:in` desc 'full set of request param types using `:in`', - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do optional :in_query, type: String, documentation: { in: 'query' } optional :in_header, type: String, documentation: { in: 'header' } @@ -57,7 +61,8 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types using `:in`', - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do requires :in_path, type: Integer optional :in_query, type: String, documentation: { in: 'query' } @@ -68,7 +73,8 @@ class ParamTypeApi < Grape::API { 'declared_params' => declared(params) } end - desc 'full set of request param types using `:in`' + desc 'full set of request param types using `:in`', + consumes: ['application/x-www-form-urlencoded'] params do optional :in_path, type: Integer optional :in_query, type: String, documentation: { in: 'query' } @@ -81,7 +87,8 @@ class ParamTypeApi < Grape::API # file desc 'file download', - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do requires :name, type: String end @@ -91,7 +98,8 @@ class ParamTypeApi < Grape::API end desc 'file upload', - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do requires :name, type: File end diff --git a/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb b/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb index e03d44aaf..aeffa47a5 100644 --- a/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb @@ -7,7 +7,9 @@ module TheApi class RequestParamFix < Grape::API resource :bookings do - desc 'Update booking' + desc 'Update booking' do + consumes ['application/x-www-form-urlencoded'] + end params do optional :name, type: String end @@ -15,17 +17,23 @@ class RequestParamFix < Grape::API { 'declared_params' => declared(params) } end - desc 'Get booking details' + desc 'Get booking details' do + consumes ['application/x-www-form-urlencoded'] + end get ':id' do { 'declared_params' => declared(params) } end - desc 'Get booking details by access_number' + desc 'Get booking details by access_number' do + consumes ['application/x-www-form-urlencoded'] + end get '/conf/:access_number' do { 'declared_params' => declared(params) } end - desc 'Remove booking' + desc 'Remove booking' do + consumes ['application/x-www-form-urlencoded'] + end delete ':id' do { 'declared_params' => declared(params) } end diff --git a/spec/swagger_v2/api_swagger_v2_response_spec.rb b/spec/swagger_v2/api_swagger_v2_response_spec.rb index a01959a05..31fb2f030 100644 --- a/spec/swagger_v2/api_swagger_v2_response_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_response_spec.rb @@ -11,33 +11,37 @@ class ResponseApi < Grape::API format :json desc 'This returns something', - params: Entities::UseResponse.documentation, - failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] + consumes: ['application/x-www-form-urlencoded'], + params: Entities::UseResponse.documentation, + failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] post '/params_given' do { 'declared_params' => declared(params) } end desc 'This returns something', - entity: Entities::UseResponse, - failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::UseResponse, + failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] get '/entity_response' do { 'declared_params' => declared(params) } end desc 'This returns something', - entity: Entities::UseItemResponseAsType, - failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::UseItemResponseAsType, + failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] get '/nested_type' do { 'declared_params' => declared(params) } end desc 'This returns something', - success: [ - { code: 200, message: 'Request has succeeded' }, - { code: 201, message: 'Successful Operation' }, - { code: 204, message: 'Request was fulfilled' } - ], - failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] + consumes: ['application/x-www-form-urlencoded'], + success: [ + { code: 200, message: 'Request has succeeded' }, + { code: 201, message: 'Successful Operation' }, + { code: 204, message: 'Request was fulfilled' } + ], + failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] get '/multiple-success-responses' do { 'declared_params' => declared(params) } end @@ -102,7 +106,7 @@ def app expect(subject['paths']['/params_given']['post']).to eql( 'description' => 'This returns something', 'produces' => ['application/json'], - 'consumes' => ['application/json'], + 'consumes' => ['application/x-www-form-urlencoded'], 'parameters' => [ { 'in' => 'formData', 'name' => 'description', 'type' => 'string', 'required' => false }, { 'in' => 'formData', 'name' => '$responses', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false } diff --git a/spec/swagger_v2/api_swagger_v2_spec.rb b/spec/swagger_v2/api_swagger_v2_spec.rb index 9f9920c35..c5a84f0ab 100644 --- a/spec/swagger_v2/api_swagger_v2_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_spec.rb @@ -11,6 +11,7 @@ def app # Thing stuff desc 'This gets Things.' do + consumes ['application/x-www-form-urlencoded'] params Entities::Something.documentation http_codes [{ code: 401, message: 'Unauthorized', model: Entities::ApiError }] end @@ -20,6 +21,7 @@ def app end desc 'This gets Things.' do + consumes ['application/x-www-form-urlencoded'] http_codes [ { code: 200, message: 'get Horses', model: Entities::Something }, { code: 401, message: 'HorsesOutError', model: Entities::ApiError } @@ -31,6 +33,7 @@ def app end desc 'This gets Thing.' do + consumes ['application/x-www-form-urlencoded'] http_codes [{ code: 200, message: 'getting a single thing' }, { code: 401, message: 'Unauthorized' }] end params do @@ -42,7 +45,8 @@ def app end desc 'This creates Thing.', - success: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + success: Entities::Something params do requires :text, type: String, documentation: { type: 'string', desc: 'Content of something.' } requires :links, type: Array, documentation: { type: 'link', is_array: true } @@ -53,7 +57,8 @@ def app end desc 'This updates Thing.', - success: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + success: Entities::Something params do requires :id, type: Integer optional :text, type: String, desc: 'Content of something.' @@ -65,7 +70,8 @@ def app end desc 'This deletes Thing.', - entity: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::Something params do requires :id, type: Integer end @@ -75,7 +81,8 @@ def app end desc 'dummy route.', - failure: [{ code: 401, message: 'Unauthorized' }] + consumes: ['application/x-www-form-urlencoded'], + failure: [{ code: 401, message: 'Unauthorized' }] params do requires :id, type: Integer end @@ -85,11 +92,12 @@ def app namespace :other_thing do desc 'nested route inside namespace', - entity: Entities::QueryInput, - x: { - 'amazon-apigateway-auth' => { type: 'none' }, - 'amazon-apigateway-integration' => { type: 'aws', uri: 'foo_bar_uri', httpMethod: 'get' } - } + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::QueryInput, + x: { + 'amazon-apigateway-auth' => { type: 'none' }, + 'amazon-apigateway-integration' => { type: 'aws', uri: 'foo_bar_uri', httpMethod: 'get' } + } params do requires :elements, documentation: { diff --git a/spec/swagger_v2/api_swagger_v2_type-format_spec.rb b/spec/swagger_v2/api_swagger_v2_type-format_spec.rb index ab44aa8c4..deae7605b 100644 --- a/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_type-format_spec.rb @@ -28,7 +28,8 @@ module TheApi class TypeFormatApi < Grape::API desc 'full set of request data types', - success: Entities::TypedDefinition + consumes: ['application/x-www-form-urlencoded'], + success: Entities::TypedDefinition params do # grape supported data types diff --git a/spec/swagger_v2/boolean_params_spec.rb b/spec/swagger_v2/boolean_params_spec.rb index 8565eded7..a0f926622 100644 --- a/spec/swagger_v2/boolean_params_spec.rb +++ b/spec/swagger_v2/boolean_params_spec.rb @@ -7,6 +7,9 @@ def app Class.new(Grape::API) do format :json + desc 'splines' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :a_boolean, type: Grape::API::Boolean optional :another_boolean, type: Grape::API::Boolean, default: false diff --git a/spec/swagger_v2/float_api_spec.rb b/spec/swagger_v2/float_api_spec.rb index a4387d190..de2bd7067 100644 --- a/spec/swagger_v2/float_api_spec.rb +++ b/spec/swagger_v2/float_api_spec.rb @@ -7,6 +7,9 @@ def app Class.new(Grape::API) do format :json + desc 'splines' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :a_float, type: Float end diff --git a/spec/swagger_v2/form_params_spec.rb b/spec/swagger_v2/form_params_spec.rb index d1caf44f0..03ce90062 100644 --- a/spec/swagger_v2/form_params_spec.rb +++ b/spec/swagger_v2/form_params_spec.rb @@ -7,6 +7,9 @@ def app Class.new(Grape::API) do format :json + desc 'get items' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :name, type: String, desc: 'name of item' end @@ -14,6 +17,9 @@ def app {} end + desc 'get item' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :id, type: Integer, desc: 'id of item' requires :name, type: String, desc: 'name of item' @@ -32,6 +38,9 @@ def app {} end + desc 'create item' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :id, type: Integer, desc: 'id of item' requires :name, type: String, desc: 'name of item' diff --git a/spec/swagger_v2/param_multi_type_spec.rb b/spec/swagger_v2/param_multi_type_spec.rb index 047f9fff1..fdd6b6dba 100644 --- a/spec/swagger_v2/param_multi_type_spec.rb +++ b/spec/swagger_v2/param_multi_type_spec.rb @@ -7,6 +7,9 @@ def app Class.new(Grape::API) do format :json + desc 'action' do + consumes ['application/x-www-form-urlencoded'] + end params do if Grape::VERSION < '0.14' requires :input, type: [String, Integer] @@ -52,7 +55,9 @@ def app Class.new(Grape::API) do format :json - desc 'Some API', headers: { 'My-Header' => { required: true, description: 'Set this!' } } + desc 'Some API', + consumes: ['application/x-www-form-urlencoded'], + headers: { 'My-Header' => { required: true, description: 'Set this!' } } params do if Grape::VERSION < '0.14' requires :input, type: [String, Integer] diff --git a/spec/swagger_v2/param_type_spec.rb b/spec/swagger_v2/param_type_spec.rb index 21c696403..779fa1bd5 100644 --- a/spec/swagger_v2/param_type_spec.rb +++ b/spec/swagger_v2/param_type_spec.rb @@ -7,6 +7,9 @@ def app Class.new(Grape::API) do format :json + desc 'action' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :input, type: String end @@ -14,6 +17,9 @@ def app { message: 'hi' } end + desc 'action_with_doc' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :input, type: String, default: '14', documentation: { type: 'email', default: '42' } end @@ -46,7 +52,9 @@ def app Class.new(Grape::API) do format :json - desc 'Some API', headers: { 'My-Header' => { required: true, description: 'Set this!' } } + desc 'Some API', + consumes: ['application/x-www-form-urlencoded'], + headers: { 'My-Header' => { required: true, description: 'Set this!' } } params do requires :input, type: String end diff --git a/spec/swagger_v2/param_values_spec.rb b/spec/swagger_v2/param_values_spec.rb index 1e189ca85..0220a9e73 100644 --- a/spec/swagger_v2/param_values_spec.rb +++ b/spec/swagger_v2/param_values_spec.rb @@ -8,6 +8,9 @@ def app Class.new(Grape::API) do format :json + desc 'plain_array' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :letter, type: String, values: %w[a b c] end @@ -15,6 +18,9 @@ def app { message: 'hi' } end + desc 'array_in_proc' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :letter, type: String, values: proc { %w[d e f] } end @@ -22,6 +28,9 @@ def app { message: 'hi' } end + desc 'range_letter' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :letter, type: String, values: 'a'..'z' end @@ -29,6 +38,9 @@ def app { message: 'hi' } end + desc 'range_integer' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :integer, type: Integer, values: -5..5 end @@ -107,6 +119,9 @@ def app Class.new(Grape::API) do format :json + desc 'non_array_in_proc' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :letter, type: String, values: proc { 'string' } end @@ -114,6 +129,9 @@ def app { message: 'hi' } end + desc 'range_float' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :float, type: Float, values: -5.0..5.0 end diff --git a/spec/swagger_v2/params_array_spec.rb b/spec/swagger_v2/params_array_spec.rb index 5b8eeaeb6..8484c3301 100644 --- a/spec/swagger_v2/params_array_spec.rb +++ b/spec/swagger_v2/params_array_spec.rb @@ -13,6 +13,9 @@ Class.new(Grape::API) do format :json + desc 'groups' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :required_group, type: Array do requires :required_param_1 @@ -23,6 +26,9 @@ { 'declared_params' => declared(params) } end + desc 'type_given' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :typed_group, type: Array do requires :id, type: Integer, desc: 'integer given' @@ -38,6 +44,10 @@ # as body parameters it would be interpreted a bit different, # cause it could not be distinguished anymore, so this would be translated to one array, # see also next example for the difference + desc 'array_of_type' do + consumes ['application/x-www-form-urlencoded'] + end + params do requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'nested array of strings', example: %w[a b] } requires :array_of_integer, type: Array[Integer], documentation: { param_type: 'body', desc: 'nested array of integers' } @@ -47,6 +57,10 @@ { 'declared_params' => declared(params) } end + desc 'object_and_array' do + consumes ['application/x-www-form-urlencoded'] + end + params do requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'array of strings' } requires :integer_value, type: Integer, documentation: { param_type: 'body', desc: 'integer value' } @@ -56,6 +70,10 @@ { 'declared_params' => declared(params) } end + desc 'array_of_type_in_form' do + consumes ['application/x-www-form-urlencoded'] + end + params do requires :array_of_string, type: Array[String] requires :array_of_integer, type: Array[Integer] @@ -65,6 +83,10 @@ { 'declared_params' => declared(params) } end + desc 'array_of_entities' do + consumes ['application/x-www-form-urlencoded'] + end + params do requires :array_of_entities, type: Array[Entities::ApiError] end diff --git a/spec/swagger_v2/params_hash_spec.rb b/spec/swagger_v2/params_hash_spec.rb index 73943f367..158d4078f 100644 --- a/spec/swagger_v2/params_hash_spec.rb +++ b/spec/swagger_v2/params_hash_spec.rb @@ -7,6 +7,9 @@ def app Class.new(Grape::API) do format :json + desc 'use groups' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :required_group, type: Hash do requires :required_param_1 @@ -17,6 +20,9 @@ def app { 'declared_params' => declared(params) } end + desc 'use given type' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :typed_group, type: Hash do requires :id, type: Integer, desc: 'integer given' diff --git a/spec/swagger_v2/params_nested_spec.rb b/spec/swagger_v2/params_nested_spec.rb index 259b7a3d8..73d743a59 100644 --- a/spec/swagger_v2/params_nested_spec.rb +++ b/spec/swagger_v2/params_nested_spec.rb @@ -10,6 +10,9 @@ Class.new(Grape::API) do format :json + desc 'nested array' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :a_array, type: Array do requires :param_1, type: Integer @@ -26,6 +29,9 @@ { 'declared_params' => declared(params) } end + desc 'nested hash' do + consumes ['application/x-www-form-urlencoded'] + end params do requires :a_hash, type: Hash do requires :param_1, type: Integer diff --git a/spec/swagger_v2/simple_mounted_api_spec.rb b/spec/swagger_v2/simple_mounted_api_spec.rb index b2dd03f8f..6b60208f8 100644 --- a/spec/swagger_v2/simple_mounted_api_spec.rb +++ b/spec/swagger_v2/simple_mounted_api_spec.rb @@ -9,20 +9,23 @@ class CustomType; end # rubocop:enable Lint/EmptyClass class SimpleMountedApi < Grape::API - desc 'Document root' + desc 'Document root', + consumes: ['application/x-www-form-urlencoded'] get do { message: 'hi' } end desc 'This gets something.', - notes: '_test_' + consumes: ['application/x-www-form-urlencoded'], + notes: '_test_' get '/simple' do { bla: 'something' } end desc 'This gets something for URL using - separator.', - notes: '_test_' + consumes: ['application/x-www-form-urlencoded'], + notes: '_test_' get '/simple-test' do { bla: 'something' } @@ -37,32 +40,35 @@ class SimpleMountedApi < Grape::API end desc 'this gets something else', - headers: { - 'XAuthToken' => { description: 'A required header.', required: true }, - 'XOtherHeader' => { description: 'An optional header.', required: false } - }, - http_codes: [ - { code: 403, message: 'invalid pony' }, - { code: 405, message: 'no ponies left!' } - ] + consumes: ['application/x-www-form-urlencoded'], + headers: { + 'XAuthToken' => { description: 'A required header.', required: true }, + 'XOtherHeader' => { description: 'An optional header.', required: false } + }, + http_codes: [ + { code: 403, message: 'invalid pony' }, + { code: 405, message: 'no ponies left!' } + ] get '/simple_with_headers' do { bla: 'something_else' } end desc 'this takes an array of parameters', - params: { - 'items[]' => { description: 'array of items', is_array: true } - } + consumes: ['application/x-www-form-urlencoded'], + params: { + 'items[]' => { description: 'array of items', is_array: true } + } post '/items' do {} end desc 'this uses a custom parameter', - params: { - 'custom' => { type: CustomType, description: 'array of items', is_array: true } - } + consumes: ['application/x-www-form-urlencoded'], + params: { + 'custom' => { type: CustomType, description: 'array of items', is_array: true } + } get '/custom' do {} @@ -166,7 +172,7 @@ def app 'post' => { 'description' => 'this takes an array of parameters', 'produces' => ['application/json'], - 'consumes' => ['application/json'], + 'consumes' => ['application/x-www-form-urlencoded'], 'parameters' => [{ 'in' => 'formData', 'name' => 'items[]', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'string' } }], 'tags' => ['items'], 'operationId' => 'postItems', @@ -290,7 +296,7 @@ def app 'post' => { 'description' => 'this takes an array of parameters', 'produces' => ['application/json'], - 'consumes' => ['application/json'], + 'consumes' => ['application/x-www-form-urlencoded'], 'parameters' => [{ 'in' => 'formData', 'name' => 'items[]', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'string' } }], 'tags' => ['items'], 'operationId' => 'postItems', From a23624c010779bc170c2cd35d5a0623abcfaf9c5 Mon Sep 17 00:00:00 2001 From: Dhruv Paranjape Date: Tue, 7 May 2024 09:33:28 +0200 Subject: [PATCH 2/5] fix rubocop warnings and update todos. --- .rubocop.yml | 3 ++ .rubocop_todo.yml | 11 ++--- lib/grape-swagger/doc_methods/parse_params.rb | 6 ++- .../579_align_put_post_parameters_spec.rb | 36 +++++++-------- ...rameter_location_based_on_consumes_spec.rb | 6 +-- .../swagger_v2/api_swagger_v2_mounted_spec.rb | 28 ++++++------ .../api_swagger_v2_param_type_spec.rb | 30 ++++++------- .../api_swagger_v2_response_spec.rb | 32 +++++++------- spec/swagger_v2/api_swagger_v2_spec.rb | 28 ++++++------ .../api_swagger_v2_type-format_spec.rb | 4 +- spec/swagger_v2/param_multi_type_spec.rb | 6 +-- spec/swagger_v2/param_type_spec.rb | 4 +- spec/swagger_v2/simple_mounted_api_spec.rb | 44 +++++++++---------- 13 files changed, 123 insertions(+), 115 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 9930e5608..dab6a1b3a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -75,3 +75,6 @@ Style/RegexpLiteral: Style/SlicingWithRange: Enabled: false + +Metrics/ParameterLists: + Max: 6 \ No newline at end of file diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 405b89cac..811dd12a2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-05-20 18:23:47 UTC using RuboCop version 1.51.0. +# on 2024-05-07 07:32:56 UTC using RuboCop version 1.63.4. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -13,17 +13,17 @@ Gemspec/RequiredRubyVersion: Exclude: - 'grape-swagger.gemspec' -# Offense count: 32 +# Offense count: 33 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: - Max: 56 + Max: 59 -# Offense count: 30 +# Offense count: 32 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: Max: 28 -# Offense count: 8 +# Offense count: 9 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/PerceivedComplexity: Max: 16 @@ -35,6 +35,7 @@ Style/ClassVars: # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowedReceivers. Style/CollectionCompact: Exclude: - 'lib/grape-swagger/endpoint.rb' diff --git a/lib/grape-swagger/doc_methods/parse_params.rb b/lib/grape-swagger/doc_methods/parse_params.rb index 4e75d6d79..4776bc23f 100644 --- a/lib/grape-swagger/doc_methods/parse_params.rb +++ b/lib/grape-swagger/doc_methods/parse_params.rb @@ -155,7 +155,11 @@ def param_type(value_type, consumes) elsif param_type param_type elsif %w[POST PUT PATCH].include?(value_type[:method]) - consumes.include?('application/x-www-form-urlencoded') || consumes.include?('multipart/form-data') ? 'formData' : 'body' + if consumes.include?('application/x-www-form-urlencoded') || consumes.include?('multipart/form-data') + 'formData' + else + 'body' + end else 'query' end diff --git a/spec/issues/579_align_put_post_parameters_spec.rb b/spec/issues/579_align_put_post_parameters_spec.rb index 65464bb3b..466f06565 100644 --- a/spec/issues/579_align_put_post_parameters_spec.rb +++ b/spec/issues/579_align_put_post_parameters_spec.rb @@ -21,9 +21,9 @@ class Spec < Grape::Entity namespace :implicit do namespace :body_parameter do desc 'update spec', - consumes: ['application/x-www-form-urlencoded'], - success: BodySpec, - params: BodySpec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: BodySpec, + params: BodySpec.documentation put ':guid' do # your code goes here end @@ -31,9 +31,9 @@ class Spec < Grape::Entity namespace :form_parameter do desc 'update spec', - consumes: ['application/x-www-form-urlencoded'], - success: Spec, - params: Spec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: Spec, + params: Spec.documentation put ':guid' do # your code goes here end @@ -43,9 +43,9 @@ class Spec < Grape::Entity namespace :explicit do namespace :body_parameter do desc 'update spec', - consumes: ['application/x-www-form-urlencoded'], - success: BodySpec, - params: BodySpec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: BodySpec, + params: BodySpec.documentation params do requires :guid end @@ -56,9 +56,9 @@ class Spec < Grape::Entity namespace :form_parameter do desc 'update spec', - consumes: ['application/x-www-form-urlencoded'], - success: Spec, - params: Spec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: Spec, + params: Spec.documentation params do requires :guid end @@ -72,9 +72,9 @@ class Spec < Grape::Entity route_param :guid do namespace :body_parameter do desc 'update spec', - consumes: ['application/x-www-form-urlencoded'], - success: BodySpec, - params: BodySpec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: BodySpec, + params: BodySpec.documentation put do # your code goes here end @@ -82,9 +82,9 @@ class Spec < Grape::Entity namespace :form_parameter do desc 'update spec', - consumes: ['application/x-www-form-urlencoded'], - success: Spec, - params: Spec.documentation + consumes: ['application/x-www-form-urlencoded'], + success: Spec, + params: Spec.documentation put do # your code goes here end diff --git a/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb b/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb index a42b9408a..c0ce06af2 100644 --- a/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb +++ b/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb @@ -49,14 +49,14 @@ specify do expect(post_parameters).to eql( - [{'in'=>'body', 'name'=>'postIssue721', 'required'=>true, 'schema'=>{'$ref'=>'#/definitions/postIssue721'}}] + [{ 'in' => 'body', 'name' => 'postIssue721', 'required' => true, 'schema' => { '$ref' => '#/definitions/postIssue721' } }] ) expect(post_schema).to eql( - {'description'=>'create item', 'properties'=>{'logs'=>{'type'=>'string'}, 'phone_number'=>{'format'=>'int32', 'type'=>'integer'}}, 'required'=>['logs'], 'type'=>'object'} + { 'description' => 'create item', 'properties' => { 'logs' => { 'type' => 'string' }, 'phone_number' => { 'format' => 'int32', 'type' => 'integer' } }, 'required' => ['logs'], 'type' => 'object' } ) puts put_parameters expect(put_parameters).to eql( - [{'in'=>'path', 'name'=>'id', 'type'=>'integer', 'format'=>'int32', 'required'=>true}, {'in'=>'formData', 'name'=>'logs', 'type'=>'string', 'required'=>true}, {'in'=>'formData', 'name'=>'phone_number', 'type'=>'integer', 'format'=>'int32', 'required'=>false}] + [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }, { 'in' => 'formData', 'name' => 'logs', 'type' => 'string', 'required' => true }, { 'in' => 'formData', 'name' => 'phone_number', 'type' => 'integer', 'format' => 'int32', 'required' => false }] ) end end diff --git a/spec/swagger_v2/api_swagger_v2_mounted_spec.rb b/spec/swagger_v2/api_swagger_v2_mounted_spec.rb index 700e55d16..b1400edcb 100644 --- a/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_mounted_spec.rb @@ -46,8 +46,8 @@ def app end desc 'This creates Thing.', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + success: Entities::Something params do requires :text, type: String, documentation: { type: 'string', desc: 'Content of something.' } requires :links, type: Array, documentation: { type: 'link', is_array: true } @@ -58,8 +58,8 @@ def app end desc 'This updates Thing.', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + success: Entities::Something params do requires :id, type: Integer optional :text, type: String, desc: 'Content of something.' @@ -71,8 +71,8 @@ def app end desc 'This deletes Thing.', - consumes: ['application/x-www-form-urlencoded'], - entity: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::Something params do requires :id, type: Integer end @@ -82,8 +82,8 @@ def app end desc 'dummy route.', - consumes: ['application/x-www-form-urlencoded'], - failure: [{ code: 401, message: 'Unauthorized' }] + consumes: ['application/x-www-form-urlencoded'], + failure: [{ code: 401, message: 'Unauthorized' }] params do requires :id, type: Integer end @@ -93,12 +93,12 @@ def app namespace :other_thing do desc 'nested route inside namespace', - consumes: ['application/x-www-form-urlencoded'], - entity: Entities::QueryInput, - x: { - 'amazon-apigateway-auth' => { type: 'none' }, - 'amazon-apigateway-integration' => { type: 'aws', uri: 'foo_bar_uri', httpMethod: 'get' } - } + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::QueryInput, + x: { + 'amazon-apigateway-auth' => { type: 'none' }, + 'amazon-apigateway-integration' => { type: 'aws', uri: 'foo_bar_uri', httpMethod: 'get' } + } params do requires :elements, documentation: { diff --git a/spec/swagger_v2/api_swagger_v2_param_type_spec.rb b/spec/swagger_v2/api_swagger_v2_param_type_spec.rb index 4e6d5d2a3..ba7a9a291 100644 --- a/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_param_type_spec.rb @@ -10,8 +10,8 @@ module TheApi class ParamTypeApi < Grape::API # using `:param_type` desc 'full set of request param types', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do optional :in_query, type: String, documentation: { param_type: 'query' } optional :in_header, type: String, documentation: { param_type: 'header' } @@ -22,8 +22,8 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do requires :in_path, type: Integer optional :in_query, type: String, documentation: { param_type: 'query' } @@ -35,8 +35,8 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do optional :in_path, type: Integer optional :in_query, type: String, documentation: { param_type: 'query' } @@ -49,8 +49,8 @@ class ParamTypeApi < Grape::API # using `:in` desc 'full set of request param types using `:in`', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do optional :in_query, type: String, documentation: { in: 'query' } optional :in_header, type: String, documentation: { in: 'header' } @@ -61,8 +61,8 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types using `:in`', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do requires :in_path, type: Integer optional :in_query, type: String, documentation: { in: 'query' } @@ -74,7 +74,7 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types using `:in`', - consumes: ['application/x-www-form-urlencoded'] + consumes: ['application/x-www-form-urlencoded'] params do optional :in_path, type: Integer optional :in_query, type: String, documentation: { in: 'query' } @@ -87,8 +87,8 @@ class ParamTypeApi < Grape::API # file desc 'file download', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do requires :name, type: String end @@ -98,8 +98,8 @@ class ParamTypeApi < Grape::API end desc 'file upload', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::UseResponse + consumes: ['application/x-www-form-urlencoded'], + success: Entities::UseResponse params do requires :name, type: File end diff --git a/spec/swagger_v2/api_swagger_v2_response_spec.rb b/spec/swagger_v2/api_swagger_v2_response_spec.rb index 31fb2f030..4374fa34e 100644 --- a/spec/swagger_v2/api_swagger_v2_response_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_response_spec.rb @@ -11,37 +11,37 @@ class ResponseApi < Grape::API format :json desc 'This returns something', - consumes: ['application/x-www-form-urlencoded'], - params: Entities::UseResponse.documentation, - failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] + consumes: ['application/x-www-form-urlencoded'], + params: Entities::UseResponse.documentation, + failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] post '/params_given' do { 'declared_params' => declared(params) } end desc 'This returns something', - consumes: ['application/x-www-form-urlencoded'], - entity: Entities::UseResponse, - failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::UseResponse, + failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] get '/entity_response' do { 'declared_params' => declared(params) } end desc 'This returns something', - consumes: ['application/x-www-form-urlencoded'], - entity: Entities::UseItemResponseAsType, - failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::UseItemResponseAsType, + failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] get '/nested_type' do { 'declared_params' => declared(params) } end desc 'This returns something', - consumes: ['application/x-www-form-urlencoded'], - success: [ - { code: 200, message: 'Request has succeeded' }, - { code: 201, message: 'Successful Operation' }, - { code: 204, message: 'Request was fulfilled' } - ], - failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] + consumes: ['application/x-www-form-urlencoded'], + success: [ + { code: 200, message: 'Request has succeeded' }, + { code: 201, message: 'Successful Operation' }, + { code: 204, message: 'Request was fulfilled' } + ], + failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] get '/multiple-success-responses' do { 'declared_params' => declared(params) } end diff --git a/spec/swagger_v2/api_swagger_v2_spec.rb b/spec/swagger_v2/api_swagger_v2_spec.rb index c5a84f0ab..5f6c523f0 100644 --- a/spec/swagger_v2/api_swagger_v2_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_spec.rb @@ -45,8 +45,8 @@ def app end desc 'This creates Thing.', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + success: Entities::Something params do requires :text, type: String, documentation: { type: 'string', desc: 'Content of something.' } requires :links, type: Array, documentation: { type: 'link', is_array: true } @@ -57,8 +57,8 @@ def app end desc 'This updates Thing.', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + success: Entities::Something params do requires :id, type: Integer optional :text, type: String, desc: 'Content of something.' @@ -70,8 +70,8 @@ def app end desc 'This deletes Thing.', - consumes: ['application/x-www-form-urlencoded'], - entity: Entities::Something + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::Something params do requires :id, type: Integer end @@ -81,8 +81,8 @@ def app end desc 'dummy route.', - consumes: ['application/x-www-form-urlencoded'], - failure: [{ code: 401, message: 'Unauthorized' }] + consumes: ['application/x-www-form-urlencoded'], + failure: [{ code: 401, message: 'Unauthorized' }] params do requires :id, type: Integer end @@ -92,12 +92,12 @@ def app namespace :other_thing do desc 'nested route inside namespace', - consumes: ['application/x-www-form-urlencoded'], - entity: Entities::QueryInput, - x: { - 'amazon-apigateway-auth' => { type: 'none' }, - 'amazon-apigateway-integration' => { type: 'aws', uri: 'foo_bar_uri', httpMethod: 'get' } - } + consumes: ['application/x-www-form-urlencoded'], + entity: Entities::QueryInput, + x: { + 'amazon-apigateway-auth' => { type: 'none' }, + 'amazon-apigateway-integration' => { type: 'aws', uri: 'foo_bar_uri', httpMethod: 'get' } + } params do requires :elements, documentation: { diff --git a/spec/swagger_v2/api_swagger_v2_type-format_spec.rb b/spec/swagger_v2/api_swagger_v2_type-format_spec.rb index deae7605b..30cc9161b 100644 --- a/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_type-format_spec.rb @@ -28,8 +28,8 @@ module TheApi class TypeFormatApi < Grape::API desc 'full set of request data types', - consumes: ['application/x-www-form-urlencoded'], - success: Entities::TypedDefinition + consumes: ['application/x-www-form-urlencoded'], + success: Entities::TypedDefinition params do # grape supported data types diff --git a/spec/swagger_v2/param_multi_type_spec.rb b/spec/swagger_v2/param_multi_type_spec.rb index fdd6b6dba..afcc10b0a 100644 --- a/spec/swagger_v2/param_multi_type_spec.rb +++ b/spec/swagger_v2/param_multi_type_spec.rb @@ -55,9 +55,9 @@ def app Class.new(Grape::API) do format :json - desc 'Some API', - consumes: ['application/x-www-form-urlencoded'], - headers: { 'My-Header' => { required: true, description: 'Set this!' } } + desc 'Some API', + consumes: ['application/x-www-form-urlencoded'], + headers: { 'My-Header' => { required: true, description: 'Set this!' } } params do if Grape::VERSION < '0.14' requires :input, type: [String, Integer] diff --git a/spec/swagger_v2/param_type_spec.rb b/spec/swagger_v2/param_type_spec.rb index 779fa1bd5..4dde18f98 100644 --- a/spec/swagger_v2/param_type_spec.rb +++ b/spec/swagger_v2/param_type_spec.rb @@ -53,8 +53,8 @@ def app format :json desc 'Some API', - consumes: ['application/x-www-form-urlencoded'], - headers: { 'My-Header' => { required: true, description: 'Set this!' } } + consumes: ['application/x-www-form-urlencoded'], + headers: { 'My-Header' => { required: true, description: 'Set this!' } } params do requires :input, type: String end diff --git a/spec/swagger_v2/simple_mounted_api_spec.rb b/spec/swagger_v2/simple_mounted_api_spec.rb index 6b60208f8..d3a919a96 100644 --- a/spec/swagger_v2/simple_mounted_api_spec.rb +++ b/spec/swagger_v2/simple_mounted_api_spec.rb @@ -10,22 +10,22 @@ class CustomType; end class SimpleMountedApi < Grape::API desc 'Document root', - consumes: ['application/x-www-form-urlencoded'] + consumes: ['application/x-www-form-urlencoded'] get do { message: 'hi' } end desc 'This gets something.', - consumes: ['application/x-www-form-urlencoded'], - notes: '_test_' + consumes: ['application/x-www-form-urlencoded'], + notes: '_test_' get '/simple' do { bla: 'something' } end desc 'This gets something for URL using - separator.', - consumes: ['application/x-www-form-urlencoded'], - notes: '_test_' + consumes: ['application/x-www-form-urlencoded'], + notes: '_test_' get '/simple-test' do { bla: 'something' } @@ -40,35 +40,35 @@ class SimpleMountedApi < Grape::API end desc 'this gets something else', - consumes: ['application/x-www-form-urlencoded'], - headers: { - 'XAuthToken' => { description: 'A required header.', required: true }, - 'XOtherHeader' => { description: 'An optional header.', required: false } - }, - http_codes: [ - { code: 403, message: 'invalid pony' }, - { code: 405, message: 'no ponies left!' } - ] + consumes: ['application/x-www-form-urlencoded'], + headers: { + 'XAuthToken' => { description: 'A required header.', required: true }, + 'XOtherHeader' => { description: 'An optional header.', required: false } + }, + http_codes: [ + { code: 403, message: 'invalid pony' }, + { code: 405, message: 'no ponies left!' } + ] get '/simple_with_headers' do { bla: 'something_else' } end desc 'this takes an array of parameters', - consumes: ['application/x-www-form-urlencoded'], - params: { - 'items[]' => { description: 'array of items', is_array: true } - } + consumes: ['application/x-www-form-urlencoded'], + params: { + 'items[]' => { description: 'array of items', is_array: true } + } post '/items' do {} end desc 'this uses a custom parameter', - consumes: ['application/x-www-form-urlencoded'], - params: { - 'custom' => { type: CustomType, description: 'array of items', is_array: true } - } + consumes: ['application/x-www-form-urlencoded'], + params: { + 'custom' => { type: CustomType, description: 'array of items', is_array: true } + } get '/custom' do {} From 64dddddd3823f50a765e7af882b20a238eb306c7 Mon Sep 17 00:00:00 2001 From: Dhruv Paranjape Date: Tue, 7 May 2024 09:37:30 +0200 Subject: [PATCH 3/5] fix spec --- spec/swagger_v2/params_array_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/swagger_v2/params_array_spec.rb b/spec/swagger_v2/params_array_spec.rb index 8484c3301..510309b21 100644 --- a/spec/swagger_v2/params_array_spec.rb +++ b/spec/swagger_v2/params_array_spec.rb @@ -142,6 +142,7 @@ specify do expect(subject['definitions']['postArrayOfType']).to eql( 'type' => 'object', + 'description' => 'array_of_type', 'properties' => { 'array_of_string' => { 'items' => { 'type' => 'string' }, 'type' => 'array', 'description' => 'nested array of strings', 'example' => %w[a b] From db9ab9e6123339b305de0b56304d4fd7ae5f5e6b Mon Sep 17 00:00:00 2001 From: Dhruv Paranjape Date: Thu, 9 May 2024 11:07:19 +0200 Subject: [PATCH 4/5] change rubocop fix --- .rubocop.yml | 3 --- lib/grape-swagger/doc_methods/parse_params.rb | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index dab6a1b3a..9930e5608 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -75,6 +75,3 @@ Style/RegexpLiteral: Style/SlicingWithRange: Enabled: false - -Metrics/ParameterLists: - Max: 6 \ No newline at end of file diff --git a/lib/grape-swagger/doc_methods/parse_params.rb b/lib/grape-swagger/doc_methods/parse_params.rb index 4776bc23f..0347ffe0e 100644 --- a/lib/grape-swagger/doc_methods/parse_params.rb +++ b/lib/grape-swagger/doc_methods/parse_params.rb @@ -4,7 +4,7 @@ module GrapeSwagger module DocMethods class ParseParams class << self - def call(param, settings, path, route, definitions, consumes) + def call(param, settings, path, route, definitions, consumes) # rubocop:disable Metrics/ParameterLists method = route.request_method additional_documentation = settings.fetch(:documentation, {}) settings.merge!(additional_documentation) From 8d5d9ac4d5787bd73795ca7ae69cbfdf89b70437 Mon Sep 17 00:00:00 2001 From: Dhruv Paranjape Date: Thu, 9 May 2024 11:14:22 +0200 Subject: [PATCH 5/5] correct PR number in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce05360b3..6de919671 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ #### Features -* [#880](https://github.com/ruby-grape/grape-swagger/pull/880): Set default parameter location based on consumes - [@spaceraccoon](https://github.com/spaceraccoon) +* [#927](https://github.com/ruby-grape/grape-swagger/pull/927): Set default parameter location based on consumes - [@spaceraccoon](https://github.com/spaceraccoon) * Your contribution here. #### Fixes