Class: Jsapi::Meta::RequestBody::Model

Inherits:
Base::Model show all
Includes:
OpenAPI::Extensions
Defined in:
lib/jsapi/meta/request_body/model.rb

Overview

Specifies a request body.

Constant Summary

Constants included from Base::Attributes

Base::Attributes::DEFAULT_ARRAY, Base::Attributes::DEFAULT_HASH

Instance Method Summary collapse

Methods included from OpenAPI::Extensions

#add_openapi_extension, #openapi_extensions, #openapi_extensions=

Methods inherited from Base::Model

#inspect, #merge!, #reference?, #resolve

Methods included from Base::Attributes

#attribute, #attribute_names

Constructor Details

#initialize(keywords = {}) ⇒ Model

Returns a new instance of Model.



32
33
34
35
36
37
38
39
40
# File 'lib/jsapi/meta/request_body/model.rb', line 32

def initialize(keywords = {})
  keywords = keywords.dup
  super(keywords.extract!(:content_type, :description, :examples, :openapi_extensions))

  add_example(value: keywords.delete(:example)) if keywords.key?(:example)
  keywords[:ref] = keywords.delete(:schema) if keywords.key?(:schema)

  @schema = Schema.new(keywords)
end

Instance Method Details

#content_typeObject

:attr: content_type The content type. "application/json" by default.



15
# File 'lib/jsapi/meta/request_body/model.rb', line 15

attribute :content_type, String, default: 'application/json'

#descriptionObject

:attr: description The description of the request body.



20
# File 'lib/jsapi/meta/request_body/model.rb', line 20

attribute :description, String

#examplesObject

:attr_reader: examples The Example objects.



25
# File 'lib/jsapi/meta/request_body/model.rb', line 25

attribute :examples, { String => Example }, default_key: 'default'

#required?Boolean

Returns true if the level of existence is greater than or equal to ALLOW_NIL, false otherwise.

Returns:

  • (Boolean)


44
45
46
# File 'lib/jsapi/meta/request_body/model.rb', line 44

def required?
  schema.existence >= Existence::ALLOW_NIL
end

#schemaObject

:attr_reader: schema The Schema of the request body.



30
# File 'lib/jsapi/meta/request_body/model.rb', line 30

attribute :schema, read_only: true

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI 3.x request body object.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jsapi/meta/request_body/model.rb', line 59

def to_openapi(version, *)
  with_openapi_extensions(
    description: description,
    content: {
      content_type => {
        schema: schema.to_openapi(version),
        examples: examples.transform_values(&:to_openapi).presence
      }.compact
    },
    required: required?
  )
end

#to_openapi_parameterObject

Returns a hash representing the OpenAPI 2.0 parameter object.



49
50
51
52
53
54
55
56
# File 'lib/jsapi/meta/request_body/model.rb', line 49

def to_openapi_parameter
  {
    name: 'body',
    in: 'body',
    description: description,
    required: required?
  }.merge(schema.to_openapi('2.0')).merge(openapi_extensions).compact
end