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

Issue with nested file uploads #45

Closed
blocknotes opened this issue Mar 26, 2022 · 2 comments · Fixed by #46
Closed

Issue with nested file uploads #45

blocknotes opened this issue Mar 26, 2022 · 2 comments · Fixed by #46

Comments

@blocknotes
Copy link
Contributor

Hey :)
I get this linting error with an endpoint that receives an uploaded file: unknown tag !<!ruby/object:File>

The spec is structured in this way:

    let(:valid_attributes) do
      {
        look_media_attachment: {
          visible: true,
          caption: 'This is the caption text',
          image: fixture_file_upload("#{Rails.root}/spec/fixtures/images/test_image.jpg", 'image/jpeg')
        }
      }
    end

    it do
      post "/api/v1/look_media_attachments.json", params: valid_attributes, headers: auth_credentials
      # ...
    end

The failing output is the following (the error is pointing to the line tmpfile: &1 !ruby/object:File {} below):

      # ...
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                look_media_attachment:
                  type: object
                  properties:
                    visible:
                      type: string
                    caption:
                      type: string
                    image:
                      type: string
                      format: binary
            example:
              look_media_attachment:
                visible: 'true'
                caption: This is the caption text
                image: !ruby/object:ActionDispatch::Http::UploadedFile
                  tempfile: !ruby/object:Tempfile
                    unlinked: true
                    mode: 2562
                    tmpfile: &1 !ruby/object:File {}
                    opts:
                      :perm: 384
                    delegate_dc_obj: *1
                  original_filename: test_image.jpg
                  content_type: image/jpeg
                  headers: "Content-Disposition: form-data; name=\"look_media_attachment[image]\";
                    filename=\"test_image.jpg\"\r\nContent-Type: image/jpeg\r\nContent-Length:
                    74165\r\n"
      # ...

Investigating a bit...

I think that the problem is here: schema_builder.rb:161
In my example value includes another level look_media_attachment:

{"look_media_attachment"=>
  {"visible"=>"true",
   "caption"=>"This is the caption text",
   "image"=>
    #<ActionDispatch::Http::UploadedFile:0x00007ff2b647c088
     @content_type="image/jpeg",
     @headers="Content-Disposition: form-data; name=\"look_media_attachment[image]\"; filename=\"test_image.jpg\"\r\nContent-Type: image/jpeg\r\nContent-Length: 74165\r\n",
     @original_filename="test_image.jpg",
     @tempfile=#<File:/var/folders/vm/jtxmp1t120b57cfnv0q322kr0000gn/T/RackMultipart20220326-18892-dzbg74.jpg (closed)>>}}

Perhaps in build_example the value hash should be traversed recursively.

@k0kubun
Copy link
Collaborator

k0kubun commented Mar 26, 2022

Same as #44 (comment); could you try preparing a repository that reproduces the issue?

@blocknotes
Copy link
Contributor Author

blocknotes commented Mar 26, 2022

Ok, here it is: https://github.com/blocknotes/rspec-openapi/commit/7e357dbd87ddf60380a64f479a711ddaa89a0630

Reproduced with:

OPENAPI=1 bundle exec rspec spec/requests/rails_spec.rb

grep 'ruby/object' spec/rails/doc/openapi.yaml
# data: !ruby/object:ActionDispatch::Http::UploadedFile
#   tempfile: !ruby/object:Tempfile
#     tmpfile: &1 !ruby/object:File {}

@blocknotes blocknotes changed the title Issue with attachments Issue with nested file uploads Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants