jhthorsen / mojolicious-plugin-openapi

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

Response-validation fails when using $ref for responses #248

Closed localh0rst closed 3 months ago

localh0rst commented 10 months ago

When using $ref in the response for i.e. a get request, the response is not validated. POC:

#!/usr/bin/env perl
use v5.24;
use warnings;
use Mojolicious::Lite -signatures;

get "/test" => sub ($c) {
   $c->openapi->valid_input or return;
   $c->render(
      status  => 200,
      # Should be invalid because prop2 is missing?
      openapi => {prop1 => 200, different => 'WTF'}
   );

  },
  'test';

plugin OpenAPI => {url => "data:///spec.yaml"};
app->start;

__DATA__
@@ spec.yaml
openapi: 3.0.0
info:
  title: Response validation test
  version: 1.0.0
paths:
  /test:
    get:
      x-mojo-name: test
      responses:
        200:
          $ref: '#/components/responses/should_be_validated'
components:
  schemas:
    test_schema:
      type: object
      required: [prop1, prop2]
      properties:
        prop1:
          type: integer
        prop2:
          type: string
  responses:
    should_be_validated:
      description: schema should be validated
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/test_schema'

Maybe i am missicng something?

jhthorsen commented 3 months ago

This sounds related to #251.