-
Notifications
You must be signed in to change notification settings - Fork 416
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
Request body examples #555
Conversation
We need this exact feature. I tested this branch out in our project and it worked out great. For us, we wanted to be able to use the actual request from the spec as the example, so we did it in an after block. But I think what you have here is good enough for our use case. I'd love to see this get merged! Thanks for your work! config.after(:each, operation: true, use_as_request_example: true) do |spec|
spec.metadata[:operation][:request_examples] ||= []
example = {
value: JSON.parse(request.body.string, symbolize_names: true),
name: 'request_example_1',
summary: 'A request example'
}
spec.metadata[:operation][:request_examples] << example
end |
aa99973
to
6b430a3
Compare
6b430a3
to
0ab2025
Compare
@@ -144,6 +145,7 @@ There is also a generator which can help get you started `rails generate rspec:s | |||
tags 'Blogs', 'Another Tag' | |||
produces 'application/json', 'application/xml' | |||
parameter name: :id, in: :path, type: :string | |||
request_example value: { some_field: 'Foo' }, name: 'basic', summary: 'Request example description' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method is request_body_example
and not request_example
so either README has wrong name or the method. Not sure what was the intention. request_body_example
is probably more intuitive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome 😄
response(200, "successful", use_as_request_example: true) do |
Since this PR is merged but not yet in a gem release, I am using this feature by loading the gem from github with the following entry in my gem "rswag-specs", git: "https://github.com/rswag/rswag.git", branch: "master",
glob: "rswag-specs/rswag-specs.gemspec" # to get request examples, merged in https://github.com/rswag/rswag/pull/555 |
For my project I edited the above example to look like this: config.after(:each, operation: true, use_as_request_example: true) do |spec|
spec.metadata[:operation][:request_examples] ||= []
example = {
value: JSON.parse(request.body.string, symbolize_names: true),
name: spec.metadata[:response][:description].parameterize.underscore,
summary: spec.metadata[:response][:description]
}
spec.metadata[:operation][:request_examples] << example
end The main difference is that the name and summary of the example come from the spec's description. This means that you can have multiple |
Problem
Closes #380
Solution
Cherry-picking @kriston-costa's solution from instacart#4
This concerns this parts of the Open API Specification:
The changes I made are compatible with:
Related Issues
Checklist
Steps to Test or Reproduce