jhthorsen / mojolicious-plugin-openapi

OpenAPI / Swagger plugin for Mojolicious

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parameter in `body` no longer supported under v2.x?

christopherraa opened this issue · comments

Given a route that looks something like this:

/echo:
  post:
    summary: Echo message
    description: Echo message back to user
    operationId: echo
    x-mojo-to:
      controller: Echo
      action: echo
    parameters:
      - name: echo
        in: body
        description: Text that should be echoed
        schema:
          type: "string"
    responses:
      200:
        $ref: "#/responses/response200"

should it not be expected that one could send arbitrary text data in the body of a request and have that go through to the controller method? Currently this is rendered to the client when calling $c->openapi->valid_input:

{
    "errors":   [
        {
            "message": "Expected string - got null.",
            "path": "/echo"
        }
    ],
    "status": 400
}

According to documentation on swagger.io:

Note: The name of the body parameter is ignored. It is used for documentation purposes only.

Based on that I have concluded that naming should not be an issue here.

I know that the above structure used to work, as the reason we discovered this issue was by running our test suite against the newest version of everything. Tested now using:

  • Mojolicious: 9.19
  • Mojolicious::Plugin::OpenAPI: 4.04
  • JSON::Validator: 4.20

Is this currently expected behaviour?

Here is an example that demonstrates the issue:

#!/usr/bin/env perl
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
use Mojolicious::Lite;

post '/echo' => sub {
	my $c = shift->openapi->valid_input or return;
	$c->render(text => $c->req->body);
}, 'echo';

plugin('OpenAPI' => {url => "data:///spec.json"});

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

$t->post_ok('/v1/echo' => 'Echo echo!')->status_is(200)->content_is('Echo echo!');

done_testing();


__DATA__
@@ spec.json
{
  "swagger": "2.0",
  "info": {
    "version": "0.0.1",
    "title": "Echo API"
  },
  "basePath": "/v1",
  "paths": {
    "/echo": {
      "post": {
        "summary": "Echo the thing",
        "description": "This endpoint echos the thing",
        "operationId": "echo",
        "x-mojo-name": "echo",
        "consumes": [
          "text/plain"
        ],
        "parameters": [
          {
            "name": "echodata",
            "in": "body",
            "description": "Payload that should be echoed",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "$ref": "#/responses/response200_ok"
          }
        }
      }
    }
  },
  "responses": {
    "response200_ok": {
      "description": "Successful response",
      "schema": {
        "type": "string"
      }
    }
  }
}

This used to work just fine and is according to my reading of the swagger docs perfectly valid. If running with these versions the above tests succeed:

  • Mojolicious@8.73
  • JSON::Validator@4.13
  • Mojolicious::Plugin::OpenAPI@3.41

I have not checked at which point the breakage was introduced.

Update: Tested running the tests for every version of JSON::Validator from 4.13 to 4.20. All succeed. Assuming the problem is in M::P::OpenAPI. Will do some testing there as well.

Verified now that it breaks in Mojolicious::Plugin::OpenAPI@4.00. So at least we know when the bug was introduced.

That was a rather bad regression 😞 It should be fixed in 4.05.