Module: Scorpio::OpenAPI::V3_1

Defined in:
lib/scorpio/openapi/v3_1.rb

Defined Under Namespace

Modules: Document, Ext, JSONSchemaDraft202012, Unscoped

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.document_schema_modules_by_dialect_idObject

Returns the value of attribute document_schema_modules_by_dialect_id.



7
8
9
# File 'lib/scorpio/openapi/v3_1.rb', line 7

def document_schema_modules_by_dialect_id
  @document_schema_modules_by_dialect_id
end

Class Method Details

.document_name_subschemas(document_schema_module, namespace) ⇒ Object



12
13
14
15
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
# File 'lib/scorpio/openapi/v3_1.rb', line 12

def self.document_name_subschemas(document_schema_module, namespace)
  namespace.const_set(:Info,         document_schema_module.defs['info'])
  namespace.const_set(:Contact,       document_schema_module.defs['contact'])
  namespace.const_set(:License,        document_schema_module.defs['license'])
  namespace.const_set(:Server,          document_schema_module.defs['server'])
  namespace.const_set(:ServerVariable,   document_schema_module.defs['server-variable'])
  namespace.const_set(:Components,        document_schema_module.defs['components'])
  namespace.const_set(:Paths,              document_schema_module.defs['paths'])
  namespace.const_set(:PathItem,            document_schema_module.defs['path-item'])
  namespace.const_set(:Operation,            document_schema_module.defs['operation'])
  namespace.const_set(:ExternalDocumentation, document_schema_module.defs['external-documentation'])
  namespace.const_set(:Parameter,            document_schema_module.defs['parameter'])
  namespace.const_set(:RequestBody,         document_schema_module.defs['request-body'])
  namespace.const_set(:Content,            document_schema_module.defs['content'])
  namespace.const_set(:MediaType,         document_schema_module.defs['media-type'])
  namespace.const_set(:Encoding,         document_schema_module.defs['encoding'])
  namespace.const_set(:Responses,       document_schema_module.defs['responses'])
  namespace.const_set(:Response,       document_schema_module.defs['response'])
  namespace.const_set(:Callbacks,     document_schema_module.defs['callbacks'])
  namespace.const_set(:Example,      document_schema_module.defs['example'])
  namespace.const_set(:Link,         document_schema_module.defs['link'])
  namespace.const_set(:Header,        document_schema_module.defs['header'])
  namespace.const_set(:Tag,            document_schema_module.defs['tag'])
  namespace.const_set(:Reference,       document_schema_module.defs['reference'])
  namespace.const_set(:Schema,           document_schema_module.defs['schema'])
  namespace.const_set(:SecurityScheme,    document_schema_module.defs['security-scheme'])
  namespace.const_set(:OAuthFlows,         document_schema_module.defs['oauth-flows'])
  namespace.const_set(:SecurityRequirement, document_schema_module.defs['security-requirement'])
  namespace.const_set(:Examples,            document_schema_module.defs['examples'])
  namespace.const_set(:MapOfStrings,         document_schema_module.defs['map-of-strings'])
  namespace.const_set(:ExplodeForForm,        document_schema_module.defs['explode-for-form'])
  namespace.const_set(:SpecificationExtension, document_schema_module.defs['specification-extensions'].patternProperties["^x-"])
end

.document_schema_module_by_dialect_id(dialect_id) ⇒ Object

This is pretty much: Unscoped::Document.with_dynamic_scope_from(JSI.registry.find(dialect_id)) plus set_up_document_schema_module.

However, this also supports a dialect whose meta-schema isn't aware of dynamic scope and doesn't have a $dynamicAnchor: "meta", e.g. jsonSchemaDialect: "http://json-schema.org/draft-07/schema".

A schema like Scorpio::OpenAPI::V3_1::Ext::ExtDocument exists to $ref to Scorpio::OpenAPI::V3_1::Unscoped::Document with anchor meta in dynamic scope, with the $dynamicAnchor: "meta" schema $refing to Scorpio::OpenAPI::V3_1::Ext::MetaSchema. This method obviates the need for such a schema, directly applying dynamic scope.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/scorpio/openapi/v3_1.rb', line 70

def self.document_schema_module_by_dialect_id(dialect_id)
  dialect_uri = JSI::Util.uri(dialect_id)
  document_schema_modules_by_dialect_id[dialect_uri] ||= begin
    metaschema = JSI.registry.find(dialect_uri)
    dynamic_anchor_map = metaschema.jsi_next_schema_dynamic_anchor_map
    unless dynamic_anchor_map.key?('meta')
      # hax: pretend that the identified meta-schema has `$dynamicAnchor: "meta"`
      # this enables e.g. `jsonSchemaDialect: "http://json-schema.org/draft-07/schema"` to work
      # this is non-API JSI internals.
      dynamic_anchor_map = dynamic_anchor_map.merge({
        'meta' => [metaschema, [].freeze].freeze,
      }).freeze
    end
    document_schema = Unscoped::Document.schema.jsi_with_schema_dynamic_anchor_map(dynamic_anchor_map)
    set_up_document_schema_module(document_schema.jsi_schema_module)
  end
end

.new_document(instance, **new_param) ⇒ JSI::Base + Scorpio::OpenAPI::V3_1::Document

Instantiates instance v3.1 OAD with schemas of the dialect indicated by jsonSchemaDialect

Parameters:

  • instance (#to_hash)

Returns:



91
92
93
94
95
96
97
# File 'lib/scorpio/openapi/v3_1.rb', line 91

def self.new_document(instance, **new_param)
  #jsonSchemaDialect = Scorpio::OpenAPI::V3_1::Unscoped::Document.new_jsi(instance, **new_param).jsonSchemaDialect(use_default: true)
  jsonSchemaDialect = instance.fetch('jsonSchemaDialect') { Unscoped::Document.properties['jsonSchemaDialect'].default }
  document_schema_module = document_schema_module_by_dialect_id(jsonSchemaDialect)

  document_schema_module.new_jsi(instance, **new_param)
end

.set_up_document_schema_module(document_schema_module) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/scorpio/openapi/v3_1.rb', line 46

def self.set_up_document_schema_module(document_schema_module)
  document_schema_module.include(OpenAPI::V3_1::Document)
  document_schema_module.defs['response'].include(OpenAPI::Response)
  document_schema_module.defs['operation'].include(OpenAPI::Operation::V3Methods)
  document_schema_module.defs['reference'].include(OpenAPI::Reference)
  document_schema_module.defs['tag'].include(OpenAPI::Tag)
  document_schema_module.defs['server'].include(OpenAPI::Server)
  document_schema_module.defs['paths'].include(OpenAPI::Paths)
  document_schema_module.defs['path-item'].include(OpenAPI::PathItem)
  document_schema_module.defs['path-item'].include(OpenAPI::Reference)
  document_schema_module.defs['security-scheme'].include(OpenAPI::SecurityScheme)

  document_schema_module
end