Class: GrapeOAS::Exporter::OAS2::Schema
- Inherits:
-
Object
- Object
- GrapeOAS::Exporter::OAS2::Schema
- Defined in:
- lib/grape_oas/exporter/oas2/schema.rb
Instance Method Summary collapse
- #apply_constraints(schema_hash, schema = @schema) ⇒ Object
- #apply_extensions(schema_hash) ⇒ Object
- #build ⇒ Object
- #build_base_hash ⇒ Object
-
#initialize(schema, ref_tracker = nil, nullable_strategy: nil) ⇒ Schema
constructor
A new instance of Schema.
Constructor Details
#initialize(schema, ref_tracker = nil, nullable_strategy: nil) ⇒ Schema
Returns a new instance of Schema.
7 8 9 10 11 |
# File 'lib/grape_oas/exporter/oas2/schema.rb', line 7 def initialize(schema, ref_tracker = nil, nullable_strategy: nil) @schema = schema @ref_tracker = ref_tracker @nullable_strategy = nullable_strategy end |
Instance Method Details
#apply_constraints(schema_hash, schema = @schema) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/grape_oas/exporter/oas2/schema.rb', line 59 def apply_constraints(schema_hash, schema = @schema) schema_hash["minimum"] = schema.minimum unless schema.minimum.nil? schema_hash["maximum"] = schema.maximum unless schema.maximum.nil? schema_hash["exclusiveMinimum"] = schema.exclusive_minimum if schema.exclusive_minimum schema_hash["exclusiveMaximum"] = schema.exclusive_maximum if schema.exclusive_maximum schema_hash["minLength"] = schema.min_length unless schema.min_length.nil? schema_hash["maxLength"] = schema.max_length unless schema.max_length.nil? schema_hash["pattern"] = schema.pattern if schema.pattern schema_hash["minItems"] = schema.min_items unless schema.min_items.nil? schema_hash["maxItems"] = schema.max_items unless schema.max_items.nil? end |
#apply_extensions(schema_hash) ⇒ Object
71 72 73 74 |
# File 'lib/grape_oas/exporter/oas2/schema.rb', line 71 def apply_extensions(schema_hash) schema_hash["x-nullable"] = true if @nullable_strategy == Constants::NullableStrategy::EXTENSION && nullable? schema_hash.merge!(@schema.extensions) if @schema.extensions end |
#build ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/grape_oas/exporter/oas2/schema.rb', line 13 def build return {} unless @schema # Handle allOf composition (for inheritance) return build_all_of_schema if @schema.all_of && !@schema.all_of.empty? # Handle oneOf/anyOf by using first type (OAS2 doesn't support oneOf/anyOf) # Skip if schema has explicit type - use type with extensions instead return build_first_of_schema(:one_of) if @schema.one_of && !@schema.one_of.empty? && !@schema.type return build_first_of_schema(:any_of) if @schema.any_of && !@schema.any_of.empty? && !@schema.type schema_hash = build_base_hash apply_constraints(schema_hash) apply_extensions(schema_hash) schema_hash.compact end |
#build_base_hash ⇒ Object
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 |
# File 'lib/grape_oas/exporter/oas2/schema.rb', line 30 def build_base_hash schema_hash = { "type" => @schema.type, "format" => @schema.format, "description" => @schema.description&.to_s, "properties" => build_properties(@schema.properties), "enum" => normalize_enum(@schema.enum, @schema.type) } 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 && @nullable_strategy == Constants::NullableStrategy::EXTENSION && @schema.items.respond_to?(:nullable) && @schema.items.nullable schema_hash["x-nullable"] = true end end if schema_hash["properties"].nil? || schema_hash["properties"].empty? || @schema.type != Constants::SchemaTypes::OBJECT schema_hash.delete("properties") end schema_hash["example"] = @schema.examples if @schema.examples schema_hash["required"] = @schema.required if @schema.required && !@schema.required.empty? schema_hash["discriminator"] = @schema.discriminator if @schema.discriminator schema_hash["default"] = @schema.default unless @schema.default.nil? schema_hash end |