Skip to content

Commit

Permalink
Add extension properties to schema for body params (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
numbata authored Jan 22, 2024
1 parent cb92e17 commit de6f094
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#### Fixes

* Your contribution here.
* [#918](https://github.com/ruby-grape/grape-swagger/pull/918): Fix params extension does not work when param_type is body - [@numbata](https://github.com/numbata)

### 2.0.1 (Januar 2, 2024)

Expand Down
7 changes: 7 additions & 0 deletions lib/grape-swagger/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def build_properties(params)
else
document_as_property(param)
end
add_extension_properties(properties[name], param)

required << name if deletable?(param) && param[:required]
end
Expand All @@ -102,6 +103,12 @@ def document_as_array(param)
end
end

def add_extension_properties(definition, values)
values.each do |key, value|
definition[key] = value if key.start_with?('x-')
end
end

def document_as_property(param)
property_keys.each_with_object({}) do |x, memo|
next unless param.key?(x)
Expand Down
4 changes: 4 additions & 0 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def parse_array_item(definitions, type, value_type)
set_additional_properties, additional_properties = parse_additional_properties(definitions, value_type)
array_items[:additionalProperties] = additional_properties if set_additional_properties

if value_type.key?(:items)
GrapeSwagger::DocMethods::Extensions.add_extensions_to_root(value_type[:items], array_items)
end

array_items
end

Expand Down
49 changes: 49 additions & 0 deletions spec/issues/901_extensions_on_body_params_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require 'spec_helper'

describe '#901 params extension does not work when param_type is body' do
let(:app) do
Class.new(Grape::API) do
namespace :issue_901 do
params do
requires :user_id, type: Integer, documentation: { type: 'integer', param_type: 'body', x: { nullable: true } }
requires :friend_ids, type: [Integer], documentation: { type: 'integer', is_array: true, param_type: 'body', x: { type: 'array' }, items: { x: { type: 'item' } } }
requires :address, type: Hash, documentation: { type: 'object', param_type: 'body', x: { type: 'address' } } do
requires :city_id, type: Integer, documentation: { type: 'integer', x: { type: 'city' } }
end
end
post do
present params
end
end

add_swagger_documentation format: :json
end
end

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

let(:definition) { subject['definitions']['postIssue901'] }

specify do
expect(definition['properties']).to match(
'user_id' => hash_including('type' => 'integer', 'x-nullable' => true),
'address' => hash_including(
'type' => 'object',
'x-type' => 'address',
'properties' => {
'city_id' => hash_including('type' => 'integer', 'x-type' => 'city')
}
),
'friend_ids' => hash_including(
'type' => 'array',
'x-type' => 'array',
'items' => hash_including('type' => 'integer', 'x-type' => 'item')
)
)
end
end

0 comments on commit de6f094

Please sign in to comment.