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/CHANGELOG.md b/CHANGELOG.md index 09dbbbf8c..6de919671 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### Features +* [#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 diff --git a/lib/grape-swagger/doc_methods/parse_params.rb b/lib/grape-swagger/doc_methods/parse_params.rb index cad75083d..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) + 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) @@ -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,18 @@ 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' + if consumes.include?('application/x-www-form-urlencoded') || consumes.include?('multipart/form-data') + 'formData' + else + 'body' + end 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..466f06565 100644 --- a/spec/issues/579_align_put_post_parameters_spec.rb +++ b/spec/issues/579_align_put_post_parameters_spec.rb @@ -21,6 +21,7 @@ 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 put ':guid' do @@ -30,6 +31,7 @@ class Spec < Grape::Entity namespace :form_parameter do desc 'update spec', + consumes: ['application/x-www-form-urlencoded'], success: Spec, params: Spec.documentation put ':guid' do @@ -41,6 +43,7 @@ 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 params do @@ -53,6 +56,7 @@ class Spec < Grape::Entity namespace :form_parameter do desc 'update spec', + consumes: ['application/x-www-form-urlencoded'], success: Spec, params: Spec.documentation params do @@ -68,6 +72,7 @@ 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 put do @@ -77,6 +82,7 @@ class Spec < Grape::Entity namespace :form_parameter do desc 'update spec', + consumes: ['application/x-www-form-urlencoded'], success: Spec, params: Spec.documentation put do 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..c0ce06af2 --- /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..b1400edcb 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,6 +46,7 @@ def app end desc 'This creates Thing.', + consumes: ['application/x-www-form-urlencoded'], success: Entities::Something params do requires :text, type: String, documentation: { type: 'string', desc: 'Content of something.' } @@ -54,6 +58,7 @@ def app end desc 'This updates Thing.', + consumes: ['application/x-www-form-urlencoded'], success: Entities::Something params do requires :id, type: Integer @@ -66,6 +71,7 @@ def app end desc 'This deletes Thing.', + consumes: ['application/x-www-form-urlencoded'], entity: Entities::Something params do requires :id, type: Integer @@ -76,6 +82,7 @@ def app end desc 'dummy route.', + consumes: ['application/x-www-form-urlencoded'], failure: [{ code: 401, message: 'Unauthorized' }] params do requires :id, type: Integer @@ -86,6 +93,7 @@ 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' }, 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..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,6 +10,7 @@ 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 params do optional :in_query, type: String, documentation: { param_type: 'query' } @@ -21,6 +22,7 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types', + consumes: ['application/x-www-form-urlencoded'], success: Entities::UseResponse params do requires :in_path, type: Integer @@ -33,6 +35,7 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types', + consumes: ['application/x-www-form-urlencoded'], success: Entities::UseResponse params do optional :in_path, type: Integer @@ -46,6 +49,7 @@ class ParamTypeApi < Grape::API # using `:in` desc 'full set of request param types using `:in`', + consumes: ['application/x-www-form-urlencoded'], success: Entities::UseResponse params do optional :in_query, type: String, documentation: { in: 'query' } @@ -57,6 +61,7 @@ class ParamTypeApi < Grape::API end desc 'full set of request param types using `:in`', + consumes: ['application/x-www-form-urlencoded'], success: Entities::UseResponse params do requires :in_path, type: Integer @@ -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,6 +87,7 @@ class ParamTypeApi < Grape::API # file desc 'file download', + consumes: ['application/x-www-form-urlencoded'], success: Entities::UseResponse params do requires :name, type: String @@ -91,6 +98,7 @@ class ParamTypeApi < Grape::API end desc 'file upload', + consumes: ['application/x-www-form-urlencoded'], success: Entities::UseResponse params do requires :name, type: File 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..4374fa34e 100644 --- a/spec/swagger_v2/api_swagger_v2_response_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_response_spec.rb @@ -11,6 +11,7 @@ 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 }] post '/params_given' do @@ -18,6 +19,7 @@ class ResponseApi < Grape::API end desc 'This returns something', + consumes: ['application/x-www-form-urlencoded'], entity: Entities::UseResponse, failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] get '/entity_response' do @@ -25,6 +27,7 @@ class ResponseApi < Grape::API end desc 'This returns something', + consumes: ['application/x-www-form-urlencoded'], entity: Entities::UseItemResponseAsType, failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] get '/nested_type' do @@ -32,6 +35,7 @@ class ResponseApi < Grape::API end desc 'This returns something', + consumes: ['application/x-www-form-urlencoded'], success: [ { code: 200, message: 'Request has succeeded' }, { code: 201, message: 'Successful Operation' }, @@ -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..5f6c523f0 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,6 +45,7 @@ def app end desc 'This creates Thing.', + consumes: ['application/x-www-form-urlencoded'], success: Entities::Something params do requires :text, type: String, documentation: { type: 'string', desc: 'Content of something.' } @@ -53,6 +57,7 @@ def app end desc 'This updates Thing.', + consumes: ['application/x-www-form-urlencoded'], success: Entities::Something params do requires :id, type: Integer @@ -65,6 +70,7 @@ def app end desc 'This deletes Thing.', + consumes: ['application/x-www-form-urlencoded'], entity: Entities::Something params do requires :id, type: Integer @@ -75,6 +81,7 @@ def app end desc 'dummy route.', + consumes: ['application/x-www-form-urlencoded'], failure: [{ code: 401, message: 'Unauthorized' }] params do requires :id, type: Integer @@ -85,6 +92,7 @@ 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' }, 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..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,6 +28,7 @@ module TheApi class TypeFormatApi < Grape::API desc 'full set of request data types', + consumes: ['application/x-www-form-urlencoded'], success: Entities::TypedDefinition params do 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..afcc10b0a 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..4dde18f98 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..510309b21 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 @@ -120,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] 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..d3a919a96 100644 --- a/spec/swagger_v2/simple_mounted_api_spec.rb +++ b/spec/swagger_v2/simple_mounted_api_spec.rb @@ -9,12 +9,14 @@ 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.', + consumes: ['application/x-www-form-urlencoded'], notes: '_test_' get '/simple' do @@ -22,6 +24,7 @@ class SimpleMountedApi < Grape::API end desc 'This gets something for URL using - separator.', + consumes: ['application/x-www-form-urlencoded'], notes: '_test_' get '/simple-test' do @@ -37,6 +40,7 @@ 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 } @@ -51,6 +55,7 @@ class SimpleMountedApi < Grape::API end desc 'this takes an array of parameters', + consumes: ['application/x-www-form-urlencoded'], params: { 'items[]' => { description: 'array of items', is_array: true } } @@ -60,6 +65,7 @@ class SimpleMountedApi < Grape::API end desc 'this uses a custom parameter', + consumes: ['application/x-www-form-urlencoded'], params: { 'custom' => { type: CustomType, description: 'array of items', is_array: true } } @@ -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',