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

Can we remove required parameters for request body? #195

Closed
akira-19 opened this issue Mar 27, 2024 · 6 comments · Fixed by #196
Closed

Can we remove required parameters for request body? #195

akira-19 opened this issue Mar 27, 2024 · 6 comments · Fixed by #196

Comments

@akira-19
Copy link

akira-19 commented Mar 27, 2024

When I generate a document, parameters for request body are set as required.
Is there any way to set the parameters optional?
I would like to remove the following required section.

requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                param1:
                  type: boolean
                param2:
                  type: boolean
                param3:
                  type: boolean
              required:
              - param1
              - param2
              - param3
@exoego
Copy link
Owner

exoego commented Mar 27, 2024

It is to add a test case that use only required parameters.

it `...` do
  # use all parameters
end

it `...` do
  # use only required parameters
end

Then, rspec-openapi exclude unused parameters from required

@akira-19
Copy link
Author

@exoego Thank you. I want it to have no required field. I mean all parameters are optional.

it `...` do
  # use all parameters
end

it `...` do
  # use no parameters
end

I tried this, and it didn't work though what you showed worked.

@exoego
Copy link
Owner

exoego commented Mar 27, 2024

Hmm, that is strange.
If a minimum reproduction is provided, I will look.

These tests correspond to this feature (automatic required generation for request body).

describe '#create' do
it 'accepts missing avatar_url' do
post '/users', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'alice',
}.to_json
expect(response.status).to eq(201)
end
it 'accepts nested object' do
post '/users', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'alice',
foo: {
bar: {
baz: 42,
},
},
}.to_json
expect(response.status).to eq(201)
end
it 'returns an user' do
post '/users', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'alice',
avatar_url: 'https://example.com/avatar.png',
}.to_json
expect(response.status).to eq(201)
end
end

The below fixture illustrates the result. Always-specified parameters are included in required, others are not.
For this example, the top-level field name is required, and foo is not.

PostUsersRequest:
type: object
properties:
name:
type: string
avatar_url:
type: string
foo:
type: object
properties:
bar:
type: object
properties:
baz:
type: integer
required:
- baz
required:
- bar
required:
- name

@akira-19
Copy link
Author

akira-19 commented Mar 27, 2024

@exoego Your example works, but it doesn't work when I try to make all the parameters optional like below.

it 'returns 201' do
  params = {
    param1: true,
    param2: true,
    param3: true
  }
  patch api_path, params: params, headers: token
  expect(response.status).to eq 201
end

it 'returns 201' do
  params = {}
  patch api_path, params: params, headers: token
  expect(response.status).to eq 201
end

@exoego
Copy link
Owner

exoego commented Mar 28, 2024

@akira-19 This was fixed in #196 and landed in v0.16.0.

@akira-19
Copy link
Author

@exoego Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants