Class: GrapeSwagger::DocMethods::BuildModelDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/grape-swagger/doc_methods/build_model_definition.rb

Class Method Summary collapse

Class Method Details

.build(_model, properties, required, other_def_properties = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/grape-swagger/doc_methods/build_model_definition.rb', line 7

def build(_model, properties, required, other_def_properties = {})
  definition = { type: 'object', properties: properties }.merge(other_def_properties)

  definition[:required] = required if required.is_a?(Array) && required.any?

  definition
end

.parse_params_from_model(parsed_response, model, model_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/grape-swagger/doc_methods/build_model_definition.rb', line 15

def parse_params_from_model(parsed_response, model, model_name)
  if parsed_response.is_a?(Hash) && parsed_response.keys.first == :allOf
    refs_or_models = parsed_response[:allOf]
    parsed = parse_refs_and_models(refs_or_models, model)

    {
      allOf: parsed
    }
  else
    properties, required = parsed_response
    unless properties&.any?
      raise GrapeSwagger::Errors::SwaggerSpec,
            "Empty model #{model_name}, swagger 2.0 doesn't support empty definitions."
    end
    properties, other_def_properties = parse_properties(properties)

    build(
      model, properties, required, other_def_properties
    )
  end
end

.parse_properties(properties) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/grape-swagger/doc_methods/build_model_definition.rb', line 37

def parse_properties(properties)
  other_properties = {}

  discriminator_key, discriminator_value =
    properties.find do |_key, value|
      value[:documentation].try(:[], :is_discriminator)
    end

  if discriminator_key
    discriminator_value.delete(:documentation)
    properties[discriminator_key] = discriminator_value

    other_properties[:discriminator] = discriminator_key
  end

  [properties, other_properties]
end

.parse_refs_and_models(refs_or_models, model) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/grape-swagger/doc_methods/build_model_definition.rb', line 55

def parse_refs_and_models(refs_or_models, model)
  refs_or_models.map do |ref_or_models|
    if ref_or_models.is_a?(Hash) && ref_or_models.keys.first == '$ref'
      ref_or_models
    else
      properties, required = ref_or_models
      GrapeSwagger::DocMethods::BuildModelDefinition.build(model, properties, required)
    end
  end
end