swagger_autogenerate 1.0.1 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -1
- data/README.md +15 -6
- data/lib/swagger_autogenerate/swagger_trace.rb +45 -36
- data/lib/swagger_autogenerate/version.rb +1 -1
- data/lib/swagger_autogenerate.rb +1 -1
- data/swagger_autogenerate-1.0.2.gem +0 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3f7b9b67269b270dff6c7ec738c7b4ef8cafa149494c41e74671b0a193bc9d1
|
4
|
+
data.tar.gz: dadf1b845cb96e70f375a245d606b0b4a487b6a945e8ea2508bb102d9e0fbca3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 261b31e5e7c4d02f72c3a60c0262d7034210685e690d66babc899c006886bcf4887380e8d0e58904eafa433552fc56f7ee4ac0ffe7c58c2654c2c9c216c2a3ac
|
7
|
+
data.tar.gz: 28034a707e1568ecf04a668427b186729ea92a9709e847d4db6a29d59a02fd866fd394ed6f10e3fa26e34b51d2438309fa4047f0acbf6b86a5e36cdb9aad7821
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -7,6 +7,12 @@ automating Swagger YAML generation in Ruby on Rails offers a range of benefits f
|
|
7
7
|
|
8
8
|
The gem automatically observes the request/response patterns during the execution of test scenarios, generating accurate Swagger YAML files that reflect the API's behavior. developers and consumers can better understand and interact with the APIs.
|
9
9
|
|
10
|
+
## Dependencies
|
11
|
+
|
12
|
+
The SwaggerAutogenerate gem depends on the rspec-rails gem, which brings the RSpec testing framework to Ruby on Rails.
|
13
|
+
Please install rspec-rails first: https://github.com/rspec/rspec-rails
|
14
|
+
Then continue the installation process.
|
15
|
+
|
10
16
|
## Installation
|
11
17
|
|
12
18
|
1) Open your Gemfile located at ./Gemfile
|
@@ -18,7 +24,9 @@ The gem automatically observes the request/response patterns during the executio
|
|
18
24
|
end
|
19
25
|
```
|
20
26
|
3) Install the gem and add to the application's Gemfile by executing:
|
21
|
-
|
27
|
+
```
|
28
|
+
bundle install
|
29
|
+
```
|
22
30
|
|
23
31
|
## Configuration
|
24
32
|
|
@@ -31,6 +39,11 @@ To configure the swagger_autogenerate gem in your Rails application, follow thes
|
|
31
39
|
```
|
32
40
|
include SwaggerAutogenerate if Rails.env.test? && ENV['SWAGGER'].present?
|
33
41
|
```
|
42
|
+
in the #Example-step you will set the ENV['SWAGGER'],
|
43
|
+
|
44
|
+
By setting the ENV['SWAGGER'] environment variable, you can specify the path to the new file,
|
45
|
+
whether it's a specific file (e.g., ../myfile.yaml) or a directory (e.g., ../docs).
|
46
|
+
This flexibility ensures that the documentation can be easily integrated into your project's structure and workflow.
|
34
47
|
|
35
48
|
### Step 2 (optional)
|
36
49
|
1) Create a file called swagger_autogenerate.rb in the ./config/initializers
|
@@ -56,7 +69,7 @@ To generate Swagger YAML documentation for the APIs implemented in the Employees
|
|
56
69
|
$ spec/your_path/employees_controller_spec.rb
|
57
70
|
This file should contain the test scenarios for each action (e.g., index, show, create) of the controller.
|
58
71
|
|
59
|
-
3)Run the spec code using the rspec command and set the environment variable SWAGGER to the desired YAML file name. For example:
|
72
|
+
3) Run the spec code using the rspec command and set the environment variable SWAGGER to the desired YAML file name. For example:
|
60
73
|
```
|
61
74
|
SWAGGER='employee_apis.yaml' rspec spec/your_path/employees_controller_spec.rb
|
62
75
|
```
|
@@ -69,7 +82,3 @@ that the generated documentation will depend on the test scenarios defined in yo
|
|
69
82
|
## License
|
70
83
|
|
71
84
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
72
|
-
|
73
|
-
## Code of Conduct
|
74
|
-
|
75
|
-
Everyone interacting in the SwaggerAutogenerate project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/swagger_autogenerate/blob/master/CODE_OF_CONDUCT.md).
|
@@ -1,31 +1,40 @@
|
|
1
1
|
require_relative 'swagger_public_methods'
|
2
|
+
require_relative 'configuration'
|
2
3
|
|
3
4
|
module SwaggerAutogenerate
|
4
5
|
class SwaggerTrace
|
5
|
-
WITH_CONFIG = true
|
6
|
-
WITH_MULTIPLE_EXAMPLES = true
|
7
|
-
WITH_EXAMPLE_DESCRIPTION = true
|
8
|
-
WITH_RESPONSE_DESCRIPTION = true
|
9
|
-
SWAGGER_ENVIRONMENT_VARIABLE = 'SWAGGER'
|
10
|
-
|
11
6
|
include SwaggerPublicMethods
|
12
7
|
|
13
8
|
def initialize(request, response)
|
9
|
+
@with_config = ::SwaggerAutogenerate.configuration.with_config
|
10
|
+
@with_multiple_examples = ::SwaggerAutogenerate.configuration.with_multiple_examples
|
11
|
+
@with_example_description = ::SwaggerAutogenerate.configuration.with_example_description
|
12
|
+
@with_response_description = ::SwaggerAutogenerate.configuration.with_response_description
|
14
13
|
@request = request
|
15
14
|
@response = response
|
16
|
-
|
15
|
+
@@paths = {}
|
17
16
|
end
|
18
17
|
|
19
18
|
def call
|
20
|
-
if ENV[
|
19
|
+
if ENV[swagger_environment_variable].present?
|
21
20
|
read_swagger_trace
|
22
21
|
write_swagger_trace
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
25
|
+
def self.swagger_environment_variable
|
26
|
+
::SwaggerAutogenerate.configuration.swagger_environment_variable
|
27
|
+
end
|
28
|
+
|
29
|
+
def swagger_environment_variable
|
30
|
+
SwaggerTrace.swagger_environment_variable
|
31
|
+
end
|
32
|
+
|
26
33
|
private
|
27
34
|
|
28
|
-
attr_reader :request, :response, :current_path, :yaml_file
|
35
|
+
attr_reader :request, :response, :current_path, :yaml_file, :configuration,
|
36
|
+
:with_config, :with_multiple_examples, :with_example_description,
|
37
|
+
:with_response_description
|
29
38
|
|
30
39
|
# main methods
|
31
40
|
|
@@ -51,13 +60,13 @@ module SwaggerAutogenerate
|
|
51
60
|
}
|
52
61
|
|
53
62
|
hash[method].except!('requestBody') if hash[method]['requestBody'].blank?
|
54
|
-
|
55
|
-
|
63
|
+
paths[path.to_s] ||= {}
|
64
|
+
paths[path.to_s].merge!(hash)
|
56
65
|
end
|
57
66
|
|
58
67
|
def write_swagger_trace
|
59
|
-
if
|
60
|
-
|
68
|
+
if paths[current_path][request.method.downcase].present?
|
69
|
+
paths[current_path][request.method.downcase]['responses'] = swagger_response
|
61
70
|
end
|
62
71
|
|
63
72
|
if File.exist?(swagger_location)
|
@@ -69,8 +78,8 @@ module SwaggerAutogenerate
|
|
69
78
|
|
70
79
|
def create_file
|
71
80
|
File.open(swagger_location, 'w') do |file|
|
72
|
-
data =
|
73
|
-
data['paths'] =
|
81
|
+
data = with_config ? swagger_config : {}
|
82
|
+
data['paths'] = paths
|
74
83
|
organize_result(data['paths'])
|
75
84
|
data = data.to_hash
|
76
85
|
result = add_quotes_to_dates(YAML.dump(data))
|
@@ -85,15 +94,15 @@ module SwaggerAutogenerate
|
|
85
94
|
permitted_classes: [Symbol, Date, ActiveSupport::HashWithIndifferentAccess]
|
86
95
|
)
|
87
96
|
|
88
|
-
return create_file if
|
97
|
+
return create_file if yaml_file.nil? || yaml_file['paths'].nil?
|
89
98
|
|
90
|
-
|
99
|
+
yaml_file.merge!(swagger_config) if with_config
|
91
100
|
|
92
101
|
apply_yaml_file_changes
|
93
|
-
organize_result(
|
94
|
-
@yaml_file = convert_to_hash(
|
102
|
+
organize_result(yaml_file['paths'])
|
103
|
+
@yaml_file = convert_to_hash(yaml_file)
|
95
104
|
File.open(swagger_location, 'w') do |file|
|
96
|
-
result = add_quotes_to_dates(YAML.dump(
|
105
|
+
result = add_quotes_to_dates(YAML.dump(yaml_file))
|
97
106
|
file.write(result)
|
98
107
|
end
|
99
108
|
end
|
@@ -185,7 +194,7 @@ module SwaggerAutogenerate
|
|
185
194
|
swagger_response = { 'file' => 'file/data' }
|
186
195
|
end
|
187
196
|
|
188
|
-
hash['description'] = response_description if
|
197
|
+
hash['description'] = response_description if with_response_description
|
189
198
|
hash['headers'] = {} # response.headers
|
190
199
|
hash['content'] = content_json_example(swagger_response)
|
191
200
|
|
@@ -298,7 +307,7 @@ module SwaggerAutogenerate
|
|
298
307
|
# Static
|
299
308
|
|
300
309
|
def paths
|
301
|
-
|
310
|
+
@@paths ||= {}
|
302
311
|
end
|
303
312
|
|
304
313
|
def security
|
@@ -315,10 +324,10 @@ module SwaggerAutogenerate
|
|
315
324
|
def swagger_location
|
316
325
|
return @swagger_location if instance_variable_defined?(:@swagger_location)
|
317
326
|
|
318
|
-
if ENV[
|
319
|
-
@swagger_location = Rails.root.join(ENV.fetch(
|
327
|
+
if ENV[swagger_environment_variable].include?('.yaml') || ENV[swagger_environment_variable].include?('.yml')
|
328
|
+
@swagger_location = Rails.root.join(ENV.fetch(swagger_environment_variable, nil).to_s).to_s
|
320
329
|
else
|
321
|
-
directory_path = Rails.root.join(ENV.fetch(
|
330
|
+
directory_path = Rails.root.join(ENV.fetch(swagger_environment_variable, nil).to_s).to_s
|
322
331
|
FileUtils.mkdir_p(directory_path) unless File.directory?(directory_path)
|
323
332
|
@swagger_location = "#{directory_path}/#{tags.first}.yaml"
|
324
333
|
end
|
@@ -344,7 +353,7 @@ module SwaggerAutogenerate
|
|
344
353
|
}
|
345
354
|
}
|
346
355
|
}
|
347
|
-
hash['application/json']['examples']['example-0']['description'] = "payload => #{example_description}" if
|
356
|
+
hash['application/json']['examples']['example-0']['description'] = "payload => #{example_description}" if with_example_description && !example_description.empty?
|
348
357
|
|
349
358
|
hash
|
350
359
|
end
|
@@ -386,7 +395,7 @@ module SwaggerAutogenerate
|
|
386
395
|
unless old_examples.value?(current_example)
|
387
396
|
last_example = json_example_plus_one(old_examples.keys.last)
|
388
397
|
last_example ||= 'example-0'
|
389
|
-
last_example = 'example-0' unless
|
398
|
+
last_example = 'example-0' unless with_multiple_examples
|
390
399
|
yaml_file['paths'][current_path][request.method.downcase]['responses'][response.status.to_s]['content']['application/json']['examples'][last_example] = current_example
|
391
400
|
end
|
392
401
|
|
@@ -420,7 +429,7 @@ module SwaggerAutogenerate
|
|
420
429
|
|
421
430
|
def check_path
|
422
431
|
unless old_paths.key?(current_path)
|
423
|
-
yaml_file['paths'].merge!({ current_path =>
|
432
|
+
yaml_file['paths'].merge!({ current_path => paths[current_path] })
|
424
433
|
end
|
425
434
|
end
|
426
435
|
|
@@ -444,29 +453,29 @@ module SwaggerAutogenerate
|
|
444
453
|
|
445
454
|
def check_parameters
|
446
455
|
if old_paths[current_path][request.method.downcase]['parameters'].blank?
|
447
|
-
yaml_file['paths'][current_path][request.method.downcase]['parameters'] =
|
456
|
+
yaml_file['paths'][current_path][request.method.downcase]['parameters'] = paths[current_path][request.method.downcase]['parameters']
|
448
457
|
end
|
449
458
|
end
|
450
459
|
|
451
460
|
def check_parameter
|
452
|
-
param_names =
|
461
|
+
param_names = paths[current_path][request.method.downcase]['parameters'].pluck('name') - yaml_file['paths'][current_path][request.method.downcase]['parameters'].pluck('name')
|
453
462
|
param_names.each do |param_name|
|
454
|
-
param =
|
463
|
+
param = paths[current_path][request.method.downcase]['parameters'].find { |parameter| parameter['name'] == param_name }
|
455
464
|
yaml_file['paths'][current_path][request.method.downcase]['parameters'].push(param)
|
456
465
|
end
|
457
466
|
end
|
458
467
|
|
459
468
|
def check_request_bodys
|
460
|
-
if
|
461
|
-
yaml_file['paths'][current_path][request.method.downcase]['requestBody'] =
|
469
|
+
if paths[current_path][request.method.downcase]['requestBody'].present? && old_paths[current_path][request.method.downcase]['requestBody'].nil?
|
470
|
+
yaml_file['paths'][current_path][request.method.downcase]['requestBody'] = paths[current_path][request.method.downcase]['requestBody']
|
462
471
|
end
|
463
472
|
end
|
464
473
|
|
465
474
|
def check_request_body
|
466
|
-
if
|
467
|
-
param_names =
|
475
|
+
if paths[current_path][request.method.downcase]['requestBody'].present?
|
476
|
+
param_names = paths[current_path][request.method.downcase]['requestBody']['content']['multipart/form-data']['schema']['properties'].keys - yaml_file['paths'][current_path][request.method.downcase]['requestBody']['content']['multipart/form-data']['schema']['properties'].keys
|
468
477
|
param_names.each do |param_name|
|
469
|
-
param =
|
478
|
+
param = paths[current_path][request.method.downcase]['requestBody']['content']['multipart/form-data']['schema']['properties'].select { |parameter| parameter == param_name }
|
470
479
|
yaml_file['paths'][current_path][request.method.downcase]['requestBody']['content']['multipart/form-data']['schema']['properties'].merge!(param)
|
471
480
|
end
|
472
481
|
end
|
data/lib/swagger_autogenerate.rb
CHANGED
@@ -5,7 +5,7 @@ module SwaggerAutogenerate
|
|
5
5
|
require_relative 'swagger_autogenerate/swagger_trace.rb'
|
6
6
|
|
7
7
|
included do
|
8
|
-
if defined?(Rails) &&
|
8
|
+
if defined?(Rails) && ENV[SwaggerTrace.swagger_environment_variable].present?
|
9
9
|
def process_action(*args)
|
10
10
|
super
|
11
11
|
|
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swagger_autogenerate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MohammedBuraiah
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- sig/swagger_autogenerate.rbs
|
32
32
|
- swagger_autogenerate-0.1.0.gem
|
33
33
|
- swagger_autogenerate-0.1.1.gem
|
34
|
+
- swagger_autogenerate-1.0.2.gem
|
34
35
|
- swagger_autogenerate.gemspec
|
35
36
|
homepage: https://github.com/MohammedBuraiah/swagger_autogenerate
|
36
37
|
licenses:
|