Skip to content

Make entity fields required by default #81

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-09-09 22:35:29 UTC using RuboCop version 1.66.1.
# on 2025-05-08 20:42:22 UTC using RuboCop version 1.75.5.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -44,7 +44,7 @@ Metrics/AbcSize:
# Offense count: 2
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 112
Max: 117

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand All @@ -64,7 +64,7 @@ Metrics/PerceivedComplexity:
# Offense count: 5
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
# SupportedStyles: snake_case, normalcase, non_integer
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
Naming/VariableNumber:
Exclude:
- 'spec/grape-swagger/entities/response_model_spec.rb'
Expand Down Expand Up @@ -100,9 +100,9 @@ RSpec/DescribeClass:
# Offense count: 4
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 187
Max: 211

# Offense count: 24
# Offense count: 26
RSpec/LeakyConstantDeclaration:
Exclude:
- 'spec/grape-swagger/entities/response_model_spec.rb'
Expand All @@ -118,14 +118,14 @@ RSpec/MultipleDescribes:
RSpec/MultipleExpectations:
Max: 11

# Offense count: 20
# Offense count: 21
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
# SupportedStyles: always, named_only
RSpec/NamedSubject:
Exclude:
- 'spec/grape-swagger/entities/response_model_spec.rb'

# Offense count: 39
# Offense count: 40
# Configuration parameters: AllowedGroups.
RSpec/NestedGroups:
Max: 5
Expand Down
12 changes: 9 additions & 3 deletions lib/grape-swagger/entity/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,15 @@ def parse_nested(entity_name, entity_options, parent_model = nil)
end

def required_params(params)
params.select { |_, options| options.fetch(:documentation, {}).fetch(:required, false) }
.map { |(key, options)| [options.fetch(:as, key), options] }
.map(&:first)
params.each_with_object(Set.new) do |(key, options), accum|
required = if options.fetch(:documentation, {}).key?(:required)
options.dig(:documentation, :required)
else
!options.key?(:if) && !options.key?(:unless)
end

accum.add(options.fetch(:as, key)) if required
end.to_a
end

def with_required(hash, required)
Expand Down
97 changes: 66 additions & 31 deletions spec/grape-swagger/entities/response_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def app
'properties' => {
'code' => { 'type' => 'string', 'description' => 'Error code' },
'message' => { 'type' => 'string', 'description' => 'Error message' }
}
},
'required' => %w[code message]
)

expect(subject['definitions'].keys).to include 'ThisApi_Entities_Something'
Expand All @@ -66,24 +67,32 @@ def app
'code' => { 'type' => 'string', 'description' => 'Error code' },
'message' => { 'type' => 'string', 'description' => 'Error message' },
'attr' => { 'type' => 'string', 'description' => 'Attribute' } },
'required' => ['attr']
'required' => %w[text colors hidden_attr created_at kind kind2 kind3 tags relation
attr code message]
)

expect(subject['definitions'].keys).to include 'ThisApi_Entities_Kind'
expect(subject['definitions']['ThisApi_Entities_Kind']).to eq(
'type' => 'object', 'properties' => { 'title' => { 'type' => 'string', 'description' => 'Title of the kind.' },
'content' => { 'description' => 'Content', 'type' => 'string',
'x-some' => 'stuff' } }
'type' => 'object',
'properties' => {
'title' => { 'type' => 'string', 'description' => 'Title of the kind.' },
'content' => { 'type' => 'string', 'description' => 'Content', 'x-some' => 'stuff' }
},
'required' => %w[title content]
)

expect(subject['definitions'].keys).to include 'ThisApi_Entities_Relation'
expect(subject['definitions']['ThisApi_Entities_Relation']).to eq(
'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } }
'type' => 'object',
'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } },
'required' => %w[name]
)

expect(subject['definitions'].keys).to include 'ThisApi_Entities_Tag'
expect(subject['definitions']['ThisApi_Entities_Tag']).to eq(
'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } }
'type' => 'object',
'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } },
'required' => %w[name]
)
end
end
Expand Down Expand Up @@ -134,8 +143,10 @@ class Nested < Grape::Entity
end
expose :nested_required do
expose :some1, documentation: { required: true, desc: 'Required some 1' }
expose :attr, as: :some2, documentation: { required: true, desc: 'Required some 2' }
expose :some3, documentation: { desc: 'Optional some 3' }
expose :attr, as: :some2, documentation: { desc: 'Required some 2' }
expose :some3, documentation: { required: false, desc: 'Optional some 3' }
expose :some4, if: -> { true }, documentation: { desc: 'Optional some 4' }
expose :some5, unless: -> { true }, documentation: { desc: 'Optional some 5' }
end

