Class: Gitlab::GrapeOpenapi::Converters::EntityConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/grape_openapi/converters/entity_converter.rb

Constant Summary collapse

OBJECT_TYPE =
'object'
ARRAY_TYPE =
'array'
DEFAULT_TYPE =
'string'
REF_KEY =
'$ref'
SCHEMA_PATH_PREFIX =
'#/components/schemas/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_class, schema_registry) ⇒ EntityConverter

Returns a new instance of EntityConverter.



37
38
39
40
# File 'lib/gitlab/grape_openapi/converters/entity_converter.rb', line 37

def initialize(entity_class, schema_registry)
  @entity_class = entity_class
  @schema_registry = schema_registry
end

Instance Attribute Details

#entity_classObject (readonly)

Returns the value of attribute entity_class.



7
8
9
# File 'lib/gitlab/grape_openapi/converters/entity_converter.rb', line 7

def entity_class
  @entity_class
end

#schema_registryObject (readonly)

Returns the value of attribute schema_registry.



7
8
9
# File 'lib/gitlab/grape_openapi/converters/entity_converter.rb', line 7

def schema_registry
  @schema_registry
end

Class Method Details

.grape_entity?(klass) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/gitlab/grape_openapi/converters/entity_converter.rb', line 33

def self.grape_entity?(klass)
  klass.is_a?(Class) && klass.ancestors.include?(Grape::Entity)
end

.register(entity, schema_registry) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/grape_openapi/converters/entity_converter.rb', line 15

def self.register(entity, schema_registry)
  case entity
  when Class
    return unless grape_entity?(entity)

    new(entity, schema_registry).convert
  when Hash
    return unless entity[:model] && grape_entity?(entity[:model])

    new(entity[:model], schema_registry).convert
  when Array
    # Array elements may be Class (`success [Entities::Foo]`) or
    # Hash-with-:model (`success [{ code:, model: Foo }]`). Recurse so
    # both are registered. Mirrors `ResponseConverter`'s handling.
    entity.each { |item| register(item, schema_registry) }
  end
end

Instance Method Details

#convert(register: true) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/gitlab/grape_openapi/converters/entity_converter.rb', line 42

def convert(register: true)
  normalized_name = schema_registry.normalize_entity_class(entity_class)
  return schema_registry.schemas[normalized_name] if schema_registry.schemas.key?(normalized_name)

  schema = build_schema
  schema_registry.register(entity_class, schema) if register
  schema
end