Skip to content

Commit

Permalink
Add required with multiple present (#815)
Browse files Browse the repository at this point in the history
* Add required with multiple present

* Change Readme

* Fix rubocop to 1.6.1

* Fix Changelog

* Up rubocop
  • Loading branch information
MaximeRVY committed Jan 7, 2021
1 parent 80b96c0 commit 5c71861
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
#### Features

* Your contribution here.
* [#815](https://github.com/ruby-grape/grape-swagger/pull/815): Add required for multiple presents - [@MaximeRDY](https://github.com/MaximeRDY).

#### Fixes

Expand Down
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -1302,7 +1302,7 @@ You can also specify if the response is an array, with the `is_array` key:
desc 'Multiple response with array',
success: [
{ model: Entities::EnumValues, as: :gender },
{ model: Entities::Something, as: :somethings, is_array: true }
{ model: Entities::Something, as: :somethings, is_array: true, required: true }
]
end

Expand All @@ -1327,7 +1327,8 @@ The result will look like following:
"$ref":"#/definitions/Something"
}
}
}
},
"required": ["somethings"]
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions lib/grape-swagger/endpoint.rb
Expand Up @@ -270,11 +270,18 @@ def tag_object(route, path)
def build_memo_schema(memo, route, value, response_model, options)
if memo[value[:code]][:schema] && value[:as]
memo[value[:code]][:schema][:properties].merge!(build_reference(route, value, response_model, options))

if value[:required]
memo[value[:code]][:schema][:required] ||= []
memo[value[:code]][:schema][:required] << value[:as].to_s
end

elsif value[:as]
memo[value[:code]][:schema] = {
type: :object,
properties: build_reference(route, value, response_model, options)
}
memo[value[:code]][:schema][:required] = [value[:as].to_s] if value[:required]
else
memo[value[:code]][:schema] = build_reference(route, value, response_model, options)
end
Expand Down Expand Up @@ -413,6 +420,7 @@ def success_code_from_entity(route, entity)
default_code[:headers] = entity[:headers] if entity[:headers]
default_code[:as] = entity[:as] if entity[:as]
default_code[:is_array] = entity[:is_array] if entity[:is_array]
default_code[:required] = entity[:required] if entity[:required]
else
default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
default_code[:model] = entity if entity
Expand Down
2 changes: 2 additions & 0 deletions lib/grape-swagger/errors.rb
Expand Up @@ -3,7 +3,9 @@
module GrapeSwagger
module Errors
class UnregisteredParser < StandardError; end

class SwaggerSpec < StandardError; end

class SwaggerSpecDeprecated < SwaggerSpec
class << self
def tell!(what)
Expand Down
7 changes: 5 additions & 2 deletions spec/issues/776_multiple_presents_spec.rb
Expand Up @@ -11,7 +11,7 @@
desc 'Get multiple presents',
success: [
{ model: Entities::EnumValues, as: :gender },
{ model: Entities::Something, as: :somethings, is_array: true }
{ model: Entities::Something, as: :somethings, is_array: true, required: true }
]

get do
Expand Down Expand Up @@ -50,7 +50,10 @@
'$ref' => '#/definitions/EnumValues'
}
},
'type' => 'object'
'type' => 'object',
'required' => [
'somethings'
]
})
end
end
2 changes: 2 additions & 0 deletions spec/support/empty_model_parser.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true

# rubocop:disable Lint/EmptyClass
class EmptyClass
end
# rubocop:enable Lint/EmptyClass

module GrapeSwagger
class EmptyModelParser
Expand Down
16 changes: 16 additions & 0 deletions spec/support/model_parsers/mock_parser.rb
Expand Up @@ -40,21 +40,37 @@ def documentation
end

class UseNestedWithAddress < OpenStruct; end

class TypedDefinition < OpenStruct; end

class UseItemResponseAsType < OpenStruct; end

class OtherItem < OpenStruct; end

class EnumValues < OpenStruct; end

class AliasedThing < OpenStruct; end

class FourthLevel < OpenStruct; end

class ThirdLevel < OpenStruct; end

class SecondLevel < OpenStruct; end

class FirstLevel < OpenStruct; end

class QueryInputElement < OpenStruct; end

class QueryInput < OpenStruct; end

class ApiError < OpenStruct; end

class SecondApiError < OpenStruct; end

class RecursiveModel < OpenStruct; end

class DocumentedHashAndArrayModel < OpenStruct; end

module NestedModule
class ApiResponse < OpenStruct; end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/support/namespace_tags.rb
Expand Up @@ -3,7 +3,9 @@
RSpec.shared_context 'namespace example' do
before :all do
module TheApi
# rubocop:disable Lint/EmptyClass
class CustomType; end
# rubocop:enable Lint/EmptyClass

class NamespaceApi < Grape::API
namespace :hudson do
Expand Down
1 change: 1 addition & 0 deletions spec/swagger_v2/inheritance_and_discriminator_spec.rb
Expand Up @@ -33,6 +33,7 @@ class Cat < Pet
}
end
end

class NameApi < Grape::API
add_swagger_documentation models: [Entities::Pet, Entities::Cat]
end
Expand Down
2 changes: 2 additions & 0 deletions spec/swagger_v2/simple_mounted_api_spec.rb
Expand Up @@ -4,7 +4,9 @@

describe 'a simple mounted api' do
before :all do
# rubocop:disable Lint/EmptyClass
class CustomType; end
# rubocop:enable Lint/EmptyClass

class SimpleMountedApi < Grape::API
desc 'Document root'
Expand Down

0 comments on commit 5c71861

Please sign in to comment.