Module: Gitlab::GrapeOpenapi::GrapeCompat
- Defined in:
- lib/gitlab/grape_openapi/grape_compat.rb
Overview
Isolates the Grape internals this gem reads so version differences live in one place.
Grape < 3.2 stores each declared validation as a Hash; Grape >= 3.2 stores a frozen validator instance instead (ruby-grape/grape#2657). Both shapes are normalized to the Hash the converters expect.
Class Method Summary collapse
-
.route_param_desc(route, name) ⇒ Object
The
desc:an author writes onroute_param. -
.validations_for(route, attribute) ⇒ Object
Declared validations for a single attribute, newest scope only.
Class Method Details
.route_param_desc(route, name) ⇒ Object
The desc: an author writes on route_param. Grape forwards only type: into
the requires it declares internally and discards the rest, so the description
never reaches the route's params. It survives on the namespace whose space is
the :placeholder segment. Searched innermost first.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/gitlab/grape_openapi/grape_compat.rb', line 32 def route_param_desc(route, name) return unless route.app.respond_to?(:inheritable_setting) namespaces = route.app.inheritable_setting&.namespace_stackable&.[](:namespace) || [] namespace = namespaces.reverse.find { |candidate| candidate.space.to_s == ":#{name}" } = namespace&. return unless .is_a?(Hash) desc = [:desc] desc if desc.is_a?(String) end |
.validations_for(route, attribute) ⇒ Object
Declared validations for a single attribute, newest scope only.
Reads new_values rather than [] or route[:saved_validations] on
purpose: those also include validations inherited from parent scopes,
which would change the generated output.
18 19 20 21 22 23 24 25 26 |
# File 'lib/gitlab/grape_openapi/grape_compat.rb', line 18 def validations_for(route, attribute) validations = route.app.inheritable_setting.namespace_stackable.new_values[:validations] return unless validations validations.filter_map do |validation| normalized = normalize(validation) normalized if normalized && normalized[:attributes].include?(attribute) end end |