open_api_2_json_schema 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/CHANGELOG.md +3 -1
- data/lib/attribute_handlers/all_of.rb +21 -0
- data/lib/open_api_2_json_schema/version.rb +1 -1
- data/lib/open_api_2_json_schema.rb +9 -4
- data/lib/ref_schema_parser.rb +35 -0
- metadata +5 -4
- data/Gemfile.lock +0 -66
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b7f9dc1848c8bc1810adba623e3a909937e781a8d916530f008791d9fafbb03
|
4
|
+
data.tar.gz: 17847b2a3ae5114c7a331e811c59dc062d018aa8646b109c099fb35260d497db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81d9359bc0f47a7ce8f7660af70bb44190b0c241f33b1031b1eeb04d51358dae4962f7259f83dbed511caa8a62bdac137db5050932b0d3f891d245d963d0b9b7
|
7
|
+
data.tar.gz: 8019ae57689e5386956cd2222c1f4a457ba37e9dd03942095486fec12d60297bddcb8fb935db709ab32041e09530af2a3dbb790195565eed3e76db1720c62e66
|
data/.rspec
CHANGED
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative '../ref_schema_parser'
|
2
|
+
|
3
|
+
module OpenApi2JsonSchema
|
4
|
+
module AttributeHandlers
|
5
|
+
class AllOf
|
6
|
+
def call(all_of_schemas)
|
7
|
+
raise 'allOf schema must be an Array' unless all_of_schemas.is_a?(Array)
|
8
|
+
|
9
|
+
all_of_schemas.each_with_index.map do |schema, index|
|
10
|
+
raise "Invalid schema: allOf[#{index}]" unless schema.is_a?(Hash)
|
11
|
+
|
12
|
+
if schema['$ref']
|
13
|
+
OpenApi2JsonSchema::RefSchemaParser.new.call(schema['$ref'])
|
14
|
+
else
|
15
|
+
schema
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -3,11 +3,12 @@
|
|
3
3
|
require 'json'
|
4
4
|
require 'yaml'
|
5
5
|
require_relative "open_api_2_json_schema/version"
|
6
|
+
require_relative 'attribute_handlers/all_of'
|
6
7
|
|
7
8
|
module OpenApi2JsonSchema
|
8
9
|
module_function
|
9
10
|
|
10
|
-
STRUCTS = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties']
|
11
|
+
STRUCTS = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties', 'schema']
|
11
12
|
NOT_SUPPORTED = [
|
12
13
|
'nullable', 'discriminator', 'readOnly',
|
13
14
|
'writeOnly', 'xml', 'externalDocs',
|
@@ -47,11 +48,15 @@ module OpenApi2JsonSchema
|
|
47
48
|
def convert_schema(schema)
|
48
49
|
json_schema = schema.except(*NOT_SUPPORTED)
|
49
50
|
|
50
|
-
schema.slice(STRUCTS).each do |struct, data|
|
51
|
+
schema.slice(*STRUCTS).each do |struct, data|
|
51
52
|
case data
|
52
53
|
when Array
|
53
|
-
|
54
|
-
|
54
|
+
if struct == 'allOf'
|
55
|
+
json_schema[struct] = AttributeHandlers::AllOf.new.call(data)
|
56
|
+
else
|
57
|
+
json_schema[struct] = data.map do |item|
|
58
|
+
convert_schema(item)
|
59
|
+
end
|
55
60
|
end
|
56
61
|
when Hash
|
57
62
|
json_schema[struct] = convert_schema(data)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module OpenApi2JsonSchema
|
4
|
+
class RefSchemaParser
|
5
|
+
def call(ref_path)
|
6
|
+
file_path, fragments = normalize_path(ref_path)
|
7
|
+
ref_schema = load_file(file_path)
|
8
|
+
fragment_schema = ref_schema
|
9
|
+
|
10
|
+
if fragments
|
11
|
+
fragments.split('/').each do |fragment|
|
12
|
+
if fragment && fragment != '' && fragment_schema[fragment]
|
13
|
+
fragment_schema = fragment_schema[fragment]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
fragment_schema
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def normalize_path(path)
|
24
|
+
temp_path = path.split('#')
|
25
|
+
raise 'Invalid reference path' if temp_path.size > 2
|
26
|
+
|
27
|
+
temp_path
|
28
|
+
end
|
29
|
+
|
30
|
+
def load_file(path)
|
31
|
+
data = File.read(path)
|
32
|
+
YAML.safe_load(data)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_api_2_json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hieuk09
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: 'Due to the OpenAPI v3.0 and JSON Schema discrepancy, you can use this
|
14
14
|
JS library to convert OpenAPI Schema objects to proper JSON Schema. '
|
@@ -22,14 +22,15 @@ files:
|
|
22
22
|
- ".rubocop.yml"
|
23
23
|
- CHANGELOG.md
|
24
24
|
- Gemfile
|
25
|
-
- Gemfile.lock
|
26
25
|
- LICENSE.txt
|
27
26
|
- README.md
|
28
27
|
- Rakefile
|
29
28
|
- bin/console
|
30
29
|
- bin/setup
|
30
|
+
- lib/attribute_handlers/all_of.rb
|
31
31
|
- lib/open_api_2_json_schema.rb
|
32
32
|
- lib/open_api_2_json_schema/version.rb
|
33
|
+
- lib/ref_schema_parser.rb
|
33
34
|
homepage: https://github.com/hieuk09/open_api_2_json_schema
|
34
35
|
licenses:
|
35
36
|
- MIT
|
@@ -51,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
52
|
- !ruby/object:Gem::Version
|
52
53
|
version: '0'
|
53
54
|
requirements: []
|
54
|
-
rubygems_version: 3.
|
55
|
+
rubygems_version: 3.0.8
|
55
56
|
signing_key:
|
56
57
|
specification_version: 4
|
57
58
|
summary: OpenAPI v3.0 schema to JSON Schema draft 4 converter
|
data/Gemfile.lock
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
open_api_2_json_schema (0.1.0)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
ast (2.4.2)
|
10
|
-
attr_extras (6.2.5)
|
11
|
-
byebug (11.1.3)
|
12
|
-
diff-lcs (1.5.0)
|
13
|
-
optimist (3.0.1)
|
14
|
-
parallel (1.22.1)
|
15
|
-
parser (3.1.1.0)
|
16
|
-
ast (~> 2.4.1)
|
17
|
-
patience_diff (1.2.0)
|
18
|
-
optimist (~> 3.0)
|
19
|
-
rainbow (3.1.1)
|
20
|
-
rake (13.0.6)
|
21
|
-
regexp_parser (2.2.1)
|
22
|
-
rexml (3.2.5)
|
23
|
-
rspec (3.11.0)
|
24
|
-
rspec-core (~> 3.11.0)
|
25
|
-
rspec-expectations (~> 3.11.0)
|
26
|
-
rspec-mocks (~> 3.11.0)
|
27
|
-
rspec-core (3.11.0)
|
28
|
-
rspec-support (~> 3.11.0)
|
29
|
-
rspec-expectations (3.11.0)
|
30
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
-
rspec-support (~> 3.11.0)
|
32
|
-
rspec-mocks (3.11.0)
|
33
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
-
rspec-support (~> 3.11.0)
|
35
|
-
rspec-support (3.11.0)
|
36
|
-
rubocop (1.26.1)
|
37
|
-
parallel (~> 1.10)
|
38
|
-
parser (>= 3.1.0.0)
|
39
|
-
rainbow (>= 2.2.2, < 4.0)
|
40
|
-
regexp_parser (>= 1.8, < 3.0)
|
41
|
-
rexml
|
42
|
-
rubocop-ast (>= 1.16.0, < 2.0)
|
43
|
-
ruby-progressbar (~> 1.7)
|
44
|
-
unicode-display_width (>= 1.4.0, < 3.0)
|
45
|
-
rubocop-ast (1.16.0)
|
46
|
-
parser (>= 3.1.1.0)
|
47
|
-
ruby-progressbar (1.11.0)
|
48
|
-
super_diff (0.8.0)
|
49
|
-
attr_extras (>= 6.2.4)
|
50
|
-
diff-lcs
|
51
|
-
patience_diff
|
52
|
-
unicode-display_width (2.1.0)
|
53
|
-
|
54
|
-
PLATFORMS
|
55
|
-
x86_64-darwin-19
|
56
|
-
|
57
|
-
DEPENDENCIES
|
58
|
-
byebug
|
59
|
-
open_api_2_json_schema!
|
60
|
-
rake (~> 13.0)
|
61
|
-
rspec (~> 3.0)
|
62
|
-
rubocop (~> 1.21)
|
63
|
-
super_diff
|
64
|
-
|
65
|
-
BUNDLED WITH
|
66
|
-
2.2.32
|