Class: GrapeOAS::Exporter::OAS3::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_oas/exporter/oas3/schema.rb

Direct Known Subclasses

GrapeOAS::Exporter::OAS31::Schema

Instance Method Summary collapse

Constructor Details

#initialize(schema, ref_tracker = nil, nullable_strategy: Constants::NullableStrategy::KEYWORD) ⇒ Schema

Returns a new instance of Schema.



7
8
9
10
11
# File 'lib/grape_oas/exporter/oas3/schema.rb', line 7

def initialize(schema, ref_tracker = nil, nullable_strategy: Constants::NullableStrategy::KEYWORD)
  @schema = schema
  @ref_tracker = ref_tracker
  @nullable_strategy = nullable_strategy
end

Instance Method Details

#apply_all_constraints(schema_hash, schema = @schema) ⇒ Object



76
77
78
79
80
# File 'lib/grape_oas/exporter/oas3/schema.rb', line 76

def apply_all_constraints(schema_hash, schema = @schema)
  apply_numeric_constraints(schema_hash, schema)
  apply_string_constraints(schema_hash, schema)
  apply_array_constraints(schema_hash, schema)
end

#apply_examples(schema_hash) ⇒ Object



58
59
60
61
62
63
# File 'lib/grape_oas/exporter/oas3/schema.rb', line 58

def apply_examples(schema_hash)
  return unless @schema.examples

  examples = Array(@schema.examples).map { |ex| coerce_example(ex, schema_hash["type"]) }
  schema_hash["example"] = examples.first
end

#apply_extensions_and_extra_properties(schema_hash) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/grape_oas/exporter/oas3/schema.rb', line 65

def apply_extensions_and_extra_properties(schema_hash)
  schema_hash.merge!(@schema.extensions) if @schema.extensions
  schema_hash.delete("properties") if schema_hash["properties"]&.empty? || @schema.type != Constants::SchemaTypes::OBJECT
  schema_hash["additionalProperties"] = @schema.additional_properties unless @schema.additional_properties.nil?
  if @nullable_strategy == Constants::NullableStrategy::TYPE_ARRAY
    schema_hash["unevaluatedProperties"] = @schema.unevaluated_properties unless @schema.unevaluated_properties.nil?
    schema_hash["$defs"] = @schema.defs if @schema.defs && !@schema.defs.empty?
  end
  schema_hash["discriminator"] = build_discriminator if @schema.discriminator
end

#buildObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/grape_oas/exporter/oas3/schema.rb', line 13

def build
  return {} unless @schema
  return build_all_of_schema if @schema.all_of && !@schema.all_of.empty?
  return build_one_of_schema if @schema.one_of && !@schema.one_of.empty?
  return build_any_of_schema if @schema.any_of && !@schema.any_of.empty?

  schema_hash = build_base_hash
  apply_examples(schema_hash)
  sanitize_enum_against_type(schema_hash)
  apply_extensions_and_extra_properties(schema_hash)
  apply_all_constraints(schema_hash)
  schema_hash.compact
end

#build_base_hashObject



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
# File 'lib/grape_oas/exporter/oas3/schema.rb', line 27

def build_base_hash
  schema_hash = {}
  schema_hash["type"] = nullable_type
  schema_hash["format"] = @schema.format
  schema_hash["description"] = @schema.description.to_s if @schema.description
  apply_nullable(schema_hash)
  props = build_properties(@schema.properties)
  schema_hash["properties"] = props if props
  if @schema.items
    schema_hash["items"] = build_schema_or_ref(@schema.items, include_metadata: false)
    if !schema_hash["description"] && @schema.items.respond_to?(:description) && @schema.items.description
      schema_hash["description"] = @schema.items.description.to_s
    end
    if @schema.items.respond_to?(:canonical_name) && @schema.items.canonical_name &&
       @schema.items.respond_to?(:nullable) && @schema.items.nullable
      case @nullable_strategy
      when Constants::NullableStrategy::KEYWORD
        schema_hash["nullable"] = true
      when Constants::NullableStrategy::EXTENSION
        schema_hash["x-nullable"] = true
      when Constants::NullableStrategy::TYPE_ARRAY
        schema_hash["type"] = (Array(schema_hash["type"]) | ["null"])
      end
    end
  end
  schema_hash["required"] = @schema.required if @schema.required && !@schema.required.empty?
  schema_hash["enum"] = normalize_enum(@schema.enum, schema_hash["type"]) if @schema.enum
  schema_hash["default"] = @schema.default unless @schema.default.nil?
  schema_hash
end