-
-
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.
Merge pull request #2233 from dnesteryuk/feature/endpoints-without-docs
a setting for disabling documentation to internal APIs
- Loading branch information
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.