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

Support dictionary-like type (additionalProperties) #178

Merged
merged 8 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/rspec/openapi/schema_merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def merge_schema!(base, spec)
# parameters need to be merged as if `name` and `in` were the Hash keys.
merge_arrays(base, key, value)
else
base[key] = value
# do not ADD `properties` or `required` fields if `additionalProperties` field is present
base[key] = value unless base.key?('additionalProperties') && %w[properties required].include?(key)
end
end
base
Expand Down
10 changes: 10 additions & 0 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,13 @@ class EngineExtraRoutesTest < ActionDispatch::IntegrationTest
assert_response 200
end
end

class AdditionalPropertiesTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

test 'returns some content' do
get '/additional_properties'
assert_response 200
end
end
13 changes: 13 additions & 0 deletions spec/rails/app/controllers/additional_properties_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AdditionalPropertiesController < ApplicationController
def index
response = {
required_key: 'value',
variadic_key: {
gold: 1,
silver: 2,
bronze: 3
}
}
render json: response
end
end
2 changes: 2 additions & 0 deletions spec/rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
get '/test_block' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['A TEST']] }

get '/secret_items' => 'secret_items#index'

get '/additional_properties' => 'additional_properties#index'
end
end
43 changes: 43 additions & 0 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,49 @@
}
],
"paths": {
"/additional_properties": {
"get": {
"summary": "index",
"tags": [
"AdditionalProperty"
],
"responses": {
"200": {
"description": "returns some content",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"required_key": {
"type": "string"
},
"variadic_key": {
"type": "object",
"additionalProperties": {
"type": "integer"
}
}
},
"required": [
"required_key",
"variadic_key"
]
},
"example": {
"required_key": "value",
"variadic_key": {
"gold": 1,
"silver": 2,
"bronze": 3
}
}
}
}
}
}
}
},
"/images": {
"get": {
"summary": "index",
Expand Down
28 changes: 28 additions & 0 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ info:
servers:
- url: http://localhost:3000
paths:
"/additional_properties":
get:
summary: index
tags:
- AdditionalProperty
responses:
'200':
description: returns some content
content:
application/json:
schema:
type: object
properties:
required_key:
type: string
variadic_key:
type: object
additionalProperties:
type: integer
required:
- required_key
- variadic_key
example:
required_key: value
variadic_key:
gold: 1
silver: 2
bronze: 3
"/images":
get:
summary: index
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,12 @@
end
end
end

RSpec.describe 'Additional Properties test', type: :request do
describe '#test' do
it 'returns some content' do
get '/additional_properties'
expect(response.status).to eq(200)
end
end
end