From 6592af4477299a245dae0c69cf848a1cf70866d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Thu, 28 Mar 2024 15:54:21 +0900 Subject: [PATCH] Generate empty request body (#196) * Check optional field * Extract common function * Generate empty request body --- lib/rspec/openapi/schema_builder.rb | 8 ++++---- spec/rails/app/controllers/users_controller.rb | 2 +- spec/rails/doc/openapi.json | 5 +---- spec/rails/doc/openapi.yaml | 2 -- spec/rails/doc/smart/expected.yaml | 4 ---- spec/requests/rails_smart_merge_spec.rb | 15 +++++++++++++++ 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/rspec/openapi/schema_builder.rb b/lib/rspec/openapi/schema_builder.rb index f99e4a50..8faadbc3 100644 --- a/lib/rspec/openapi/schema_builder.rb +++ b/lib/rspec/openapi/schema_builder.rb @@ -25,16 +25,17 @@ def build(record) end end + http_method = record.http_method.downcase { paths: { normalize_path(record.path) => { - record.http_method.downcase => { + http_method => { summary: record.summary, tags: record.tags, operationId: record.operation_id, security: record.security, parameters: build_parameters(record), - requestBody: build_request_body(record), + requestBody: http_method == 'get' ? nil : build_request_body(record), responses: { record.status.to_s => response, }, @@ -47,7 +48,7 @@ def build(record) private def enrich_with_required_keys(obj) - obj[:required] = obj[:properties]&.keys + obj[:required] = obj[:properties]&.keys || [] obj end @@ -123,7 +124,6 @@ def build_parameter_name(key, value) def build_request_body(record) return nil if record.request_content_type.nil? - return nil if record.request_params.empty? return nil if record.status >= 400 { diff --git a/spec/rails/app/controllers/users_controller.rb b/spec/rails/app/controllers/users_controller.rb index 8acafe6b..2a66a2a2 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/openapi.json b/spec/rails/doc/openapi.json index c9f5c5f6..dcce6ad2 100644 --- a/spec/rails/doc/openapi.json +++ b/spec/rails/doc/openapi.json @@ -854,10 +854,7 @@ "no_content": { "type": "string" } - }, - "required": [ - "no_content" - ] + } }, "example": { "no_content": "true" diff --git a/spec/rails/doc/openapi.yaml b/spec/rails/doc/openapi.yaml index b9f0e068..f77c6612 100644 --- a/spec/rails/doc/openapi.yaml +++ b/spec/rails/doc/openapi.yaml @@ -555,8 +555,6 @@ paths: properties: no_content: type: string - required: - - no_content example: no_content: 'true' get: diff --git a/spec/rails/doc/smart/expected.yaml b/spec/rails/doc/smart/expected.yaml index afa610e2..99f795b7 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 7afa0ab1..4b1da2a1 100644 --- a/spec/requests/rails_smart_merge_spec.rb +++ b/spec/requests/rails_smart_merge_spec.rb @@ -82,6 +82,21 @@ 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',