Skip to content

Commit 93b7a2e

Browse files
authored
Merge pull request #405 from SrMouraSilva/issues-393-402-403-open-api-parameters
Fix: Issues #191 #402 and #403: Add example property and remove duplicated parameters
2 parents 6a4a173 + 7ec3886 commit 93b7a2e

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ This [format](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/
360360
361361
* Several new options on `parameter` helper.
362362
363-
- `with_example: true`. This option will adjust your description of the parameter with the passed value.
363+
- `with_example: true`. This option will adjust your example of the parameter with the passed value.
364+
- `example: <value>`. Will provide a example value for the parameter.
364365
- `default: <value>`. Will provide a default value for the parameter.
365366
- `minimum: <integer>`. Will setup upper limit for your parameter.
366367
- `maximum: <integer>`. Will setup lower limit for your parameter.

features/open_api.feature

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,25 +424,39 @@ Feature: Generate Open API Specification from test examples
424424
{
425425
"name": "one_level_arr",
426426
"in": "query",
427-
"description": " one level arr\nEg, `[\"value1\", \"value2\"]`",
427+
"description": " one level arr",
428428
"required": false,
429429
"type": "array",
430430
"items": {
431431
"type": "string"
432-
}
432+
},
433+
"example": [
434+
"value1",
435+
"value2"
436+
]
433437
},
434438
{
435439
"name": "two_level_arr",
436440
"in": "query",
437-
"description": " two level arr\nEg, `[[5.1, 3.0], [1.0, 4.5]]`",
441+
"description": " two level arr",
438442
"required": false,
439443
"type": "array",
440444
"items": {
441445
"type": "array",
442446
"items": {
443447
"type": "number"
444448
}
445-
}
449+
},
450+
"example": [
451+
[
452+
5.1,
453+
3.0
454+
],
455+
[
456+
1.0,
457+
4.5
458+
]
459+
]
446460
}
447461
],
448462
"responses": {

lib/rspec_api_documentation/open_api/parameter.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,9 @@ class Parameter < Node
1616
add_setting :minimum
1717
add_setting :maximum
1818
add_setting :enum
19-
20-
def description_with_example
21-
str = description_without_example.dup || ''
22-
if with_example && value
23-
str << "\n" unless str.empty?
24-
str << "Eg, `#{value}`"
25-
end
26-
str
27-
end
19+
add_setting :example, :default => lambda { |parameter| parameter.with_example ? parameter.value : nil }
2820

2921
alias_method :description_without_example, :description
30-
alias_method :description, :description_with_example
3122
end
3223
end
3324
end

lib/rspec_api_documentation/writers/open_api_writer.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ def extract_schema(fields)
156156
end
157157

158158
def extract_parameters(example)
159-
extract_known_parameters(example.extended_parameters.select { |p| !p[:in].nil? }) +
160-
extract_unknown_parameters(example, example.extended_parameters.select { |p| p[:in].nil? })
159+
parameters = example.extended_parameters.uniq { |parameter| parameter[:name] }
160+
161+
extract_known_parameters(parameters.select { |p| !p[:in].nil? }) +
162+
extract_unknown_parameters(example, parameters.select { |p| p[:in].nil? })
161163
end
162164

163165
def extract_parameter(opts)
@@ -170,6 +172,7 @@ def extract_parameter(opts)
170172
value: opts[:value],
171173
with_example: opts[:with_example],
172174
default: opts[:default],
175+
example: opts[:example],
173176
).tap do |elem|
174177
if elem.type == :array
175178
elem.items = opts[:items] || OpenApi::Helper.extract_items(opts[:value][0], { minimum: opts[:minimum], maximum: opts[:maximum], enum: opts[:enum] })

0 commit comments

Comments
 (0)