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

how make upload request #53

Closed
thg303 opened this issue Apr 12, 2017 · 7 comments
Closed

how make upload request #53

thg303 opened this issue Apr 12, 2017 · 7 comments

Comments

@thg303
Copy link
Contributor

thg303 commented Apr 12, 2017

I tried this code to document an upload via put method, but it simply breaks. am I doing something wrong?

     put 'update a puzzle details' do
      consumes: 'multipart/form-data'
      parameter name: 'Authorization', in: :header, type: :string, required: true
      parameter name: 'Content-Type', in: :header, type: :string, required: true
      parameter name: 'puzzle_id', in: :path, type: :string, required: true
      parameter name: 'puzzle[kind]', in: :formData, type: :string, enum: %w(freezed motioning), required: false
      parameter name: 'puzzle[picture]', in: :formData, type: :file, required: false
      response 200, 'successful' do
        let(:puzzle) { create :puzzle }
        let(:Authorization) { "Bearer #{get_token(puzzle.user)}"}
        let(:'Content-Type') { 'multipart/form-data' }
        let(:puzzle_id) { puzzle.id }
        let(:'puzzle[kind]') { 'freezed' }
        let(:'puzzle[picture]') { uploaded_file }
        run_test!
      end
    end
@thg303
Copy link
Contributor Author

thg303 commented Apr 12, 2017

mm.. looks like :formData is not implemented

@raydog153
Copy link

Do you have a typo? for the parameter of type file you have in as 'formDate' and not 'formData'. I'm still trying to figure out how to set header and host for the request.

@thg303
Copy link
Contributor Author

thg303 commented Apr 13, 2017

@raydog153 oh yeah, that was a typo. but it does not work anyway.
for the header you may use my code, that works for simple queries.

@domaindrivendev
Copy link
Collaborator

@thg303 - you're right form data not supported. I think it could be trivial enough - fancy making a PR?

@pifleo
Copy link

pifleo commented Apr 14, 2017

Hello,

We had the same problem today and found out it was not implemented. We didn't manage to upload a file with :body.

So here is our patch to get a working file upload with :formData and type: :file:

# rswag-specs/lib/rswag/specs/request_factory.rb

module Rswag
  module Specs
    class RequestFactory

      # ...

      def build_body(example)
        if parameters_in(:formData).present?
          build_form_data(example)
        else
          body_parameter = parameters_in(:body).first
          body_parameter.nil? ? '' : example.send(body_parameter[:name]).to_json
        end
      end

      private

      def build_form_data_value(param, example)
        param[:type] == :file ? example.send(param[:name]) : example.send(param[:name]).to_s
      end

      def build_form_data(example)
        Hash[ parameters_in(:formData).map { |p| [ p[:name], build_form_data_value(p, example) ] } ]
      end

      # ...
    end
  end
end

@thg303
Copy link
Contributor Author

thg303 commented Apr 14, 2017

did it, @domaindrivendev mind to check my PR?

@domaindrivendev
Copy link
Collaborator

Should be fixed by #54

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

No branches or pull requests

4 participants