Skip to content

Commit a8f2c24

Browse files
authored
Additional properties override (#178)
1 parent 5469c81 commit a8f2c24

File tree

7 files changed

+107
-1
lines changed

7 files changed

+107
-1
lines changed

lib/rspec/openapi/schema_merger.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def merge_schema!(base, spec)
4242
# parameters need to be merged as if `name` and `in` were the Hash keys.
4343
merge_arrays(base, key, value)
4444
else
45-
base[key] = value
45+
# do not ADD `properties` or `required` fields if `additionalProperties` field is present
46+
base[key] = value unless base.key?('additionalProperties') && %w[properties required].include?(key)
4647
end
4748
end
4849
base

spec/integration_tests/rails_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,13 @@ class EngineExtraRoutesTest < ActionDispatch::IntegrationTest
245245
assert_response 200
246246
end
247247
end
248+
249+
class AdditionalPropertiesTest < ActionDispatch::IntegrationTest
250+
i_suck_and_my_tests_are_order_dependent!
251+
openapi!
252+
253+
test 'returns some content' do
254+
get '/additional_properties'
255+
assert_response 200
256+
end
257+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class AdditionalPropertiesController < ApplicationController
2+
def index
3+
response = {
4+
required_key: 'value',
5+
variadic_key: {
6+
gold: 1,
7+
silver: 2,
8+
bronze: 3
9+
}
10+
}
11+
render json: response
12+
end
13+
end

spec/rails/config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@
2020
get '/test_block' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['A TEST']] }
2121

2222
get '/secret_items' => 'secret_items#index'
23+
24+
get '/additional_properties' => 'additional_properties#index'
2325
end
2426
end

spec/rails/doc/openapi.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,49 @@
1515
}
1616
],
1717
"paths": {
18+
"/additional_properties": {
19+
"get": {
20+
"summary": "index",
21+
"tags": [
22+
"AdditionalProperty"
23+
],
24+
"responses": {
25+
"200": {
26+
"description": "returns some content",
27+
"content": {
28+
"application/json": {
29+
"schema": {
30+
"type": "object",
31+
"properties": {
32+
"required_key": {
33+
"type": "string"
34+
},
35+
"variadic_key": {
36+
"type": "object",
37+
"additionalProperties": {
38+
"type": "integer"
39+
}
40+
}
41+
},
42+
"required": [
43+
"required_key",
44+
"variadic_key"
45+
]
46+
},
47+
"example": {
48+
"required_key": "value",
49+
"variadic_key": {
50+
"gold": 1,
51+
"silver": 2,
52+
"bronze": 3
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}
60+
},
1861
"/images": {
1962
"get": {
2063
"summary": "index",

spec/rails/doc/openapi.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,34 @@ info:
1414
servers:
1515
- url: http://localhost:3000
1616
paths:
17+
"/additional_properties":
18+
get:
19+
summary: index
20+
tags:
21+
- AdditionalProperty
22+
responses:
23+
'200':
24+
description: returns some content
25+
content:
26+
application/json:
27+
schema:
28+
type: object
29+
properties:
30+
required_key:
31+
type: string
32+
variadic_key:
33+
type: object
34+
additionalProperties:
35+
type: integer
36+
required:
37+
- required_key
38+
- variadic_key
39+
example:
40+
required_key: value
41+
variadic_key:
42+
gold: 1
43+
silver: 2
44+
bronze: 3
1745
"/images":
1846
get:
1947
summary: index

spec/requests/rails_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,12 @@
246246
end
247247
end
248248
end
249+
250+
RSpec.describe 'Additional Properties test', type: :request do
251+
describe '#test' do
252+
it 'returns some content' do
253+
get '/additional_properties'
254+
expect(response.status).to eq(200)
255+
end
256+
end
257+
end

0 commit comments

Comments
 (0)