Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Issues #191 #402 and #403: Add example property and remove duplicated parameters #405

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ This [format](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/

* Several new options on `parameter` helper.

- `with_example: true`. This option will adjust your description of the parameter with the passed value.
- `with_example: true`. This option will adjust your example of the parameter with the passed value.
- `example: <value>`. Will provide a example value for the parameter.
- `default: <value>`. Will provide a default value for the parameter.
- `minimum: <integer>`. Will setup upper limit for your parameter.
- `maximum: <integer>`. Will setup lower limit for your parameter.
Expand Down
22 changes: 18 additions & 4 deletions features/open_api.feature
Original file line number Diff line number Diff line change
Expand Up @@ -424,25 +424,39 @@ Feature: Generate Open API Specification from test examples
{
"name": "one_level_arr",
"in": "query",
"description": " one level arr\nEg, `[\"value1\", \"value2\"]`",
"description": " one level arr",
"required": false,
"type": "array",
"items": {
"type": "string"
}
},
"example": [
"value1",
"value2"
]
},
{
"name": "two_level_arr",
"in": "query",
"description": " two level arr\nEg, `[[5.1, 3.0], [1.0, 4.5]]`",
"description": " two level arr",
"required": false,
"type": "array",
"items": {
"type": "array",
"items": {
"type": "number"
}
}
},
"example": [
[
5.1,
3.0
],
[
1.0,
4.5
]
]
}
],
"responses": {
Expand Down
11 changes: 1 addition & 10 deletions lib/rspec_api_documentation/open_api/parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,9 @@ class Parameter < Node
add_setting :minimum
add_setting :maximum
add_setting :enum

def description_with_example
str = description_without_example.dup || ''
if with_example && value
str << "\n" unless str.empty?
str << "Eg, `#{value}`"
end
str
end
add_setting :example, :default => lambda { |parameter| parameter.with_example ? parameter.value : nil }

alias_method :description_without_example, :description
alias_method :description, :description_with_example
end
end
end
7 changes: 5 additions & 2 deletions lib/rspec_api_documentation/writers/open_api_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ def extract_schema(fields)
end

def extract_parameters(example)
extract_known_parameters(example.extended_parameters.select { |p| !p[:in].nil? }) +
extract_unknown_parameters(example, example.extended_parameters.select { |p| p[:in].nil? })
parameters = example.extended_parameters.uniq { |parameter| parameter[:name] }

extract_known_parameters(parameters.select { |p| !p[:in].nil? }) +
extract_unknown_parameters(example, parameters.select { |p| p[:in].nil? })
end

def extract_parameter(opts)
Expand All @@ -170,6 +172,7 @@ def extract_parameter(opts)
value: opts[:value],
with_example: opts[:with_example],
default: opts[:default],
example: opts[:example],
).tap do |elem|
if elem.type == :array
elem.items = opts[:items] || OpenApi::Helper.extract_items(opts[:value][0], { minimum: opts[:minimum], maximum: opts[:maximum], enum: opts[:enum] })
Expand Down