jhthorsen / mojolicious-plugin-openapi

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

SpecRenderer produces an invalid specification #199

Closed christopherraa closed 3 years ago

christopherraa commented 3 years ago

What is happening?

If you give a valid specification to Mojolicious::Plugin::OpenAPI it loads it just fine. Something is also rendered on /$basePath, but neither is it the same spec nor is it a valid spec. When trying to consume that spec with eg OpenAPI::Client the following errors are thrown:

Invalid schema: http://localhost:3000/v1/api has the following errors:
/definitions/spec_yaml-responses_bad_request-3a54f638a7: Properties not allowed: schema.
/definitions/spec_yaml-responses_forbidden-3a54f638a7: Properties not allowed: schema.
/definitions/spec_yaml-responses_internal_server_error-3a54f638a7: Properties not allowed: schema.
/paths/~1silly/post/parameters/0: /oneOf/0/oneOf/0 Properties not allowed: accepts, type.
/paths/~1silly/post/parameters/0: /oneOf/0/oneOf/1/oneOf/0 Properties not allowed: accepts, schema.
/paths/~1silly/post/parameters/0: /oneOf/0/oneOf/1/oneOf/1 Properties not allowed: accepts, schema.
/paths/~1silly/post/parameters/0: /oneOf/0/oneOf/1/oneOf/2 Properties not allowed: accepts, schema.
/paths/~1silly/post/parameters/0: /oneOf/0/oneOf/1/oneOf/3 Properties not allowed: accepts, schema.
/paths/~1silly/post/parameters/0: /oneOf/1 Properties not allowed: accepts, description, in, name, required, schema, type. at ./local/lib/perl5/OpenAPI/Client.pm line 67.

What is expected behaviour?

I expect that SpecRenderer will render the specification, or at least some kind of valid specification.

Steps to reproduce

Hilarity ensues.

Versions

jhthorsen commented 3 years ago

I think the test below illustrates the same issue, which is that all refs in an OpenAPIv2 schema is placed under "definitions" instead of both "definitions" and "responses".

use Mojo::Base -strict;
use JSON::Validator::Schema::OpenAPIv2;
use Test::More;

my $schema = JSON::Validator::Schema::OpenAPIv2->new('data://main/spec.yaml');
is_deeply $schema->errors, [], 'valid spec' or diag explain($schema->errors);

my $bundled = $schema->bundle;
is_deeply $bundled->errors, [], 'valid bundled spec' or diag explain($bundled->errors);
ok $bundled->get('/definitions/error_response'), 'error_response in definitions';
ok $bundled->get('/parameters/error_response'),  'error_response in parameters';
ok $bundled->get('/responses/bad_request'),      'bad_request in responses';

done_testing;

__DATA__
@@ spec.yaml
---
swagger: "2.0"
info:
  title: Silly API
  version: 0.0.1
basePath: /v1/api
paths:
  /silly:
    post:
      summary: Create a new silly thing
      description: Create a new kind of silly thing. Don't know how.
      parameters:
        - $ref: "#/parameters/silly_body"
      responses:
        400:
          $ref: "#/responses/bad_request"

definitions:
  error_response:
    type: object
    required:
      - status
      - message
    properties:
      status:
        type: integer
        format: int32
      message:
        type: string

parameters:
  silly_body:
    in: body
    name: body
    description: Payload with some silly stuff
    required: true
    schema:
      $ref: "#/definitions/error_response"

responses:
  bad_request:
    description: Doing bad things.
    schema:
      $ref: "#/definitions/error_response"

This a JSON::Validator bug, but I'll keep this issue open as reference.

jhthorsen commented 3 years ago

I think this has been fixed in https://github.com/jhthorsen/json-validator/commit/8b451ba4adc4420fb80d77709163649f7b451d61

Going to make a new release shortly that depend on JSON::Validator 4.16.