Class: Jsapi::Meta::Definitions
- Inherits:
-
Model::Base
- Object
- Model::Base
- Jsapi::Meta::Definitions
- Includes:
- OpenAPI::Extensions
- Defined in:
- lib/jsapi/meta/definitions.rb
Constant Summary
Constants included from Model::Attributes
Model::Attributes::DEFAULT_ARRAY, Model::Attributes::DEFAULT_HASH
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
The class to which this instance is assigned.
-
#parent ⇒ Object
readonly
The
Definitionsinstance from which this instance inherits.
Instance Method Summary collapse
-
#add_operation(name = nil, keywords = {}) ⇒ Object
:nodoc:.
-
#add_parameter(name, keywords = {}) ⇒ Object
:nodoc:.
-
#ancestors ⇒ Object
Returns an array containing itself and all of the
Definitionsinstances inherited/included. -
#base_path ⇒ Object
:attr: base_path The base path of the API.
-
#callbacks ⇒ Object
:attr: callbacks The reusable Callback objects.
-
#default_value(type, context: nil) ⇒ Object
Returns the default value for
typewithincontext. -
#defaults ⇒ Object
:attr: defaults The Defaults.
-
#examples ⇒ Object
:attr: examples The reusable Example objects.
-
#external_docs ⇒ Object
:attr: external_docs The ExternalDocumentation object.
-
#find_operation(name = nil) ⇒ Object
Returns the operation with the specified name.
-
#find_parameter(name) ⇒ Object
Returns the reusable parameter with the specified name.
-
#find_request_body(name) ⇒ Object
Returns the reusable request body with the specified name.
-
#find_response(name) ⇒ Object
Returns the reusable response with the specified name.
-
#find_schema(name) ⇒ Object
Returns the reusable schema with the specified name.
-
#headers ⇒ Object
:attr: headers The reusable Header objects.
-
#host ⇒ Object
:attr: host The host serving the API.
-
#include(definitions) ⇒ Object
Includes
definitions. -
#info ⇒ Object
:attr: info The Info object.
-
#initialize(keywords = {}) ⇒ Definitions
constructor
A new instance of Definitions.
-
#json_schema_document(name) ⇒ Object
Returns a hash representing the JSON Schema document for
name. -
#links ⇒ Object
:attr: links The reusable Link objects.
-
#on_rescue_callbacks ⇒ Object
Returns the methods or procs to be called when rescuing an exception.
-
#on_rescues ⇒ Object
:attr: on_rescues The methods or procs to be called whenever an exception is rescued.
-
#openapi_document(version = nil) ⇒ Object
Returns a hash representing the OpenAPI document for
version. -
#operations ⇒ Object
:attr: operations The Operation objects.
-
#parameters ⇒ Object
:attr: parameters The reusable Parameter objects.
-
#request_bodies ⇒ Object
:attr: request_bodies The reusable RequestBody objects.
-
#rescue_handler_for(exception) ⇒ Object
Returns the first RescueHandler to handle
exception, or nil if no one could be found. -
#rescue_handlers ⇒ Object
:attr: rescue_handlers The RescueHandler objects.
-
#responses ⇒ Object
:attr: responses The reusable Response objects.
-
#schemas ⇒ Object
:attr: schemas The reusable Schema objects.
-
#schemes ⇒ Object
:attr: schemes The array of transfer protocols supported by the API.
-
#security_requirements ⇒ Object
:attr: security_requirements The array of SecurityRequirement objects.
-
#security_schemes ⇒ Object
:attr: security_schemes The SecurityScheme objects.
-
#servers ⇒ Object
:attr: servers The array of Server objects.
-
#tags ⇒ Object
:attr: tags The array of Tag objects.
Methods included from OpenAPI::Extensions
Methods inherited from Model::Base
#inspect, #merge!, #reference?, #resolve
Methods included from Model::Attributes
Constructor Details
#initialize(keywords = {}) ⇒ Definitions
Returns a new instance of Definitions.
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/jsapi/meta/definitions.rb', line 128 def initialize(keywords = {}) keywords = keywords.dup @owner = keywords.delete(:owner) @parent = keywords.delete(:parent) included = keywords.delete(:include) super(keywords) Array(included).each do |definitions| include(definitions) end @parent&.inherited(self) end |
Instance Attribute Details
#owner ⇒ Object (readonly)
The class to which this instance is assigned.
123 124 125 |
# File 'lib/jsapi/meta/definitions.rb', line 123 def owner @owner end |
#parent ⇒ Object (readonly)
The Definitions instance from which this instance inherits.
126 127 128 |
# File 'lib/jsapi/meta/definitions.rb', line 126 def parent @parent end |
Instance Method Details
#add_operation(name = nil, keywords = {}) ⇒ Object
:nodoc:
141 142 143 144 145 |
# File 'lib/jsapi/meta/definitions.rb', line 141 def add_operation(name = nil, keywords = {}) # :nodoc: name = name.nil? ? default_operation_name : name.to_s keywords = keywords.reverse_merge(path: default_operation_path) (@operations ||= {})[name] = Operation.new(name, keywords) end |
#add_parameter(name, keywords = {}) ⇒ Object
:nodoc:
147 148 149 150 |
# File 'lib/jsapi/meta/definitions.rb', line 147 def add_parameter(name, keywords = {}) # :nodoc: name = name.to_s (@parameters ||= {})[name] = Parameter.new(name, keywords) end |
#ancestors ⇒ Object
Returns an array containing itself and all of the Definitions instances inherited/included.
154 155 156 157 158 159 160 |
# File 'lib/jsapi/meta/definitions.rb', line 154 def ancestors @ancestors ||= [self].tap do |ancestors| [@included_definitions, @parent].flatten.each do |definitions| ancestors.push(*definitions.ancestors) if definitions end end.uniq end |
#base_path ⇒ Object
:attr: base_path The base path of the API. Applies to OpenAPI 2.0.
11 |
# File 'lib/jsapi/meta/definitions.rb', line 11 attribute :base_path, String |
#callbacks ⇒ Object
:attr: callbacks The reusable Callback objects. Applies to OpenAPI 3.0 and higher.
16 |
# File 'lib/jsapi/meta/definitions.rb', line 16 attribute :callbacks, { String => Callback } |
#default_value(type, context: nil) ⇒ Object
Returns the default value for type within context.
163 164 165 |
# File 'lib/jsapi/meta/definitions.rb', line 163 def default_value(type, context: nil) objects.dig(:defaults, type.to_s)&.value(context: context) end |
#defaults ⇒ Object
:attr: defaults The Defaults.
21 |
# File 'lib/jsapi/meta/definitions.rb', line 21 attribute :defaults, { String => Defaults }, keys: Schema::TYPES |
#examples ⇒ Object
:attr: examples The reusable Example objects. Applies to OpenAPI 3.0 and higher.
26 |
# File 'lib/jsapi/meta/definitions.rb', line 26 attribute :examples, { String => Example } |
#external_docs ⇒ Object
:attr: external_docs The ExternalDocumentation object.
31 |
# File 'lib/jsapi/meta/definitions.rb', line 31 attribute :external_docs, ExternalDocumentation |
#find_operation(name = nil) ⇒ Object
Returns the operation with the specified name.
168 169 170 171 172 173 |
# File 'lib/jsapi/meta/definitions.rb', line 168 def find_operation(name = nil) return objects.dig(:operations, name.to_s) if name.present? # Return the one and only operation operations.values.first if operations.one? end |
#find_parameter(name) ⇒ Object
Returns the reusable parameter with the specified name.
176 177 178 |
# File 'lib/jsapi/meta/definitions.rb', line 176 def find_parameter(name) objects.dig(:parameters, name&.to_s) end |
#find_request_body(name) ⇒ Object
Returns the reusable request body with the specified name.
181 182 183 |
# File 'lib/jsapi/meta/definitions.rb', line 181 def find_request_body(name) objects.dig(:request_bodies, name&.to_s) end |
#find_response(name) ⇒ Object
Returns the reusable response with the specified name.
186 187 188 |
# File 'lib/jsapi/meta/definitions.rb', line 186 def find_response(name) objects.dig(:responses, name&.to_s) end |
#find_schema(name) ⇒ Object
Returns the reusable schema with the specified name.
191 192 193 |
# File 'lib/jsapi/meta/definitions.rb', line 191 def find_schema(name) objects.dig(:schemas, name&.to_s) end |
#headers ⇒ Object
:attr: headers The reusable Header objects. Applies to OpenAPI 3.0 and higher.
36 |
# File 'lib/jsapi/meta/definitions.rb', line 36 attribute :headers, { String => Header } |
#host ⇒ Object
:attr: host The host serving the API. Applies to OpenAPI 2.0.
41 |
# File 'lib/jsapi/meta/definitions.rb', line 41 attribute :host, String |
#include(definitions) ⇒ Object
Includes definitions.
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/jsapi/meta/definitions.rb', line 196 def include(definitions) if circular_dependency?(definitions) raise ArgumentError, 'detected circular dependency between ' \ "#{owner.inspect} and " \ "#{definitions.owner.inspect}" end (@included_definitions ||= []) << definitions definitions.included(self) invalidate_ancestors self end |
#info ⇒ Object
:attr: info The Info object.
46 |
# File 'lib/jsapi/meta/definitions.rb', line 46 attribute :info, Info |
#json_schema_document(name) ⇒ Object
Returns a hash representing the JSON Schema document for name.
211 212 213 214 215 216 217 |
# File 'lib/jsapi/meta/definitions.rb', line 211 def json_schema_document(name) find_schema(name)&.to_json_schema&.tap do |json_schema_document| if (schemas = objects[:schemas].except(name.to_s)).any? json_schema_document[:definitions] = schemas.transform_values(&:to_json_schema) end end end |
#links ⇒ Object
:attr: links The reusable Link objects. Applies to OpenAPI 3.0 and higher.
51 |
# File 'lib/jsapi/meta/definitions.rb', line 51 attribute :links, { String => Link } |
#on_rescue_callbacks ⇒ Object
Returns the methods or procs to be called when rescuing an exception.
220 221 222 |
# File 'lib/jsapi/meta/definitions.rb', line 220 def on_rescue_callbacks objects[:on_rescues] end |
#on_rescues ⇒ Object
:attr: on_rescues The methods or procs to be called whenever an exception is rescued.
56 |
# File 'lib/jsapi/meta/definitions.rb', line 56 attribute :on_rescues, [] |
#openapi_document(version = nil) ⇒ Object
Returns a hash representing the OpenAPI document for version.
Raises an ArgumentError if version is not supported.
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 294 295 296 297 |
# File 'lib/jsapi/meta/definitions.rb', line 227 def openapi_document(version = nil) version = OpenAPI::Version.from(version) operations = objects[:operations].values openapi_paths = operations.group_by { |operation| operation.path || default_operation_path } .transform_values do |operations_by_path| operations_by_path.index_by(&:method).transform_values do |operation| operation.to_openapi(version, self) end end.presence openapi_objects = if version.major == 2 %i[base_path external_docs info host parameters responses parameters schemas schemes security_requirements security_schemes tags] else %i[callbacks examples external_docs headers info links parameters request_bodies responses schemas security_requirements security_schemes servers tags] end.to_h { |key| [key, object_to_openapi(objects[key], version).presence] } with_openapi_extensions( if version.major == 2 openapi_server = objects[:servers].first || default_server uri = URI(openapi_server.url) if openapi_server { # Order according to the OpenAPI specification 2.x swagger: '2.0', info: openapi_objects[:info], host: openapi_objects[:host] || uri&.hostname, basePath: openapi_objects[:base_path] || uri&.path, schemes: openapi_objects[:schemes] || Array(uri&.scheme).presence, consumes: operations.filter_map do |operation| operation.consumes(self) end.uniq.sort.presence, produces: operations.flat_map do |operation| operation.produces(self) end.uniq.sort.presence, paths: openapi_paths, definitions: openapi_objects[:schemas], parameters: openapi_objects[:parameters], responses: openapi_objects[:responses], securityDefinitions: openapi_objects[:security_schemes] } else { # Order according to the OpenAPI specification 3.x openapi: version.minor.zero? ? '3.0.3' : '3.1.1', info: openapi_objects[:info], servers: openapi_objects[:servers] || [default_server&.to_openapi].compact.presence, paths: openapi_paths, components: { schemas: openapi_objects[:schemas], responses: openapi_objects[:responses], parameters: openapi_objects[:parameters], examples: openapi_objects[:examples], requestBodies: openapi_objects[:request_bodies], headers: openapi_objects[:headers], securitySchemes: openapi_objects[:security_schemes], links: openapi_objects[:links], callbacks: openapi_objects[:callbacks] }.compact.presence } end.merge( security: openapi_objects[:security_requirements], tags: openapi_objects[:tags], externalDocs: openapi_objects[:external_docs] ).compact ) end |
#operations ⇒ Object
:attr: operations The Operation objects.
61 |
# File 'lib/jsapi/meta/definitions.rb', line 61 attribute :operations, { String => Operation }, accessors: %i[reader writer] |
#parameters ⇒ Object
:attr: parameters The reusable Parameter objects.
66 |
# File 'lib/jsapi/meta/definitions.rb', line 66 attribute :parameters, { String => Parameter }, accessors: %i[reader writer] |
#request_bodies ⇒ Object
:attr: request_bodies The reusable RequestBody objects.
76 |
# File 'lib/jsapi/meta/definitions.rb', line 76 attribute :request_bodies, { String => RequestBody } |
#rescue_handler_for(exception) ⇒ Object
Returns the first RescueHandler to handle exception, or nil if no one could be found.
300 301 302 |
# File 'lib/jsapi/meta/definitions.rb', line 300 def rescue_handler_for(exception) objects[:rescue_handlers].find { |r| r.match?(exception) } end |
#rescue_handlers ⇒ Object
:attr: rescue_handlers The RescueHandler objects.
71 |
# File 'lib/jsapi/meta/definitions.rb', line 71 attribute :rescue_handlers, [RescueHandler] |
#responses ⇒ Object
:attr: responses The reusable Response objects.
81 |
# File 'lib/jsapi/meta/definitions.rb', line 81 attribute :responses, { String => Response } |
#schemas ⇒ Object
:attr: schemas The reusable Schema objects.
86 |
# File 'lib/jsapi/meta/definitions.rb', line 86 attribute :schemas, { String => Schema } |
#schemes ⇒ Object
:attr: schemes The array of transfer protocols supported by the API. Possible values are:
-
"http" -
"https" -
"ws" -
"wss"
Applies to OpenAPI 2.0.
98 |
# File 'lib/jsapi/meta/definitions.rb', line 98 attribute :schemes, [String], values: %w[http https ws wss] |
#security_requirements ⇒ Object
:attr: security_requirements The array of SecurityRequirement objects.
103 |
# File 'lib/jsapi/meta/definitions.rb', line 103 attribute :security_requirements, [SecurityRequirement] |
#security_schemes ⇒ Object
:attr: security_schemes The SecurityScheme objects.
110 |
# File 'lib/jsapi/meta/definitions.rb', line 110 attribute :security_schemes, { String => SecurityScheme } |
#servers ⇒ Object
:attr: servers The array of Server objects. Applies to OpenAPI 3.0 and higher.
115 |
# File 'lib/jsapi/meta/definitions.rb', line 115 attribute :servers, [Server] |
#tags ⇒ Object
:attr: tags The array of Tag objects.
120 |
# File 'lib/jsapi/meta/definitions.rb', line 120 attribute :tags, [Tag] |