mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 23:32:58 +00:00
Compare commits
61 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a46a5be3bb | ||
|
|
ccce4d6360 | ||
|
|
663294b84b | ||
|
|
889dd0922a | ||
|
|
99281f96be | ||
|
|
3dc0909a6a | ||
|
|
dad99930aa | ||
|
|
c108a8367c | ||
|
|
8d8fc11e55 | ||
|
|
8d04646341 | ||
|
|
f8fb73fb14 | ||
|
|
4516ad4b78 | ||
|
|
2d5da62bf1 | ||
|
|
dc161fe275 | ||
|
|
eeb1026691 | ||
|
|
2c0f3c9396 | ||
|
|
acab437a7d | ||
|
|
3ff1de5d65 | ||
|
|
0e04635b15 | ||
|
|
73b84101cc | ||
|
|
2cf6812ae0 | ||
|
|
90d7fada85 | ||
|
|
c14f72a45e | ||
|
|
9722419647 | ||
|
|
bf6bd59094 | ||
|
|
ea2ed6ca49 | ||
|
|
d12c018cb7 | ||
|
|
d84a89510d | ||
|
|
8d7385fcac | ||
|
|
a18d9e5163 | ||
|
|
5b1dde772a | ||
|
|
29becf98f8 | ||
|
|
1d5ed8345f | ||
|
|
71423cffb6 | ||
|
|
0def9fad6f | ||
|
|
e5eb44191c | ||
|
|
28245d4dd0 | ||
|
|
07c4c74d75 | ||
|
|
b1e089af6a | ||
|
|
47eb1e49ee | ||
|
|
4d29e09010 | ||
|
|
189a7ef061 | ||
|
|
5ee880b769 | ||
|
|
778d250385 | ||
|
|
dd2eaf2feb | ||
|
|
77d00407a4 | ||
|
|
8b61984fb3 | ||
|
|
3542cd0857 | ||
|
|
69edcd1be6 | ||
|
|
b41d4e1276 | ||
|
|
dc2ebd94bb | ||
|
|
17d7e020e8 | ||
|
|
57da84d055 | ||
|
|
5c9154864e | ||
|
|
0246ff164f | ||
|
|
49b5059273 | ||
|
|
9eb2d3ddec | ||
|
|
51b47f32e8 | ||
|
|
29c9f7cae2 | ||
|
|
bfd3d66ec2 | ||
|
|
c9bda862b6 |
17
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
17
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
## Describe the bug
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
## Steps to Test or Reproduce
|
||||
Please provide an example repo or the steps to reproduce the behavior.
|
||||
|
||||
## Expected behavior
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
## Screenshots
|
||||
If applicable, add screenshots to help explain your problem.
|
||||
|
||||
## Additional context
|
||||
Add any other context about the problem here.
|
||||
|
||||
## Rswag Version
|
||||
The version of rswag are you using.
|
||||
11
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
11
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
## Is your feature request related to a problem? Please describe.
|
||||
A clear and concise description of what the problem is.
|
||||
|
||||
## Describe the solution you'd like
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
## Describe alternatives you've considered
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
## Additional context
|
||||
Add any other context or screenshots about the feature request here.
|
||||
15
.github/PULL_REQUEST_TEMPLATE/pull_request_template.md
vendored
Normal file
15
.github/PULL_REQUEST_TEMPLATE/pull_request_template.md
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
## Problem
|
||||
A clear and concise description of what the problem is.
|
||||
|
||||
## Solution
|
||||
A clear and concise description of what the solution is.
|
||||
|
||||
### Related Issues
|
||||
Links to any related issues.
|
||||
|
||||
### Checklist
|
||||
- [ ] Added tests
|
||||
- [ ] Changelog updated
|
||||
|
||||
### Steps to Test or Reproduce
|
||||
Outline the steps to test or reproduce the PR here.
|
||||
@@ -1 +1 @@
|
||||
2.6.4
|
||||
2.6.3
|
||||
|
||||
10
.travis.yml
10
.travis.yml
@@ -1,11 +1,11 @@
|
||||
language: ruby
|
||||
|
||||
dist: xenial
|
||||
dist: bionic
|
||||
services:
|
||||
- xvfb
|
||||
|
||||
rvm:
|
||||
- 2.6.4
|
||||
- 2.6.3
|
||||
|
||||
env:
|
||||
- RAILS_VERSION=6.0.0
|
||||
@@ -19,7 +19,7 @@ addons:
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- /home/travis/.rvm/gems/ruby-2.6.4
|
||||
- /home/travis/.rvm/gems/ruby-2.6.3
|
||||
|
||||
install: ./ci/build.sh
|
||||
|
||||
@@ -29,6 +29,7 @@ jobs:
|
||||
include:
|
||||
- stage: publish components
|
||||
script: 'cd rswag-api'
|
||||
if: tag IS present
|
||||
deploy:
|
||||
gemspec: rswag-api.gemspec
|
||||
provider: rubygems
|
||||
@@ -39,6 +40,7 @@ jobs:
|
||||
|
||||
- stage: publish components
|
||||
script: 'cd rswag-specs'
|
||||
if: tag IS present
|
||||
deploy:
|
||||
gemspec: rswag-specs.gemspec
|
||||
provider: rubygems
|
||||
@@ -49,6 +51,7 @@ jobs:
|
||||
|
||||
- stage: publish components
|
||||
script: 'cd rswag-ui'
|
||||
if: tag IS present
|
||||
deploy:
|
||||
gemspec: rswag-ui.gemspec
|
||||
provider: rubygems
|
||||
@@ -60,6 +63,7 @@ jobs:
|
||||
|
||||
- stage: publish rswag
|
||||
script: 'cd rswag'
|
||||
if: tag IS present
|
||||
deploy:
|
||||
gemspec: rswag.gemspec
|
||||
provider: rubygems
|
||||
|
||||
21
CHANGELOG.md
21
CHANGELOG.md
@@ -12,15 +12,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
### Fixed
|
||||
### Security
|
||||
|
||||
## [2.2.0] - 2019-11-01
|
||||
### Added
|
||||
- New swagger_format config option for setting YAML output [#251](https://github.com/rswag/rswag/pull/251)
|
||||
### Changed
|
||||
- rswag-api will serve yaml files as yaml [#251](https://github.com/rswag/rswag/pull/251)
|
||||
|
||||
## [2.1.1] - 2019-10-18
|
||||
### Fixed
|
||||
- Fix incorrect require reference for swagger_generator [#248](https://github.com/rswag/rswag/issues/248)
|
||||
|
||||
## [2.1.0] - 2019-10-17
|
||||
### Added
|
||||
- New Spec Generator [#75](https://github.com/rswag/rswag/pull/75)
|
||||
- Support for Options and Trace verbs; You must use a framework that supports this, for Options Rails 6.1+ Rails 6 does not support Trace. [#237](https://github.com/rswag/rswag/pull/75)
|
||||
### Changed
|
||||
- Update swagger-ui to 3.18.2 [#240](https://github.com/rswag/rswag/pull/240)
|
||||
|
||||
## [2.0.6] - 2019-10-03
|
||||
### Added
|
||||
- Support for Rails 6 [#228](https://github.com/rswag/rswag/pull/228)
|
||||
- Support for Windows paths [#176](https://github.com/rswag/rswag/pull/176)
|
||||
### Changed
|
||||
- Show response body when error code is not expected [#117](https://github.com/rswag/rswag/pull/177)
|
||||
### Deprecated
|
||||
### Removed
|
||||
### Fixed
|
||||
### Security
|
||||
|
||||
## [2.0.5] - 2018-07-10
|
||||
|
||||
39
README.md
39
README.md
@@ -1,10 +1,11 @@
|
||||
rswag
|
||||
=========
|
||||
[](https://travis-ci.org/rswag/rswag)
|
||||
[](https://codeclimate.com/github/rswag/rswag/maintainability)
|
||||
|
||||
[Swagger](http://swagger.io) tooling for Rails API's. Generate beautiful API documentation, including a UI to explore and test operations, directly from your rspec integration tests.
|
||||
|
||||
Rswag extends rspec-rails "request specs" with a Swagger-based DSL for describing and testing API operations. You describe your API operations with a succinct, intuitive syntax, and it automaticaly runs the tests. Once you have green tests, run a rake task to auto-generate corresponding Swagger files and expose them as JSON endpoints. Rswag also provides an embedded version of the awesome [swagger-ui](https://github.com/swagger-api/swagger-ui) that's powered by the exposed JSON. This toolchain makes it seamless to go from integration specs, which youre probably doing in some form already, to living documentation for your API consumers.
|
||||
Rswag extends rspec-rails "request specs" with a Swagger-based DSL for describing and testing API operations. You describe your API operations with a succinct, intuitive syntax, and it automaticaly runs the tests. Once you have green tests, run a rake task to auto-generate corresponding Swagger files and expose them as YAML or JSON endpoints. Rswag also provides an embedded version of the awesome [swagger-ui](https://github.com/swagger-api/swagger-ui) that's powered by the exposed file. This toolchain makes it seamless to go from integration specs, which youre probably doing in some form already, to living documentation for your API consumers.
|
||||
|
||||
And that's not all ...
|
||||
|
||||
@@ -14,8 +15,8 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
|
||||
|
||||
|Rswag Version|Swagger (OpenAPI) Spec.|swagger-ui|
|
||||
|----------|----------|----------|
|
||||
|[master](https://github.com/rswag/rswag/tree/master)|2.0|3.17.3|
|
||||
|[2.0.5](https://github.com/rswag/rswag/tree/2.0.4)|2.0|3.17.3|
|
||||
|[master](https://github.com/rswag/rswag/tree/master)|2.0|3.18.2|
|
||||
|[2.2.0](https://github.com/rswag/rswag/tree/2.2.0)|2.0|3.18.2|
|
||||
|[1.6.0](https://github.com/rswag/rswag/tree/1.6.0)|2.0|2.2.5|
|
||||
|
||||
## Getting Started ##
|
||||
@@ -26,14 +27,15 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
|
||||
gem 'rswag'
|
||||
```
|
||||
|
||||
or if you like to avoid loading rspec in other bundler groups.
|
||||
or if you like to avoid loading rspec in other bundler groups load the rswag-specs component separately.
|
||||
Note: Adding it to the :development group is not strictly necessary, but without it, generators and rake tasks must be preceded by RAILS_ENV=test.
|
||||
|
||||
```ruby
|
||||
# Gemfile
|
||||
gem 'rswag-api'
|
||||
gem 'rswag-ui'
|
||||
|
||||
group :test do
|
||||
group :development, :test do
|
||||
gem 'rspec-rails'
|
||||
gem 'rswag-specs'
|
||||
end
|
||||
@@ -48,7 +50,8 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
|
||||
Or run the install generators for each package separately if you installed Rswag as separate gems, as indicated above:
|
||||
|
||||
```ruby
|
||||
rails g rswag:api:install rswag:ui:install
|
||||
rails g rswag:api:install
|
||||
rails g rswag:ui:install
|
||||
RAILS_ENV=test rails g rswag:specs:install
|
||||
```
|
||||
|
||||
@@ -120,12 +123,17 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
|
||||
end
|
||||
```
|
||||
|
||||
There is also a generator which can help get you started `rails generate rspec:swagger API::MyController`
|
||||
|
||||
|
||||
4. Generate the Swagger JSON file(s)
|
||||
|
||||
```ruby
|
||||
rake rswag:specs:swaggerize
|
||||
```
|
||||
|
||||
This common command is also aliased as `rake rswag`.
|
||||
|
||||
5. Spin up your app and check out the awesome, auto-generated docs at _/api-docs_!
|
||||
|
||||
## The rspec DSL ##
|
||||
@@ -185,7 +193,7 @@ describe 'Blogs API' do
|
||||
end
|
||||
end
|
||||
```
|
||||
*Note:* the OAI v3 may be released soon(ish?) and include a nullable property. This may have an effect on the need/use of custom extension to the draft. Do not use this property if you don't understand the implications.
|
||||
*Note:* OAI v3 has a nullable property. Rswag will work to support this soon. This may have an effect on the need/use of custom extension to the draft. Do not use this property if you don't understand the implications.
|
||||
<https://github.com/OAI/OpenAPI-Specification/issues/229#issuecomment-280376087>
|
||||
|
||||
### Global Metadata ###
|
||||
@@ -208,8 +216,8 @@ RSpec.configure do |config|
|
||||
basePath: '/api/v1'
|
||||
},
|
||||
|
||||
'v2/swagger.json' => {
|
||||
swagger: '2.0',
|
||||
'v2/swagger.yaml' => {
|
||||
openapi: '3.0.0',
|
||||
info: {
|
||||
title: 'API V2',
|
||||
version: 'v2',
|
||||
@@ -226,7 +234,7 @@ By default, the paths, operations and responses defined in your spec files will
|
||||
|
||||
```ruby
|
||||
# spec/integration/v2/blogs_spec.rb
|
||||
describe 'Blogs API', swagger_doc: 'v2/swagger.json' do
|
||||
describe 'Blogs API', swagger_doc: 'v2/swagger.yaml' do
|
||||
|
||||
path '/blogs' do
|
||||
...
|
||||
@@ -443,6 +451,17 @@ RSpec.configure do |config|
|
||||
config.swagger_dry_run = false
|
||||
end
|
||||
```
|
||||
|
||||
### Running tests without documenting ###
|
||||
|
||||
If you want to use Rswag for testing without adding it to you swagger docs, you can simply nullify the response metadata after the test run.
|
||||
|
||||
```ruby
|
||||
after do |example|
|
||||
example.metadata[:response] = null
|
||||
end
|
||||
```
|
||||
|
||||
### Route Prefix for Swagger JSON Endpoints ###
|
||||
|
||||
The functionality to expose Swagger files, such as those generated by rswag-specs, as JSON endpoints is implemented as a Rails Engine. As with any Engine, you can change it's mount prefix in _routes.rb_:
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
require 'json'
|
||||
require 'yaml'
|
||||
require 'rack/mime'
|
||||
|
||||
module Rswag
|
||||
module Api
|
||||
@@ -14,13 +16,15 @@ module Rswag
|
||||
filename = "#{@config.resolve_swagger_root(env)}/#{path}"
|
||||
|
||||
if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
|
||||
swagger = load_json(filename)
|
||||
swagger = parse_file(filename)
|
||||
@config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
|
||||
mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain')
|
||||
body = unload_swagger(filename, swagger)
|
||||
|
||||
return [
|
||||
'200',
|
||||
{ 'Content-Type' => 'application/json' },
|
||||
[ JSON.dump(swagger) ]
|
||||
{ 'Content-Type' => mime },
|
||||
[ body ]
|
||||
]
|
||||
end
|
||||
|
||||
@@ -29,9 +33,29 @@ module Rswag
|
||||
|
||||
private
|
||||
|
||||
def parse_file(filename)
|
||||
if /\.ya?ml$/ === filename
|
||||
load_yaml(filename)
|
||||
else
|
||||
load_json(filename)
|
||||
end
|
||||
end
|
||||
|
||||
def load_yaml(filename)
|
||||
YAML.safe_load(File.read(filename))
|
||||
end
|
||||
|
||||
def load_json(filename)
|
||||
JSON.parse(File.read(filename))
|
||||
end
|
||||
|
||||
def unload_swagger(filename, swagger)
|
||||
if /\.ya?ml$/ === filename
|
||||
YAML.dump(swagger)
|
||||
else
|
||||
JSON.dump(swagger)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
5
rswag-api/spec/rswag/api/fixtures/swagger/v1/swagger.yml
Normal file
5
rswag-api/spec/rswag/api/fixtures/swagger/v1/swagger.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
swagger: '2.0'
|
||||
info:
|
||||
title: API V1
|
||||
version: v1
|
||||
paths: {}
|
||||
@@ -76,6 +76,21 @@ module Rswag
|
||||
expect(response[2].join).to include('"host":"tempuri.org"')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when a path maps to a yaml swagger file' do
|
||||
let(:env) { env_defaults.merge('PATH_INFO' => 'v1/swagger.yml') }
|
||||
|
||||
it 'returns a 200 status' do
|
||||
expect(response.length).to eql(3)
|
||||
expect(response.first).to eql('200')
|
||||
end
|
||||
|
||||
it 'returns contents of the swagger file' do
|
||||
expect(response.length).to eql(3)
|
||||
expect(response[1]).to include( 'Content-Type' => 'text/yaml')
|
||||
expect(response[2].join).to include('title: API V1')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
9
rswag-specs/lib/generators/rspec/USAGE
Normal file
9
rswag-specs/lib/generators/rspec/USAGE
Normal file
@@ -0,0 +1,9 @@
|
||||
Description:
|
||||
This creates an RSpec request spec to define Swagger documentation for a
|
||||
controller. It will create a test for each of the controller's methods.
|
||||
|
||||
Example:
|
||||
rails generate rspec:swagger V3::AccountsController
|
||||
|
||||
This will create:
|
||||
spec/requests/v3/accounts_spec.rb
|
||||
22
rswag-specs/lib/generators/rspec/swagger_generator.rb
Normal file
22
rswag-specs/lib/generators/rspec/swagger_generator.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
require 'rswag/route_parser'
|
||||
require 'rails/generators'
|
||||
|
||||
module Rspec
|
||||
class SwaggerGenerator < ::Rails::Generators::NamedBase
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
def setup
|
||||
@routes = Rswag::RouteParser.new(controller_path).routes
|
||||
end
|
||||
|
||||
def create_spec_file
|
||||
template 'spec.rb', File.join('spec', 'requests', "#{controller_path}_spec.rb")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def controller_path
|
||||
file_path.chomp('_controller')
|
||||
end
|
||||
end
|
||||
end
|
||||
30
rswag-specs/lib/generators/rspec/templates/spec.rb
Normal file
30
rswag-specs/lib/generators/rspec/templates/spec.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
require 'swagger_helper'
|
||||
|
||||
RSpec.describe '<%= controller_path %>', type: :request do
|
||||
<% @routes.each do | template, path_item | %>
|
||||
path '<%= template %>' do
|
||||
<% unless path_item[:params].empty? -%>
|
||||
# You'll want to customize the parameter types...
|
||||
<% path_item[:params].each do |param| -%>
|
||||
parameter name: '<%= param %>', in: :path, type: :string, description: '<%= param %>'
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
<% path_item[:actions].each do | action, details | %>
|
||||
<%= action %>('<%= details[:summary] %>') do
|
||||
response(200, 'successful') do
|
||||
<% unless path_item[:params].empty? -%>
|
||||
<% path_item[:params].each do |param| -%>
|
||||
let(:<%= param %>) { '123' }
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
|
||||
after do |example|
|
||||
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
|
||||
end
|
||||
run_test!
|
||||
end
|
||||
end
|
||||
<% end -%>
|
||||
end
|
||||
<% end -%>
|
||||
end
|
||||
@@ -13,8 +13,8 @@ RSpec.configure do |config|
|
||||
# document below. You can override this behavior by adding a swagger_doc tag to the
|
||||
# the root example_group in your specs, e.g. describe '...', swagger_doc: 'v2/swagger.json'
|
||||
config.swagger_docs = {
|
||||
'v1/swagger.json' => {
|
||||
swagger: '2.0',
|
||||
'v1/swagger.yaml' => {
|
||||
openapi: '3.0.1',
|
||||
info: {
|
||||
title: 'API V1',
|
||||
version: 'v1'
|
||||
@@ -22,4 +22,10 @@ RSpec.configure do |config|
|
||||
paths: {}
|
||||
}
|
||||
}
|
||||
|
||||
# Specify the format of the output Swagger file when running 'rswag:specs:swaggerize'.
|
||||
# The swagger_docs configuration option has the filename including format in
|
||||
# the key, this may want to be changed to avoid putting yaml in json files.
|
||||
# Defaults to json. Accepts ':json' and ':yaml'.
|
||||
config.swagger_format = :yaml
|
||||
end
|
||||
|
||||
58
rswag-specs/lib/rswag/route_parser.rb
Normal file
58
rswag-specs/lib/rswag/route_parser.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
module Rswag
|
||||
class RouteParser
|
||||
attr_reader :controller
|
||||
|
||||
def initialize(controller)
|
||||
@controller = controller
|
||||
end
|
||||
|
||||
def routes
|
||||
::Rails.application.routes.routes.select do |route|
|
||||
route.defaults[:controller] == controller
|
||||
end.reduce({}) do |tree, route|
|
||||
path = path_from(route)
|
||||
verb = verb_from(route)
|
||||
tree[path] ||= { params: params_from(route), actions: {} }
|
||||
tree[path][:actions][verb] = { summary: summary_from(route) }
|
||||
tree
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def path_from(route)
|
||||
route.path.spec.to_s
|
||||
.chomp('(.:format)') # Ignore any format suffix
|
||||
.gsub(/:([^\/.?]+)/, '{\1}') # Convert :id to {id}
|
||||
end
|
||||
|
||||
def verb_from(route)
|
||||
verb = route.verb
|
||||
if verb.kind_of? String
|
||||
verb.downcase
|
||||
else
|
||||
verb.source.gsub(/[$^]/, '').downcase
|
||||
end
|
||||
end
|
||||
|
||||
def summary_from(route)
|
||||
verb = route.requirements[:action]
|
||||
noun = route.requirements[:controller].split('/').last.singularize
|
||||
|
||||
# Apply a few customizations to make things more readable
|
||||
case verb
|
||||
when 'index'
|
||||
verb = 'list'
|
||||
noun = noun.pluralize
|
||||
when 'destroy'
|
||||
verb = 'delete'
|
||||
end
|
||||
|
||||
"#{verb} #{noun}"
|
||||
end
|
||||
|
||||
def params_from(route)
|
||||
route.segments - ['format']
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -12,6 +12,7 @@ module Rswag
|
||||
c.add_setting :swagger_root
|
||||
c.add_setting :swagger_docs
|
||||
c.add_setting :swagger_dry_run
|
||||
c.add_setting :swagger_format
|
||||
c.extend ExampleGroupHelpers, type: :request
|
||||
c.include ExampleHelpers, type: :request
|
||||
end
|
||||
|
||||
@@ -31,6 +31,14 @@ module Rswag
|
||||
end
|
||||
end
|
||||
|
||||
def swagger_format
|
||||
@swagger_format ||= begin
|
||||
@rspec_config.swagger_format = :json if @rspec_config.swagger_format.nil? || @rspec_config.swagger_format.empty?
|
||||
raise ConfigurationError, "Unknown swagger_format '#{@rspec_config.swagger_format}'" unless [:json, :yaml].include?(@rspec_config.swagger_format)
|
||||
@rspec_config.swagger_format
|
||||
end
|
||||
end
|
||||
|
||||
def get_swagger_doc(name)
|
||||
return swagger_docs.values.first if name.nil?
|
||||
raise ConfigurationError, "Unknown swagger_doc '#{name}'" unless swagger_docs[name]
|
||||
|
||||
@@ -7,7 +7,7 @@ module Rswag
|
||||
describe(template, metadata, &block)
|
||||
end
|
||||
|
||||
[ :get, :post, :patch, :put, :delete, :head ].each do |verb|
|
||||
[ :get, :post, :patch, :put, :delete, :head, :options, :trace ].each do |verb|
|
||||
define_method(verb) do |summary, &block|
|
||||
api_metadata = { operation: { verb: verb, summary: summary } }
|
||||
describe(verb, api_metadata, &block)
|
||||
|
||||
@@ -5,6 +5,10 @@ module Rswag
|
||||
rake_tasks do
|
||||
load File.expand_path('../../../tasks/rswag-specs_tasks.rake', __FILE__)
|
||||
end
|
||||
|
||||
generators do
|
||||
require 'generators/rspec/swagger_generator.rb'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,7 +37,7 @@ module Rswag
|
||||
FileUtils.mkdir_p dirname unless File.exists?(dirname)
|
||||
|
||||
File.open(file_path, 'w') do |file|
|
||||
file.write(JSON.pretty_generate(doc))
|
||||
file.write(pretty_generate(doc))
|
||||
end
|
||||
|
||||
@output.puts "Swagger doc generated at #{file_path}"
|
||||
@@ -46,6 +46,20 @@ module Rswag
|
||||
|
||||
private
|
||||
|
||||
def pretty_generate(doc)
|
||||
if @config.swagger_format == :yaml
|
||||
clean_doc = yaml_prepare(doc)
|
||||
YAML.dump(clean_doc)
|
||||
else # config errors are thrown in 'def swagger_format', no throw needed here
|
||||
JSON.pretty_generate(doc)
|
||||
end
|
||||
end
|
||||
|
||||
def yaml_prepare(doc)
|
||||
json_doc = JSON.pretty_generate(doc)
|
||||
clean_doc = JSON.parse(json_doc)
|
||||
end
|
||||
|
||||
def metadata_to_swagger(metadata)
|
||||
response_code = metadata[:response][:code]
|
||||
response = metadata[:response].reject { |k,v| k == :code }
|
||||
|
||||
@@ -16,3 +16,6 @@ namespace :rswag do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
task :rswag => ['rswag:specs:swaggerize']
|
||||
|
||||
|
||||
44
rswag-specs/spec/generators/rspec/swagger_generator_spec.rb
Normal file
44
rswag-specs/spec/generators/rspec/swagger_generator_spec.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require 'generator_spec'
|
||||
require 'generators/rspec/swagger_generator'
|
||||
require 'tmpdir'
|
||||
|
||||
module Rspec
|
||||
describe SwaggerGenerator do
|
||||
include GeneratorSpec::TestCase
|
||||
destination Dir.mktmpdir
|
||||
|
||||
before(:all) do
|
||||
prepare_destination
|
||||
fixtures_dir = File.expand_path('../fixtures', __FILE__)
|
||||
FileUtils.cp_r("#{fixtures_dir}/spec", destination_root)
|
||||
end
|
||||
|
||||
after(:all) do
|
||||
|
||||
end
|
||||
|
||||
it 'installs the swagger_helper for rspec' do
|
||||
allow_any_instance_of(Rswag::RouteParser).to receive(:routes).and_return(fake_routes)
|
||||
run_generator ['Posts::CommentsController']
|
||||
|
||||
assert_file('spec/requests/posts/comments_spec.rb') do |content|
|
||||
assert_match(/parameter name: 'post_id', in: :path, type: :string/, content)
|
||||
assert_match(/patch\('update_comments comment'\)/, content)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fake_routes
|
||||
{
|
||||
"/posts/{post_id}/comments/{id}" => {
|
||||
:params => ["post_id", "id"],
|
||||
:actions => {
|
||||
"get" => { :summary=>"show comment" },
|
||||
"patch" => { :summary=>"update_comments comment" }
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -4,7 +4,7 @@ require 'generators/rswag/specs/install/install_generator'
|
||||
module Rswag
|
||||
module Specs
|
||||
|
||||
describe InstallGenerator do
|
||||
RSpec.describe InstallGenerator do
|
||||
include GeneratorSpec::TestCase
|
||||
destination File.expand_path('../tmp', __FILE__)
|
||||
|
||||
|
||||
@@ -3,10 +3,12 @@ require 'rswag/specs/configuration'
|
||||
module Rswag
|
||||
module Specs
|
||||
|
||||
describe Configuration do
|
||||
RSpec.describe Configuration do
|
||||
subject { described_class.new(rspec_config) }
|
||||
|
||||
let(:rspec_config) { OpenStruct.new(swagger_root: swagger_root, swagger_docs: swagger_docs) }
|
||||
let(:rspec_config) do
|
||||
OpenStruct.new(swagger_root: swagger_root, swagger_docs: swagger_docs, swagger_format: swagger_format)
|
||||
end
|
||||
let(:swagger_root) { 'foobar' }
|
||||
let(:swagger_docs) do
|
||||
{
|
||||
@@ -14,6 +16,7 @@ module Rswag
|
||||
'v2/swagger.json' => { info: { title: 'v2' } }
|
||||
}
|
||||
end
|
||||
let(:swagger_format) { :yaml }
|
||||
|
||||
describe '#swagger_root' do
|
||||
let(:response) { subject.swagger_root }
|
||||
@@ -46,6 +49,26 @@ module Rswag
|
||||
end
|
||||
end
|
||||
|
||||
describe '#swagger_format' do
|
||||
let(:response) { subject.swagger_format }
|
||||
|
||||
context 'provided in rspec config' do
|
||||
it { expect(response).to be_an_instance_of(Symbol) }
|
||||
end
|
||||
|
||||
context 'unsupported format provided' do
|
||||
let(:swagger_format) { :xml }
|
||||
|
||||
it { expect { response }.to raise_error ConfigurationError }
|
||||
end
|
||||
|
||||
context 'not provided' do
|
||||
let(:swagger_format) { nil }
|
||||
|
||||
it { expect(response).to eq(:json) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#get_swagger_doc(tag=nil)' do
|
||||
let(:swagger_doc) { subject.get_swagger_doc(tag) }
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ require 'rswag/specs/example_group_helpers'
|
||||
module Rswag
|
||||
module Specs
|
||||
|
||||
describe ExampleGroupHelpers do
|
||||
RSpec.describe ExampleGroupHelpers do
|
||||
subject { double('example_group') }
|
||||
|
||||
before do
|
||||
@@ -24,7 +24,7 @@ module Rswag
|
||||
end
|
||||
end
|
||||
|
||||
describe '#get|post|patch|put|delete|head(verb, summary)' do
|
||||
describe '#get|post|patch|put|delete|head|options|trace(verb, summary)' do
|
||||
before { subject.post('Creates a blog') }
|
||||
|
||||
it "delegates to 'describe' with 'operation' metadata" do
|
||||
|
||||
@@ -3,7 +3,7 @@ require 'rswag/specs/example_helpers'
|
||||
module Rswag
|
||||
module Specs
|
||||
|
||||
describe ExampleHelpers do
|
||||
RSpec.describe ExampleHelpers do
|
||||
subject { double('example') }
|
||||
|
||||
before do
|
||||
|
||||
@@ -3,7 +3,7 @@ require 'rswag/specs/request_factory'
|
||||
module Rswag
|
||||
module Specs
|
||||
|
||||
describe RequestFactory do
|
||||
RSpec.describe RequestFactory do
|
||||
subject { RequestFactory.new(config) }
|
||||
|
||||
before do
|
||||
|
||||
@@ -3,7 +3,7 @@ require 'rswag/specs/response_validator'
|
||||
module Rswag
|
||||
module Specs
|
||||
|
||||
describe ResponseValidator do
|
||||
RSpec.describe ResponseValidator do
|
||||
subject { ResponseValidator.new(config) }
|
||||
|
||||
before do
|
||||
|
||||
@@ -4,7 +4,7 @@ require 'ostruct'
|
||||
module Rswag
|
||||
module Specs
|
||||
|
||||
describe SwaggerFormatter do
|
||||
RSpec.describe SwaggerFormatter do
|
||||
subject { described_class.new(output, config) }
|
||||
|
||||
# Mock out some infrastructure
|
||||
@@ -53,14 +53,30 @@ module Rswag
|
||||
'v1/swagger.json' => { info: { version: 'v1' } },
|
||||
'v2/swagger.json' => { info: { version: 'v2' } }
|
||||
)
|
||||
allow(config).to receive(:swagger_format).and_return(swagger_format)
|
||||
subject.stop(notification)
|
||||
end
|
||||
|
||||
let(:notification) { double('notification') }
|
||||
context 'with default format' do
|
||||
let(:swagger_format) { :json }
|
||||
|
||||
it 'writes the swagger_doc(s) to file' do
|
||||
expect(File).to exist("#{swagger_root}/v1/swagger.json")
|
||||
expect(File).to exist("#{swagger_root}/v2/swagger.json")
|
||||
it 'writes the swagger_doc(s) to file' do
|
||||
expect(File).to exist("#{swagger_root}/v1/swagger.json")
|
||||
expect(File).to exist("#{swagger_root}/v2/swagger.json")
|
||||
expect { JSON.parse(File.read("#{swagger_root}/v2/swagger.json")) }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context 'with yaml format' do
|
||||
let(:swagger_format) { :yaml }
|
||||
|
||||
it 'writes the swagger_doc(s) as yaml' do
|
||||
expect(File).to exist("#{swagger_root}/v1/swagger.json")
|
||||
expect { JSON.parse(File.read("#{swagger_root}/v1/swagger.json")) }.to raise_error(JSON::ParserError)
|
||||
# Psych::DisallowedClass would be raised if we do not pre-process ruby symbols
|
||||
expect { YAML.safe_load(File.read("#{swagger_root}/v1/swagger.json")) }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
after do
|
||||
|
||||
@@ -2,9 +2,9 @@ Rswag::Ui.configure do |c|
|
||||
|
||||
# List the Swagger endpoints that you want to be documented through the swagger-ui
|
||||
# The first parameter is the path (absolute or relative to the UI host) to the corresponding
|
||||
# JSON endpoint and the second is a title that will be displayed in the document selector
|
||||
# NOTE: If you're using rspec-api to expose Swagger files (under swagger_root) as JSON endpoints,
|
||||
# endpoint and the second is a title that will be displayed in the document selector
|
||||
# NOTE: If you're using rspec-api to expose Swagger files (under swagger_root) as JSON or YAML endpoints,
|
||||
# then the list below should correspond to the relative paths for those endpoints
|
||||
|
||||
c.swagger_endpoint '/api-docs/v1/swagger.json', 'API V1 Docs'
|
||||
c.swagger_endpoint '/api-docs/v1/swagger.yaml', 'API V1 Docs'
|
||||
end
|
||||
|
||||
6
rswag-ui/package-lock.json
generated
6
rswag-ui/package-lock.json
generated
@@ -5,9 +5,9 @@
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"swagger-ui-dist": {
|
||||
"version": "3.17.3",
|
||||
"resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.17.3.tgz",
|
||||
"integrity": "sha1-37lkCMzEZ3UVX3NpGQxdSyAW/lw="
|
||||
"version": "3.18.2",
|
||||
"resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.18.2.tgz",
|
||||
"integrity": "sha512-pWAEiKkgWUJvjmLW9AojudnutJ+NTn5g6OdNLj1iIJWwCkoy40K3Upwa24DqFbmIE4vLX4XplND61hp2L+s5vg=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"swagger-ui-dist": "3.17.3"
|
||||
"swagger-ui-dist": "3.18.2"
|
||||
}
|
||||
}
|
||||
|
||||
2
test-app/app/assets/config/manifest.js
Normal file
2
test-app/app/assets/config/manifest.js
Normal file
@@ -0,0 +1,2 @@
|
||||
//= link_tree ../images
|
||||
//= link_directory ../stylesheets .css
|
||||
0
test-app/app/assets/images/.keep
Normal file
0
test-app/app/assets/images/.keep
Normal file
@@ -2,15 +2,15 @@
|
||||
# of editing this file, please use the migrations feature of Active Record to
|
||||
# incrementally modify your database, and then regenerate this schema definition.
|
||||
#
|
||||
# This file is the source Rails uses to define your schema when running `rails
|
||||
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
|
||||
# be faster and is potentially less error prone than running all of your
|
||||
# migrations from scratch. Old migrations may fail to apply correctly if those
|
||||
# migrations use external dependencies or application code.
|
||||
# Note that this schema.rb definition is the authoritative source for your
|
||||
# database schema. If you need to create the application database on another
|
||||
# system, you should be using db:schema:load, not running all the migrations
|
||||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
||||
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2016_02_18_212104) do
|
||||
ActiveRecord::Schema.define(version: 20160218212104) do
|
||||
|
||||
create_table "blogs", force: :cascade do |t|
|
||||
t.string "title"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require 'rails_helper'
|
||||
|
||||
feature 'swagger-ui', js: true do
|
||||
RSpec.feature 'swagger-ui', js: true do
|
||||
|
||||
scenario 'browsing api-docs' do
|
||||
visit '/api-docs'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require 'swagger_helper'
|
||||
|
||||
describe 'Auth Tests API', type: :request, swagger_doc: 'v1/swagger.json' do
|
||||
RSpec.describe 'Auth Tests API', type: :request, swagger_doc: 'v1/swagger.json' do
|
||||
|
||||
path '/auth-tests/basic' do
|
||||
post 'Authenticates with basic auth' do
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require 'swagger_helper'
|
||||
|
||||
describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
||||
RSpec.describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
||||
let(:api_key) { 'fake_key' }
|
||||
|
||||
path '/blogs' do
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'spec_helper'
|
||||
require 'rake'
|
||||
|
||||
describe 'rswag:specs:swaggerize' do
|
||||
RSpec.describe 'rswag:specs:swaggerize' do
|
||||
let(:swagger_root) { Rails.root.to_s + '/swagger' }
|
||||
before do
|
||||
TestApp::Application.load_tasks
|
||||
|
||||
Reference in New Issue
Block a user