open_api_json_schema_converter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0f976564809e17e5acb3fa09fe0d1bb74ca8a659834bf007104a5ab84ef51fa5
4
+ data.tar.gz: 38c192bb08fcc043aa43de492b6f85502e596366afc4b0f95a21093ff39e878f
5
+ SHA512:
6
+ metadata.gz: d3a7bcc5350eba543f560d3230834a4c2426d2021dd0548b29b2a11d2820609a62fc2373173705a33bced862e520f03df844ad98929b7f76c9f5fe850846ce31
7
+ data.tar.gz: af72d54b5fb4d73b5df58912b6b22f3553f9f8bc49ebfc441b1a9eff3a0d00a44e9b85320b0252ae8b0fe8ef61afdcbb3a505dd8cdbf6234040ce2b9cc97ee20
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,37 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Metrics/BlockLength:
13
+ Enabled: false
14
+
15
+ Metrics/MethodLength:
16
+ Enabled: false
17
+
18
+ Metrics/AbcSize:
19
+ Enabled: false
20
+
21
+ Metrics/PerceivedComplexity:
22
+ Enabled: false
23
+
24
+ Metrics/CyclomaticComplexity:
25
+ Enabled: false
26
+
27
+ Style/SafeNavigation:
28
+ Enabled: false
29
+
30
+ Style/Documentation:
31
+ Enabled: false
32
+
33
+ Metrics/ClassLength:
34
+ Enabled: false
35
+
36
+ Style/HashTransformValues:
37
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # [0.1.0] - 2025-08-23
2
+
3
+ - Release!
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rake", "~> 13.0"
8
+
9
+ gem "rspec", "~> 3.13.1"
10
+ gem "super_diff"
11
+
12
+ gem "rubocop", "~> 1.76.1"
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,87 @@
1
+ # OpenAPI JSON Schema Converter
2
+
3
+ A Ruby gem for converting OpenAPI specifications to JSON Schema format.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'open_api_json_schema_converter'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ gem install open_api_json_schema_converter
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Examples:
28
+
29
+ ```ruby
30
+ require 'open_api_json_schema_converter'
31
+
32
+ json_schema = {
33
+ 'type' => 'object',
34
+ 'properties' => {
35
+ 'name' => { 'type' => 'string' },
36
+ 'email' => { 'type' => ['string', 'null'] }
37
+ }
38
+ }
39
+
40
+ result = OpenApiJsonSchemaConverter.convert(json_schema)
41
+
42
+ # Result:
43
+ # {
44
+ # 'type' => 'object',
45
+ # 'properties' => {
46
+ # 'name' => { 'type' => 'string' },
47
+ # 'email' => { 'type' => 'string', 'nullable' => true }
48
+ # }
49
+ # }
50
+ ```
51
+
52
+ ```ruby
53
+ require 'open_api_json_schema_converter'
54
+
55
+ json_schema = {
56
+ 'type' => 'object',
57
+ 'properties' => {
58
+ 'status' => {
59
+ 'const' => 'active',
60
+ 'description' => 'Account status'
61
+ },
62
+ 'version' => {
63
+ 'const' => '1.0'
64
+ }
65
+ }
66
+ }
67
+
68
+ result = OpenApiJsonSchemaConverter.convert(json_schema)
69
+
70
+ # Result:
71
+ # {
72
+ # 'type' => 'object',
73
+ # 'properties' => {
74
+ # 'status' => {
75
+ # 'enum' => ['active'],
76
+ # 'description' => 'Account status'
77
+ # },
78
+ # 'version' => {
79
+ # 'enum' => ['1.0']
80
+ # }
81
+ # }
82
+ # }
83
+ ```
84
+
85
+ ## License
86
+
87
+ 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_json_schema_converter"
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,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenApiJsonSchemaConverter
4
+ ALLOWED_KEYWORDS = %w[
5
+ $ref
6
+ definitions
7
+ title
8
+ multipleOf
9
+ maximum
10
+ exclusiveMaximum
11
+ minimum
12
+ exclusiveMinimum
13
+ maxLength
14
+ minLength
15
+ pattern
16
+ maxItems
17
+ minItems
18
+ uniqueItems
19
+ maxProperties
20
+ minProperties
21
+ required
22
+ enum
23
+ type
24
+ not
25
+ allOf
26
+ oneOf
27
+ anyOf
28
+ items
29
+ properties
30
+ additionalProperties
31
+ description
32
+ format
33
+ default
34
+ nullable
35
+ discriminator
36
+ readOnly
37
+ writeOnly
38
+ example
39
+ externalDocs
40
+ deprecated
41
+ xml
42
+ ].freeze
43
+
44
+ VALID_TYPES = %w[
45
+ null
46
+ boolean
47
+ object
48
+ array
49
+ number
50
+ string
51
+ integer
52
+ ].freeze
53
+
54
+ OAS_EXTENSION_PREFIX = "x-"
55
+ end
@@ -0,0 +1,310 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "constants"
4
+
5
+ module OpenApiJsonSchemaConverter
6
+ class InvalidTypeError < StandardError; end
7
+
8
+ class Converter
9
+ def convert(schema)
10
+ # Always clone to avoid modifying the input
11
+ cloned_schema = deep_clone(schema)
12
+
13
+ converted_schema = convert_schema(cloned_schema)
14
+
15
+ # Convert unreferenced definitions
16
+ if converted_schema["definitions"]
17
+ converted_schema["definitions"].each do |def_name, definition|
18
+ converted_schema["definitions"][def_name] = handle_definition(definition)
19
+ end
20
+ end
21
+
22
+ converted_schema
23
+ end
24
+
25
+ private
26
+
27
+ def handle_definition(definition)
28
+ return definition unless definition.is_a?(Hash)
29
+
30
+ if definition["type"]
31
+ convert_schema(deep_clone(definition))
32
+ elsif definition.is_a?(Array)
33
+ # Handle array types with null
34
+ type_arr = definition
35
+ has_null = type_arr.include?("null")
36
+ if has_null
37
+ actual_types = type_arr.reject { |t| t == "null" }
38
+ {
39
+ "type" => actual_types.length == 1 ? actual_types.first : actual_types,
40
+ "nullable" => true
41
+ }
42
+ else
43
+ definition
44
+ end
45
+ else
46
+ definition
47
+ end
48
+ end
49
+
50
+ def convert_schema(schema)
51
+ return schema unless schema
52
+ return schema unless schema.is_a?(Hash)
53
+
54
+ # First process the current level
55
+ schema = strip_illegal_keywords(schema)
56
+ schema = convert_types(schema)
57
+ schema = rewrite_const(schema)
58
+ schema = convert_dependencies(schema)
59
+ schema = convert_nullable(schema)
60
+ schema = rewrite_if_then_else(schema)
61
+ schema = rewrite_exclusive_min_max(schema)
62
+ schema = convert_examples(schema)
63
+
64
+ schema = convert_pattern_properties(schema) if schema["patternProperties"].is_a?(Hash)
65
+
66
+ # Arrays must have items property in OpenAPI
67
+ schema["items"] = {} if schema["type"] == "array" && !schema.key?("items")
68
+
69
+ # Recursively process nested schemas
70
+ schema = process_nested_schemas(schema)
71
+
72
+ # Should be called last
73
+ convert_illegal_keywords_as_extensions(schema)
74
+ end
75
+
76
+ def process_nested_schemas(schema)
77
+ return schema unless schema.is_a?(Hash)
78
+
79
+ # Process properties
80
+ if schema["properties"].is_a?(Hash)
81
+ schema["properties"].each do |key, value|
82
+ schema["properties"][key] = convert_schema(value)
83
+ end
84
+ end
85
+
86
+ # Process items
87
+ if schema["items"]
88
+ schema["items"] = if schema["items"].is_a?(Array)
89
+ schema["items"].map { |item| convert_schema(item) }
90
+ else
91
+ convert_schema(schema["items"])
92
+ end
93
+ end
94
+
95
+ # Process additionalProperties
96
+ if schema["additionalProperties"].is_a?(Hash)
97
+ schema["additionalProperties"] = convert_schema(schema["additionalProperties"])
98
+ end
99
+
100
+ # Process allOf, anyOf, oneOf
101
+ %w[allOf anyOf oneOf].each do |key|
102
+ schema[key] = schema[key].map { |item| convert_schema(item) } if schema[key].is_a?(Array)
103
+ end
104
+
105
+ # Process not
106
+ schema["not"] = convert_schema(schema["not"]) if schema["not"].is_a?(Hash)
107
+
108
+ # Process definitions
109
+ if schema["definitions"].is_a?(Hash)
110
+ schema["definitions"].each do |key, value|
111
+ schema["definitions"][key] = convert_schema(value)
112
+ end
113
+ end
114
+
115
+ # Process patternProperties (before it's converted to x-patternProperties)
116
+ if schema["patternProperties"].is_a?(Hash)
117
+ schema["patternProperties"].each do |key, value|
118
+ schema["patternProperties"][key] = convert_schema(value)
119
+ end
120
+ end
121
+
122
+ schema
123
+ end
124
+
125
+ def strip_illegal_keywords(schema)
126
+ return schema unless schema.is_a?(Hash)
127
+
128
+ schema.delete("$schema")
129
+ schema.delete("$id")
130
+ schema.delete("id")
131
+
132
+ schema
133
+ end
134
+
135
+ def convert_types(schema)
136
+ return schema unless schema.is_a?(Hash)
137
+ return schema unless schema.key?("type")
138
+
139
+ validate_type(schema["type"])
140
+
141
+ if schema["type"].is_a?(Array)
142
+ schema["nullable"] = true if schema["type"].include?("null")
143
+
144
+ types_without_null = schema["type"].reject { |t| t == "null" }
145
+
146
+ if types_without_null.empty?
147
+ schema.delete("type")
148
+ elsif types_without_null.length == 1
149
+ schema["type"] = types_without_null.first
150
+ else
151
+ schema.delete("type")
152
+ schema["anyOf"] = types_without_null.map { |type| { "type" => type } }
153
+ end
154
+ elsif schema["type"] == "null"
155
+ schema.delete("type")
156
+ schema["nullable"] = true
157
+ end
158
+
159
+ schema
160
+ end
161
+
162
+ def validate_type(type)
163
+ return if type.is_a?(Hash) && type["$ref"] # Refs are allowed
164
+ return if type.is_a?(Hash) && type["properties"] # De-referenced circular ref
165
+
166
+ types = type.is_a?(Array) ? type : [type]
167
+ types.each do |t|
168
+ next unless t
169
+ unless OpenApiJsonSchemaConverter::VALID_TYPES.include?(t)
170
+ raise InvalidTypeError, "Type \"#{t}\" is not a valid type"
171
+ end
172
+ end
173
+ end
174
+
175
+ def rewrite_const(schema)
176
+ return schema unless schema.is_a?(Hash)
177
+
178
+ if schema.key?("const")
179
+ schema["enum"] = [schema["const"]]
180
+ schema.delete("const")
181
+ end
182
+
183
+ schema
184
+ end
185
+
186
+ def convert_dependencies(schema)
187
+ return schema unless schema.is_a?(Hash)
188
+
189
+ deps = schema["dependencies"]
190
+ return schema unless deps.is_a?(Hash)
191
+
192
+ schema.delete("dependencies")
193
+ schema["allOf"] ||= []
194
+
195
+ deps.each do |key, value|
196
+ one_of = {
197
+ "oneOf" => [
198
+ { "not" => { "required" => [key] } },
199
+ { "required" => [key, *Array(value)].flatten }
200
+ ]
201
+ }
202
+ schema["allOf"] << one_of
203
+ end
204
+
205
+ schema
206
+ end
207
+
208
+ def convert_nullable(schema)
209
+ return schema unless schema.is_a?(Hash)
210
+
211
+ %w[oneOf anyOf].each do |key|
212
+ schemas = schema[key]
213
+ next unless schemas.is_a?(Array)
214
+
215
+ has_nullable = schemas.any? { |item| item.is_a?(Hash) && item["type"] == "null" }
216
+ next unless has_nullable
217
+
218
+ filtered = schemas.reject { |item| item.is_a?(Hash) && item["type"] == "null" }
219
+ filtered.each do |schema_entry|
220
+ schema_entry["nullable"] = true if schema_entry.is_a?(Hash)
221
+ end
222
+
223
+ schema[key] = filtered
224
+ end
225
+
226
+ schema
227
+ end
228
+
229
+ def rewrite_if_then_else(schema)
230
+ return schema unless schema.is_a?(Hash)
231
+ return schema unless schema["if"] && schema["then"]
232
+
233
+ schema["oneOf"] = [
234
+ { "allOf" => [schema["if"], schema["then"]].compact },
235
+ { "allOf" => [{ "not" => schema["if"] }, schema["else"]].compact }
236
+ ]
237
+
238
+ schema.delete("if")
239
+ schema.delete("then")
240
+ schema.delete("else")
241
+
242
+ schema
243
+ end
244
+
245
+ def rewrite_exclusive_min_max(schema)
246
+ return schema unless schema.is_a?(Hash)
247
+
248
+ if schema["exclusiveMaximum"].is_a?(Numeric)
249
+ schema["maximum"] = schema["exclusiveMaximum"]
250
+ schema["exclusiveMaximum"] = true
251
+ end
252
+
253
+ if schema["exclusiveMinimum"].is_a?(Numeric)
254
+ schema["minimum"] = schema["exclusiveMinimum"]
255
+ schema["exclusiveMinimum"] = true
256
+ end
257
+
258
+ schema
259
+ end
260
+
261
+ def convert_examples(schema)
262
+ return schema unless schema.is_a?(Hash)
263
+
264
+ if schema["examples"].is_a?(Array) && !schema["examples"].empty?
265
+ schema["example"] = schema["examples"].first
266
+ schema.delete("examples")
267
+ end
268
+
269
+ schema
270
+ end
271
+
272
+ def convert_pattern_properties(schema)
273
+ return schema unless schema.is_a?(Hash)
274
+
275
+ schema["x-patternProperties"] = schema["patternProperties"]
276
+ schema.delete("patternProperties")
277
+ schema["additionalProperties"] = true unless schema.key?("additionalProperties")
278
+
279
+ schema
280
+ end
281
+
282
+ def convert_illegal_keywords_as_extensions(schema)
283
+ return schema unless schema.is_a?(Hash)
284
+
285
+ schema.keys.each do |keyword|
286
+ next if keyword.start_with?(OpenApiJsonSchemaConverter::OAS_EXTENSION_PREFIX)
287
+ next if OpenApiJsonSchemaConverter::ALLOWED_KEYWORDS.include?(keyword)
288
+
289
+ extension_key = "#{OpenApiJsonSchemaConverter::OAS_EXTENSION_PREFIX}#{keyword}"
290
+ schema[extension_key] = schema[keyword]
291
+ schema.delete(keyword)
292
+ end
293
+
294
+ schema
295
+ end
296
+
297
+ def deep_clone(obj)
298
+ case obj
299
+ when Hash
300
+ obj.each_with_object({}) do |(k, v), h|
301
+ h[k] = deep_clone(v)
302
+ end
303
+ when Array
304
+ obj.map { |item| deep_clone(item) }
305
+ else
306
+ obj
307
+ end
308
+ end
309
+ end
310
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenApiJsonSchemaConverter
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require_relative "open_api_json_schema_converter/constants"
5
+ require_relative "open_api_json_schema_converter/converter"
6
+ require_relative "open_api_json_schema_converter/version"
7
+
8
+ module OpenApiJsonSchemaConverter
9
+ module_function
10
+
11
+ def convert(schema)
12
+ Converter.new.convert(schema)
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_api_json_schema_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Ishkov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-08-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This project was written to help convert structured outputs required
14
+ for OpenAI to Gemini format.
15
+ email:
16
+ - dmitry@hey.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/open_api_json_schema_converter.rb
31
+ - lib/open_api_json_schema_converter/constants.rb
32
+ - lib/open_api_json_schema_converter/converter.rb
33
+ - lib/open_api_json_schema_converter/version.rb
34
+ homepage: https://github.com/therusskiy/open_api_json_schema_converter
35
+ licenses:
36
+ - MIT
37
+ metadata:
38
+ homepage_uri: https://github.com/therusskiy/open_api_json_schema_converter
39
+ source_code_uri: https://github.com/therusskiy/open_api_json_schema_converter
40
+ changelog_uri: https://github.com/therusskiy/open_api_json_schema_converter/blob/main/CHANGELOG.md
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.7.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.1.6
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: OpenAPI v3.0 schema to JSON Schema converter
60
+ test_files: []