jhthorsen / mojolicious-plugin-openapi

OpenAPI / Swagger plugin for Mojolicious
54 stars 42 forks source link

Validation of multipart/form for file upload in V3 #171

Closed plk closed 4 years ago

plk commented 4 years ago

I have a spec with a standard multipart/form body for a file upload:

      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                files:
                  description: something
                  type: string
                  format: binary
              required:
                - files

In V2, this was:

consumes:
        - multipart/form-data
 parameters:
    - name: files
       in: formData
        required: true
        description: something
        type: file

The same client call:

$ua->post($url => { form => {files => {file => '/some/file.zip'}});

works fine with the V2 spec but fails in valid_input with the V3 spec:

[{"message":"Missing property.","path":"\/body\/files"}]

I see the file asset in the request on the server:

                          parts           => [
                                               bless({
                                                 asset        => bless({
                                                                   auto_upgrade => 1,
                                                                   content => "PK\3........

Is this an issue withe validation in the plugin or am I missing something here?

jhthorsen commented 4 years ago

I'm not super happy with https://github.com/jhthorsen/mojolicious-plugin-openapi/commit/b7e0113a96290e9ee1d015329978b0d25f12d387#diff-9a6a9327b16cdf365665485886135c77R286. Will hopefully make a better rewrite after #160.

tigrankhachikyan commented 7 months ago

Hi, I'm still facing an issue with multipart/form-data file parameter validation (required).

I've created a testcase to reproduce it based on a test file from the Plugin repo (t/v3-file.t), see below.

In short, schema expects parameter in form data named image, which is missed, and I'm expected to get error response, like message => 'Missing property.', path => '/body/image'

Please let me know if I should open a new issue.

use Mojo::Base -strict;
use Test::Mojo;
use Test::More;

use Mojolicious::Lite;
post '/upload' => sub {
  my $c = shift->openapi->valid_input or return;
  $c->render(openapi => {size => $c->req->upload('image')->size});
  },
  'upload';

plugin OpenAPI => {url => 'data://main/openapi.yaml'};

my $t = Test::Mojo->new;

my $image = Mojo::Asset::Memory->new->add_chunk('smileyface');
$t->post_ok('/api/upload', form => {imageParam => {file => $image}})->status_is(400)
  ->json_is('/errors/0', {message => 'Missing property.', path => '/body/image'});

$t->post_ok(
  '/api/upload',
  {Accept => 'application/json'},
  form => {id => 1, image => {file => $image}}
)->content_like(qr{"size"})->status_is(200);

done_testing;

__DATA__
@@ openapi.yaml
---
openapi: 3.0.0
info:
  title: Upload test
  version: 1.0.0
servers:
- url: http://example.com/api
paths:
  /upload:
    post:
      operationId: upload
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [ image ]
              properties:
                image:
                  type: string
                  format: binary
      responses:
        200:
          description: Accepted
          content:
            application/json:
              schema:
                required: [ size ]
                properties:
                  size:
                    type: integer

Mojolicious::Plugin::OpenAPI - 5.09