jhthorsen / mojolicious-plugin-openapi

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

Return req body instead of json #175

Closed shubhaguha closed 4 years ago

shubhaguha commented 4 years ago

In OpenAPI 3.0, you can specify any content type for the request body, not just JSON.

https://swagger.io/docs/specification/describing-request-body/

The requestBody is more flexible in that it lets you consume different media types, such as JSON, XML, form data, plain text, and others, and use different schemas for different media types.

jhthorsen commented 4 years ago

The same goes for OpenAPIv2. Unfortunately this PR breaks many tests and it also does not have any new tests, so I cannot accept it. I hope to address this in the new API I'm working on in #160 and json-validator#189.

Can you say a bit more about why you need this change? Maybe there's another way to solve this with the current functionality.

shubhaguha commented 4 years ago

The same goes for OpenAPIv2. Unfortunately this PR breaks many tests and it also does not have any new tests, so I cannot accept it. I hope to address this in the new API I'm working on in #160 and json-validator#189.

Can you say a bit more about why you need this change? Maybe there's another way to solve this with the current functionality.

Sorry for the delayed response. I haven't had a chance to dive deeper into the test failures yet and somehow your comment didn't appear to me until just now.

The reason is that I need to implement an API endpoint that takes a file in the request body. The file can be one of many types (text, xml, json, pdf, etc.) which in OpenAPI 3.0 can be denoted like so:

      requestBody:
        content:
          */*:
            schema:
              type: string
              format: binary

But with the latest version of this plugin, my application invalidates the request by trying to force this raw body to json.

shubhaguha commented 4 years ago

Currently, my workaround is in the controller sub:

    sub process {
        my $c = shift();
        $c->req()->{json} = $c->req()->body();
        my $self = $c->openapi()->valid_input() or return;

EDIT: But even this workaround does not fully work because the request body seems to be stripped of all newline characters. I have not yet tracked down where in the code this happens.

Another OpenAPI 3 reference, about how to specify file types: https://swagger.io/docs/specification/data-models/data-types/#file

jhthorsen commented 4 years ago

I'm closing this PR, since no new tests or code has been provided.