expose :nested_array, documentation: { type: 'Array', desc: 'Nested array' } do
Expand Down Expand Up @@ -235,7 +246,8 @@ def app
'uuid' => { 'type' => 'string', 'format' => 'own', 'description' => 'customer uuid',
'example' => 'e3008fba-d53d-4bcc-a6ae-adc56dff8020' },
'color' => { 'type' => 'string', 'enum' => %w[red blue], 'description' => 'Color' }
}
},
'required' => %w[guid uuid color]
)
expect(subject['TheseApi_Entities_Kind']).to eql(
'type' => 'object',
Expand All @@ -244,30 +256,37 @@ def app
'readOnly' => true },
'title' => { 'type' => 'string', 'description' => 'Title of the kind.', 'readOnly' => false },
'type' => { 'type' => 'string', 'description' => 'Type of the kind.', 'readOnly' => true }
}
},
'required' => %w[id title type]
)
expect(subject['TheseApi_Entities_Tag']).to eql(
'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name',
'example' => 'random_tag' } }
'type' => 'object',
'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name', 'example' => 'random_tag' } },
'required' => %w[name]
)
expect(subject['TheseApi_Entities_Relation']).to eql(
'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } }
'type' => 'object',
'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } },
'required' => %w[name]
)
expect(subject['TheseApi_Entities_Nested']).to eq(
'type' => 'object',
'properties' => {
'nested' => {
'type' => 'object',
'properties' => {
'some1' => { 'type' => 'string', 'description' => 'Nested some 1' },
'some2' => { 'type' => 'string', 'description' => 'Nested some 2' }
},
'description' => 'Nested entity'
'description' => 'Nested entity',
'required' => %w[some1 some2]
},
'aliased' => {
'type' => 'object',
'properties' => {
'some1' => { 'type' => 'string', 'description' => 'Alias some 1' }
}
},
'required' => %w[some1]
},
'deep_nested' => {
'type' => 'object',
Expand All @@ -277,17 +296,21 @@ def app
'properties' => {
'level_2' => { 'type' => 'string', 'description' => 'Level 2' }
},
'description' => 'More deepest nested entity'
'description' => 'More deepest nested entity',
'required' => %w[level_2]
}
},
'description' => 'Deep nested entity'
'description' => 'Deep nested entity',
'required' => %w[level_1]
},
'nested_required' => {
'type' => 'object',
'properties' => {
'some1' => { 'type' => 'string', 'description' => 'Required some 1' },
'some2' => { 'type' => 'string', 'description' => 'Required some 2' },
'some3' => { 'type' => 'string', 'description' => 'Optional some 3' }
'some3' => { 'type' => 'string', 'description' => 'Optional some 3' },
'some4' => { 'type' => 'string', 'description' => 'Optional some 4' },
'some5' => { 'type' => 'string', 'description' => 'Optional some 5' }
},
'required' => %w[some1 some2]
},
Expand All @@ -298,14 +321,16 @@ def app
'properties' => {
'id' => { 'type' => 'integer', 'format' => 'int32', 'description' => 'Collection element id' },
'name' => { 'type' => 'string', 'description' => 'Collection element name' }
}
},
'required' => %w[id name]
},
'description' => 'Nested array'
}
},
'type' => 'object'
'required' => %w[nested aliased deep_nested nested_required nested_array]
)
expect(subject['TheseApi_Entities_NestedChild']).to eq(
'type' => 'object',
'properties' => {
'nested' => {
'type' => 'object',
Expand All @@ -314,14 +339,16 @@ def app
'some2' => { 'type' => 'string', 'description' => 'Nested some 2' },
'some3' => { 'type' => 'string', 'description' => 'Nested some 3' }
},
'description' => 'Nested entity'
'description' => 'Nested entity',
'required' => %w[some1 some2 some3]
},
'aliased' => {
'type' => 'object',
'properties' => {
'some1' => { 'type' => 'string', 'description' => 'Alias some 1' },
'some2' => { 'type' => 'string', 'description' => 'Alias some 2' }
}
},
'required' => %w[some1 some2]
},
'deep_nested' => {
'type' => 'object',
Expand All @@ -337,20 +364,25 @@ def app
'description' => 'Level 3'
}
},
'description' => 'Level 2'
'description' => 'Level 2',
'required' => %w[level_3]
}
},
'description' => 'More deepest nested entity'
'description' => 'More deepest nested entity',
'required' => %w[level_2]
}
},
'description' => 'Deep nested entity'
'description' => 'Deep nested entity',
'required' => %w[level_1]
},
'nested_required' => {
'type' => 'object',
'properties' => {
'some1' => { 'type' => 'string', 'description' => 'Required some 1' },
'some2' => { 'type' => 'string', 'description' => 'Required some 2' },
'some3' => { 'type' => 'string', 'description' => 'Optional some 3' }
'some3' => { 'type' => 'string', 'description' => 'Optional some 3' },
'some4' => { 'type' => 'string', 'description' => 'Optional some 4' },
'some5' => { 'type' => 'string', 'description' => 'Optional some 5' }
},
'required' => %w[some1 some2]
},
Expand All @@ -362,12 +394,13 @@ def app
'id' => { 'type' => 'integer', 'format' => 'int32', 'description' => 'Collection element id' },
'name' => { 'type' => 'string', 'description' => 'Collection element name' },
'category' => { 'type' => 'string', 'description' => 'Collection element category' }
}
},
'required' => %w[id name category]
},
'description' => 'Nested array'
}
},
'type' => 'object'
'required' => %w[nested aliased deep_nested nested_required nested_array]
)
expect(subject['TheseApi_Entities_Polymorphic']).to eql(
'type' => 'object',
Expand All @@ -380,14 +413,15 @@ def app
)

expect(subject['TheseApi_Entities_MixedType']).to eql(
'type' => 'object',
'properties' => {
'tags' => {
'description' => 'Tags',
'items' => { '$ref' => '#/definitions/TheseApi_Entities_TagType' },
'type' => 'array'
}
},
'type' => 'object'
'required' => %w[tags]
)

expect(subject['TheseApi_Entities_SomeEntity']).to eql(
Expand All @@ -414,7 +448,8 @@ def app
},
'attr' => { 'type' => 'string', 'description' => 'Attribute' }
},
'required' => %w[attr],
'required' => %w[text kind kind2 kind3 tags relation values nested nested_child
polymorphic mixed attr code message],
'description' => 'TheseApi_Entities_SomeEntity model'
)
end
Expand Down