jhthorsen / mojolicious-plugin-openapi

OpenAPI / Swagger plugin for Mojolicious
53 stars 41 forks source link

Invalid Content-Type error with empty, optional body in V3 #170

Closed plk closed 3 years ago

plk commented 4 years ago

I am not sure about this but this looks like it might be a bug. In the example below, there is an optional body but if no Content-Type header is passed, it gives an "Invalid Content-Type" error in validation. Should the Content-Type header have to exist if there is no content?

use v5.24;
use Mojo::Base -strict;
use Mojolicious::Lite;

post '/test' => sub {
  my $c = shift->openapi->valid_input or return;
  $c->render(status => 200, openapi => "test");
  },
  'File';

plugin OpenAPI => {schema => 'v3', url => 'data://main/file.yaml'};

my $ua = Mojo::UserAgent->new;
my $res = $ua->post("/v1/test?paramone=someparam" => {Accept => 'application/json'})->res;

package main;
__DATA__
@@ file.yaml
openapi: 3.0.0
info:
  title: Title
  description: Description
  version: 1.0.0
servers:
  - url: 'https://server1.com/v1'
paths:
  '/test':
    post:
      operationId: "File"
      parameters:
        - name: paramone
          in: query
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Test"
      responses:
        default:
          description: something
          content:
            application/json:
              schema:
                type: string
components:
  schemas:
    Test:
      type: object
      required:
        - username
      properties:
        username:
          type: string
jhthorsen commented 3 years ago

I'm not sure to be honest. Can you try to look up to see what the specification says?

HEM42 commented 3 years ago

According to the spec I think its fine to assume that the requestBody is optional unless required is set to true; https://swagger.io/specification/#request-body-object

So it fair to assume that if no requestBody is given in the request, it just not be validated

jhthorsen commented 3 years ago

This should now be resolved by @potatogim 👍