swagger23 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: 16a875f37fe15619525cdfe4ac7ce4f4c942f15113423221d7a323fe9671792e
4
+ data.tar.gz: f3de37ab1a4f608db7a97d3589d0883de89763afa8e6d4a24eb43fffbb6903bd
5
+ SHA512:
6
+ metadata.gz: 1f452044813adb1fed314ca38303aacf92f0776afe11c253e478033edfcb56645439ad7f632a56b81b73f21bb5c1cc95368a119ddb55d06a16dfc70113db667f
7
+ data.tar.gz: a063e8949e75e1361d47416723d3088eea1d5e61ffafeece2fd811b5df75dea1958dfa83227be423ebafacf2d637c8cb9cf1bf29fc8d4a72ceb27b63a082564e
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Maxim Veysgeym
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # swagger23
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/swagger23.svg)](https://rubygems.org/gems/swagger23)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
5
+
6
+ A Ruby gem that converts [Swagger 2.0](https://swagger.io/specification/v2/) API specifications into [OpenAPI 3.0.3](https://spec.openapis.org/oas/v3.0.3) specifications.
7
+
8
+ Accepts **JSON or YAML** input, produces **JSON or YAML** output. Works as a Ruby library or a standalone CLI tool.
9
+
10
+ ---
11
+
12
+ ## Requirements
13
+
14
+ - Ruby **≥ 3.2**
15
+
16
+ ---
17
+
18
+ ## Installation
19
+
20
+ Add to your `Gemfile`:
21
+
22
+ ```ruby
23
+ gem "swagger23"
24
+ ```
25
+
26
+ Then run:
27
+
28
+ ```bash
29
+ bundle install
30
+ ```
31
+
32
+ Or install globally:
33
+
34
+ ```bash
35
+ gem install swagger23
36
+ ```
37
+
38
+ ---
39
+
40
+ ## Usage
41
+
42
+ ### CLI
43
+
44
+ ```
45
+ swagger23 [INPUT [OUTPUT]]
46
+ ```
47
+
48
+ | Argument | Description |
49
+ |---|---|
50
+ | `INPUT` | Path to a Swagger 2.0 file (`.json`, `.yaml`, or `.yml`). Reads from **stdin** if omitted. |
51
+ | `OUTPUT` | Path to write the OpenAPI 3.0 result. Format is determined by the file extension: `.yaml` / `.yml` → YAML, anything else → JSON. Writes JSON to **stdout** if omitted. |
52
+
53
+ **Options:**
54
+
55
+ ```
56
+ -v, --version Print the gem version and exit.
57
+ -h, --help Print this help message and exit.
58
+ ```
59
+
60
+ **Examples:**
61
+
62
+ ```bash
63
+ swagger23 petstore.json openapi.json
64
+ swagger23 petstore.yaml openapi.yaml
65
+ swagger23 petstore.yaml openapi.json
66
+ swagger23 petstore.json openapi.yaml
67
+
68
+ # Print to stdout
69
+ swagger23 petstore.json
70
+
71
+ # Pipe from stdin
72
+ cat petstore.yaml | swagger23
73
+ cat petstore.json | swagger23 > openapi.json
74
+ ```
75
+
76
+ ### Ruby library
77
+
78
+ ```ruby
79
+ require "swagger23"
80
+
81
+ # Hash → Hash
82
+ swagger_hash = JSON.parse(File.read("petstore.json"))
83
+ openapi_hash = Swagger23.convert(swagger_hash)
84
+
85
+ # String (JSON or YAML) → JSON string
86
+ json_string = Swagger23.convert_string(File.read("swagger.yaml", encoding: "utf-8"))
87
+
88
+ # String (JSON or YAML) → YAML string
89
+ yaml_string = Swagger23.convert_to_yaml(File.read("swagger.json", encoding: "utf-8"))
90
+
91
+ # Parse only
92
+ hash = Swagger23.parse(source)
93
+ ```
94
+
95
+ ---
96
+
97
+ ## Running tests
98
+
99
+ ```bash
100
+ bundle exec rspec
101
+ ```
102
+
103
+ ---
104
+
105
+ ## Contributing
106
+
107
+ 1. Fork [github.com/Qew7/swagger23](https://github.com/Qew7/swagger23).
108
+ 2. Create a feature branch: `git checkout -b my-feature`.
109
+ 3. Add tests for your change.
110
+ 4. Make sure the full suite passes: `bundle exec rspec`.
111
+ 5. Submit a pull request.
112
+
113
+ Bug reports and feature requests are welcome on the [issue tracker](https://github.com/Qew7/swagger23/issues).
114
+
115
+ ---
116
+
117
+ ## Author
118
+
119
+ [Maxim Veysgeym](https://github.com/Qew7) — Ruby enthusiast, Moscow.
120
+
121
+ ---
122
+
123
+ ## License
124
+
125
+ Released under the [MIT License](LICENSE) © 2026 Maxim Veysgeym.
data/bin/swagger23 ADDED
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # CLI for swagger23 – converts a Swagger 2.0 file to OpenAPI 3.0.
5
+ #
6
+ # Usage:
7
+ # swagger23 input.json [output.json]
8
+ # swagger23 input.yaml [output.yaml]
9
+ # swagger23 input.yaml output.json # cross-format
10
+ # swagger23 input.json # writes to STDOUT (JSON)
11
+ # cat input.yaml | swagger23 # reads from STDIN, writes to STDOUT (JSON)
12
+ # swagger23 --version
13
+ # swagger23 --help
14
+
15
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
16
+ require "swagger23"
17
+ require "json"
18
+
19
+ def usage
20
+ <<~USAGE
21
+ Usage:
22
+ swagger23 [INPUT [OUTPUT]]
23
+
24
+ Arguments:
25
+ INPUT Path to Swagger 2.0 file (.json or .yaml/.yml).
26
+ Reads from STDIN if omitted.
27
+ OUTPUT Path to write OpenAPI 3.0 result.
28
+ Format is determined by the file extension:
29
+ .yaml / .yml → YAML output
30
+ anything else → JSON output (default)
31
+ Writes JSON to STDOUT if omitted.
32
+
33
+ Options:
34
+ -v, --version Print the gem version and exit.
35
+ -h, --help Print this help message and exit.
36
+
37
+ Examples:
38
+ swagger23 petstore.json openapi.json
39
+ swagger23 petstore.yaml openapi.yaml
40
+ swagger23 petstore.yaml openapi.json
41
+ swagger23 petstore.json
42
+ cat petstore.yaml | swagger23
43
+ USAGE
44
+ end
45
+
46
+ # ── Argument parsing ──────────────────────────────────────────────────────────
47
+
48
+ args = ARGV.dup
49
+
50
+ if args.include?("--help") || args.include?("-h")
51
+ puts usage
52
+ exit 0
53
+ end
54
+
55
+ if args.include?("--version") || args.include?("-v")
56
+ puts Swagger23::VERSION
57
+ exit 0
58
+ end
59
+
60
+ input_path = args.shift
61
+ output_path = args.shift
62
+
63
+ # ── Read input ────────────────────────────────────────────────────────────────
64
+
65
+ begin
66
+ raw = input_path ? File.read(input_path, encoding: "utf-8") : $stdin.read
67
+ rescue Errno::ENOENT => e
68
+ warn "swagger23: #{e.message}"
69
+ exit 1
70
+ end
71
+
72
+ # ── Parse input (JSON or YAML, auto-detected) ─────────────────────────────────
73
+
74
+ begin
75
+ swagger = Swagger23.parse(raw)
76
+ rescue Swagger23::Error => e
77
+ warn "swagger23: #{e.message}"
78
+ exit 1
79
+ end
80
+
81
+ # ── Convert ───────────────────────────────────────────────────────────────────
82
+
83
+ begin
84
+ openapi = Swagger23.convert(swagger)
85
+ rescue Swagger23::InvalidSwaggerError => e
86
+ warn "swagger23: #{e.message}"
87
+ exit 2
88
+ rescue => e
89
+ warn "swagger23: unexpected error: #{e.class}: #{e.message}"
90
+ warn e.backtrace.first(5).join("\n")
91
+ exit 3
92
+ end
93
+
94
+ # ── Serialise to output format ────────────────────────────────────────────────
95
+
96
+ yaml_output = output_path && output_path.match?(/\.ya?ml\z/i)
97
+
98
+ if yaml_output
99
+ require "yaml"
100
+ output = YAML.dump(openapi)
101
+ else
102
+ output = JSON.pretty_generate(openapi)
103
+ end
104
+
105
+ # ── Write output ──────────────────────────────────────────────────────────────
106
+
107
+ if output_path
108
+ File.write(output_path, output)
109
+ format = yaml_output ? "YAML" : "JSON"
110
+ warn "swagger23: written #{format} to #{output_path}"
111
+ else
112
+ puts output
113
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Swagger23
4
+ # Orchestrates the full Swagger 2.0 → OpenAPI 3.0 conversion.
5
+ #
6
+ # Usage:
7
+ # swagger_hash = JSON.parse(File.read("petstore.json"))
8
+ # openapi_hash = Swagger23::Converter.new(swagger_hash).convert
9
+ class Converter
10
+ SUPPORTED_SWAGGER_VERSION = "2.0"
11
+ TARGET_OPENAPI_VERSION = "3.0.3"
12
+
13
+ # @param swagger [Hash] parsed Swagger 2.0 document
14
+ def initialize(swagger)
15
+ @swagger = swagger
16
+ end
17
+
18
+ # @return [Hash] OpenAPI 3.0 document
19
+ def convert
20
+ validate!
21
+
22
+ result = {}
23
+
24
+ result["openapi"] = TARGET_OPENAPI_VERSION
25
+ result["info"] = Converters::Info.convert(@swagger)
26
+ result["servers"] = Converters::Servers.convert(@swagger)
27
+
28
+ # Tags (identical structure between 2.0 and 3.0)
29
+ result["tags"] = @swagger["tags"] if @swagger.key?("tags")
30
+
31
+ # External docs (identical)
32
+ result["externalDocs"] = @swagger["externalDocs"] if @swagger.key?("externalDocs")
33
+
34
+ # Paths
35
+ result["paths"] = Converters::Paths.convert(@swagger)
36
+
37
+ # Components
38
+ components = Converters::Components.convert(@swagger)
39
+ security_schemes = Converters::Security.convert(@swagger)
40
+ components["securitySchemes"] = security_schemes unless security_schemes.empty?
41
+
42
+ result["components"] = components unless components.empty?
43
+
44
+ # Top-level security requirements (identical structure)
45
+ result["security"] = @swagger["security"] if @swagger.key?("security")
46
+
47
+ # Top-level extensions
48
+ @swagger.each do |key, value|
49
+ result[key] = value if key.start_with?("x-")
50
+ end
51
+
52
+ # Pass 1: rewrite all $ref values (Swagger 2.0 paths → OpenAPI 3.0 paths)
53
+ rewritten = RefRewriter.rewrite(result)
54
+
55
+ # Pass 2: schema-level semantic transformations
56
+ # x-nullable → nullable, discriminator string → object, type arrays → nullable
57
+ SchemaProcessor.process(rewritten)
58
+ end
59
+
60
+ private
61
+
62
+ def validate!
63
+ version = @swagger["swagger"]
64
+ return if version.to_s == SUPPORTED_SWAGGER_VERSION
65
+
66
+ raise InvalidSwaggerError,
67
+ "Expected swagger version '#{SUPPORTED_SWAGGER_VERSION}', " \
68
+ "got '#{version}'. Only Swagger 2.0 documents are supported."
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Swagger23
4
+ module Converters
5
+ # Converts Swagger 2.0 top-level reusable objects into OpenAPI 3.0
6
+ # `components` section.
7
+ #
8
+ # Mapping:
9
+ # definitions → components/schemas
10
+ # parameters → components/parameters
11
+ # responses → components/responses
12
+ # securityDefinitions → components/securitySchemes (see Security converter)
13
+ module Components
14
+ def self.convert(swagger)
15
+ components = {}
16
+
17
+ if (definitions = swagger["definitions"])
18
+ components["schemas"] = definitions
19
+ end
20
+
21
+ if (parameters = swagger["parameters"])
22
+ converted_params = {}
23
+ parameters.each do |name, param|
24
+ converted_params[name] = Paths.convert_parameter(param)
25
+ end
26
+ components["parameters"] = converted_params
27
+ end
28
+
29
+ if (responses = swagger["responses"])
30
+ converted_responses = {}
31
+ responses.each do |name, resp|
32
+ converted_responses[name] = convert_response(resp, swagger)
33
+ end
34
+ components["responses"] = converted_responses
35
+ end
36
+
37
+ components
38
+ end
39
+
40
+ # Convert a single Swagger 2.0 response object to OpenAPI 3.0 format.
41
+ def self.convert_response(resp, swagger)
42
+ result = {}
43
+ result["description"] = resp["description"] if resp["description"]
44
+
45
+ if (schema = resp["schema"])
46
+ produces = swagger["produces"] || ["application/json"]
47
+ content = {}
48
+ produces.each do |mime|
49
+ content[mime] = { "schema" => schema }
50
+ end
51
+ result["content"] = content
52
+ end
53
+
54
+ # OAS 3.0 Header Object: description stays at the top level; type/format go
55
+ # inside a nested `schema` object (Header Object mirrors Parameter Object).
56
+ if (headers = resp["headers"])
57
+ converted_headers = {}
58
+ headers.each do |name, header|
59
+ h = {}
60
+ h["description"] = header["description"] if header.key?("description")
61
+ schema = {}
62
+ %w[type format].each { |f| schema[f] = header[f] if header.key?(f) }
63
+ h["schema"] = schema unless schema.empty?
64
+ header.each { |k, v| h[k] = v if k.start_with?("x-") }
65
+ converted_headers[name] = h
66
+ end
67
+ result["headers"] = converted_headers
68
+ end
69
+
70
+ # pass through extensions
71
+ resp.each do |key, value|
72
+ result[key] = value if key.start_with?("x-")
73
+ end
74
+
75
+ result
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Swagger23
4
+ module Converters
5
+ # Converts the top-level `info` object.
6
+ # The structure is identical between Swagger 2.0 and OpenAPI 3.0,
7
+ # so this is essentially a deep-copy with no structural changes.
8
+ module Info
9
+ def self.convert(swagger)
10
+ info = swagger["info"] || {}
11
+ # Pass through all known and extension fields as-is
12
+ info.dup
13
+ end
14
+ end
15
+ end
16
+ end