Class: GrapeOAS::ApiModel::Schema

Inherits:
Node
  • Object
show all
Defined in:
lib/grape_oas/api_model/schema.rb

Overview

Represents a schema object in the DTO model for OpenAPI v2/v3. Used to describe data types, properties, and structure for parameters, request bodies, and responses.

See Also:

Constant Summary collapse

VALID_ATTRIBUTES =
%i[
  canonical_name type format properties items description
  required nullable enum additional_properties unevaluated_properties defs
  examples default extensions
  min_length max_length pattern
  minimum maximum exclusive_minimum exclusive_maximum
  min_items max_items
  discriminator all_of one_of any_of
].freeze

Constants inherited from Node

Node::BUCKET_NAMES

Instance Attribute Summary

Attributes inherited from Node

#id

Instance Method Summary collapse

Methods inherited from Node

bucket, #ref

Constructor Details

#initialize(**attrs) ⇒ Schema

Returns a new instance of Schema.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/grape_oas/api_model/schema.rb', line 23

def initialize(**attrs)
  super()

  @properties = {}
  @required = []
  @nullable = false
  @enum = nil
  @additional_properties = nil
  @unevaluated_properties = nil
  @defs = {}
  @discriminator = nil
  @all_of = nil
  @one_of = nil
  @any_of = nil

  attrs.each do |k, v|
    unless VALID_ATTRIBUTES.include?(k)
      raise ArgumentError, "Unknown Schema attribute: #{k}. Valid attributes: #{VALID_ATTRIBUTES.join(", ")}"
    end

    public_send("#{k}=", v)
  end
end

Instance Method Details

#add_property(name, schema, required: false) ⇒ Object



53
54
55
56
57
58
# File 'lib/grape_oas/api_model/schema.rb', line 53

def add_property(name, schema, required: false)
  key = name.to_s
  @properties[key] = schema
  @required << key if required && !@required.include?(key)
  schema
end

#empty?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/grape_oas/api_model/schema.rb', line 47

def empty?
  return false if @all_of&.any? || @one_of&.any? || @any_of&.any?

  @properties.nil? || @properties.empty?
end