Closed skaurus closed 4 years ago
I forgot to add that requests are validated with $self->openapi->validate
, and while with the old versions this code returned some errors, with new versions I get an empty list.
With Mojolicious 7.94, Plugin::OpenAPI 1.30 and JSON::Validator 2.18 test runs fine.
Hello @skaurus.
I got some questions/comments:
Hello @jhthorsen,
I'm trying to make a test case. it turned out to be harder to replicate than I thought. Also, I don't have a lot of time for it, but I think I'll make it. Thanks for the test example!
Thanks @elcamlost!
@skaurus: I'll leave it open for now, but hoping you won't be able to make a failing test, hehe
I found the reason. We call $self->openapi->validate
in under
callback, so request isn't routed yet, so $c->stash('openapi.path')
is empty, so local $op_spec->{parameters}
in _helper_validate
is empty.
The test case is coming a bit later.
Here it is.
What makes it work on older versions is that little hack with openapi.parameters
.
Is there more idiomatic way of checking errors in under
callbacks, which is unlikely to break with updates?
use Mojo::Base -strict;
use Test::Mojo;
use Test::More;
use Mojolicious::Lite;
my $under = sub {
my $c = shift;
my $spec_parameters = $c->match->endpoint->pattern->defaults->{'openapi.parameters'};
$c->stash('openapi.parameters' => $spec_parameters);
my @errors = $c->openapi->validate;
return @errors ? 0 : 1;
};
post '/foo' => sub {
my $c = shift;
$c->render(openapi => { id => $c->param('rgid') });
}, 'allOf';
plugin OpenAPI => {
url => "data://main/path-parameters-allof.yml",
route => app->routes->under('/api/allOf')->to('cb' => $under),
};
my $t = Test::Mojo->new;
$t->post_ok('/api/allOf/foo' => json => { 'url' => 'https://google.com' })->status_is(400);
done_testing;
__DATA__
@@ path-parameters-allof.yml
swagger: '2.0'
basePath: "/api/allOf"
info:
title: allOf behaviour
version: '1'
paths:
/foo:
post:
operationId: allOf
produces:
- application/json
parameters:
- name: body
in: body
required: true
schema:
$ref: "#/definitions/view"
responses:
'200':
description: ''
'500':
description: "Internal server error"
'401':
description: "Authorization failed"
'400':
description: "Bad Request"
definitions:
genericEvent:
type: object
required:
- url
properties:
url:
type: string
format: uri
view:
type: object
allOf:
- $ref: "#/definitions/genericEvent"
- properties:
rgid:
type: string
required:
- rgid
Hack like this can make this work in new versions:
my $basePath = quotemeta($c->openapi->spec('/basePath'));
my $path = $c->match->endpoint->to_string;
$path =~ s/^$basePath//;
local $c->stash->{'openapi.path'} = $path;
Funnily enough, $c->openapi
and $c->match->endpoint
do work with not-quite-routed-yet request
I don't think this is a JSON::Validator issue anymore.
Please open a new (cleaned up) issue in https://github.com/jhthorsen/mojolicious-plugin-openapi/issues/.
I have a schema like this (important parts here are
view
path andview
definition, the rest is provided for the sake of completeness):And I have some tests. Tests try to use
view
path withoutrgid
parameter and expect to get 400 status code. It was indeed so with Mojolicious 7.46, Plugin::OpenAPI 1.26 and JSON::Validator 2.05. After updating code to 8.17, 2.14 and 3.11 this test now gets status 202 and fails.I'm probably missing something obvious here, could you help?