Class: Praxis::Docs::OpenApi::RequestBodyObject

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/docs/open_api/request_body_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute:) ⇒ RequestBodyObject

Returns a new instance of RequestBodyObject.



12
13
14
# File 'lib/praxis/docs/open_api/request_body_object.rb', line 12

def initialize(attribute:)
  @attribute = attribute
end

Instance Attribute Details

#attributeObject (readonly)



10
11
12
# File 'lib/praxis/docs/open_api/request_body_object.rb', line 10

def attribute
  @attribute
end

Instance Method Details

#dumpObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/praxis/docs/open_api/request_body_object.rb', line 16

def dump
  h = {}
  h[:description] = attribute.options[:description] if attribute.options[:description]
  h[:required] = attribute.options[:required] || false

  # OpenApi wants a set of bodies per MediaType/Content-Type
  # For us there's really only one schema (regardless of encoding)...
  # so we'll show all the supported MTs...but repeating the schema
  # dumped_schema = SchemaObject.new(info: attribute).dump_schema

  example_handlers = \
    if attribute.type < Praxis::Types::MultipartArray
      ident = MediaTypeIdentifier.load('multipart/form-data')
      [{ ident.to_s => 'json' }] # Multipart content type
    else
      # TODO: We could run it through other handlers I guess...if they're registered
      [{ 'application/json' => 'json' }]
    end

  h[:content] = MediaTypeObject.create_content_attribute_helper(type: attribute.type,
                                                                example_payload: attribute.example(nil),
                                                                example_handlers: example_handlers)
  # # Key string (of MT) , value MTObject
  # content_hash = info[:examples].each_with_object({}) do |(handler, example_hash),accum|
  #   content_type = example_hash[:content_type]
  #   accum[content_type] = MediaTypeObject.new(
  #     schema: dumped_schema, # Every MT will have the same exact type..oh well
  #     example: info[:examples][handler][:body],
  #   ).dump
  # end
  # # TODO! Handle Multipart types! they look like arrays now in the schema...etc
  # h[:content] = content_hash
  h
end