Module: Swagger::Blocks::Nodes::SchemaBuilder

Extended by:
ActiveSupport::Concern
Included in:
ItemsNode, PropertyNode, SchemaNode
Defined in:
lib/swagger/blocks/schema_builder.rb

Constant Summary collapse

SUPPORTED_TYPES =
%w(Mongoid::Boolean
BSON::ObjectId
Object
Time
String
Integer
Array
Date
Symbol).freeze

Instance Method Summary collapse

Instance Method Details

#build_model_schema(model_class, include_only_required_fields = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/swagger/blocks/schema_builder.rb', line 27

def build_model_schema(model_class, include_only_required_fields=false)
  required_fields = get_required_fields(model_class)

  key :required, required_fields

  model_class.fields.each do |name, options|
    type = options.type.to_s
    defaul_value = options.options[:default]

    next unless SUPPORTED_TYPES.include?(type)

    if include_only_required_fields
      next if name == '_id'
      next unless required_fields.include?(name.to_sym)
    end

    property name do
      case type
      when 'Symbol'
        klass = options.options[:klass].to_s
        constant = name.sub('_', '').upcase
        values = "#{klass}::#{constant}".constantize

        key :type, :string
        key :enum, values

      when 'Array'
        key :type, :array
        # TODO: autodetect type of Array Item
        items do
          key :type, :string
        end

      when 'BSON::ObjectId'
        key :type, :string
        key :format, :uuid

      when 'Date'
        key :type, :string
        key :format, :date

      when 'Time'
        key :type, :string
        key :format, 'date-time'

      when 'Mongoid::Boolean'
        key :type, :boolean
        key :default, defaul_value

      when 'Integer'
        key :type, :integer
        key :default, defaul_value.to_i

      else
        key :type, :string
        key :default, defaul_value.to_s

      end
    end
  end
end

#get_required_fields(model_class) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/swagger/blocks/schema_builder.rb', line 17

def get_required_fields(model_class)
  presence_validators = model_class.
    validators.
    select { |v| v.class == Mongoid::Validatable::PresenceValidator }

  required_fields = presence_validators.map { |v| v.attributes.first }
  required_fields << '_id'
  required_fields
end