Class: Gitlab::GrapeOpenapi::RequestBodyRegistry

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

Constant Summary collapse

SCHEMA_PATH_PREFIX =
'#/components/schemas/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequestBodyRegistry

Returns a new instance of RequestBodyRegistry.



12
13
14
15
# File 'lib/gitlab/grape_openapi/request_body_registry.rb', line 12

def initialize
  @schemas = {}
  @schema_hashes = {}
end

Instance Attribute Details

#schemasObject (readonly)

Returns the value of attribute schemas.



10
11
12
# File 'lib/gitlab/grape_openapi/request_body_registry.rb', line 10

def schemas
  @schemas
end

Instance Method Details

#register(schema) ⇒ Object



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

def register(schema)
  return nil if schema.blank?

  schema_hash = compute_hash(schema)

  if @schema_hashes.key?(schema_hash)
    existing_name = @schema_hashes[schema_hash]
    return { '$ref' => "#{SCHEMA_PATH_PREFIX}#{existing_name}" }
  end

  name = "RequestBody_#{short_hash(schema_hash)}"
  @schemas[name] = schema
  @schema_hashes[schema_hash] = name

  { '$ref' => "#{SCHEMA_PATH_PREFIX}#{name}" }
end