open_api_2_json_schema 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fb64e501ef7cce37f6a1cd60f7ce61b7fff4688800699281dad4202cace7a9d9
4
+ data.tar.gz: c8ac0fa10f9eacfd26e8e862ad56fb4b09e64998965fdd3dcbfd8711a95ef8df
5
+ SHA512:
6
+ metadata.gz: 0d15e51a8346868a3941eaff9f6741c33490cf155f47f8459ae8c46967d9222d86d643b0ea62943042dd16ff64653a1950c7c6816782acc62ac2f968f2ce1bc8
7
+ data.tar.gz: 18e5244cbeebc8591542eea1a0bd85cc5e7bd9721ff798890f3506c87398775f0ac471087b931cfb31b48b68ad09d450d4b731df7b2f02090256ae746e29a1d4
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-26
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in open_api_2_json_schema.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+ gem 'super_diff'
14
+ gem 'byebug'
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
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
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 hieuk09
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # OpenApi2JsonSchema
2
+
3
+ This is a simplified ruby port of [Javascript library
4
+ openapi-schema-to-json-schema](https://github.com/openapi-contrib/openapi-schema-to-json-schema)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'open_api_2_json_schema'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install open_api_2_json_schema
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ # if you want to convert from file
26
+ OpenApi2JsonSchema.convert_from_file("path/to/open_api.yml")
27
+
28
+ # if you want to convert from stringify hash
29
+ schema = {
30
+ 'type' => 'string',
31
+ 'format' => 'date-time',
32
+ 'nullable' => true
33
+ }
34
+ OpenApi2JsonSchema.convert(schema)
35
+ ```
36
+
37
+ ## Development
38
+
39
+ 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.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hieuk09/open_api_2_json_schema.
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "open_api_2_json_schema"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenApi2JsonSchema
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'yaml'
5
+ require_relative "open_api_2_json_schema/version"
6
+
7
+ module OpenApi2JsonSchema
8
+ module_function
9
+
10
+ STRUCTS = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties']
11
+ NOT_SUPPORTED = [
12
+ 'nullable', 'discriminator', 'readOnly',
13
+ 'writeOnly', 'xml', 'externalDocs',
14
+ 'example', 'deprecated'
15
+ ]
16
+ VALID_TYPES = ['integer', 'number', 'string', 'boolean', 'object', 'array', 'null']
17
+ VALID_FORMATS = ['date-time', 'email', 'hostname', 'ipv4', 'ipv6', 'uri', 'uri-reference']
18
+
19
+ MIN_INT_32 = 0 - 2 ** 31
20
+ MAX_INT_32 = 2 ** 31 - 1
21
+ MIN_INT_64 = 0 - 2 ** 63
22
+ MAX_INT_64 = 2 ** 63 - 1
23
+ MIN_FLOAT = 0 - 2 ** 128
24
+ MAX_FLOAT = 2 ** 128 - 1
25
+ MAX_DOUBLE = 1.7976931348623157e+308
26
+ MIN_DOUBLE = 0 - MAX_DOUBLE
27
+ BYTE_PATTERN = '^[\\w\\d+\\/=]*$'
28
+
29
+ class Error < StandardError; end
30
+ class InvalidTypeError < Error
31
+ def self.build(type)
32
+ new("Type #{type} is not a valid type")
33
+ end
34
+ end
35
+
36
+ def convert_from_file(path)
37
+ data = File.read(path)
38
+ convert(YAML.load(data))
39
+ end
40
+
41
+ def convert(schema)
42
+ convert_schema(schema).tap do |json_schema|
43
+ json_schema['$schema'] = 'http://json-schema.org/draft-04/schema#'
44
+ end.to_json
45
+ end
46
+
47
+ def convert_schema(schema)
48
+ json_schema = schema.except(*NOT_SUPPORTED)
49
+
50
+ schema.slice(STRUCTS).each do |struct, data|
51
+ case data
52
+ when Array
53
+ json_schema[struct] = data.map do |item|
54
+ convert_schema(item)
55
+ end
56
+ when Hash
57
+ json_schema[struct] = convert_schema(data)
58
+ else
59
+ # ignore
60
+ end
61
+ end
62
+
63
+ if schema.key?('properties')
64
+ new_properties = convert_properties(schema['properties'])
65
+
66
+ if schema['required'].is_a?(Array)
67
+ new_required = clean_required(schema['required'], schema['properties'])
68
+ json_schema['required'] = new_required if new_required.any?
69
+ end
70
+
71
+ json_schema['properties'] = new_properties if new_properties.any?
72
+ end
73
+
74
+ validate_type(schema['type'])
75
+
76
+ json_schema.merge!(convert_types(schema))
77
+ json_schema.merge!(convert_format(schema))
78
+
79
+ json_schema
80
+ end
81
+
82
+ def validate_type(type)
83
+ unless type.nil? || VALID_TYPES.include?(type)
84
+ raise InvalidTypeError.build(type)
85
+ end
86
+ end
87
+
88
+ def convert_properties(properties)
89
+ return {} unless properties.is_a?(Hash)
90
+
91
+ properties.reduce({}) do |result, (key, property)|
92
+ next result unless property.is_a?(Hash)
93
+
94
+ result.merge(key => convert_schema(property))
95
+ end
96
+ end
97
+
98
+ def convert_types(schema)
99
+ if schema['type'] && schema['nullable'] == true
100
+ type = [schema['type'], 'null']
101
+
102
+ if schema['enum'].is_a?(Array)
103
+ {
104
+ 'type' => type,
105
+ 'enum' => schema['enum'].append(nil)
106
+ }
107
+ else
108
+ { 'type' => type }
109
+ end
110
+ else
111
+ {}
112
+ end
113
+ end
114
+
115
+ def convert_format(schema)
116
+ case schema['format']
117
+ when 'int32'
118
+ {
119
+ 'minimum' => [MIN_INT_32, schema['minimum'] || MIN_INT_32].max,
120
+ 'maximum' => [MAX_INT_32, schema['maximum'] || MAX_INT_32].min
121
+ }
122
+ when 'int64'
123
+ {
124
+ 'minimum' => [MIN_INT_64, schema['minimum'] || MIN_INT_64].max,
125
+ 'maximum' => [MAX_INT_64, schema['maximum'] || MAX_INT_64].min
126
+ }
127
+ when 'float'
128
+ {
129
+ 'minimum' => [MIN_FLOAT, schema['minimum'] || MIN_FLOAT].max,
130
+ 'maximum' => [MAX_FLOAT, schema['maximum'] || MAX_FLOAT].min
131
+ }
132
+ when 'double'
133
+ {
134
+ 'minimum' => [MIN_DOUBLE, schema['minimum'] || MIN_DOUBLE].max,
135
+ 'maximum' => [MAX_DOUBLE, schema['maximum'] || MAX_DOUBLE].min
136
+ }
137
+ when 'byte'
138
+ { 'pattern' => BYTE_PATTERN }
139
+ else
140
+ {}
141
+ end
142
+ end
143
+
144
+ def clean_required(required, properties)
145
+ required ||= []
146
+ properties ||= {}
147
+
148
+ properties.slice(*required).keys
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_api_2_json_schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hieuk09
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Due to the OpenAPI v3.0 and JSON Schema discrepancy, you can use this
14
+ JS library to convert OpenAPI Schema objects to proper JSON Schema. '
15
+ email:
16
+ - hieuk09@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - lib/open_api_2_json_schema.rb
32
+ - lib/open_api_2_json_schema/version.rb
33
+ homepage: https://github.com/hieuk09/open_api_2_json_schema
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://github.com/hieuk09/open_api_2_json_schema
38
+ source_code_uri: https://github.com/hieuk09/open_api_2_json_schema
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.6.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.2.32
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: OpenAPI v3.0 schema to JSON Schema draft 4 converter
58
+ test_files: []