-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a setting for disabling documentation to internal APIs
Our application has 3 APIs, 2 of them are internal and don't require documentations. However, there has not been any option to prevent Grape from documenting parameters for internal APIs. This change adds a `do_not_document!` setting which instructs Grape to not document parameters, thus needless objects allocation is avoided. class Api < Grape::API do_not_document! end The logic for documenting parameters was moved to a separate class, the `Grape::Validations::ParamsScope` class has to many responsibilities, it would be better to split it.
- Loading branch information
1 parent
6a10a1e
commit 1c65d8b
Showing
11 changed files
with
307 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module Grape | ||
module Validations | ||
class ParamsScope | ||
# Documents parameters of an endpoint. If documentation isn't needed (for instance, it is an | ||
# internal API), the class only cleans up attributes to avoid junk in RAM. | ||
class AttributesDoc | ||
attr_accessor :type, :values | ||
|
||
# @param api [Grape::API::Instance] | ||
# @param scope [Validations::ParamsScope] | ||
def initialize(api, scope) | ||
@api = api | ||
@scope = scope | ||
@type = type | ||
end | ||
|
||
def extract_details(validations) | ||
details[:required] = validations.key?(:presence) | ||
|
||
desc = validations.delete(:desc) || validations.delete(:description) | ||
|
||
details[:desc] = desc if desc | ||
|
||
documentation = validations.delete(:documentation) | ||
|
||
details[:documentation] = documentation if documentation | ||
|
||
details[:default] = validations[:default] if validations.key?(:default) | ||
end | ||
|
||
def document(attrs) | ||
return if @api.namespace_inheritable(:do_not_document) | ||
|
||
details[:type] = type.to_s if type | ||
details[:values] = values if values | ||
|
||
documented_attrs = attrs.each_with_object({}) do |name, memo| | ||
memo[@scope.full_name(name)] = details | ||
end | ||
|
||
@api.namespace_stackable(:params, documented_attrs) | ||
end | ||
|
||
def required | ||
details[:required] | ||
end | ||
|
||
protected | ||
|
||
def details | ||
@details ||= {} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe Grape::API do | ||
subject { Class.new(described_class) } | ||
|
||
let(:app) { subject } | ||
|
||
context 'an endpoint with documentation' do | ||
it 'documents parameters' do | ||
subject.params do | ||
requires 'price', type: Float, desc: 'Sales price' | ||
end | ||
subject.get '/' | ||
|
||
expect(subject.routes.first.params['price']).to eq(required: true, | ||
type: 'Float', | ||
desc: 'Sales price') | ||
end | ||
|
||
it 'allows documentation with a hash' do | ||
documentation = { example: 'Joe' } | ||
|
||
subject.params do | ||
requires 'first_name', documentation: documentation | ||
end | ||
subject.get '/' | ||
|
||
expect(subject.routes.first.params['first_name'][:documentation]).to eq(documentation) | ||
end | ||
end | ||
|
||
context 'an endpoint without documentation' do | ||
before do | ||
subject.do_not_document! | ||
|
||
subject.params do | ||
requires :city, type: String, desc: 'Should be ignored' | ||
optional :postal_code, type: Integer | ||
end | ||
subject.post '/' do | ||
declared(params).to_json | ||
end | ||
end | ||
|
||
it 'does not document parameters for the endpoint' do | ||
expect(subject.routes.first.params).to eq({}) | ||
end | ||
|
||
it 'still declares params internally' do | ||
data = { city: 'Berlin', postal_code: 10_115 } | ||
|
||
post '/', data | ||
|
||
expect(last_response.body).to eq(data.to_json) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.