Skip to content

Commit

Permalink
Check optional field
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Mar 28, 2024
1 parent 5c875d7 commit e17fc89
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def build(record)
private

def enrich_with_required_keys(obj)
obj[:required] = obj[:properties]&.keys
obj[:required] = obj[:properties]&.keys || []
obj
end

Expand Down
2 changes: 1 addition & 1 deletion spec/rails/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class UsersController < ApplicationController
def create
res = {
name: params[:name],
name: params[:name] || 'alice',
relations: {
avatar: {
url: params[:avatar_url] || 'https://example.com/avatar.png',
Expand Down
4 changes: 0 additions & 4 deletions spec/rails/doc/smart/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,5 @@ components:
properties:
baz:
type: integer
required:
- baz
required:
- bar
required:
- name
16 changes: 16 additions & 0 deletions spec/requests/rails_smart_merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@
expect(response.status).to eq(201)
end

it 'accepts nested object where some fields are missing' do
post '/users', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'alice',
foo: {
bar: {

Check notice

Code scanning / Rubocop

Use spaces inside hash literal braces - or don't. Note test

Layout/SpaceInsideHashLiteralBraces: Space inside empty hash literal braces detected.
},
},
}.to_json
expect(response.status).to eq(201)
end

it 'can accept empty body' do
post '/users', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {}.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',
Expand Down

0 comments on commit e17fc89

Please sign in to comment.