From f4426c8e9ea94aa2de1b95be1faa5e288939174e Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Mon, 28 Mar 2022 21:47:01 +0200 Subject: [PATCH] Fix nested uploaded file examples --- lib/rspec/openapi/schema_builder.rb | 6 ++++++ spec/rails/doc/openapi.json | 19 +++++++++++++++---- spec/rails/doc/openapi.yaml | 15 +++++++++++---- spec/requests/rails_spec.rb | 4 +++- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/rspec/openapi/schema_builder.rb b/lib/rspec/openapi/schema_builder.rb index 9b877ec7..435d4992 100644 --- a/lib/rspec/openapi/schema_builder.rb +++ b/lib/rspec/openapi/schema_builder.rb @@ -158,9 +158,15 @@ def try_cast(value) def build_example(value) return nil if value.nil? value = value.dup + adjust_params(value) + end + + def adjust_params(value) value.each do |key, v| if v.is_a?(ActionDispatch::Http::UploadedFile) value[key] = v.original_filename + elsif v.is_a?(Hash) + adjust_params(v) end end end diff --git a/spec/rails/doc/openapi.json b/spec/rails/doc/openapi.json index 4f8ce1d0..0a0aceb6 100644 --- a/spec/rails/doc/openapi.json +++ b/spec/rails/doc/openapi.json @@ -390,14 +390,25 @@ "schema": { "type": "object", "properties": { - "image": { - "type": "string", - "format": "binary" + "nested": { + "type": "object", + "properties": { + "image": { + "type": "string", + "format": "binary" + }, + "caption": { + "type": "string" + } + } } } }, "example": { - "image": "test.png" + "nested": { + "image": "#", + "caption": "Some caption" + } } } } diff --git a/spec/rails/doc/openapi.yaml b/spec/rails/doc/openapi.yaml index 95f8a637..1c5bc521 100644 --- a/spec/rails/doc/openapi.yaml +++ b/spec/rails/doc/openapi.yaml @@ -261,11 +261,18 @@ paths: schema: type: object properties: - image: - type: string - format: binary + nested: + type: object + properties: + image: + type: string + format: binary + caption: + type: string example: - image: test.png + nested: + image: test.png + caption: Some caption responses: '200': description: returns a table diff --git a/spec/requests/rails_spec.rb b/spec/requests/rails_spec.rb index 4c0e5ca9..237c6a13 100644 --- a/spec/requests/rails_spec.rb +++ b/spec/requests/rails_spec.rb @@ -88,7 +88,9 @@ end let(:image) { Rack::Test::UploadedFile.new('test.png', 'image/png') } it 'returns a table' do - patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { image: image } + patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { + nested: { image: image, caption: 'Some caption' } + } expect(response.status).to eq(200) end end