Class: GrapeOpenapi3::Readers::EntityReader
- Inherits:
-
Object
- Object
- GrapeOpenapi3::Readers::EntityReader
- Defined in:
- lib/grape_openapi3/readers/entity_reader.rb
Overview
Reads Grape::Entity subclasses and converts them to OpenAPI 3.0 schema objects.
Maintains an internal registry so that:
- Each entity class is only processed once
- Circular references are broken (placeholder set before recursing)
- Nested entities (using: SomeEntity) become $ref pointers
Usage:
reader = EntityReader.new
name = reader.register(MyEntity) # → "MyEntity", adds to schemas
reader.schemas # → { "MyEntity" => { ... } }
Constant Summary collapse
- REPRESENT_EXPOSURE =
"Grape::Entity::Exposure::RepresentExposure"
Instance Attribute Summary collapse
-
#schemas ⇒ Object
readonly
Returns the value of attribute schemas.
Instance Method Summary collapse
-
#initialize ⇒ EntityReader
constructor
A new instance of EntityReader.
-
#register(entity_class) ⇒ Object
Register an entity class and return its schema name.
Constructor Details
#initialize ⇒ EntityReader
Returns a new instance of EntityReader.
21 22 23 |
# File 'lib/grape_openapi3/readers/entity_reader.rb', line 21 def initialize @schemas = {} end |
Instance Attribute Details
#schemas ⇒ Object (readonly)
Returns the value of attribute schemas.
19 20 21 |
# File 'lib/grape_openapi3/readers/entity_reader.rb', line 19 def schemas @schemas end |
Instance Method Details
#register(entity_class) ⇒ Object
Register an entity class and return its schema name. Safe to call multiple times with the same class.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/grape_openapi3/readers/entity_reader.rb', line 27 def register(entity_class) entity_class = resolve_class(entity_class) return nil unless entity_class.respond_to?(:root_exposures) name = schema_name(entity_class) return name if @schemas.key?(name) @schemas[name] = nil # break circular refs before recursing @schemas[name] = build_schema(entity_class) name end |