jhthorsen / mojolicious-plugin-openapi

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

v4.xx generates invalid OpenAPI spec #215

Closed augensalat closed 2 years ago

augensalat commented 3 years ago

This is a serious bug IMHO, but also a very weird one.

Run the following example code morbo echo.pl.

use Mojolicious::Lite;

get "/echo/:id" => sub {
  my $c = shift->openapi->valid_input or return;
  my $id = $c->param('id');

  $c->render(openapi => {id => $id});
}, "get_echo";

# Load specification and start web server
plugin OpenAPI => {url => "data:///spec.yml", schema => 'v3'};
app->start;

__DATA__
@@ spec.yml
openapi: 3.0.0

info:
  version: "0"
  title: Demo

servers:
  - url: /api

paths:
  "/echo/{id}":
    get:
      parameters:
        - $ref: "#/components/parameters/idParam"
      responses:
        "200":
          description: Echo response
          content:
            "*/*":
              schema:
                type: object

components:
  parameters:
    idParam:
      name: id
      in: path
      required: true
      description: An id
      schema:
        type: integer
        format: int32
        minimum: 1
      example: 1

Open http://127.0.0.1:3000/api . In the JSON output you will find components.parameters.idParam.type = "integer". This is wrong and it does not come from the spec above, which has components.parameters.idParam.schema.type = "integer" (which is also in the JSON output).

After some hours of trial & error I found the easy "fix": You have to add paths."/echo/{id}".get.operationId: get_echo. But this kind of fix is not an option with a non-Lite app, and it is also not a comprehensable one.

As the subject says: this started with 4.00.

jhthorsen commented 2 years ago

This will be fixed in the upcoming 5.x release of JSON::Validator