Class: Jsapi::Meta::Operation

Inherits:
Model::Base show all
Includes:
Jsapi::Meta::OpenAPI::Extensions, Model::Wrappable
Defined in:
lib/jsapi/meta/operation.rb,
lib/jsapi/meta/callback/base.rb

Overview

Specifies an API operation.

Defined Under Namespace

Classes: Wrapper

Instance Method Summary collapse

Methods included from Jsapi::Meta::OpenAPI::Extensions

included

Methods inherited from Model::Base

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

Methods included from Model::Attributes

#attributes_frozen?, #freeze_attributes, included

Constructor Details

#initialize(name, parent_path = nil, keywords = {}) ⇒ Operation

Returns a new instance of Operation.



201
202
203
204
205
206
207
# File 'lib/jsapi/meta/operation.rb', line 201

def initialize(name, parent_path = nil, keywords = {})
  parent_path, keywords = nil, parent_path if parent_path.is_a?(Hash)

  @name = name&.to_s
  @parent_path = Pathname.from(parent_path)
  super(keywords)
end

Instance Method Details

#add_parameter(name, keywords = {}) ⇒ Object

:nodoc:



209
210
211
212
213
214
215
# File 'lib/jsapi/meta/operation.rb', line 209

def add_parameter(name, keywords = {}) # :nodoc:
  try_modify_attribute!(:parameters) do
    name = name.to_s

    (@parameters ||= {})[name.to_s] = Parameter.new(name, keywords)
  end
end

#callbacksObject

:attr: callbacks The callbacks that can be triggered by the operation. Maps strings to Callback objects or references.



99
# File 'lib/jsapi/meta/operation.rb', line 99

attribute :callbacks, { String => Callback }

#deprecatedObject

:attr: deprecated Specifies whether the operation is marked as deprecated.



104
# File 'lib/jsapi/meta/operation.rb', line 104

attribute :deprecated, values: [true, false]

#descriptionObject

:attr: description The description of the operation.



109
# File 'lib/jsapi/meta/operation.rb', line 109

attribute :description, String

#external_docsObject

:attr: external_docs The additional external documentation for this operation.

See ExternalDocumentation for further information.



116
# File 'lib/jsapi/meta/operation.rb', line 116

attribute :external_docs, ExternalDocumentation

#full_pathObject

Returns the full path of the operation as a Pathname.



218
219
220
# File 'lib/jsapi/meta/operation.rb', line 218

def full_path
  parent_path + path
end

#methodObject

:attr: method The HTTP method of the operation, "get" by default.



121
# File 'lib/jsapi/meta/operation.rb', line 121

attribute :method, String, default: 'get'

#modelObject

:attr: model The model class to access top-level parameters by.



126
# File 'lib/jsapi/meta/operation.rb', line 126

attribute :model, Class

#nameObject

:attr_reader: name The name of the operation.



131
# File 'lib/jsapi/meta/operation.rb', line 131

attribute :name, accessors: i[reader]

#parametersObject

:attr: parameters The parameters of the operation. Maps parameter names to Parameter objects or references.



137
# File 'lib/jsapi/meta/operation.rb', line 137

attribute :parameters, { String => Parameter }, accessors: i[reader writer]

#parent_pathObject

:attr_reader: parent_path The parent path as a Pathname.



142
# File 'lib/jsapi/meta/operation.rb', line 142

attribute :parent_path, Pathname, accessors: i[reader]

#pathObject

:attr: path The relative path of the operation as a Pathname.



147
# File 'lib/jsapi/meta/operation.rb', line 147

attribute :path, Pathname

#request_bodyObject

:attr: request_body The request body of the operation as a RequestBody object or reference.



152
# File 'lib/jsapi/meta/operation.rb', line 152

attribute :request_body, RequestBody

#responsesObject

:attr: responses The responses that can be produced by the operation. Maps instances of Status::Base to Response objects or references.



158
# File 'lib/jsapi/meta/operation.rb', line 158

attribute :responses, { Status => Response }, default_key: Status::DEFAULT

#schemesObject

:attr: schemes The transfer protocols supported by the operation. Can contain one or more of:

  • "http"

  • "https"

  • "ws"

  • "wss"

Applies to OpenAPI 2.0 only.



171
# File 'lib/jsapi/meta/operation.rb', line 171

attribute :schemes, [String], values: %w[http https ws wss]

#security_requirementsObject

:attr: security_requirements The security requirements that override the top-level security requirements.

See SecurityRequirement for further information.



178
# File 'lib/jsapi/meta/operation.rb', line 178

attribute :security_requirements, [SecurityRequirement], default: :nil

#serversObject

:attr: servers The servers providing the operation.

Applies to OpenAPI 3.0 and higher.

See Server for further information.



189
# File 'lib/jsapi/meta/operation.rb', line 189

attribute :servers, [Server]

#summaryObject

:attr: summary The short description of the operation.



194
# File 'lib/jsapi/meta/operation.rb', line 194

attribute :summary, String

#tagsObject

:attr: tags The tags used to group operations in an OpenAPI document.



199
# File 'lib/jsapi/meta/operation.rb', line 199

attribute :tags, [String]

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI operation object.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/jsapi/meta/operation.rb', line 223

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

  responses = (
    definitions.common_responses(full_path)&.merge(self.responses) ||
    self.responses
  ).reject do |status, response|
    response.resolve_lazily(definitions).nodoc ||
      version == OpenAPI::V2_0 && status.is_a?(Status::Range)
  end

  with_openapi_extensions(
    operationId: name,
    tags:
      [tags, definitions.common_tags(full_path)]
        .compact.flatten.uniq.presence,
    summary: summary,
    description: description,
    externalDocs: external_docs&.to_openapi,
    **if version == OpenAPI::V2_0
        resolved_request_body =
          (request_body || definitions.common_request_body(full_path))
          &.resolve(definitions)
        {
          consumes:
            [resolved_request_body&.default_media_range]
              .compact.presence,
          produces:
            responses.values.filter_map do |response|
              response.resolve(definitions).default_media_type
            end.uniq.sort.presence,
          schemes: schemes.presence,
          parameters:
            begin
              params = parameters.values.flat_map do |parameter|
                parameter.to_openapi_parameters(version, definitions)
              end
              if resolved_request_body
                params << resolved_request_body.to_openapi_parameter
              end
              params
            end
        }
      else
        {
          servers:
            servers.map do |server|
              server.to_openapi(version)
            end.presence,
          callbacks:
            callbacks.transform_values do |callback|
              callback.to_openapi(version, definitions)
            end.presence,
          parameters:
            parameters.values.flat_map do |parameter|
              parameter.to_openapi_parameters(version, definitions)
            end,
          request_body: request_body&.to_openapi(version)
        }
      end,
    responses:
      responses.transform_values do |response|
        response.to_openapi(version, definitions)
      end,
    deprecated: deprecated?.presence,
    security:
      [security_requirements, definitions.common_security_requirements(full_path)]
        .compact.presence&.flatten&.map(&:to_openapi)
  )
end