项目作者: drewish

项目描述 :
Generate Swagger 2.0 docs for Rails apps using RSpec request specs. Test results can be captured as response examples.
高级语言: Ruby
项目地址: git://github.com/drewish/rspec-rails-swagger.git
创建时间: 2016-09-14T14:37:49Z
项目社区:https://github.com/drewish/rspec-rails-swagger

开源协议:MIT License

下载


RSpec Rails Swagger

Build Status
Code Climate

This gem helps you generate Swagger docs by using RSpec to document the paths.
You execute a command to run the tests and generate the .yaml or .json output.
Running the tests ensures that your API and docs are in agreement, and generates
output that can be saved as response examples.

The design of this was heavily influenced by the awesome swagger_rails gem.

Setup

Add the gem to your Rails app’s Gemfile:

  1. group :development, :test do
  2. gem 'rspec-rails-swagger'
  3. end

Update your bundle:

  1. bundle install

If you don’t have a spec/rails_helper.rb file:

  1. rails generate rspec:install

Create the spec/swagger_helper.rb file:

  1. rails generate rspec:swagger_install

Documenting Your API

Now you can edit spec/swagger_helper.rb and start filling in the top level
Swagger documentation, e.g. basePath, definitions,
parameters,
tags, etc.

You can use the generator to create a spec to documentation a controller:

  1. rails generate rspec:swagger PostsController

That will create a spec/requests/posts_spec.rb file with the paths, operations
and some default requests filled in. With the structure in place you should only
need to add before calls to create records and then update the lets to
return the appropriate values.

Generate the JSON or YAML

To create the Swagger files use the rake task:

  1. bundle exec rake swagger

Now you can use Swagger UI or the renderer of your choice to display the
formatted documentation. swagger_engine
works pretty well and supports multiple documents.

RSpec DSL

The DSL follows the hierarchy of the Swagger Schema:

Here’s an example of a spec with comments to for the corresponding objects:

  1. require 'swagger_helper'
  2. # Paths Object
  3. RSpec.describe "Posts Controller", type: :request do
  4. before { Post.new.save }
  5. # Path Item Object
  6. path '/posts' do
  7. # Operation Object
  8. operation "GET", summary: "fetch list" do
  9. # Response Object
  10. response 200, description: "successful"
  11. end
  12. end
  13. # Path Object
  14. path '/posts/{post_id}' do
  15. # Parameter Object
  16. parameter "post_id", { in: :path, type: :integer }
  17. let(:post_id) { 1 }
  18. # Operation Object
  19. get summary: "fetch item" do
  20. # Response Object
  21. response 200, description: "success"
  22. end
  23. end
  24. # Path Post Object
  25. path '/posts/' do
  26. # Parameter Object for content type could be defined like:
  27. consumes 'application/json'
  28. # or:
  29. parameter 'Content-Type', { in: :header, type: :string }
  30. let(:'Content-Type') { 'application/json' }
  31. # one of them would be considered
  32. # authorization token in the header:
  33. parameter 'Authorization', { in: :header, type: :string }
  34. let(:'Authorization') { 'Bearer <token-here>' }
  35. # Parameter Object
  36. parameter "post_id", { in: :path, type: :integer }
  37. let(:post_id) { 1 }
  38. # Parameter Object for Body
  39. parameter "body", { in: :body, required: true, schema: {
  40. type: :object,
  41. properties: {
  42. title: { type: :string },
  43. author_email: { type: :email }
  44. }
  45. }
  46. let (:body) {
  47. { post:
  48. { title: 'my example',
  49. author_email: 'me@example.com' }
  50. }
  51. }
  52. }
  53. # checkout http://swagger.io/specification/#parameter-object-44 for more information, options and details
  54. # Operation Object
  55. post summary: "update an item" do
  56. # Response Object
  57. response 200, description: "success"
  58. end
  59. # ...
  60. end

Paths Object

These methods are available inside of an RSpec contexts with the type: :request tag.

path(template, attributes = {}, &block)

Defines a new Path Item.

The attributes parameter accepts:

  • swagger_doc a key in RSpec.configuration.swagger_docs that determines
    which file the path belongs in.
  • tags with an array of tags that will be applied to the child Operations.

