r2-oas 0.3.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +55 -1
- data/GEMSPEC.md +2 -2
- data/README.ja.md +73 -43
- data/README.md +75 -39
- data/lib/r2-oas.rb +4 -5
- data/lib/r2-oas/app_configuration.rb +17 -5
- data/lib/r2-oas/app_configuration/deprecation.rb +28 -0
- data/lib/r2-oas/configuration.rb +57 -11
- data/lib/r2-oas/configuration/paths_config.rb +6 -3
- data/lib/r2-oas/deploy/client.rb +1 -1
- data/lib/r2-oas/errors.rb +5 -0
- data/lib/r2-oas/helpers/file_helper.rb +68 -0
- data/lib/r2-oas/hooks/hook.rb +7 -5
- data/lib/r2-oas/lib/core_ext/all.rb +5 -0
- data/lib/r2-oas/lib/core_ext/string/filters.rb +29 -0
- data/lib/r2-oas/plugin/base.rb +44 -0
- data/lib/r2-oas/plugin/executor.rb +148 -0
- data/lib/r2-oas/plugin/hookable.rb +47 -0
- data/lib/r2-oas/plugin/public.rb +3 -0
- data/lib/r2-oas/plugin/transform/transform.rb +10 -0
- data/lib/r2-oas/plugin/transform/v3/transform.rb +20 -0
- data/lib/r2-oas/plugin/transform/v3/visitable.rb +37 -0
- data/lib/r2-oas/public.rb +3 -0
- data/lib/r2-oas/schema/base.rb +1 -1
- data/lib/r2-oas/schema/builder.rb +1 -1
- data/lib/r2-oas/schema/editor.rb +9 -2
- data/lib/r2-oas/schema/monitor.rb +1 -1
- data/lib/r2-oas/schema/ui.rb +1 -1
- data/lib/r2-oas/schema/v3/analyzer/path_analyzer.rb +1 -1
- data/lib/r2-oas/schema/v3/builder.rb +1 -1
- data/lib/r2-oas/schema/v3/builder/base_builder.rb +14 -2
- data/lib/r2-oas/schema/v3/builder/doc_builder.rb +10 -2
- data/lib/r2-oas/schema/v3/generator/base_generator.rb +7 -2
- data/lib/r2-oas/schema/v3/generator/doc_generator.rb +1 -1
- data/lib/r2-oas/schema/v3/manager/file/include_ref_base_file_manager.rb +3 -2
- data/lib/r2-oas/schema/v3/object/from_files/base_object.rb +62 -0
- data/lib/r2-oas/schema/v3/object/from_files/components/request_body_object.rb +103 -0
- data/lib/r2-oas/schema/v3/object/from_files/components/schema_object.rb +102 -0
- data/lib/r2-oas/schema/v3/object/from_files/components_object.rb +46 -0
- data/lib/r2-oas/schema/v3/object/from_files/external_document_object.rb +23 -0
- data/lib/r2-oas/schema/v3/object/from_files/info_object.rb +23 -0
- data/lib/r2-oas/schema/v3/object/from_files/openapi_object.rb +61 -0
- data/lib/r2-oas/schema/v3/object/from_files/path_item_object.rb +110 -0
- data/lib/r2-oas/schema/v3/object/from_files/paths_object.rb +33 -0
- data/lib/r2-oas/schema/v3/object/from_files/utils/all.rb +4 -0
- data/lib/r2-oas/schema/v3/object/from_files/utils/deep_methods.rb +27 -0
- data/lib/r2-oas/schema/v3/object/from_files/utils/refs.rb +151 -0
- data/lib/r2-oas/schema/v3/object/{public.rb → from_routes/all.rb} +0 -0
- data/lib/r2-oas/schema/v3/object/from_routes/base_object.rb +41 -0
- data/lib/r2-oas/schema/v3/object/{components → from_routes/components}/request_body_object.rb +19 -28
- data/lib/r2-oas/schema/v3/object/{components → from_routes/components}/schema_object.rb +12 -16
- data/lib/r2-oas/schema/v3/object/{components_object.rb → from_routes/components_object.rb} +19 -13
- data/lib/r2-oas/schema/v3/object/{external_document_object.rb → from_routes/external_document_object.rb} +9 -2
- data/lib/r2-oas/schema/v3/object/{info_object.rb → from_routes/info_object.rb} +9 -2
- data/lib/r2-oas/schema/v3/object/{openapi_object.rb → from_routes/openapi_object.rb} +16 -11
- data/lib/r2-oas/schema/v3/object/{path_item_object.rb → from_routes/path_item_object.rb} +8 -8
- data/lib/r2-oas/schema/v3/object/{paths_object.rb → from_routes/paths_object.rb} +13 -30
- data/lib/r2-oas/schema/v3/object/{server_object.rb → from_routes/server_object.rb} +0 -0
- data/lib/r2-oas/schema/v3/object/{tag_object.rb → from_routes/tag_object.rb} +2 -1
- data/lib/r2-oas/schema/v3/object/store.rb +54 -0
- data/lib/r2-oas/shared/all.rb +1 -0
- data/lib/r2-oas/shared/callable.rb +17 -0
- data/lib/r2-oas/store.rb +20 -16
- data/lib/r2-oas/support/deprecation.rb +26 -0
- data/lib/r2-oas/support/deprecation/behavior.rb +21 -0
- data/lib/r2-oas/support/deprecation/instance_delegator.rb +42 -0
- data/lib/r2-oas/support/deprecation/reporting.rb +91 -0
- data/lib/r2-oas/task.rb +1 -2
- data/lib/r2-oas/task_logging.rb +3 -7
- data/lib/r2-oas/tasks/common.rake +1 -2
- data/lib/r2-oas/tasks/main.rake +59 -9
- data/lib/r2-oas/version.rb +1 -1
- data/r2-oas.gemspec +2 -7
- metadata +55 -69
- data/.gitignore +0 -16
- data/.rspec +0 -3
- data/.rubocop.yml +0 -10
- data/.rubocop_todo.yml +0 -291
- data/.travis.yml +0 -26
- data/Appraisals +0 -13
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -12
- data/Rakefile +0 -8
- data/lib/r2-oas/app_configuration/tool.rb +0 -31
- data/lib/r2-oas/app_configuration/tool/paths/stats.rb +0 -43
- data/lib/r2-oas/pluggable_configuration.rb +0 -33
- data/lib/r2-oas/plugins/schema/v3/object/hookable_base_object.rb +0 -100
- data/lib/r2-oas/schema/v3/object/base_object.rb +0 -65
- data/lib/r2-oas/tasks/tool.rake +0 -83
- data/lib/r2-oas/tool/paths/ls.rb +0 -15
- data/lib/r2-oas/tool/paths/stats.rb +0 -84
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'r2-oas/shared/callable'
|
4
|
+
require 'r2-oas/errors'
|
5
|
+
require_relative '../base_object'
|
6
|
+
require_relative '../utils/all'
|
7
|
+
|
8
|
+
module R2OAS
|
9
|
+
module Schema
|
10
|
+
module V3
|
11
|
+
module FromFiles
|
12
|
+
module Components
|
13
|
+
class SchemaObject < ::R2OAS::Schema::V3::FromFiles::BaseObject
|
14
|
+
include ::R2OAS::Callable
|
15
|
+
include DeepMethods
|
16
|
+
|
17
|
+
def initialize(doc, ref, opts)
|
18
|
+
super(opts)
|
19
|
+
@doc = doc
|
20
|
+
@parent_ref = Components::SchemaRef.new(ref)
|
21
|
+
resolve_dependencies!
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_doc
|
25
|
+
call_ref_path!
|
26
|
+
|
27
|
+
# MEMO:
|
28
|
+
# If it is overwritten, it may lead to unexpected problems, so give a copy
|
29
|
+
execute_transform_plugins(:components_schema, @doc, ref_dup)
|
30
|
+
@doc
|
31
|
+
end
|
32
|
+
|
33
|
+
def resolve_dependencies!
|
34
|
+
deep_replace!(@doc, '$ref') do |ref_path|
|
35
|
+
schema_obj, schema_type, pure_schema_name = ref_path.split('/').slice(1..-1)
|
36
|
+
schema_doc = root_doc&.fetch(schema_obj, nil)&.fetch(schema_type, nil)&.fetch(pure_schema_name, nil) || {}
|
37
|
+
|
38
|
+
ref = create_child_ref(pure_schema_name)
|
39
|
+
obj = Components::SchemaObject.new(schema_doc, ref, opts)
|
40
|
+
|
41
|
+
obj_store.add('components/schemas', pure_schema_name, obj)
|
42
|
+
obj
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def call_ref_path!
|
47
|
+
callback = proc { |obj| obj.ref_path }
|
48
|
+
deep_call(@doc, '$ref', callback)
|
49
|
+
end
|
50
|
+
|
51
|
+
def schema_name
|
52
|
+
return @resolved_schema_name if @resolved_schema_name.present?
|
53
|
+
|
54
|
+
before_schema_name = ref_dup[:schema_name]
|
55
|
+
|
56
|
+
_ref_dup = ref_dup
|
57
|
+
execute_transform_plugins(:components_schema_name, _ref_dup)
|
58
|
+
@resolved_schema_name = _ref_dup[:schema_name]
|
59
|
+
|
60
|
+
if before_schema_name != @resolved_schema_name
|
61
|
+
if reserved_schema_name_list.include?(@resolved_schema_name)
|
62
|
+
raise DepulicateSchemaNameError, "Transformed schema name: '#{@resolved_schema_name}' cannot be used. It already exists."
|
63
|
+
else
|
64
|
+
obj_store.appended_components_schema_name_list.push(@resolved_schema_name)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
@resolved_schema_name
|
69
|
+
end
|
70
|
+
|
71
|
+
def ref_path
|
72
|
+
"#/components/schemas/#{schema_name}"
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def ref_dup
|
78
|
+
@parent_ref.dup
|
79
|
+
end
|
80
|
+
|
81
|
+
def reserved_schema_name_list
|
82
|
+
(
|
83
|
+
obj_store.components_schema_name_list +
|
84
|
+
obj_store.appended_components_schema_name_list
|
85
|
+
).uniq
|
86
|
+
end
|
87
|
+
|
88
|
+
def create_child_ref(schema_name)
|
89
|
+
local_ref_hash = ref_dup.to_h
|
90
|
+
parent_schema_name = local_ref_hash[:schema_name]
|
91
|
+
depth = local_ref_hash[:depth] + 1
|
92
|
+
ref_data = local_ref_hash.merge({ from: :schema, schema_name: schema_name, parent_schema_name: parent_schema_name, depth: depth })
|
93
|
+
ref = Components::SchemaRef.new(ref_data)
|
94
|
+
ref.send(:parent=, ref_dup)
|
95
|
+
ref
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base_object'
|
4
|
+
require_relative 'components/schema_object'
|
5
|
+
require_relative 'components/request_body_object'
|
6
|
+
|
7
|
+
module R2OAS
|
8
|
+
module Schema
|
9
|
+
module V3
|
10
|
+
module FromFiles
|
11
|
+
class ComponentsObject < BaseObject
|
12
|
+
def initialize(doc, opts = {})
|
13
|
+
super(opts)
|
14
|
+
@doc = doc
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_doc
|
18
|
+
create_doc
|
19
|
+
@doc
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def create_doc
|
25
|
+
create_components_schema_docs!
|
26
|
+
create_components_request_body_docs!
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_components_schema_docs!
|
30
|
+
result = obj_store.gets('components/schemas').each_with_object({}) do |obj, data|
|
31
|
+
data[obj.schema_name] = obj.to_doc
|
32
|
+
end
|
33
|
+
@doc.merge!('schemas' => result) if result.present?
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_components_request_body_docs!
|
37
|
+
result = obj_store.gets('components/requestBodies').each_with_object({}) do |obj, data|
|
38
|
+
data[obj.schema_name] = obj.to_doc
|
39
|
+
end
|
40
|
+
@doc.merge!('requestBodies' => result) if result.present?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base_object'
|
4
|
+
|
5
|
+
module R2OAS
|
6
|
+
module Schema
|
7
|
+
module V3
|
8
|
+
module FromFiles
|
9
|
+
class ExternalDocumentObject < BaseObject
|
10
|
+
def initialize(doc, opts = {})
|
11
|
+
super(opts)
|
12
|
+
@doc = doc
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_doc
|
16
|
+
execute_transform_plugins(:external_document, @doc)
|
17
|
+
@doc
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base_object'
|
4
|
+
|
5
|
+
module R2OAS
|
6
|
+
module Schema
|
7
|
+
module V3
|
8
|
+
module FromFiles
|
9
|
+
class InfoObject < BaseObject
|
10
|
+
def initialize(doc, opts = {})
|
11
|
+
super(opts)
|
12
|
+
@doc = doc
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_doc
|
16
|
+
execute_transform_plugins(:info, @doc)
|
17
|
+
@doc
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base_object'
|
4
|
+
require_relative 'info_object'
|
5
|
+
require_relative 'paths_object'
|
6
|
+
require_relative 'external_document_object'
|
7
|
+
require_relative 'components_object'
|
8
|
+
|
9
|
+
module R2OAS
|
10
|
+
module Schema
|
11
|
+
module V3
|
12
|
+
module FromFiles
|
13
|
+
class OpenapiObject < BaseObject
|
14
|
+
def initialize(doc, opts = {})
|
15
|
+
super(opts)
|
16
|
+
@doc = doc
|
17
|
+
set_root_doc(doc)
|
18
|
+
set_components_name_list(doc)
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_doc
|
22
|
+
execute_transform_plugins(:setup)
|
23
|
+
# MEMO:
|
24
|
+
# Make sure paths_doc is run first
|
25
|
+
# This is because the components object is stored in the store by executing paths_doc
|
26
|
+
# and it is looped to generate the component document.
|
27
|
+
result = {
|
28
|
+
'openapi' => '3.0.0',
|
29
|
+
'info' => info_doc,
|
30
|
+
'tags' => @doc['tags'],
|
31
|
+
'paths' => paths_doc,
|
32
|
+
'externalDocs' => external_docs_doc,
|
33
|
+
'servers' => @doc['servers'],
|
34
|
+
'components' => components_doc
|
35
|
+
}
|
36
|
+
execute_transform_plugins(:teardown)
|
37
|
+
result
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def info_doc
|
43
|
+
InfoObject.new(@doc['info'], @opts).to_doc
|
44
|
+
end
|
45
|
+
|
46
|
+
def paths_doc
|
47
|
+
PathsObject.new(@doc['paths'], @opts).to_doc
|
48
|
+
end
|
49
|
+
|
50
|
+
def external_docs_doc
|
51
|
+
ExternalDocumentObject.new(@doc['externalDocs'], @opts).to_doc
|
52
|
+
end
|
53
|
+
|
54
|
+
def components_doc
|
55
|
+
ComponentsObject.new(@doc['components'], @opts).to_doc
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'r2-oas/shared/callable'
|
4
|
+
require_relative 'base_object'
|
5
|
+
require_relative 'components/schema_object'
|
6
|
+
require_relative 'components/request_body_object'
|
7
|
+
require_relative 'utils/all'
|
8
|
+
|
9
|
+
module R2OAS
|
10
|
+
module Schema
|
11
|
+
module V3
|
12
|
+
module FromFiles
|
13
|
+
class PathItemObject < BaseObject
|
14
|
+
include ::R2OAS::Callable
|
15
|
+
include DeepMethods
|
16
|
+
|
17
|
+
def initialize(doc, ref, opts = {})
|
18
|
+
super(opts)
|
19
|
+
@doc = doc
|
20
|
+
@parent_ref = PathRef.new(ref)
|
21
|
+
resolve_dependencies!
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_doc
|
25
|
+
call_ref_path!
|
26
|
+
|
27
|
+
# MEMO:
|
28
|
+
# If it is overwritten, it may lead to unexpected problems, so give a copy
|
29
|
+
execute_transform_plugins(:path_item, @doc, ref_dup)
|
30
|
+
@doc
|
31
|
+
end
|
32
|
+
|
33
|
+
# Breaking changes to @doc
|
34
|
+
def resolve_dependencies!
|
35
|
+
local_ref_hash = ref_dup.to_h
|
36
|
+
|
37
|
+
@doc.each do |verb, data_when_verb|
|
38
|
+
local_ref_hash[:verb] = verb
|
39
|
+
local_ref_hash[:tag_name] = data_when_verb['tags'].first
|
40
|
+
|
41
|
+
resolve_dependencies_at_schema!(@doc, verb, data_when_verb, local_ref_hash)
|
42
|
+
resolve_dependencies_at_request_body!(@doc, verb, data_when_verb, local_ref_hash)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def call_ref_path!
|
47
|
+
callback = proc { |obj| obj.ref_path }
|
48
|
+
deep_call(@doc, '$ref', callback)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def ref_dup
|
54
|
+
@parent_ref.dup
|
55
|
+
end
|
56
|
+
|
57
|
+
def resolve_dependencies_at_schema!(data, verb, data_when_verb, local_ref_hash)
|
58
|
+
data_when_verb['responses'].each do |http_status, data_when_http_status|
|
59
|
+
local_ref_hash[:http_status] = http_status
|
60
|
+
|
61
|
+
deep_replace!(data_when_http_status, '$ref') do |ref_path|
|
62
|
+
schema_obj, schema_type, schema_name = ref_path.split('/').slice(1..-1)
|
63
|
+
schema_doc = root_doc&.fetch(schema_obj, nil)&.fetch(schema_type, nil)&.fetch(schema_name, nil) || {}
|
64
|
+
|
65
|
+
ref = create_child_schema_ref(schema_name, local_ref_hash)
|
66
|
+
obj = Components::SchemaObject.new(schema_doc, ref, opts)
|
67
|
+
|
68
|
+
obj_store.add('components/schemas', schema_name, obj)
|
69
|
+
obj
|
70
|
+
end
|
71
|
+
|
72
|
+
data[verb]['responses'][http_status] = data_when_http_status
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def resolve_dependencies_at_request_body!(data, verb, data_when_verb, local_ref_hash)
|
77
|
+
deep_replace!(data_when_verb['requestBody'], '$ref') do |ref_path|
|
78
|
+
schema_obj, schema_type, schema_name = ref_path.split('/').slice(1..-1)
|
79
|
+
schema_doc = root_doc&.fetch(schema_obj, nil)&.fetch(schema_type, nil)&.fetch(schema_name, nil) || {}
|
80
|
+
|
81
|
+
ref = create_child_request_body_ref(schema_name, local_ref_hash)
|
82
|
+
obj = Components::RequestBodyObject.new(schema_doc, ref, opts)
|
83
|
+
|
84
|
+
obj_store.add('components/requestBodies', schema_name, obj)
|
85
|
+
|
86
|
+
data[verb]['requestBody']['$ref'] = obj
|
87
|
+
obj
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def create_child_schema_ref(schema_name, local_ref_hash)
|
92
|
+
local_ref_hash_dup = local_ref_hash.dup
|
93
|
+
ref_data = local_ref_hash_dup.merge({ from: :path_item, schema_name: schema_name, depth: 0 })
|
94
|
+
ref = Components::SchemaRef.new(ref_data)
|
95
|
+
end
|
96
|
+
|
97
|
+
def create_child_request_body_ref(schema_name, local_ref_hash)
|
98
|
+
local_ref_hash_dup = local_ref_hash.dup
|
99
|
+
# MEMO:
|
100
|
+
# requestBody does not depend on http_status
|
101
|
+
local_ref_hash_dup.delete(:http_status)
|
102
|
+
|
103
|
+
ref_data = local_ref_hash_dup.merge({ from: :path_item, schema_name: schema_name, depth: 0 })
|
104
|
+
ref = Components::RequestBodyRef.new(ref_data)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base_object'
|
4
|
+
require_relative 'path_item_object'
|
5
|
+
|
6
|
+
module R2OAS
|
7
|
+
module Schema
|
8
|
+
module V3
|
9
|
+
module FromFiles
|
10
|
+
class PathsObject < BaseObject
|
11
|
+
def initialize(doc, opts = {})
|
12
|
+
super(opts)
|
13
|
+
@doc = doc
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_doc
|
17
|
+
create_doc
|
18
|
+
@doc
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def create_doc
|
24
|
+
@doc.each do |path, doc_when_path|
|
25
|
+
ref = { path: path }
|
26
|
+
@doc[path] = PathItemObject.new(doc_when_path, ref, opts).to_doc
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module R2OAS
|
4
|
+
module Schema
|
5
|
+
module V3
|
6
|
+
module FromFiles
|
7
|
+
module DeepMethods
|
8
|
+
def deep_replace!(data, target, &blk)
|
9
|
+
return unless data.is_a?(Hash)
|
10
|
+
|
11
|
+
data.each do |key, value|
|
12
|
+
if key.eql? target
|
13
|
+
# MEMO:
|
14
|
+
# When using the same schema, it has already been replaced by an object
|
15
|
+
if value.is_a?(String)
|
16
|
+
data[key] = block_given? ? yield(value) : value
|
17
|
+
end
|
18
|
+
else
|
19
|
+
deep_replace!(value, target, &blk)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'r2-oas/errors'
|
4
|
+
|
5
|
+
module R2OAS
|
6
|
+
module Schema
|
7
|
+
module V3
|
8
|
+
module FromFiles
|
9
|
+
class BaseRef
|
10
|
+
def pretty_print(q)
|
11
|
+
pp_hash(q, to_h)
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :parent
|
15
|
+
|
16
|
+
def []=(key, value)
|
17
|
+
if writable_keys.include?(key)
|
18
|
+
send(:"#{key}=", value)
|
19
|
+
value
|
20
|
+
else
|
21
|
+
display_key = key.is_a?(Symbol) ? ":#{key}" : key
|
22
|
+
raise ::R2OAS::RefInvalidAssignment, "invalid method `[#{display_key}]=' called for #{self}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def [](key)
|
27
|
+
send(key)
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_h
|
31
|
+
valid_keys.each_with_object({}) do |key, result|
|
32
|
+
result[key] = send(key)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# MEMO:
|
39
|
+
# https://apidock.com/ruby/v1_9_3_392/PP/PPMethods/pp_hash
|
40
|
+
def pp_hash(q, obj)
|
41
|
+
q.group(1, '{', '}') do
|
42
|
+
q.seplist(obj, nil, :each_pair) do |k, v|
|
43
|
+
q.group do
|
44
|
+
q.pp k
|
45
|
+
q.text '=>'
|
46
|
+
q.group(1) do
|
47
|
+
q.breakable ''
|
48
|
+
q.pp v
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def build(ref_or_data, merge_data = {})
|
56
|
+
data = ref_or_data.to_h.merge(merge_data)
|
57
|
+
data.keys.each do |key|
|
58
|
+
instance_variable_set(:"@#{key}", data[key])
|
59
|
+
end
|
60
|
+
|
61
|
+
@parent = ref_or_data if ref_or_data.respond_to?(:parent)
|
62
|
+
end
|
63
|
+
|
64
|
+
attr_writer :parent
|
65
|
+
|
66
|
+
def valid_keys
|
67
|
+
raise NoImplementError, 'Please implement in inherited class.'
|
68
|
+
end
|
69
|
+
|
70
|
+
def writable_keys
|
71
|
+
raise NoImplementError, 'Please implement in inherited class.'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class PathRef < BaseRef
|
76
|
+
VALID_KEYS = %i[type path].freeze
|
77
|
+
|
78
|
+
attr_reader(*VALID_KEYS)
|
79
|
+
|
80
|
+
def initialize(data)
|
81
|
+
build(data, { type: :path_item })
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
attr_writer(*VALID_KEYS)
|
87
|
+
|
88
|
+
def valid_keys
|
89
|
+
VALID_KEYS
|
90
|
+
end
|
91
|
+
|
92
|
+
def writable_keys
|
93
|
+
[]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
module Components
|
98
|
+
class SchemaRef < BaseRef
|
99
|
+
READONLY_KEYS = %i[type path parent_schema_name depth tag_name verb http_status from].freeze
|
100
|
+
WRITABLE_KEYS = [:schema_name].freeze
|
101
|
+
VALID_KEYS = READONLY_KEYS + WRITABLE_KEYS
|
102
|
+
|
103
|
+
attr_reader(*VALID_KEYS)
|
104
|
+
attr_writer(*WRITABLE_KEYS)
|
105
|
+
|
106
|
+
def initialize(ref_or_data)
|
107
|
+
build(ref_or_data, { type: :schema })
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
attr_writer(*READONLY_KEYS)
|
113
|
+
|
114
|
+
def valid_keys
|
115
|
+
VALID_KEYS
|
116
|
+
end
|
117
|
+
|
118
|
+
def writable_keys
|
119
|
+
WRITABLE_KEYS
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class RequestBodyRef < BaseRef
|
124
|
+
READONLY_KEYS = %i[type path parent_schema_name depth tag_name verb from].freeze
|
125
|
+
WRITABLE_KEYS = [:schema_name].freeze
|
126
|
+
VALID_KEYS = READONLY_KEYS + WRITABLE_KEYS
|
127
|
+
|
128
|
+
attr_accessor(*VALID_KEYS)
|
129
|
+
attr_writer(*WRITABLE_KEYS)
|
130
|
+
|
131
|
+
def initialize(ref_or_data)
|
132
|
+
build(ref_or_data, { type: :request_body })
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
attr_writer(*READONLY_KEYS)
|
138
|
+
|
139
|
+
def valid_keys
|
140
|
+
VALID_KEYS
|
141
|
+
end
|
142
|
+
|
143
|
+
def writable_keys
|
144
|
+
WRITABLE_KEYS
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|