oas_contrib 0.1.1 → 0.2.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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -0
  3. data/README.md +105 -23
  4. data/example/dist/openapi_v3/meta/001_openapi.yml +2 -0
  5. data/example/dist/openapi_v3/meta/002_info.yml +6 -0
  6. data/example/dist/openapi_v3/meta/003_servers.yml +3 -0
  7. data/example/dist/openapi_v3/model/001_Pet.yml +13 -0
  8. data/example/dist/openapi_v3/model/002_Error.yml +11 -0
  9. data/example/dist/openapi_v3/model/003_Pets.yml +5 -0
  10. data/example/dist/openapi_v3/path/001_pets.yml +47 -0
  11. data/example/dist/openapi_v3/path/002_pets_{petId}.yml +27 -0
  12. data/example/dist/swagger_v2/meta/001_swagger.yml +2 -0
  13. data/example/dist/swagger_v2/meta/002_info.yml +6 -0
  14. data/example/dist/swagger_v2/meta/003_host.yml +2 -0
  15. data/example/dist/swagger_v2/meta/004_basePath.yml +2 -0
  16. data/example/dist/swagger_v2/meta/005_schemes.yml +3 -0
  17. data/example/dist/swagger_v2/meta/006_consumes.yml +3 -0
  18. data/example/dist/swagger_v2/meta/007_produces.yml +3 -0
  19. data/example/dist/swagger_v2/meta/008_components.yml +2 -0
  20. data/example/dist/swagger_v2/model/001_Pet.yml +13 -0
  21. data/example/dist/swagger_v2/model/002_Error.yml +11 -0
  22. data/example/dist/swagger_v2/model/003_Pets.yml +5 -0
  23. data/example/dist/swagger_v2/path/001_pets.yml +39 -0
  24. data/example/dist/swagger_v2/path/002_pets_{petId}.yml +22 -0
  25. data/{sample_petstore_3.yml → example/sample_petstore_openapi_v3.yml} +0 -0
  26. data/{sample_petstore_2.yml → example/sample_petstore_swagger_v2.yml} +0 -0
  27. data/lib/oas_contrib.rb +2 -4
  28. data/lib/oas_contrib/command.rb +26 -54
  29. data/lib/oas_contrib/command_resolver_base.rb +140 -0
  30. data/lib/oas_contrib/command_resolvers/divide_command_resolver.rb +34 -0
  31. data/lib/oas_contrib/command_resolvers/merge_command_resolver.rb +60 -0
  32. data/lib/oas_contrib/openapi/v3/spec.rb +21 -0
  33. data/lib/oas_contrib/swagger/v2/spec.rb +20 -0
  34. data/lib/oas_contrib/version.rb +2 -2
  35. data/oas_contrib.gemspec +1 -1
  36. metadata +30 -5
  37. data/lib/oas_contrib/command_util.rb +0 -125
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c8a1b78e6c65c71f5d0053bd8b1a268213a8cb0e1eecfe423f2321c27aea23e
4
- data.tar.gz: 8d705ccbd6fbabb6e11e418c08423707cbd14ae47f67bf3eaa8b689d52484f21
3
+ metadata.gz: 7f74cdce165853e7f21d8a71c42d3c07665166c383eb14ae920f9f13b9179647
4
+ data.tar.gz: 7a9676bc8f852f38175716222ba5dcc2cffbd89a4753621e1ee8b8c1d14cd6db
5
5
  SHA512:
