From 6769f2d87b6a1b3af8fb440456bafa9b3c18b09a Mon Sep 17 00:00:00 2001 From: lan-tripla Date: Mon, 6 May 2024 12:03:20 +0700 Subject: [PATCH] Fix array in query params issue: https://stackoverflow.com/questions/68452127/pass-array-as-query-parameter-in-swagger-openapi-3-0 --- lib/rspec/openapi/schema_builder.rb | 2 ++ spec/apps/rails/doc/openapi.json | 16 +++++++++++++++- spec/apps/rails/doc/openapi.yaml | 11 ++++++++++- spec/requests/rails_spec.rb | 5 +++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/rspec/openapi/schema_builder.rb b/lib/rspec/openapi/schema_builder.rb index 49b5272..ded1976 100644 --- a/lib/rspec/openapi/schema_builder.rb +++ b/lib/rspec/openapi/schema_builder.rb @@ -118,6 +118,8 @@ def build_parameter_name(key, value) if value.is_a?(Hash) && (value_keys = value.keys).size == 1 value_key = value_keys.first build_parameter_name("#{key}[#{value_key}]", value[value_key]) + elsif value.is_a?(Array) + "#{key}[]" else key end diff --git a/spec/apps/rails/doc/openapi.json b/spec/apps/rails/doc/openapi.json index 9b34273..6bd2c20 100644 --- a/spec/apps/rails/doc/openapi.json +++ b/spec/apps/rails/doc/openapi.json @@ -591,6 +591,20 @@ "price": "0" } }, + { + "name": "names[]", + "in": "query", + "required": false, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "example": [ + "Example Table" + ] + }, { "name": "page", "in": "query", @@ -1240,4 +1254,4 @@ } }, "custom_field": "custom_value" -} \ No newline at end of file +} diff --git a/spec/apps/rails/doc/openapi.yaml b/spec/apps/rails/doc/openapi.yaml index b87068e..c49d605 100644 --- a/spec/apps/rails/doc/openapi.yaml +++ b/spec/apps/rails/doc/openapi.yaml @@ -367,6 +367,15 @@ paths: - price example: price: '0' + - name: names[] + in: query + required: false + schema: + type: array + items: + type: string + example: + - Example Table - name: page in: query required: false @@ -381,7 +390,7 @@ paths: example: 10 responses: '200': - description: with different deep query parameters + description: with array query parameters headers: X-Cursor: schema: diff --git a/spec/requests/rails_spec.rb b/spec/requests/rails_spec.rb index 9365139..6e36357 100644 --- a/spec/requests/rails_spec.rb +++ b/spec/requests/rails_spec.rb @@ -53,6 +53,11 @@ get '/tables', params: { filter: { 'price' => 0 } }, headers: { authorization: 'k0kubun' } expect(response.status).to eq(200) end + + it 'with array query parameters' do + get '/tables', params: { names: ['Example Table'] }, headers: { authorization: 'k0kubun' } + expect(response.status).to eq(200) + end end it 'has a request spec which does not make any request' do