open_api_2_json_schema 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb64e501ef7cce37f6a1cd60f7ce61b7fff4688800699281dad4202cace7a9d9
4
- data.tar.gz: c8ac0fa10f9eacfd26e8e862ad56fb4b09e64998965fdd3dcbfd8711a95ef8df
3
+ metadata.gz: 8f25b393383b6694b23d0306c5a48f91762ba658afbd488c15fa16c40cbfa246
4
+ data.tar.gz: 5bc71e2ab8ff4019428686c39b17cc46f8ea5445d4d181af6fca65aa41b8cdd1
5
5
  SHA512:
6
- metadata.gz: 0d15e51a8346868a3941eaff9f6741c33490cf155f47f8459ae8c46967d9222d86d643b0ea62943042dd16ff64653a1950c7c6816782acc62ac2f968f2ce1bc8
7
- data.tar.gz: 18e5244cbeebc8591542eea1a0bd85cc5e7bd9721ff798890f3506c87398775f0ac471087b931cfb31b48b68ad09d450d4b731df7b2f02090256ae746e29a1d4
6
+ metadata.gz: 3263c6c3f5372ff6e6cc93a812ce429ce1ab03d64b1d7eb5cfb23e9291f48b162accca973247dfca2fbc1da1b851089d5a4b21d663cfd0a300fe4fb368f69c1c
7
+ data.tar.gz: 828d603767e21eab7b4197d4e3c8217b0cd816464e1726916c9e42571b09c0bcfdff8e1e3622f0f07928f74b67f6d54c87c752ec890dc0902119343181518ce8
data/.rspec CHANGED
@@ -1,3 +1,3 @@
1
- --format documentation
1
+ --format progress
2
2
  --color
3
3
  --require spec_helper
data/CHANGELOG.md CHANGED
@@ -1,4 +1,10 @@
1
- ## [Unreleased]
1
+ ## [0.1.2] - 2022-10-19
2
+
3
+ - Support `discriminator`
4
+
5
+ ## [0.1.1] - 2022-09-23
6
+
7
+ - Support `$ref` and `allOf`
2
8
 
3
9
  ## [0.1.0] - 2022-03-26
4
10
 
data/README.md CHANGED
@@ -34,6 +34,46 @@ schema = {
34
34
  OpenApi2JsonSchema.convert(schema)
35
35
  ```
36
36
 
37
+ ### Discriminator
38
+
39
+ ```yaml
40
+ # Sample YAML file
41
+ schema:
42
+ discriminator:
43
+ propertyName: key
44
+ mapping:
45
+ value1: 'path-to-file-2.yml'
46
+ value2: 'path-to-file-2.yml'
47
+ properties:
48
+ key:
49
+ type: string
50
+ enum:
51
+ - value1
52
+ - value2
53
+ ```
54
+
55
+ ```json
56
+ "schema": {
57
+ "properties": {
58
+ "key": {
59
+ "type": "string",
60
+ "enum": ["value1", "value2"]
61
+ }
62
+ },
63
+ "discriminator": {
64
+ "propertyName": "name",
65
+ "mapping": {
66
+ "value1": {
67
+ "type": "object"
68
+ },
69
+ "value2": {
70
+ "type": "array"
71
+ }
72
+ }
73
+ }
74
+ }
75
+ ```
76
+
37
77
  ## Development
38
78
 
39
79
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -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
@@ -0,0 +1,18 @@
1
+ require_relative "../ref_schema_parser"
2
+
3
+ module OpenApi2JsonSchema
4
+ module AttributeHandlers
5
+ class Discriminator
6
+ def call(discriminator_schema)
7
+ raise "discriminator schema must be a Hash" unless discriminator_schema.is_a?(Hash)
8
+
9
+ mapping = discriminator_schema.fetch("mapping")
10
+ mapping.each do |key, file_path|
11
+ mapping[key] = OpenApi2JsonSchema::RefSchemaParser.new.call(file_path)
12
+ end
13
+
14
+ discriminator_schema
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenApi2JsonSchema
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -3,13 +3,15 @@
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'
7
+ require_relative 'attribute_handlers/discriminator'
6
8
 
7
9
  module OpenApi2JsonSchema
8
10
  module_function
9
11
 
10
- STRUCTS = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties']
12
+ STRUCTS = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties', 'schema', 'discriminator']
11
13
  NOT_SUPPORTED = [
12
- 'nullable', 'discriminator', 'readOnly',
14
+ 'nullable', 'readOnly',
13
15
  'writeOnly', 'xml', 'externalDocs',
14
16
  'example', 'deprecated'
15
17
  ]
@@ -47,14 +49,22 @@ module OpenApi2JsonSchema
47
49
  def convert_schema(schema)
48
50
  json_schema = schema.except(*NOT_SUPPORTED)
49
51
 
50
- schema.slice(STRUCTS).each do |struct, data|
52
+ schema.slice(*STRUCTS).each do |struct, data|
51
53
  case data
52
54
  when Array
53
- json_schema[struct] = data.map do |item|
54
- convert_schema(item)
55
+ if struct == 'allOf'
56
+ json_schema[struct] = AttributeHandlers::AllOf.new.call(data)
57
+ else
58
+ json_schema[struct] = data.map do |item|
59
+ convert_schema(item)
60
+ end
55
61
  end
56
62
  when Hash
57
- json_schema[struct] = convert_schema(data)
63
+ if struct == 'discriminator'
64
+ json_schema[struct] = AttributeHandlers::Discriminator.new.call(data)
65
+ else
66
+ json_schema[struct] = convert_schema(data)
67
+ end
58
68
  else
59
69
  # ignore
60
70
  end
@@ -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.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - hieuk09
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-26 00:00:00.000000000 Z
11
+ date: 2022-10-19 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,16 @@ 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
+ - lib/attribute_handlers/discriminator.rb
31
32
  - lib/open_api_2_json_schema.rb
32
33
  - lib/open_api_2_json_schema/version.rb
34
+ - lib/ref_schema_parser.rb
33
35
  homepage: https://github.com/hieuk09/open_api_2_json_schema
34
36
  licenses:
35
37
  - MIT
@@ -51,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
53
  - !ruby/object:Gem::Version
52
54
  version: '0'
53
55
  requirements: []
54
- rubygems_version: 3.2.32
56
+ rubygems_version: 3.0.8
55
57
  signing_key:
56
58
  specification_version: 4
57
59
  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