6
- metadata.gz: 587adc04a079ca0030fd341df911947edbb4722ee583df1bd6487e78a8d7be29221833855d0665fa17424b729516422aa13630da48776a89a342f1844a278d1b
7
- data.tar.gz: 4b6876e829bd6f7a1e1bebd662c7533a01b1b6e6796d6467f5a9eb8ccd8d9b4b9a6ba9fcaaf1f3c82f036cf9e1f02f86bae850da8aa89eae19de2359be20e626
6
+ metadata.gz: f1cbb48a8848b56dc402f86329c673883ec8e1a8eb86a9363edea17903041d03f7c0a9e0a11586ccacfd330093d2754fcb7bfd46f24e0cf671acf792c6be4a4c
7
+ data.tar.gz: 18a35b2c39802c1e7e5902203b5f40ce6f891a149d8527637da0e12a54dc850798333c4b80151ef867613dd577439df91ef4df59ce50e477344bcf6879537c87
data/.gitignore CHANGED
File without changes
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/oas_contrib.svg)](https://badge.fury.io/rb/oas_contrib)
4
4
 
5
- Libraries and commands for OpenAPI Specification.
5
+ Libraries and Commands for OpenAPI Specification.
6
6
 
7
7
  ## Installation
8
8
 
@@ -26,37 +26,113 @@ Or install it yourself as:
26
26
 
27
27
  Divide the OAS file into path units and schema units.
28
28
 
29
- `$ oas_contrib divide <input_file_path> <output_directory_path> (OPTIONS)`
29
+ ```bash
30
+ $ oas_contrib divide <input_file> <output_dir> (<options>...)
31
+ ```
32
+
33
+ `<input_file>` must be `.json` or `.yml`
34
+
35
+ #### Options
36
+
37
+ | option | description | value type | default |
38
+ |------------|------------------|---------------------------|----------|
39
+ |`--out_type`| output file type | String (`yaml` or `json`) | `yaml` |
30
40
 
31
- __Options__
41
+ #### Example
32
42
 
33
- * `--in_type` input file type (`yaml` or `json`, default `yaml`)
34
- * `--out_type` output file type (`yaml` or `json`, default `yaml`)
43
+ ##### Open API 3.0
44
+
45
+ [BEFORE](https://github.com/MichinaoShimizu/oas_contrib/blob/master/example/sample_petstore_openapi_v3.yml) -> [AFTER](https://github.com/MichinaoShimizu/oas_contrib/tree/master/example/dist/openapi_v3)
46
+
47
+ ```
48
+ $ oas_contrib divide example/sample_petstore_openapi_v3.yml example/dist/openapi_v3
49
+ Load: example/sample_petstore_openapi_v3.yml
50
+ Dist: example/dist/openapi_v3/meta
51
+ Dist: example/dist/openapi_v3/meta/001_openapi.yml
52
+ Dist: example/dist/openapi_v3/meta/002_info.yml
53
+ Dist: example/dist/openapi_v3/meta/003_servers.yml
54
+ Dist: example/dist/openapi_v3/path
55
+ Dist: example/dist/openapi_v3/path/001_pets.yml
56
+ Dist: example/dist/openapi_v3/path/002_pets_{petId}.yml
57
+ Dist: example/dist/openapi_v3/model
58
+ Dist: example/dist/openapi_v3/model/001_Pet.yml
59
+ Dist: example/dist/openapi_v3/model/002_Error.yml
60
+
61
+ $ tree example/dist/openapi_v3/
62
+ example/dist/openapi_v3/
63
+ ├── meta
64
+ │ ├── 001_openapi.yml
65
+ │ ├── 002_info.yml
66
+ │ └── 003_servers.yml
67
+ ├── model
68
+ │ ├── 001_Pet.yml
69
+ │ ├── 002_Error.yml
70
+ │ └── 003_Pets.yml
71
+ └── path
72
+ ├── 001_pets.yml
73
+ └── 002_pets_{petId}.yml
74
+ ```
35
75
 
36
- You can below 4 case:
76
+ ##### Swagger v2
37
77
 
38
- * JSON OAS file -> JSON files
39
- * YAML OAS file -> YAML files
40
- * JSON OAS file -> YAML files
41
- * YAML OAS file -> JSON files
78
+ [BEFORE](https://github.com/MichinaoShimizu/oas_contrib/blob/master/example/sample_petstore_swagger_v2.yml) -> [AFTER](https://github.com/MichinaoShimizu/oas_contrib/tree/master/example/dist/swagger_v2)
79
+
80
+ ```
81
+ $ oas_contrib divide example/sample_petstore_swagger_v2.yml example/dist/swagger_v2
82
+ Load: example/sample_petstore_swagger_v2.yml
83
+ Dist: example/dist/swagger_v2/meta
84
+ Dist: example/dist/swagger_v2/meta/001_swagger.yml
85
+ Dist: example/dist/swagger_v2/meta/002_info.yml
86
+ Dist: example/dist/swagger_v2/meta/003_host.yml
87
+ Dist: example/dist/swagger_v2/meta/004_basePath.yml
88
+ Dist: example/dist/swagger_v2/meta/005_schemes.yml
89
+ Dist: example/dist/swagger_v2/meta/006_consumes.yml
90
+ Dist: example/dist/swagger_v2/meta/007_produces.yml
91
+ Dist: example/dist/swagger_v2/meta/008_components.yml
92
+ Dist: example/dist/swagger_v2/path
93
+ Dist: example/dist/swagger_v2/path/001_pets.yml
94
+ Dist: example/dist/swagger_v2/path/002_pets_{petId}.yml
95
+ Dist: example/dist/swagger_v2/model
96
+ Dist: example/dist/swagger_v2/model/001_Pet.yml
97
+ Dist: example/dist/swagger_v2/model/002_Error.yml
98
+ Dist: example/dist/swagger_v2/model/003_Pets.yml
99
+
100
+ $ tree example/dist/
101
+ example/dist/
102
+ └── swagger_v2
103
+ ├── meta
104
+ │ ├── 001_swagger.yml
105
+ │ ├── 002_info.yml
106
+ │ ├── 003_host.yml
107
+ │ ├── 004_basePath.yml
108
+ │ ├── 005_schemes.yml
109
+ │ ├── 006_consumes.yml
110
+ │ ├── 007_produces.yml
111
+ │ └── 008_components.yml
112
+ ├── model
113
+ │ ├── 001_Pet.yml
114
+ │ ├── 002_Error.yml
115
+ │ └── 003_Pets.yml
116
+ └── path
117
+ ├── 001_pets.yml
118
+ └── 002_pets_{petId}.yml
119
+ ```
42
120
 
43
121
  ### Merge command
44
122
 
45
123
  Merge multiple divided files into an OAS file.
46
124
 
47
- `$ oas_contrib merge <input_directory_path> <output_file_path> (OPTIONS)`
48
-
49
- __Options__
125
+ ```bash
126
+ $ oas_contrib merge <input_dir> <output_file> (<options>...)
127
+ ```
50
128
 
51
- * `--in_type` input file type (`yaml` or `json`, default `yaml`)
52
- * `--out_type` output file type (`yaml` or `json`, default `yaml`)
129
+ `<output_file>` must be `.json` or `.yml`
53
130
 
54
- You can below 4 case:
131
+ #### Options
55
132
 
56
- * JSON files -> JSON OAS file
57
- * YAML files -> YAML OAS file
58
- * JSON files -> YAML OAS file
59
- * YAML files -> JSON OAS file
133
+ | option | description | value type | default |
134
+ |------------|------------------|------------------------- |----------|
135
+ |`--in_type` | input file type | String (`yaml` or `json`)| `yaml` |
60
136
 
61
137
  ### Preview command
62
138
 
@@ -64,11 +140,17 @@ Preview OAS file using Swagger-UI official Docker image.
64
140
 
65
141
  The preview command needs docker.
66
142
 
67
- `$ oas_contrib preview <input_file> (OPTIONS)`
143
+ ```bash
144
+ $ oas_contrib preview <input_file> (<options>...)
145
+ ```
146
+
147
+ `<input_file>` must be `.json` or `.yml`
68
148
 
69
- __Options__
149
+ #### Options
70
150
 
71
- * `--port` Swagger UI listen port (default `50010`)
151
+ | option | description | value type | default |
152
+ |------------|-------------------------|-----------------|----------|
153
+ |`--port` | Swagger UI listen port | Integer | `50010` |
72
154
 
73
155
  ## Contributing
74
156
 
@@ -0,0 +1,2 @@
1
+ ---
2
+ openapi: 3.0.0
@@ -0,0 +1,6 @@
1
+ ---
2
+ info:
3
+ version: 1.0.0
4
+ title: Swagger Petstore
5
+ license:
6
+ name: MIT
@@ -0,0 +1,3 @@
1
+ ---
2
+ servers:
3
+ - url: http://petstore.swagger.io/v1
@@ -0,0 +1,13 @@
1
+ ---
2
+ Pet:
3
+ required:
4
+ - id
5
+ - name
6
+ properties:
7
+ id:
8
+ type: integer
9
+ format: int64
10
+ name:
11
+ type: string
12
+ tag:
13
+ type: string
@@ -0,0 +1,11 @@
1
+ ---
2
+ Error:
3
+ required:
4
+ - code
5
+ - message
6
+ properties:
7
+ code:
8
+ type: integer
9
+ format: int32
10
+ message:
11
+ type: string
@@ -0,0 +1,5 @@
1
+ ---
2
+ Pets:
3
+ type: array
4
+ items:
5
+ "$ref": "#/components/schemas/Pet"
@@ -0,0 +1,47 @@
1
+ ---
2
+ "/pets":
3
+ get:
4
+ summary: List all pets
5
+ operationId: listPets
6
+ tags:
7
+ - pets
8
+ parameters:
9
+ - name: limit
10
+ in: query
11
+ description: How many items to return at one time (max 100)
12
+ required: false
13
+ schema:
14
+ type: integer
15
+ format: int32
16
+ responses:
17
+ '200':
18
+ description: A paged array of pets
19
+ headers:
20
+ x-next:
21
+ description: A link to the next page of responses
22
+ schema:
23
+ type: string
24
+ content:
25
+ application/json:
26
+ schema:
27
+ "$ref": "#/components/schemas/Pets"
28
+ default:
29
+ description: unexpected error
30
+ content:
31
+ application/json:
32
+ schema:
33
+ "$ref": "#/components/schemas/Error"
34
+ post:
35
+ summary: Create a pet
36
+ operationId: createPets
37
+ tags:
38
+ - pets
39
+ responses:
40
+ '201':
41
+ description: Null response
42
+ default:
43
+ description: unexpected error
44
+ content:
45
+ application/json:
46
+ schema:
47
+ "$ref": "#/components/schemas/Error"
@@ -0,0 +1,27 @@
1
+ ---
2
+ "/pets/{petId}":
3
+ get:
4
+ summary: Info for a specific pet
5
+ operationId: showPetById
6
+ tags:
7
+ - pets
8
+ parameters:
9
+ - name: petId
10
+ in: path
11
+ required: true
12
+ description: The id of the pet to retrieve
13
+ schema:
14
+ type: string
15
+ responses:
16
+ '200':
17
+ description: Expected response to a valid request
18
+ content:
19
+ application/json:
20
+ schema:
21
+ "$ref": "#/components/schemas/Pets"
22
+ default:
23
+ description: unexpected error
24
+ content:
25
+ application/json:
26
+ schema:
27
+ "$ref": "#/components/schemas/Error"
@@ -0,0 +1,2 @@
1
+ ---
2
+ swagger: '2.0'
@@ -0,0 +1,6 @@
1
+ ---
2
+ info:
3
+ version: 1.0.0
4
+ title: Swagger Petstore
5
+ license:
6
+ name: MIT
@@ -0,0 +1,2 @@
1
+ ---
2
+ host: petstore.swagger.io
@@ -0,0 +1,2 @@
1
+ ---
2
+ basePath: "/v1"
@@ -0,0 +1,3 @@
1
+ ---
2
+ schemes:
3
+ - http
@@ -0,0 +1,3 @@
1
+ ---
2
+ consumes:
3
+ - application/json
@@ -0,0 +1,3 @@
1
+ ---
2
+ produces:
3
+ - application/json
@@ -0,0 +1,2 @@
1
+ ---
2
+ components: {}
@@ -0,0 +1,13 @@
1
+ ---
2
+ Pet:
3
+ required:
4
+ - id
5
+ - name
6
+ properties:
7
+ id:
8
+ type: integer
9
+ format: int64
10
+ name:
11
+ type: string
12
+ tag:
13
+ type: string
@@ -0,0 +1,11 @@
1
+ ---
2
+ Error:
3
+ required:
4
+ - code
5
+ - message
6
+ properties:
7
+ code:
8
+ type: integer
9
+ format: int32
10
+ message:
11
+ type: string
@@ -0,0 +1,5 @@
1
+ ---
2
+ Pets:
3
+ type: array
4
+ items:
5
+ "$ref": "#/definitions/Pet"
@@ -0,0 +1,39 @@
1
+ ---
2
+ "/pets":
3
+ get:
4
+ summary: List all pets
5
+ operationId: listPets
6
+ tags:
7
+ - pets
8
+ parameters:
9
+ - name: limit
10
+ in: query
11
+ description: How many items to return at one time (max 100)
12
+ required: false
13
+ type: integer
14
+ format: int32
15
+ responses:
16
+ '200':
17
+ description: A paged array of pets
18
+ headers:
19
+ x-next:
20
+ type: string
21
+ description: A link to the next page of responses
22
+ schema:
23
+ "$ref": "#/definitions/Pets"
24
+ default:
25
+ description: unexpected error
26
+ schema:
27
+ "$ref": "#/definitions/Error"
28
+ post:
29
+ summary: Create a pet
30
+ operationId: createPets
31
+ tags:
32
+ - pets
33
+ responses:
34
+ '201':
35
+ description: Null response
36
+ default:
37
+ description: unexpected error
38
+ schema:
39
+ "$ref": "#/definitions/Error"
@@ -0,0 +1,22 @@
1
+ ---
2
+ "/pets/{petId}":
3
+ get:
4
+ summary: Info for a specific pet
5
+ operationId: showPetById
6
+ tags:
7
+ - pets
8
+ parameters:
9
+ - name: petId
10
+ in: path
11
+ required: true
12
+ description: The id of the pet to retrieve
13
+ type: string
14
+ responses:
15
+ '200':
16
+ description: Expected response to a valid request
17
+ schema:
18
+ "$ref": "#/definitions/Pets"
19
+ default:
20
+ description: unexpected error
21
+ schema:
22
+ "$ref": "#/definitions/Error"
data/lib/oas_contrib.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require 'oas_contrib/version'
2
2
 
3
- #
4
- # <Description>
5
- #
3
+ # OAS Contribute module
4
+ # @author Michinao Shimizu
6
5
  module OasContrib
7
- # Your code goes here...
8
6
  end
@@ -1,83 +1,55 @@
1
1
  require 'thor'
2
- require 'oas_contrib/command_util'
2
+ require 'oas_contrib/command_resolvers/divide_command_resolver'
3
+ require 'oas_contrib/command_resolvers/merge_command_resolver'
3
4
 
4
5
  module OasContrib
5
- # OAS contrib commands
6
- # @author Michinao Shimizu
6
+ # Commands
7
7
  class Command < Thor
8
- include CommandUtil
8
+ option :out_type, type: :string, aliases: '-ot', default: 'yaml', desc: 'output file type (yaml or json)'
9
+ desc 'divide <input_file> <output_dir>', 'Divide the OAS file into path units and schema units.'
9
10
 
10
- option :in_type, :type => :string, :aliases => '-it', :default => 'yaml', :desc => 'input file type (yaml or json)'
11
- option :out_type, :type => :string, :aliases => '-ot', :default => 'yaml', :desc => 'output file type (yaml or json)'
12
- desc 'divide <input_file_path> <output_directory_path>', 'Divide the OAS file into path units and schema units.'
13
-
14
- # Divide the OAS file into path units and schema units
11
+ # Command (Divide the OAS file into path units and schema units)
15
12
  # @param [String] in_file input file path
16
13
  # @param [String] out_dir output directory path
17
- # @return [Boolean]
14
+ # @return [Integer]
18
15
  def divide(in_file, out_dir)
19
- raise ArgumentError, "in_file:[#{in_file}] is not exists." unless File.exist?(in_file)
20
- in_type = options['in_type']
21
- out_type = options['out_type']
22
- hash = input_solo(in_file, in_type)
23
- version = definition_version(hash)
24
-
25
- meta_file = get_meta_file_path(out_dir, out_type)
26
- path_dir = get_path_dir_path(out_dir)
27
- schema_dir = get_schema_dir_path(out_dir, version)
28
-
29
- FileUtils.mkdir_p(path_dir)
30
- FileUtils.mkdir_p(schema_dir)
31
-
32
- output_solo(meta_filter(hash), meta_file, out_type)
33
- output_multi(hash['paths'], path_dir, out_type)
34
- output_multi(schema_filter(hash, version), schema_dir, out_type)
16
+ resolver = CommandResolvers::DivideCommandResolver.new(in_file, out_dir, options['out_type'])
17
+ resolver.load
18
+ resolver.resolve
19
+ resolver.dist
20
+ exit(0)
35
21
  end
36
22
 
37
- option :in_type, :type => :string, :aliases => '-it', :default => 'yaml', :desc => 'input file type (yaml or json)'
38
- option :out_type, :type => :string, :aliases => '-ot', :default => 'yaml', :desc => 'output file type (yaml or json)'
39
- desc 'merge <input_directory_path> <output_file_path>', 'Merge multiple divided files into an OAS file.'
23
+ option :in_type, type: :string, aliases: '-it', default: 'yaml', desc: 'input file type (yaml or json)'
24
+ desc 'merge <input_dir> <output_file>', 'Merge multiple divided files into an OAS file.'
40
25
 
41
- # Merge multiple divided files into an OAS file.
26
+ # Command (Merge multiple divided files into an OAS file)
42
27
  # @param [String] in_dir input directory path
43
28
  # @param [String] out_file output file path
44
- # @return [Boolean]
29
+ # @return [Integer]
45
30
  def merge(in_dir, out_file)
46
- raise ArgumentError, "in_dir:[#{in_dir}] is not exists." unless File.exist?(in_dir)
47
- in_type = options['in_type']
48
- out_type = options['out_type']
49
-
50
- meta_file = get_meta_file_path(in_dir, in_type)
51
- hash = input_solo(meta_file, in_type)
52
- version = definition_version(hash)
53
-
54
- path_dir = get_path_dir_path(in_dir)
55
- schema_dir = get_schema_dir_path(in_dir, version)
56
-
57
- hash['components'] = {}
58
- hash['paths'] = input_multi(path_dir, in_type)
59
- case version
60
- when 'v2' then hash['definitions'] = input_multi(schema_dir, in_type)
61
- when 'v3' then hash['components']['schemas'] = input_multi(schema_dir, in_type)
62
- end
63
- output_solo(hash, out_file, out_type)
31
+ resolver = CommandResolvers::MergeCommandResolver.new(in_dir, out_file, options['in_type'])
32
+ resolver.load
33
+ resolver.resolve
34
+ resolver.dist
35
+ exit(0)
64
36
  end
65
37
 
66
- option :port, :type => :string, :aliases => '-p', :default => '50010', :desc => 'Swagger UI listen port'
38
+ option :port, type: :string, aliases: '-p', default: '50010', desc: 'Swagger UI listen port'
67
39
  desc 'preview <input_file>', 'Preview OAS file using Swagger-UI official Docker image.'
68
40
 
69
41
  # Preview OAS file using Swagger-UI official Docker image.
70
42
  # @param [String] in_file input file path
71
- # @return [Boolean] return code
43
+ # @return [Integer]
72
44
  def preview(in_file)
73
- raise ArgumentError, "in_file:[#{in_file}] is not exists." unless File.exist?(in_file)
74
45
  port = options['port']
75
46
  path = File.expand_path(in_file)
76
47
  basename = File.basename(path)
77
48
  puts "SwaggerUI listen: http://localhost:#{port} with: #{in_file}"
78
49
  Kernel.exec "docker run --rm --name oas_contrib_preview_swagger_ui \
79
- -p #{port}:8080 -e API_URL=#{basename} \
80
- -v #{path}:/usr/share/nginx/html/#{basename} swaggerapi/swagger-ui"
50
+ -p #{port}:8080 -e API_URL=#{basename} \
51
+ -v #{path}:/usr/share/nginx/html/#{basename} swaggerapi/swagger-ui"
52
+ exit(0)
81
53
  end
82
54
  end
83
55
  end
@@ -0,0 +1,140 @@
1
+ require 'oas_contrib/swagger/v2/spec'
2
+ require 'oas_contrib/openapi/v3/spec'
3
+ require 'yaml'
4
+ require 'json'
5
+
6
+ module OasContrib
7
+ # CommandResolver Basic
8
+ class CommandResolverBase
9
+ # Initialze
10
+ # @param [String] path input or output directory path
11
+ def initialize(path)
12
+ @meta_dir = path + '/meta'
13
+ @path_dir = path + '/path'
14
+ @model_dir = path + '/model'
15
+ end
16
+
17
+ # Load input files or directories
18
+ # @raise [NotImplementedError]
19
+ # @return [nil]
20
+ def load
21
+ raise NotImplementedError, 'This class must be implemented "load" method.'
22
+ end
23
+
24
+ # Distribute output file
25
+ # @raise [NotImplementedError]
26
+ # @return [nil]
27
+ def dist
28
+ raise NotImplementedError, 'This class must be implemented "dist" method.'
29
+ end
30
+
31
+ # Determine which type of definition data.
32
+ # @raise [StandardError]
33
+ # @return [OasContrib::Swagger::V2::Spec|OasContrib::OpenAPI::V3::Spec] spec
34
+ def resolve
35
+ return @spec = OasContrib::Swagger::V2::Spec.new(@load_data) if @load_data['swagger'] =~ /^2/
36
+ return @spec = OasContrib::OpenAPI::V3::Spec.new(@load_data) if @load_data['openapi'] =~ /^3/
37
+ raise 'Undefined OAS file.'
38
+ end
39
+
40
+ # Check the type of OpenAPI v3 definition or not
41
+ # @return [Boolean]
42
+ def openapi_v3?
43
+ @spec.is_a?(OasContrib::OpenAPI::V3::Spec)
44
+ end
45
+
46
+ # Check the type of Swagger v2 definition or not
47
+ # @return [Boolean]
48
+ def swagger_v2?
49
+ @spec.is_a?(OasContrib::Swagger::V2::Spec)
50
+ end
51
+
52
+ # Convert file type string to file extention string.
53
+ # @param [String] type file type string (yaml or json)
54
+ # @raise [ArgumentError] invalid file type string
55
+ # @return [String] file extension string (.yml or .json)
56
+ def file_type_to_ext(type)
57
+ case type
58
+ when 'yaml' then '.yml'
59
+ when 'json' then '.json'
60
+ else raise ArgumentError, 'Undefined file type'
61
+ end
62
+ end
63
+
64
+ # Load a file
65
+ # @param [String] path input file path
66
+ # @return [Hash]
67
+ def input(path)
68
+ @load_data = input_call(path)
69
+ end
70
+
71
+ # Output a file
72
+ # @param [Hash] hash data
73
+ # @param [String] path output file path
74
+ # @return [File]
75
+ def output(hash, path)
76
+ File.open(path, 'w') { |f| output_call(hash, f) }
77
+ end
78
+
79
+ # Load directory files
80
+ # @param [String] path input directory
81
+ # @return [Hash] merged input files data
82
+ def input_dir(path)
83
+ Dir.glob(path).sort.each_with_object({}, &input_call_lambda)
84
+ end
85
+
86
+ # Proc of input a yaml or json file
87
+ # @return [Proc]
88
+ def input_call_lambda
89
+ lambda do |file, result|
90
+ hash = input_call(file)
91
+ key = hash.keys[0]
92
+ result[key] = hash[key]
93
+ end
94
+ end
95
+
96
+ # Load a file depending on file extension
97
+ # @param [String] path file path
98
+ # @raise [ArgumentError] invalid file type string
99
+ # @return [Hash]
100
+ def input_call(path)
101
+ puts "Load: #{path}"
102
+ case @input_file_ext
103
+ when '.yml' then YAML.load_file(path)
104
+ when '.json' then JSON.parse(File.read(path))
105
+ else raise ArgumentError, 'Undefined file type'
106
+ end
107
+ end
108
+
109
+ # Output a file depending on file extension
110
+ # @param [String] path file path
111
+ # @raise [ArgumentError] invalid file type string
112
+ # @return [IO]
113
+ def output_call(hash, file)
114
+ puts "Dist: #{file.path}"
115
+ case @output_file_ext
116
+ when '.yml' then YAML.dump(hash, file)
117
+ when '.json' then JSON.dump(hash, file)
118
+ else raise ArgumentError, 'Undefined file type'
119
+ end
120
+ end
121
+
122
+ # Output directory and files
123
+ # @param [Hash] hash data
124
+ # @param [String] path directory path
125
+ # @return [nil]
126
+ def output_dir(hash, path)
127
+ puts "Dist: #{path}"
128
+ FileUtils.mkdir_p(path)
129
+
130
+ i = 1
131
+ hash.each do |k, _v|
132
+ key = k.tr('/', '_').gsub(/^_/, '')
133
+ val = hash.select { |hash_key, _| hash_key == k }
134
+ output(val, "#{path}/#{i.to_s.rjust(3, '0')}_#{key}#{@output_file_ext}")
135
+ i += 1
136
+ end
137
+ nil
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,34 @@
1
+ require 'oas_contrib/command_resolver_base'
2
+
3
+ module OasContrib
4
+ # Command Resolvers
5
+ module CommandResolvers
6
+ # Divide command resolver
7
+ class DivideCommandResolver < CommandResolverBase
8
+ # Initialize
9
+ # @param [String] input_file_path input file path
10
+ # @param [String] output_dir_path output directory path
11
+ # @param [String] output_file_type output file type (json or yaml)
12
+ def initialize(input_file_path, output_dir_path, output_file_type)
13
+ @input_file_path = input_file_path
14
+ @input_file_ext = File.extname(input_file_path)
15
+ @output_file_ext = file_type_to_ext(output_file_type)
16
+ super(output_dir_path)
17
+ end
18
+
19
+ # Load the OAS file
20
+ # @return [Hash] loaded data
21
+ def load
22
+ input(@input_file_path)
23
+ end
24
+
25
+ # Output divided files
26
+ # @return [nil]
27
+ def dist
28
+ output_dir(@spec.meta, @meta_dir)
29
+ output_dir(@spec.path, @path_dir)
30
+ output_dir(@spec.model, @model_dir)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,60 @@
1
+ require 'oas_contrib/command_resolver_base'
2
+
3
+ module OasContrib
4
+ # Command Resolvers
5
+ module CommandResolvers
6
+ # Merge command resolver
7
+ class MergeCommandResolver < CommandResolverBase
8
+ # Initialize
9
+ # @param [String] input_dir_path input directory path
10
+ # @param [String] output_file_path output file path
11
+ # @param [String] input_file_type input file type (json or yaml)
12
+ def initialize(input_dir_path, output_file_path, input_file_type)
13
+ @input_file_ext = file_type_to_ext(input_file_type)
14
+ @output_file_path = output_file_path
15
+ @output_file_ext = File.extname(output_file_path)
16
+ super(input_dir_path)
17
+ end
18
+
19
+ # Load divided files
20
+ # @return [Hash] load and merged data
21
+ def load
22
+ load_meta
23
+ resolve
24
+ load_path
25
+ load_model
26
+ end
27
+
28
+ # Output the OAS file
29
+ # @return [nil]
30
+ def dist
31
+ output(@load_data, @output_file_path)
32
+ end
33
+
34
+ private
35
+
36
+ # Load meta part files
37
+ # @return [Hash] loaded data
38
+ def load_meta
39
+ @load_data = input_dir(@meta_dir + '/**/*' + @input_file_ext)
40
+ end
41
+
42
+ # Load path part files
43
+ # @return [Hash] load and merged data
44
+ def load_path
45
+ @load_data['paths'] = input_dir(@path_dir + '/**/*' + @input_file_ext)
46
+ end
47
+
48
+ # Load model part files
49
+ # @return [Hash] load and merged data
50
+ def load_model
51
+ path = @model_dir + '/**/*' + @input_file_ext
52
+ @load_data['definitions'] = input_dir(path) if swagger_v2?
53
+ if openapi_v3?
54
+ @load_data['components'] = {}
55
+ @load_data['components']['schemas'] = input_dir(path)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,21 @@
1
+ module OasContrib
2
+ # Open API definition module
3
+ module OpenAPI
4
+ # Version 3 module
5
+ module V3
6
+ # Spec class
7
+ class Spec
8
+ attr_accessor :data, :meta, :path, :model
9
+
10
+ # Initialize
11
+ # @param [Hash] data mapping data
12
+ def initialize(data)
13
+ @data = data
14
+ @meta = data.select { |v| v != 'paths' && v != 'components' } || nil
15
+ @path = data['paths'] || nil
16
+ @model = data.dig('components', 'schemas') || nil
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ module OasContrib
2
+ # Swagger module
3
+ module Swagger
4
+ # Version 2 module
5
+ module V2
6
+ # Spec
7
+ class Spec
8
+ attr_accessor :data, :meta, :path, :model
9
+ # Initialize
10
+ # @param [Hash] data mapping data
11
+ def initialize(data)
12
+ @data = data
13
+ @meta = data.select { |v| v != 'paths' && v != 'definitions' } || nil
14
+ @path = data['paths'] || nil
15
+ @model = data['definitions'] || nil
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,4 +1,4 @@
1
1
  module OasContrib
2
- # @return [String] semantic version
3
- VERSION = '0.1.1'.freeze
2
+ # @return [String] version
3
+ VERSION = '0.2.0'.freeze
4
4
  end
data/oas_contrib.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = 'Open API Specification related commands and libraries.'
12
12
  spec.description = 'Open API Specification related commands and libraries.'
13
- spec.homepage = "https://github.com/MichinaoShimizu/oas_contrib"
13
+ spec.homepage = 'https://github.com/MichinaoShimizu/oas_contrib'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oas_contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michinao Shimizu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-03 00:00:00.000000000 Z
11
+ date: 2018-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -82,14 +82,39 @@ files:
82
82
  - Rakefile
83
83
  - bin/console
84
84
  - bin/setup
85
+ - example/dist/openapi_v3/meta/001_openapi.yml
86
+ - example/dist/openapi_v3/meta/002_info.yml
87
+ - example/dist/openapi_v3/meta/003_servers.yml
88
+ - example/dist/openapi_v3/model/001_Pet.yml
89
+ - example/dist/openapi_v3/model/002_Error.yml
90
+ - example/dist/openapi_v3/model/003_Pets.yml
91
+ - example/dist/openapi_v3/path/001_pets.yml
92
+ - example/dist/openapi_v3/path/002_pets_{petId}.yml
93
+ - example/dist/swagger_v2/meta/001_swagger.yml
94
+ - example/dist/swagger_v2/meta/002_info.yml
95
+ - example/dist/swagger_v2/meta/003_host.yml
96
+ - example/dist/swagger_v2/meta/004_basePath.yml
97
+ - example/dist/swagger_v2/meta/005_schemes.yml
98
+ - example/dist/swagger_v2/meta/006_consumes.yml
99
+ - example/dist/swagger_v2/meta/007_produces.yml
100
+ - example/dist/swagger_v2/meta/008_components.yml
101
+ - example/dist/swagger_v2/model/001_Pet.yml
102
+ - example/dist/swagger_v2/model/002_Error.yml
103
+ - example/dist/swagger_v2/model/003_Pets.yml
104
+ - example/dist/swagger_v2/path/001_pets.yml
105
+ - example/dist/swagger_v2/path/002_pets_{petId}.yml
106
+ - example/sample_petstore_openapi_v3.yml
107
+ - example/sample_petstore_swagger_v2.yml
85
108
  - exe/oas_contrib
86
109
  - lib/oas_contrib.rb
87
110
  - lib/oas_contrib/command.rb
88
- - lib/oas_contrib/command_util.rb
111
+ - lib/oas_contrib/command_resolver_base.rb
112
+ - lib/oas_contrib/command_resolvers/divide_command_resolver.rb
113
+ - lib/oas_contrib/command_resolvers/merge_command_resolver.rb
114
+ - lib/oas_contrib/openapi/v3/spec.rb
115
+ - lib/oas_contrib/swagger/v2/spec.rb
89
116
  - lib/oas_contrib/version.rb
90
117
  - oas_contrib.gemspec
91
- - sample_petstore_2.yml
92
- - sample_petstore_3.yml
93
118
  homepage: https://github.com/MichinaoShimizu/oas_contrib
94
119
  licenses:
95
120
  - MIT
@@ -1,125 +0,0 @@
1
- require 'yaml'
2
- require 'json'
3
-
4
- module OasContrib
5
- # Command class utility
6
- module CommandUtil
7
- # @return [String]
8
- FILE_TYPE_YAML = 'yaml'.freeze
9
- # @return [String]
10
- FILE_TYPE_JSON = 'json'.freeze
11
-
12
- def definition_version(hash)
13
- if hash['swagger']
14
- puts 'OK. input file is swagger 2.0 format.'
15
- return 'v2'
16
- end
17
-
18
- if hash['openapi']
19
- puts 'OK. input file is openapi 3.0 format.'
20
- return 'v3'
21
- end
22
-
23
- raise 'input file must be swagger 2.0, or openapi 3.0 format.'
24
- end
25
-
26
- def schema_filter(hash, version)
27
- case version
28
- when 'v2' then hash['definitions']
29
- when 'v3' then hash['components']['schemas']
30
- end
31
- end
32
-
33
- def get_meta_file_path(root, file_type)
34
- "#{root}/meta#{file_type_ext(file_type)}"
35
- end
36
-
37
- def get_path_dir_path(root)
38
- "#{root}/paths"
39
- end
40
-
41
- def get_schema_dir_path(root, version)
42
- schema_path = case version
43
- when 'v2' then '/definitions'
44
- when 'v3' then '/components/schemas'
45
- end
46
-
47
- "#{root}#{schema_path}"
48
- end
49
-
50
- # Get file extension
51
- # @param [String] file_type yaml or json
52
- # @return [String] .yml or .json
53
- def file_type_ext(file_type)
54
- case file_type
55
- when FILE_TYPE_YAML then '.yml'
56
- when FILE_TYPE_JSON then '.json'
57
- end
58
- end
59
-
60
- # Get hash from yaml or json file
61
- # @param [String] path <description>
62
- # @param [String] file_type <description>
63
- # @return [Hash]
64
- def input_solo(path, file_type)
65
- hash = case file_type
66
- when FILE_TYPE_YAML then YAML.load_file(path)
67
- when FILE_TYPE_JSON then JSON.parse(File.read(path))
68
- end
69
-
70
- puts "Load #{file_type} complete: #{path}"
71
- hash
72
- end
73
-
74
- # Load path files and get hash
75
- # @param [String] dir_path target directory path
76
- # @param [String] file_type yaml or json
77
- # @return [Hash] parsed hash
78
- def input_multi(dir_path, file_type)
79
- path = "#{dir_path}/*#{file_type_ext(file_type)}"
80
- Dir.glob(path).each_with_object({}) do |input_file_path, result|
81
- hash = input_solo(input_file_path, file_type)
82
- key = hash.keys[0]
83
- result[key] = hash[key]
84
- end
85
- end
86
-
87
- # <Description>
88
- # @param [<Type>] hash <description>
89
- # @param [<Type>] path <description>
90
- # @param [<Type>] file_type <description>
91
- # @return [<Type>] <description>
92
- def output_solo(hash, path, file_type)
93
- output_lambda = case file_type
94
- when FILE_TYPE_YAML then ->(file) { YAML.dump(hash, file) }
95
- when FILE_TYPE_JSON then ->(file) { JSON.dump(hash, file) }
96
- end
97
-
98
- File.open(path, 'w') do |f|
99
- output_lambda.call(f)
100
- puts "Generate #{file_type} complete: #{path}"
101
- end
102
- end
103
-
104
- # Output path definition files
105
- # @param [Hash] hash data source hash
106
- # @param [String] dir_path output directory path
107
- # @param [String] file_type output file type (yaml, json)
108
- # @return [Boolean]
109
- def output_multi(hash, dir_path, file_type)
110
- hash.each do |k, _|
111
- key = k.tr('/', '_').gsub(/^\_/, '')
112
- path = "#{dir_path}/#{key}#{file_type_ext(file_type)}"
113
- val = hash.select { |hash_key, _| hash_key == k }
114
- output_solo(val, path, file_type)
115
- end
116
- end
117
-
118
- # Return meta section hash
119
- # @param [Hash] hash data source hash
120
- # @return [Hash] filtered hash
121
- def meta_filter(hash)
122
- hash.select { |k, _| k != 'paths' && k != 'components' && k != 'definitions' }
123
- end
124
- end
125
- end