Skip to content

feature: Enable multiple examples for requestBody similar to responses #312

@ChiragMurarka

Description

@ChiragMurarka

Hi
Awesome Gem!!! Thanks for the share, Did a great job for my non DSL Rspecs.

I was going through related issues like
#177
#140

Right now, the multiple examples only work for responses and not for requestBody
Let's say my Rspecs are somewhat like these -

describe "customer sign_in" do
  it "otp sign_in" do
    post "/customers/sign_in",
      params: {
        phone_no: customer.phone_no,
        login_method: "otp",
        initiate: true
      },
      as: :json,
      headers: { Authorization: "Bearer #{token}" }

    expect(JSON.parse(response.body)["meta"]["code"]).to eq(200)
    expect(JSON.parse(response.body)).to include("auth_token")
  end

  it "should not allow email password sign_in" do
    post "/customers/sign_in",
      params: {
        email: customer.email,
        password: "password"
      },
      as: :json,
      headers: { Authorization: "Bearer #{token}" }

    expect(JSON.parse(response.body)["meta"]["code"]).to eq(400)
  end
end

i.e. my same endpoint have two different types of requestBody

Problem

But right now, this is the schema that gets generated with example in requestBody merged

paths:
  "/customers/sign_in":
    post:
      summary: sign_in
      tags:
      - Customer
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                phone_no:
                  type: string
                login_method:
                  type: string
                email:
                  type: string
                password:
                  type: string
                initiate:
                  type: string
            example:
              phone_no: '95195653'
              login_method: otp
              initiate: true
              email: trinidad_dooley@abshire.io
              password: password

This results in an invalid example, because in reality OTP sign-in and email/password sign-in are mutually exclusive request bodies.

If it allows for multiple examples in requestBody (which ideally it should) it would make the Docs even more extensive by showing all the possibilities, and the scenario listed in #140 would also be tackled as 400s equivalent requestBody will have separate example and will not be merged with the others as we will be segregating the examples based on description

I explored the code and found a way to implement this by modifying schema_builder.rb.
If this is considered a valid improvement, I’d love to contribute and open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions