-
Notifications
You must be signed in to change notification settings - Fork 40
Add support for additional_properties
#92
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
Open
olivier-thatch
wants to merge
1
commit into
ruby-grape:master
Choose a base branch
from
olivier-thatch:olivier/additional-properties
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,73 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module AdditionalPropertiesApi | ||
| class ValueEntity < Grape::Entity | ||
| expose :name, documentation: { type: 'string' } | ||
| end | ||
|
|
||
| class WithAdditionalProperties < Grape::Entity | ||
| expose :open_map, | ||
| documentation: { type: Hash, additional_properties: true } | ||
| expose :string_map, | ||
| documentation: { type: Hash, additional_properties: String } | ||
| expose :string_map_via_type_name, | ||
| documentation: { type: 'object', additional_properties: 'string' } | ||
| expose :entity_map, | ||
| documentation: { type: Hash, additional_properties: ValueEntity } | ||
| end | ||
| end | ||
|
|
||
| describe 'additional_properties' do | ||
| subject(:swagger_doc) do | ||
| get '/swagger_doc' | ||
| JSON.parse(last_response.body) | ||
| end | ||
|
|
||
| let(:app) do | ||
| Class.new(Grape::API) do | ||
| namespace :additional_properties do | ||
| desc 'Get a thing', success: AdditionalPropertiesApi::WithAdditionalProperties | ||
| get '/thing' do | ||
| present({}, with: AdditionalPropertiesApi::WithAdditionalProperties) | ||
| end | ||
| end | ||
|
|
||
| add_swagger_documentation format: :json | ||
| end | ||
| end | ||
|
|
||
| let(:properties) { swagger_doc['definitions']['AdditionalPropertiesApi_WithAdditionalProperties']['properties'] } | ||
|
|
||
| it 'documents boolean additional_properties' do | ||
| expect(properties['open_map']).to eq('type' => 'object', 'additionalProperties' => true) | ||
| end | ||
|
|
||
| it 'documents a primitive Ruby class as a typed schema' do | ||
| expect(properties['string_map']).to eq( | ||
| 'type' => 'object', | ||
| 'additionalProperties' => { 'type' => 'string' } | ||
| ) | ||
| end | ||
|
|
||
| it 'documents a string type name as a typed schema' do | ||
| expect(properties['string_map_via_type_name']).to eq( | ||
| 'type' => 'object', | ||
| 'additionalProperties' => { 'type' => 'string' } | ||
| ) | ||
| end | ||
|
|
||
| it 'documents an entity class as a $ref' do | ||
| expect(properties['entity_map']).to eq( | ||
| 'type' => 'object', | ||
| 'additionalProperties' => { '$ref' => '#/definitions/AdditionalPropertiesApi_ValueEntity' } | ||
| ) | ||
| end | ||
|
|
||
| it 'registers the referenced entity in definitions' do | ||
| expect(swagger_doc['definitions']).to include('AdditionalPropertiesApi_ValueEntity') | ||
| expect(swagger_doc['definitions']['AdditionalPropertiesApi_ValueEntity']).to include( | ||
| 'type' => 'object', | ||
| 'properties' => { 'name' => { 'type' => 'string' } } | ||
| ) | ||
| end | ||
| end |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, we should route
additional_propertiesvalues through the same primitive type mapping as regular documented attributes before emitting the schema.The current implementation handles
String, but formatted primitives can currently emit invalid Swagger types. For example:can become:
but should be:
Same idea for
Date,DateTime,Time,BigDecimal,Integer, etc. One possible direction:This would also avoid treating any non-primitive class as an entity, which can make PORO classes go through the entity parser path and fail.
Could we add specs for
Float,Date,DateTime, string aliases like'Boolean'/'Hash', symbol aliases like:string, an entity class, and a non-entity PORO class?