Skip to content

Commit aba8588

Browse files
authored
Merge pull request #45 from drewish/overide-doc
Standardize on swagger_doc naming
2 parents 672cdd4 + fd643be commit aba8588

File tree

9 files changed

+133
-67
lines changed

9 files changed

+133
-67
lines changed

README.md

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ rails generate rspec:swagger_install
3737
## Documenting Your API
3838

3939
Now you can edit `spec/swagger_helper.rb` and start filling in the top level
40-
Swagger documention, e.g. basePath, [definitions](http://swagger.io/specification/#definitionsObject),
40+
Swagger documentation, e.g. basePath, [definitions](http://swagger.io/specification/#definitionsObject),
4141
[parameters](http://swagger.io/specification/#parametersDefinitionsObject),
4242
[tags](http://swagger.io/specification/#tagObject), etc.
4343

@@ -66,7 +66,7 @@ works pretty well and supports multiple documents.
6666

6767
## RSpec DSL
6868

69-
The DSL follows the hierachy of the Swagger Schema:
69+
The DSL follows the hierarchy of the Swagger Schema:
7070

7171
- [Paths Object](http://swagger.io/specification/#paths-object-29)
7272
- [Path Item Object](http://swagger.io/specification/#path-item-object-32)
@@ -98,7 +98,7 @@ RSpec.describe "Posts Controller", type: :request do
9898
# Path Object
9999
path '/posts/{post_id}' do
100100
# Parameter Object
101-
parameter "post_id", {in: :path, type: :integer}
101+
parameter "post_id", { in: :path, type: :integer }
102102
let(:post_id) { 1 }
103103

104104
# Operation Object
@@ -113,30 +113,30 @@ RSpec.describe "Posts Controller", type: :request do
113113
# Parameter Object for content type could be defined like:
114114
consumes 'application/json'
115115
# or:
116-
parameter 'Content-Type', {in: :header, type: :string}
116+
parameter 'Content-Type', { in: :header, type: :string }
117117
let(:'Content-Type') { 'application/json' }
118118
# one of them would be considered
119119

120120
# authorization token in the header:
121-
parameter 'Authorization', {in: :header, type: :string}
121+
parameter 'Authorization', { in: :header, type: :string }
122122
let(:'Authorization') { 'Bearer <token-here>' }
123123

124124
# Parameter Object
125-
parameter "post_id", {in: :path, type: :integer}
125+
parameter "post_id", { in: :path, type: :integer }
126126
let(:post_id) { 1 }
127127

128128
# Parameter Object for Body
129-
parameter "body", {in: :body, required: true, schema: {
129+
parameter "body", { in: :body, required: true, schema: {
130130
type: :object,
131131
properties: {
132132
title: { type: :string },
133133
author_email: { type: :email }
134134
}
135135
}
136136
let (:body) {
137-
{ post:
138-
{ title: 'my example',
139-
author_email: '[email protected]' }
137+
{ post:
138+
{ title: 'my example',
139+
author_email: '[email protected]' }
140140
}
141141
}
142142
}
@@ -158,12 +158,44 @@ These methods are available inside of an RSpec contexts with the `type: :request
158158
#### `path(template, attributes = {}, &block)`
159159
Defines a new Path Item.
160160

161+
The `attributes` parameter accepts:
162+
- `swagger_doc` a key in `RSpec.configuration.swagger_docs` that determines
163+
which file the path belongs in.
164+
- `tags` with an array of tags that will be applied to the child Operations.
165+
166+
You can also provide a default file and tags by setting them on a parent RSpec
167+
context block:
168+
```rb
169+
RSpec.describe "Sample Requests", type: :request do
170+
# The swagger_doc will be used as a default for the paths defined within the
171+
# context block. Similarly, the tags will be merged with those set on paths
172+
# defined within them.
173+
context "setting defaults", swagger_doc: 'default_document.json', tags: [:context_tag] do
174+
path '/posts', swagger_doc: 'overridden_document.yaml' tags: ['path_tag'] do
175+
operation "GET", summary: "fetch list" do
176+
produces 'application/json'
177+
tags 'operation_tag'
178+
179+
response(200, { description: "successful" })
180+
end
181+
end
182+
end
183+
end
184+
```
185+
The `GET /posts` operation in this example will be saved in the `'overridden_document.yaml'`
186+
file and tagged with `["context_tag", "path_tag", "operation_tag"]`.
187+
188+
161189
### Path Item Object
162190
These methods are available inside of blocks passed to the `path` method.
163191

164192
#### `operation(method, attributes = {}, &block)`
165193
Defines a new Operation Object. The `method` is case insensitive.
166194

195+
The `attributes` parameter accepts:
196+
- `tags` with an array of tags. These will be merged with tags passed to the
197+
Path Item or `tags` method inside the Operation's block.
198+
167199
#### `delete(attributes = {}, &block)`
168200
Alias for `operation(:delete, attributes, block)`.
169201

@@ -203,7 +235,7 @@ parameter ref: "#/parameters/site_id"
203235
Values for the parameters are set using `let`:
204236
```rb
205237
post summary: "create" do
206-
parameter "body", in: :body, schema: { foo: :bar}
238+
parameter "body", in: :body, schema: { foo: :bar }
207239
let(:body) { { post: { title: 'asdf', body: "blah" } } }
208240
# ...
209241
end
@@ -245,7 +277,7 @@ RSpec.describe "Sample Requests", type: :request, tags: [:context_tag] do
245277
produces 'application/json'
246278
tags 'operation_tag'
247279

248-
response(200, {description: "successful"})
280+
response(200, { description: "successful" })
249281
end
250282
end
251283
end

lib/rspec/rails/swagger/formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def example_finished(notification)
3737
return unless metadata[:swagger_object] == :response
3838

3939
# Then add everything to the document
40-
document = document_for(metadata[:swagger_document])
40+
document = document_for(metadata[:swagger_doc])
4141
path_item = path_item_for(document, metadata[:swagger_path_item])
4242
operation = operation_for(path_item, metadata[:swagger_operation])
4343
response = response_for(operation, metadata[:swagger_response])

lib/rspec/rails/swagger/helpers.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def path template, attributes = {}, &block
4545
#TODO template might be a $ref
4646
meta = {
4747
swagger_object: :path_item,
48-
swagger_document: attributes[:swagger_document] || RSpec.configuration.swagger_docs.keys.first,
48+
swagger_doc: attributes[:swagger_doc] || default_document,
4949
swagger_path_item: {path: template},
5050
}
5151
# Merge tags passed into the path with those from parent contexts.
@@ -54,6 +54,12 @@ def path template, attributes = {}, &block
5454
end
5555
describe(template, meta, &block)
5656
end
57+
58+
private
59+
60+
def default_document
61+
metadata.try(:[], :swagger_doc) || RSpec.configuration.swagger_docs.keys.first
62+
end
5763
end
5864

5965
module PathItem
@@ -122,7 +128,7 @@ def parameter name, attributes = {}
122128
def resolve_document metadata
123129
# TODO: It's really inefficient to keep recreating this. It'd be nice
124130
# if we could cache them some place.
125-
name = metadata[:swagger_document]
131+
name = metadata[:swagger_doc]
126132
Document.new(RSpec.configuration.swagger_docs[name])
127133
end
128134

lib/rspec/rails/swagger/request_builder.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def initialize(metadata, instance)
1717
# and parameter references can be resolved.
1818
def document
1919
@document ||= begin
20-
name = metadata[:swagger_document]
21-
Document.new(RSpec.configuration.swagger_docs[name])
20+
Document.new(RSpec.configuration.swagger_docs[metadata[:swagger_doc]])
2221
end
2322
end
2423

lib/rspec/rails/swagger/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Rails
33
# Version information for RSpec Swagger.
44
module Swagger
55
module Version
6-
STRING = '0.1.5'
6+
STRING = '0.2.0'
77
end
88
end
99
end

spec/rspec/rails/swagger/formatter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def minimal
5151
let(:metadata) do
5252
{
5353
swagger_object: :response,
54-
swagger_document: 'doc2.json',
54+
swagger_doc: 'doc2.json',
5555
swagger_path_item: {path: "/ping"},
5656
swagger_operation: {method: :put},
5757
swagger_response: {status_code: 200, description: "OK"},
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
require 'swagger_helper'
2+
3+
RSpec.describe RSpec::Rails::Swagger::Helpers::Paths do
4+
let(:klass) do
5+
Class.new do
6+
include RSpec::Rails::Swagger::Helpers::Paths
7+
attr_accessor :metadata
8+
def describe *args ; end
9+
end
10+
end
11+
subject { klass.new }
12+
13+
it "requires the path start with a /" do
14+
expect{ subject.path("foo") }.to raise_exception(ArgumentError)
15+
expect{ subject.path("/foo") }.not_to raise_exception
16+
end
17+
18+
describe 'swagger_doc' do
19+
context 'with value specified in parent context' do
20+
before { subject.metadata = {swagger_doc: 'default.json'} }
21+
22+
it "defaults to the parent value" do
23+
expect(subject).to receive(:describe).with("/ping", {
24+
swagger_object: :path_item,
25+
swagger_doc: 'default.json',
26+
swagger_path_item: {path: '/ping'}
27+
})
28+
29+
subject.path('/ping')
30+
end
31+
32+
it "uses the argument when provided" do
33+
expect(subject).to receive(:describe).with("/ping", {
34+
swagger_object: :path_item,
35+
swagger_doc: 'overridden.json',
36+
swagger_path_item: {path: '/ping'}
37+
})
38+
39+
subject.path('/ping', swagger_doc: 'overridden.json')
40+
end
41+
end
42+
43+
context 'without a parent swagger_doc' do
44+
it "defaults to the first swagger document" do
45+
expect(subject).to receive(:describe).with("/ping", {
46+
swagger_object: :path_item,
47+
swagger_doc: RSpec.configuration.swagger_docs.keys.first,
48+
swagger_path_item: {path: '/ping'}
49+
})
50+
51+
subject.path('/ping')
52+
end
53+
54+
it "uses the argument when provided" do
55+
expect(subject).to receive(:describe).with("/ping", {
56+
swagger_object: :path_item,
57+
swagger_doc: 'overridden.json',
58+
swagger_path_item: {path: '/ping'}
59+
})
60+
61+
subject.path('/ping', swagger_doc: 'overridden.json')
62+
end
63+
end
64+
end
65+
66+
it "passes tags through to the metadata" do
67+
expect(subject).to receive(:describe).with("/ping", {
68+
swagger_object: :path_item,
69+
swagger_doc: RSpec.configuration.swagger_docs.keys.first,
70+
swagger_path_item: {path: '/ping'},
71+
tags: ['tag1']
72+
})
73+
74+
subject.path('/ping', tags: ['tag1'])
75+
end
76+
end

spec/rspec/rails/swagger/helpers_spec.rb

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,5 @@
11
require 'swagger_helper'
22

3-
RSpec.describe RSpec::Rails::Swagger::Helpers::Paths do
4-
let(:klass) do
5-
Class.new do
6-
include RSpec::Rails::Swagger::Helpers::Paths
7-
attr_accessor :metadata
8-
def describe *args ; end
9-
end
10-
end
11-
subject { klass.new }
12-
13-
it "requires the path start with a /" do
14-
expect{ subject.path("foo") }.to raise_exception(ArgumentError)
15-
expect{ subject.path("/foo") }.not_to raise_exception
16-
end
17-
18-
it "defaults to the first swagger document if not specified" do
19-
expect(subject).to receive(:describe).with("/ping", {
20-
swagger_object: :path_item,
21-
swagger_document: RSpec.configuration.swagger_docs.keys.first,
22-
swagger_path_item: {path: '/ping'}
23-
})
24-
25-
subject.path('/ping')
26-
end
27-
28-
it "accepts specified swagger document name" do
29-
expect(subject).to receive(:describe).with("/ping", {
30-
swagger_object: :path_item,
31-
swagger_document: 'hello_swagger.json',
32-
swagger_path_item: {path: '/ping'}
33-
})
34-
35-
subject.path('/ping', swagger_document: 'hello_swagger.json')
36-
end
37-
38-
it "passes tags through to the metadata" do
39-
expect(subject).to receive(:describe).with("/ping", {
40-
swagger_object: :path_item,
41-
swagger_document: RSpec.configuration.swagger_docs.keys.first,
42-
swagger_path_item: {path: '/ping'},
43-
tags: ['tag1']
44-
})
45-
46-
subject.path('/ping', tags: ['tag1'])
47-
end
48-
end
49-
503
RSpec.describe RSpec::Rails::Swagger::Helpers::PathItem do
514
let(:klass) do
525
Class.new do

spec/rspec/rails/swagger/request_builder_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
describe '#document' do
1616
subject { described_class.new(metadata, double('instance')) }
17-
let(:metadata) { { swagger_document: 'example.json' } }
17+
let(:metadata) { { swagger_doc: 'example.json' } }
1818

1919
it 'loads the document' do
2020
allow(RSpec.configuration.swagger_docs).to receive(:[]).with('example.json').and_return({foo: :bar})

0 commit comments

Comments
 (0)