Module: Gitlab::GrapeOpenapi::Converters::CoercerResolver

Included in:
ParameterConverter, Models::RequestBody::ParameterSchema
Defined in:
lib/gitlab/grape_openapi/converters/coercer_resolver.rb

Instance Method Summary collapse

Instance Method Details

#build_coerced_schema(mapping) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/grape_openapi/converters/coercer_resolver.rb', line 23

def build_coerced_schema(mapping)
  schema = {}
  schema[:type] = mapping[:type] if mapping[:type]
  schema[:items] = { type: mapping[:items_type] } if mapping[:items_type]
  schema[:format] = mapping[:format] if mapping[:format]
  schema[:additionalProperties] = mapping[:additional_properties] if mapping[:additional_properties]

  schema
end

#coercer_mapping_for(validations) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gitlab/grape_openapi/converters/coercer_resolver.rb', line 7

def coercer_mapping_for(validations)
  coercer_method = extract_coercer_method(validations)
  return unless coercer_method # Validation doesn't use `coerce_with`
  return if inline_proc?(coercer_method) # Inline Procs shouldn't need a coercer method

  coercer_name = resolve_coercer_name(coercer_method)
  config = Gitlab::GrapeOpenapi.configuration
  mapping = config.coercer_mappings.find { |pattern, _mapping| coercer_name == pattern }&.last
  return mapping if mapping

  raise GenerationError,
    "No OpenAPI schema mapping found for coercer '#{coercer_name}'. " \
      "Add an entry for '#{coercer_name}' to coercer_mappings in " \
      "config/initializers/gitlab_grape_openapi.rb, or use an inline lambda instead."
end