Skip to content

Commit 7baad46

Browse files
authored
Remove hidden attributes from required params (#87)
Addresses an issue where hidden attributes were incorrectly being marked as required in the generated Swagger documentation. The main changes ensure that attributes marked as hidden are excluded from the required fields list.
1 parent dd9a3e5 commit 7baad46

5 files changed

Lines changed: 12 additions & 5 deletions

File tree

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Metrics/AbcSize:
3737
# Offense count: 2
3838
# Configuration parameters: CountComments, CountAsOne.
3939
Metrics/ClassLength:
40-
Max: 117
40+
Max: 120
4141

4242
# Offense count: 2
4343
# Configuration parameters: AllowedMethods, AllowedPatterns.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#87](https://github.com/ruby-grape/grape-swagger-entity/pull/87): Remove hidden attributes from required - [@bogdan](https://github.com/bogdan).
1011
* [#88](https://github.com/ruby-grape/grape-swagger-entity/pull/88): Update danger workflows - [@numbata](https://github.com/numbata).
1112

1213
### 0.7.0 (2025/08/02)

lib/grape-swagger/entity/parser.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ def parse_nested(entity_name, entity_options, parent_model = nil)
136136

137137
def required_params(params)
138138
params.each_with_object(Set.new) do |(key, options), accum|
139-
required = if options.fetch(:documentation, {}).key?(:required)
140-
options.dig(:documentation, :required)
139+
documentation = options.fetch(:documentation, {})
140+
next if documentation[:hidden]
141+
142+
required = if documentation.key?(:required)
143+
documentation[:required]
141144
else
142145
!options.key?(:if) && !options.key?(:unless) && options[:expose_nil] != false
143146
end

spec/grape-swagger/entities/response_model_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ def app
6767
'code' => { 'type' => 'string', 'description' => 'Error code' },
6868
'message' => { 'type' => 'string', 'description' => 'Error message' },
6969
'attr' => { 'type' => 'string', 'description' => 'Attribute' } },
70-
'required' => %w[text colors hidden_attr created_at kind kind2 kind3 tags relation
71-
attr code message]
70+
'required' => %w[text colors created_at kind kind2 kind3 tags relation attr code message]
7271
)
7372

7473
expect(subject['definitions'].keys).to include 'ThisApi_Entities_Kind'

spec/grape-swagger/entity/parser_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
expect(properties[:kind3]['$ref']).to eq('#/definitions/Kind')
2222
end
2323

24+
it 'does not mark hidden attributes as required' do
25+
expect(required).not_to include(:hidden_attr)
26+
end
27+
2428
it 'merges attributes that have merge: true defined' do
2529
expect(properties[:merged_attribute]).to be_nil
2630
expect(properties[:code][:type]).to eq('string')

0 commit comments

Comments
 (0)