Module: Literal::Openapi::Serializable::ClassMethods

Defined in:
lib/literal/openapi/serializable.rb

Constant Summary collapse

OPENAPI_PROP_KWARGS =

OpenAPI-specific kwargs that ‘prop` should accept. Extracted before forwarding to Literal::Properties#prop.

%i[enum format items_enum].freeze

Instance Method Summary collapse

Instance Method Details

#__literal_property_class__Object



38
39
40
# File 'lib/literal/openapi/serializable.rb', line 38

def __literal_property_class__
  Literal::Openapi::Property
end

#__openapi_constraints__Object

Per-class map of property-name → extra schema constraints (minimum, maximum, pattern, etc.). Populated by the EasyTalk compat shim and merged into the property schema at build time.



59
60
61
# File 'lib/literal/openapi/serializable.rb', line 59

def __openapi_constraints__
  @__openapi_constraints__ ||= {}
end

#__openapi_optional__Object

Per-class set of property names that should NOT appear in ‘required` even if nilable/required-by-default. Populated by the shim when a property is declared with `optional: true`.



52
53
54
# File 'lib/literal/openapi/serializable.rb', line 52

def __openapi_optional__
  @__openapi_optional__ ||= ::Set.new
end

#__openapi_required__Object

Per-class set of property names that should appear in ‘required` regardless of nilability. Populated by the EasyTalk compat shim to mirror easy_talk’s “everything declared is required” behavior.



45
46
47
# File 'lib/literal/openapi/serializable.rb', line 45

def __openapi_required__
  @__openapi_required__ ||= ::Set.new
end

#openapi_additional_properties(value) ⇒ Object

Opt out of ‘additionalProperties: false` for this class. NOT inherited — each class explicitly sets it.



72
73
74
# File 'lib/literal/openapi/serializable.rb', line 72

def openapi_additional_properties(value)
  @openapi_additional_properties = value
end

#openapi_additional_properties_valueObject



76
77
78
79
80
# File 'lib/literal/openapi/serializable.rb', line 76

def openapi_additional_properties_value
  return @openapi_additional_properties if instance_variable_defined?(:@openapi_additional_properties)

  false
end

#openapi_description(value = nil) ⇒ Object

Schema-level description (easy_talk ‘description “…”` in define_schema block). Populated by the shim, emitted at build time.



65
66
67
68
# File 'lib/literal/openapi/serializable.rb', line 65

def openapi_description(value = nil)
  @openapi_description = value unless value.nil?
  @openapi_description if instance_variable_defined?(:@openapi_description)
end

#openapi_schema(adapter: Literal::Openapi::OpenAPI["3.0"]) ⇒ Object



82
83
84
85
# File 'lib/literal/openapi/serializable.rb', line 82

def openapi_schema(adapter: Literal::Openapi::OpenAPI["3.0"])
  instance = adapter.is_a?(Class) ? adapter.new : adapter
  instance.build_schema(self)
end

#prop(name, type, kind = :keyword, **kwargs, &coercion) ⇒ Object

Override Literal::Properties#prop to accept enum:, format:, items_enum:. Strips those kwargs, forwards the rest to super (which creates a Literal::Openapi::Property via literal_property_class), then tags the created property with the OpenAPI extras.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/literal/openapi/serializable.rb', line 91

def prop(name, type, kind = :keyword, **kwargs, &coercion)
  enum = kwargs.delete(:enum)
  format = kwargs.delete(:format)
  items_enum = kwargs.delete(:items_enum)

  effective_type =
    if enum
      enum_values = enum.freeze
      predicate = Literal::Types::PredicateType.new(
        message: "enum(#{enum_values.inspect})",
        block: ->(v) { enum_values.include?(v) }
      )
      Literal::Types::IntersectionType.new([type, predicate])
    else
      type
    end

  super(name, effective_type, kind, **kwargs, &coercion)

  property = literal_properties[name]
  property.instance_variable_set(:@enum, enum) if enum
  property.instance_variable_set(:@format, format) if format
  property.instance_variable_set(:@items_enum, items_enum) if items_enum

  name
end