diff --git a/lib/rspec/openapi/schema_builder.rb b/lib/rspec/openapi/schema_builder.rb index f99e4a5..dc9de21 100644 --- a/lib/rspec/openapi/schema_builder.rb +++ b/lib/rspec/openapi/schema_builder.rb @@ -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 diff --git a/spec/rails/app/controllers/users_controller.rb b/spec/rails/app/controllers/users_controller.rb index 8acafe6..2a66a2a 100644 --- a/spec/rails/app/controllers/users_controller.rb +++ b/spec/rails/app/controllers/users_controller.rb @@ -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', diff --git a/spec/rails/doc/smart/expected.yaml b/spec/rails/doc/smart/expected.yaml index afa610e..99f795b 100644 --- a/spec/rails/doc/smart/expected.yaml +++ b/spec/rails/doc/smart/expected.yaml @@ -411,9 +411,5 @@ components: properties: baz: type: integer - required: - - baz required: - bar - required: - - name diff --git a/spec/requests/rails_smart_merge_spec.rb b/spec/requests/rails_smart_merge_spec.rb index 7afa0ab..42af1f2 100644 --- a/spec/requests/rails_smart_merge_spec.rb +++ b/spec/requests/rails_smart_merge_spec.rb @@ -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: { + }, + }, + }.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',