You can also provide a default file and tags by setting them on a parent RSpec
context block:

  1. RSpec.describe "Sample Requests", type: :request do
  2. # The swagger_doc will be used as a default for the paths defined within the
  3. # context block. Similarly, the tags will be merged with those set on paths
  4. # defined within them.
  5. context "setting defaults", swagger_doc: 'default_document.json', tags: [:context_tag] do
  6. path '/posts', swagger_doc: 'overridden_document.yaml' tags: ['path_tag'] do
  7. operation "GET", summary: "fetch list" do
  8. produces 'application/json'
  9. tags 'operation_tag'
  10. response(200, { description: "successful" })
  11. end
  12. end
  13. end
  14. end

The GET /posts operation in this example will be saved in the 'overridden_document.yaml'
file and tagged with ["context_tag", "path_tag", "operation_tag"].

Path Item Object

These methods are available inside of blocks passed to the path method.

operation(method, attributes = {}, &block)

Defines a new Operation Object. The method is case insensitive.

The attributes parameter accepts:

  • tags with an array of tags. These will be merged with tags passed to the
    Path Item or tags method inside the Operation’s block.

delete(attributes = {}, &block)

Alias for operation(:delete, attributes, block).

get(attributes = {}, &block)

Alias for operation(:get, attributes, block).

head(attributes = {}, &block)

Alias for operation(:head, attributes, block).

options(attributes = {}, &block)

Alias for operation(:options, attributes, block).

patch(attributes = {}, &block)

Alias for operation(:patch, attributes, block).

post(attributes = {}, &block)

Alias for operation(:post, attributes, block).

put(attributes = {}, &block)

Alias for operation(:put, attributes, block).

Parameters

These methods are available inside of blocks passed to the path or operation method.

parameter(name, attributes = {})

Defines a new Parameter Object. You can define the parameter inline:

  1. parameter :callback_url, in: :query, type: :string, required: true

Or, via reference:

  1. parameter ref: "#/parameters/site_id"

Values for the parameters are set using let:

  1. post summary: "create" do
  2. parameter "body", in: :body, schema: { foo: :bar }
  3. let(:body) { { post: { title: 'asdf', body: "blah" } } }
  4. # ...
  5. end

Operation Object

These methods are available inside of blocks passed to the operation method.

consumes(*mime_types)

Use this to add MIME types that are specific to the operation. They will be merged
with the Swagger Object’s consumes field.

  1. consumes 'application/json', 'application/xml'

produces(*mime_types)

Use this to add MIME types that are specific to the operation. They will be merged
with the Swagger Object’s consumes field.

  1. produces 'application/json', 'application/xml'

response(status_code, attributes = {}, &block)

Defines a new Response Object. status_code must be between 1 and 599. attributes
must include a description.

tags(*tags)

Adds operation specific tags.

  1. tags :accounts, :pets

You can also provide tags through the RSpec context block and/or path method:

  1. RSpec.describe "Sample Requests", type: :request, tags: [:context_tag] do
  2. path '/posts', tags: ['path_tag'] do
  3. operation "GET", summary: "fetch list" do
  4. produces 'application/json'
  5. tags 'operation_tag'
  6. response(200, { description: "successful" })
  7. end
  8. end
  9. end

These tags will be merged with those of the operation. The GET /posts operation
in this example will be tagged with ["context_tag", "path_tag", "operation_tag"].

Response Object

These methods are available inside of blocks passed to the response method.

capture_example()

This method will capture the response body from the test and create an Example
Object for the Response.

You could also set this in an RSpec context block if you’d like examples for
multiple operations or paths:

  1. describe 'Connections', type: :request, capture_examples: true do
  2. # Any requests in this block will capture example responses
  3. end

schema(definition)

Sets the schema field for the Response Object. You can define it inline:

  1. schema(
  2. type: :array,
  3. items: {
  4. type: :object,
  5. properties: {
  6. id: { type: :string },
  7. name: { type: :string },
  8. },
  9. }
  10. )

Or, by reference:

  1. schema ref: '#/definitions/Account'