Skip to content

Commit

Permalink
Set default parameter location based on consumes
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceraccoon committed Nov 11, 2022
1 parent 1eb9fe6 commit 41c6d3a
Show file tree
Hide file tree
Showing 30 changed files with 320 additions and 97 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#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
Expand Down
8 changes: 4 additions & 4 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -144,14 +144,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
Expand Down
6 changes: 3 additions & 3 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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[: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))
Expand Down Expand Up @@ -174,7 +174,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)

Expand All @@ -186,7 +186,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)
Expand Down
4 changes: 4 additions & 0 deletions spec/issues/532_allow_custom_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
16 changes: 12 additions & 4 deletions spec/issues/553_align_array_put_post_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
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
post do
# 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]
Expand All @@ -25,15 +29,19 @@
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
post do
# 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' }
Expand Down
30 changes: 18 additions & 12 deletions spec/issues/579_align_put_post_parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ 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
end

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -68,17 +72,19 @@ 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
end

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
Expand Down
6 changes: 6 additions & 0 deletions spec/issues/650_params_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/issues/784_extensions_on_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' } }
Expand Down
3 changes: 3 additions & 0 deletions spec/issues/832_array_hash_float_decimal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/support/model_parsers/entity_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 },
Expand Down
4 changes: 2 additions & 2 deletions spec/support/model_parsers/mock_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 },
Expand Down
4 changes: 2 additions & 2 deletions spec/support/model_parsers/representable_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 },
Expand Down
12 changes: 9 additions & 3 deletions spec/swagger_v2/api_swagger_v2_hide_param_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 41c6d3a

Please sign in to comment.