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
14
15
16
17
18
# 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)

  if required.nil?
    required_attrs = required_attributes(model)
    definition[:required] = required_attrs unless required_attrs.blank?
  end

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

  definition
end

.parse_params_from_model(parsed_response, model, model_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/grape-swagger/doc_methods/build_model_definition.rb', line 20

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/grape-swagger/doc_methods/build_model_definition.rb', line 42

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/grape-swagger/doc_methods/build_model_definition.rb', line 60

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