From 312314cf69f191d41fc464ac078f362b137c62cc Mon Sep 17 00:00:00 2001 From: Nicholas von Waltsleben Date: Thu, 29 Feb 2024 11:40:09 +0200 Subject: [PATCH 1/8] allow manually set additionalProperties key to override generated properties --- lib/rspec/openapi/schema_merger.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/rspec/openapi/schema_merger.rb b/lib/rspec/openapi/schema_merger.rb index 7861ada..f8ee6a7 100644 --- a/lib/rspec/openapi/schema_merger.rb +++ b/lib/rspec/openapi/schema_merger.rb @@ -36,6 +36,9 @@ def merge_schema!(base, spec) end spec.each do |key, value| + # allow `additionalProperties` to override generated property modifications + next if base.key?('additionalProperties') && %w[properties required].include?(key) + if base[key].is_a?(Hash) && value.is_a?(Hash) merge_schema!(base[key], value) unless base[key].key?('$ref') elsif base[key].is_a?(Array) && value.is_a?(Array) From 03da2e4789db1918b69dae25e5b2c3a47e04da91 Mon Sep 17 00:00:00 2001 From: Nicholas von Waltsleben Date: Thu, 29 Feb 2024 15:02:33 +0200 Subject: [PATCH 2/8] update openapi.yaml --- spec/rails/doc/openapi.yaml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/rails/doc/openapi.yaml b/spec/rails/doc/openapi.yaml index c1be9e5..b9f0e06 100644 --- a/spec/rails/doc/openapi.yaml +++ b/spec/rails/doc/openapi.yaml @@ -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 From 4653fb3417e5819a482ed289fa133870c909c529 Mon Sep 17 00:00:00 2001 From: Nicholas von Waltsleben Date: Thu, 29 Feb 2024 15:03:53 +0200 Subject: [PATCH 3/8] update openapi.json --- spec/rails/doc/openapi.json | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/spec/rails/doc/openapi.json b/spec/rails/doc/openapi.json index 2ad78cd..c9f5c5f 100644 --- a/spec/rails/doc/openapi.json +++ b/spec/rails/doc/openapi.json @@ -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", From 47310e5d8ac02db61736b7d9afaa27d715b5da14 Mon Sep 17 00:00:00 2001 From: Nicholas von Waltsleben Date: Thu, 29 Feb 2024 15:22:32 +0200 Subject: [PATCH 4/8] update specs --- lib/rspec/openapi/schema_merger.rb | 6 ++---- spec/integration_tests/rails_test.rb | 10 ++++++++++ spec/rails/config/routes.rb | 2 ++ spec/rails/doc/smart/expected.yaml | 28 ++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/rspec/openapi/schema_merger.rb b/lib/rspec/openapi/schema_merger.rb index f8ee6a7..a79dfde 100644 --- a/lib/rspec/openapi/schema_merger.rb +++ b/lib/rspec/openapi/schema_merger.rb @@ -36,16 +36,14 @@ def merge_schema!(base, spec) end spec.each do |key, value| - # allow `additionalProperties` to override generated property modifications - next if base.key?('additionalProperties') && %w[properties required].include?(key) - if base[key].is_a?(Hash) && value.is_a?(Hash) merge_schema!(base[key], value) unless base[key].key?('$ref') elsif base[key].is_a?(Array) && value.is_a?(Array) # 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 diff --git a/spec/integration_tests/rails_test.rb b/spec/integration_tests/rails_test.rb index 8cce8dc..a4dc861 100644 --- a/spec/integration_tests/rails_test.rb +++ b/spec/integration_tests/rails_test.rb @@ -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 diff --git a/spec/rails/config/routes.rb b/spec/rails/config/routes.rb index 0a7573a..ce19da0 100644 --- a/spec/rails/config/routes.rb +++ b/spec/rails/config/routes.rb @@ -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 diff --git a/spec/rails/doc/smart/expected.yaml b/spec/rails/doc/smart/expected.yaml index 4ce7d42..1cb0f50 100644 --- a/spec/rails/doc/smart/expected.yaml +++ b/spec/rails/doc/smart/expected.yaml @@ -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 "/tables": get: summary: index From 93a55885bdf4a29a70fa67704e9722cfbba39103 Mon Sep 17 00:00:00 2001 From: Nicholas von Waltsleben Date: Thu, 29 Feb 2024 15:31:00 +0200 Subject: [PATCH 5/8] added additional properties controller --- .../controllers/additional_properties_controller.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 spec/rails/app/controllers/additional_properties_controller.rb diff --git a/spec/rails/app/controllers/additional_properties_controller.rb b/spec/rails/app/controllers/additional_properties_controller.rb new file mode 100644 index 0000000..2c4e275 --- /dev/null +++ b/spec/rails/app/controllers/additional_properties_controller.rb @@ -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 From 0596d22d3ea377d2368a77fbbaa2143480e2662c Mon Sep 17 00:00:00 2001 From: Nicholas von Waltsleben Date: Thu, 29 Feb 2024 15:31:41 +0200 Subject: [PATCH 6/8] add open-generating specs --- spec/rails/doc/openapi.yaml | 2 +- spec/requests/rails_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/rails/doc/openapi.yaml b/spec/rails/doc/openapi.yaml index b9f0e06..80fa913 100644 --- a/spec/rails/doc/openapi.yaml +++ b/spec/rails/doc/openapi.yaml @@ -21,7 +21,7 @@ paths: - AdditionalProperty responses: '200': - description: returns some content + description: returns the block content content: application/json: schema: diff --git a/spec/requests/rails_spec.rb b/spec/requests/rails_spec.rb index 9b45971..e917ff7 100644 --- a/spec/requests/rails_spec.rb +++ b/spec/requests/rails_spec.rb @@ -246,3 +246,12 @@ end end end + +RSpec.describe 'Additional Properties test', type: :request do + describe '#test' do + it 'returns the block content' do + get '/additional_properties' + expect(response.status).to eq(200) + end + end +end From 750140805354c4eb4d4f9613fc930f30d6fa806c Mon Sep 17 00:00:00 2001 From: Nicholas von Waltsleben Date: Fri, 1 Mar 2024 07:05:03 +0200 Subject: [PATCH 7/8] update comment and fixture --- lib/rspec/openapi/schema_merger.rb | 2 +- spec/rails/doc/openapi.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rspec/openapi/schema_merger.rb b/lib/rspec/openapi/schema_merger.rb index a79dfde..fea6f8e 100644 --- a/lib/rspec/openapi/schema_merger.rb +++ b/lib/rspec/openapi/schema_merger.rb @@ -42,7 +42,7 @@ 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 - # do not add `properties` or `required` fields if `additionalProperties` field is present + # 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 diff --git a/spec/rails/doc/openapi.yaml b/spec/rails/doc/openapi.yaml index 80fa913..b9f0e06 100644 --- a/spec/rails/doc/openapi.yaml +++ b/spec/rails/doc/openapi.yaml @@ -21,7 +21,7 @@ paths: - AdditionalProperty responses: '200': - description: returns the block content + description: returns some content content: application/json: schema: From 4450fa840a6da2c8ea4083270154111e6d0928ee Mon Sep 17 00:00:00 2001 From: Nicholas von Waltsleben Date: Fri, 1 Mar 2024 07:13:44 +0200 Subject: [PATCH 8/8] get smart merge tests passing again --- spec/rails/doc/smart/expected.yaml | 28 ---------------------------- spec/requests/rails_spec.rb | 2 +- 2 files changed, 1 insertion(+), 29 deletions(-) diff --git a/spec/rails/doc/smart/expected.yaml b/spec/rails/doc/smart/expected.yaml index 1cb0f50..4ce7d42 100644 --- a/spec/rails/doc/smart/expected.yaml +++ b/spec/rails/doc/smart/expected.yaml @@ -14,34 +14,6 @@ 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 "/tables": get: summary: index diff --git a/spec/requests/rails_spec.rb b/spec/requests/rails_spec.rb index e917ff7..9bcac57 100644 --- a/spec/requests/rails_spec.rb +++ b/spec/requests/rails_spec.rb @@ -249,7 +249,7 @@ RSpec.describe 'Additional Properties test', type: :request do describe '#test' do - it 'returns the block content' do + it 'returns some content' do get '/additional_properties' expect(response.status).to eq(200) end