jhthorsen / mojolicious-plugin-openapi

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

Can't have a JSON reference to a property of a schema #197

Closed yoe closed 3 years ago

yoe commented 4 years ago

When trying to run the following:

#!/usr/bin/env perl

use Mojolicious::Lite;

get '/' => sub ($) {
  my $c = shift;
  $c->render(template => 'index');
};

plugin OpenAPI => { url => "data:///test.yml", schema => "v3"};

app->start;
__DATA__

@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Welcome to the Mojolicious real-time web framework!</h1>

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head><title><%= title %></title></head>
  <body><%= content %></body>
</html>

@@ test.yml
openapi: 3.0.1
info:
  title: Test case
  description: test case for issue with Mojolicious::Plugin::OpenAPI
  contact:
    email: w@uter.be
  version: 1.0.0
paths:
  /foo:
    post:
      summary: add foo
      operationId: add_foo
      requestBody:
        description: body for foo
        content:
          schema:
            application/json:
              $ref: '#/components/schemas/Foo'
        required: true
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
  /foo/{fooId}:
    get:
      summary: retrieve a given foo
      operationId: get_foo
      parameters:
      - name: fooId
        in: path
        required: true
        schema:
          $ref: '#/components/schemas/Foo/properties/id'
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
        404:
          description: foo not found
          content: {}
components:
  schemas:
    Foo:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        startdate:
          type: string
          format: date

the output is:

wouter@pc181009:~/mvce$ ./myapp.pl 
Can't use string ("#/components/schemas/Foo/propert"...) as a HASH ref while "strict refs" in use at /usr/share/perl5/JSON/Validator/OpenAPI/Mojolicious.pm line 333.

With previous versions of Mojolicious::Plugin::OpenAPI, this would just work, so this is a regression.

Posting the YAML file into the swagger.io editor validates it just fine.

nige123 commented 3 years ago

I'm encountering the same problem. Did you manage to get it working?

jhthorsen commented 3 years ago

This is not correct:

content:
  schema:
    application/json:

It should be:

content:
  application/json:
    schema:

https://swagger.io/specification/

jhthorsen commented 3 years ago

Next time, please provide a smaller failing case.