Skip to content

Commit

Permalink
Generate empty request body (#196)
Browse files Browse the repository at this point in the history
* Check optional field

* Extract common function

* Generate empty request body
  • Loading branch information
exoego committed Mar 28, 2024
1 parent 5c875d7 commit 6592af4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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

Expand Down Expand Up @@ -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

{
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
5 changes: 1 addition & 4 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,7 @@
"no_content": {
"type": "string"
}
},
"required": [
"no_content"
]
}
},
"example": {
"no_content": "true"
Expand Down
2 changes: 0 additions & 2 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,6 @@ paths:
properties:
no_content:
type: string
required:
- no_content
example:
no_content: 'true'
get:
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
15 changes: 15 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,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',
Expand Down

0 comments on commit 6592af4

Please sign in to comment.