Method: Jsapi::Meta::Response::Base#to_openapi

Defined in:
lib/jsapi/meta/response/base.rb

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI response object.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/jsapi/meta/response/base.rb', line 67

def to_openapi(version, definitions)
  version = OpenAPI::Version.from(version)

  with_openapi_extensions(
    if version == OpenAPI::V2_0
      {
        description: description,
        schema: schema.to_openapi(version),
        headers: headers.transform_values do |header|
          header.to_openapi(version) unless header.reference?
        end.compact.presence,
        examples: (
          if (example = examples.values.first).present?
            { content_type.to_s => example.resolve(definitions).value }
          end
        )
      }
    else
      {
        summary: (summary if version >= OpenAPI::V3_2),
        description: description,
        headers: headers.transform_values do |header|
          header.to_openapi(version)
        end.presence,
        content: {
          content_type.to_s => {
            **if content_type == Media::Type::APPLICATION_JSON_SEQ &&
              schema.array? && version >= OpenAPI::V3_2
                { itemSchema: schema.items.to_openapi(version) }
              else
                { schema: schema.to_openapi(version) }
              end,
            examples: examples.transform_values(&:to_openapi).presence
          }.compact
        },
        links: links.transform_values do |link|
          link.to_openapi(version)
        end.presence
      }
    end
  )
end