Skip to content

allows custom format, for params and definition properties #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [#567](https://github.com/ruby-grape/grape-swagger/pull/567): Issue#566: removes markdown - [@LeFnord](https://github.com/LeFnord).
* [#568](https://github.com/ruby-grape/grape-swagger/pull/568): Adds code coverage w/ coveralls - [@LeFnord](https://github.com/LeFnord).
* [#570](https://github.com/ruby-grape/grape-swagger/pull/570): Removes dead code -> increases code coverage - [@LeFnord](https://github.com/LeFnord).
* [#576](https://github.com/ruby-grape/grape-swagger/pull/576): Allows custom format, for params and definition properties - [@LeFnord](https://github.com/LeFnord).

* Your contribution here.

Expand Down
5 changes: 4 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Upgrading Grape-swagger

### Upgrading to >= 0.26.0
### Upgrading to >= 0.26.1

The format can now be specified,
to achieve it for definition properties one have to use grape-swagger-entity >= 0.1.6.

Usage of option `markdown` won't no longer be supported,
cause OAPI accepts [GFM](https://help.github.com/articles/github-flavored-markdown) and plain text.
Expand Down
5 changes: 3 additions & 2 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call(param, settings, route, definitions)

# optional properties
document_description(settings)
document_type_and_format(data_type)
document_type_and_format(settings, data_type)
document_array_param(value_type, definitions) if value_type[:is_array]
document_default_value(settings)
document_range_values(settings)
Expand Down Expand Up @@ -51,13 +51,14 @@ def document_default_value(settings)
@parsed_param[:default] = settings[:default] if settings[:default].present?
end

def document_type_and_format(data_type)
def document_type_and_format(settings, data_type)
if DataType.primitive?(data_type)
data = DataType.mapping(data_type)
@parsed_param[:type], @parsed_param[:format] = data
else
@parsed_param[:type] = data_type
end
@parsed_param[:format] = settings[:format] if settings[:format].present?
end

def document_array_param(value_type, definitions)
Expand Down
36 changes: 36 additions & 0 deletions spec/issues/532_allow_custom_format_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe '#542 array of type in post params' do
let(:app) do
Class.new(Grape::API) do
namespace :issue_532 do
params do
requires :logs, type: String, documentation: { format: 'log' }
optional :phone_number, type: Integer, documentation: { format: 'phone_number' }
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(:parameters) { subject['paths']['/issue_532']['post']['parameters'] }

specify do
expect(parameters).to eql(
[
{ 'in' => 'formData', 'name' => 'logs', 'type' => 'string', 'format' => 'log', 'required' => true },
{ 'in' => 'formData', 'name' => 'phone_number', 'type' => 'integer', 'format' => 'phone_number', 'required' => false }
]
)
end
end
21 changes: 18 additions & 3 deletions spec/issues/537_enum_values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
Class.new(Grape::API) do
namespace :issue_539 do
class Spec < Grape::Entity
expose :test_property, documentation: { values: [:foo, :bar] }
expose :enum_property, documentation: { values: [:foo, :bar] }
expose :enum_property_default, documentation: { values: %w(a b c), default: 'c' }
expose :own_format, documentation: { format: 'log' }
end

desc 'create account',
Expand All @@ -23,10 +25,23 @@ class Spec < Grape::Entity
JSON.parse(last_response.body)
end

let(:property) { subject['definitions']['Spec']['properties']['test_property'] }

let(:property) { subject['definitions']['Spec']['properties']['enum_property'] }
specify do
expect(property).to include 'enum'
expect(property['enum']).to eql %w(foo bar)
end

let(:property_default) { subject['definitions']['Spec']['properties']['enum_property_default'] }
specify do
expect(property_default).to include 'enum'
expect(property_default['enum']).to eql %w(a b c)
expect(property_default).to include 'default'
expect(property_default['default']).to eql 'c'
end

let(:own_format) { subject['definitions']['Spec']['properties']['own_format'] }
specify do
expect(own_format).to include 'format'
expect(own_format['format']).to eql 'log'
end
end