Skip to content

Commit 6a86bfe

Browse files
committed
Fix API blueprint documentation.
- Path segments extensions were not following the RFC 6570
1 parent aee3bc0 commit 6a86bfe

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/rspec_api_documentation/views/api_blueprint_index.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def sections
2323
{
2424
"has_attributes?".to_sym => attrs.size > 0,
2525
"has_parameters?".to_sym => params.size > 0,
26-
route: route,
26+
route: format_route(examples[0]),
2727
route_name: examples[0][:route_name],
2828
attributes: attrs,
2929
parameters: params,
@@ -45,6 +45,17 @@ def examples
4545

4646
private
4747

48+
# APIB follows the RFC 6570 to format URI templates.
49+
# According to it, path segment expansion (used to describe URI path
50+
# hierarchies) should be represented by `{/var}` and not by `/:var`
51+
# For example `/posts/:id` should become `/posts{/id}`
52+
# cf. https://github.com/apiaryio/api-blueprint/blob/format-1A/API%20Blueprint%20Specification.md#431-resource-section
53+
# cf. https://tools.ietf.org/html/rfc6570#section-3.2.6
54+
def format_route(example)
55+
route_uri = example[:route_uri].gsub(/\/:(.*?)([.\/?{]|$)/, '{/\1}\2')
56+
"#{route_uri}#{example[:route_optionals]}"
57+
end
58+
4859
# APIB has both `parameters` and `attributes`. This generates a hash
4960
# with all of its properties, like name, description, required.
5061
# {

spec/views/api_blueprint_index_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109

110110
post_examples = post_route[:http_methods].map { |http_method| http_method[:examples] }.flatten
111111
expect(post_examples.size).to eq 2
112-
expect(post_route[:route]).to eq "/posts/:id"
112+
expect(post_route[:route]).to eq "/posts{/id}"
113113
expect(post_route[:route_name]).to eq "Single Post"
114114
expect(post_route[:has_parameters?]).to eq true
115115
expect(post_route[:parameters]).to eq [{
@@ -130,7 +130,7 @@
130130

131131
post_w_optionals_examples = post_route_with_optionals[:http_methods].map { |http_method| http_method[:examples] }.flatten
132132
expect(post_w_optionals_examples.size).to eq 1
133-
expect(post_route_with_optionals[:route]).to eq "/posts/:id{?option=:option}"
133+
expect(post_route_with_optionals[:route]).to eq "/posts{/id}{?option=:option}"
134134
expect(post_route_with_optionals[:route_name]).to eq "Single Post"
135135
expect(post_route_with_optionals[:has_parameters?]).to eq true
136136
expect(post_route_with_optionals[:parameters]).to eq [{

0 commit comments

Comments
 (0)