Skip to content

Add examples support to requests#4

Merged
alex-shum merged 3 commits intomasterfrom
task/add-requests-examples-support
Sep 23, 2025
Merged

Add examples support to requests#4
alex-shum merged 3 commits intomasterfrom
task/add-requests-examples-support

Conversation

@kriston-costa
Copy link

@kriston-costa kriston-costa commented Mar 22, 2021

A PR WITH EXAMPLE USAGE IS HERE: https://github.com/instacart/carrot/pull/155131

We want to add examples support to request bodies in Swagger. Right now our requests look like the following:

image

They just supply the type information in the field.

rswag does not support adding examples right now:
rswag#380

This PR adds some basic support for examples. It doesn't provide validation of examples. This is meant to be an initial implementation that we can add additional functionality to later if we need it.

With this change we now have the ability to call

request_body_example value: body

within the body of an rspec test in order to bind an example to the request.

You can also bind multiple examples by calling request_body_example multiple times

request_body_example value: body1, summary: "The basic example"
request_body_example value: body2, summary: "The fancy example"
request_body_example value: body3, summary: "The really fancy example" 

The summary is what appears in the dropdown seen in the image below.

By default, the summary is the summary defined by the rpec test when defining the method:
Eg, for

  path "/v2/catalog_outsource/workflows/{workflow_name}/vendor_tasks/{vendor_task_uuid}/error" do
    post "Error a vendor task" do
      tags "CatalogOutsource"
      operationId "ErrorVendorTask"
      consumes "application/json"
      produces "application/json"
     ...

The summary would be Error a vendor task

Lastly - this PR only supports static "values". Eg, you cannot reference a let (:body) { ... } it must be body = {}.
This is because I want to reduce the confusion if someone starts modifying the body object in nested contexts and ends up with a. bunch of "examples" that all have the same summary but different values.

image

@kriston-costa kriston-costa changed the title I have no idea what Im doing Add examples support to requests Mar 23, 2021
@kriston-costa kriston-costa marked this pull request as ready for review March 23, 2021 16:13
@ziyunli ziyunli requested a review from a team March 24, 2021 16:19
Copy link

@imran-iq imran-iq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this is needed as rswag /should/ already work with examples see:

image

we're not doing the above in the connect code base at the moment, but it should work

@kriston-costa
Copy link
Author

Chatted with @imran-iq offline and he clarified that we'd like to move away from swagger docs anyway. Rather than adding support here we can revisit adding support to the connect DSL in the future.

Since other example support was already delivered I'm going to close this PR.

@Mtihc
Copy link

Mtihc commented Sep 9, 2021

Quote:

I do not think this is needed as rswag /should/ already work with examples see:

image

we're not doing the above in the connect code base at the moment, but it should work

@imran-iq

The above does not give the desired result. It doesn't give you the nice dropdown.

You're defining the examples inside the schema. This results in the following swagger yml.

requestBody:
  content:
    application/json:
      schema:
        examples:
          Access token:
            client_id: 'blah etc'

where examples is nested under schema. But that's not what you want. You want examples to be a sibling of schema.

See chapter Request Body Examples here: https://swagger.io/docs/specification/describing-request-body/

In other words, this is what you want

requestBody:
  content:
    application/json:
      examples:
        Access token:
          value:
            client_id: 'blah etc'
      schema:

where examples is a sibling of schema. (side-note: also notice the extra value key)

That does give you the nice dropdown.

As far as I know there NO WAY to achieve this using rswag at the moment.

@romanblanco romanblanco mentioned this pull request Sep 20, 2022
6 tasks
@alex-shum alex-shum reopened this Sep 22, 2025
@alex-shum alex-shum closed this Sep 22, 2025
@alex-shum alex-shum reopened this Sep 23, 2025
@alex-shum alex-shum merged commit 7286e9d into master Sep 23, 2025
2 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants