Skip to content

Commit

Permalink
Avoid overwriting responses with $ref, closes jhthorsen/mojolicious-p…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Nov 20, 2021
1 parent 18eeb2d commit db4d968
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/JSON/Validator/Schema/OpenAPIv3.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ sub add_default_response {
for my $route ($self->routes->each) {
my $op = $self->get([paths => @$route{qw(path method)}]);
for my $status (@{$params->{status}}) {
$op->{responses}{$status}{description} //= $params->{description};
next if $self->get(['paths', @$route{qw(path method)}, 'responses', $status]);
$op->{responses}{$status}{content}{'application/json'} //= {schema => $ref};
$op->{responses}{$status}{description} //= $params->{description};
}
}

Expand Down
32 changes: 21 additions & 11 deletions t/openapiv3-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ subtest 'basic' => sub {
};

subtest base_url => sub {
is $schema->base_url, 'http://petstore.swagger.io/v1', 'get';
is $schema->base_url('https://api.example.com:8080/api'), $schema, 'set url';
is $schema->get('/servers/0/url'), 'https://api.example.com:8080/api', 'servers changed';
is $schema->base_url, 'http://petstore.swagger.io/v1', 'get';
is $schema->base_url('https://api.example.com:8080/api'), $schema, 'set url';
is $schema->get('/servers/0/url'), 'https://api.example.com:8080/api', 'servers changed';

is $schema->base_url(Mojo::URL->new('//api2.example.com')), $schema, 'set without scheme';
is $schema->get('/servers/0/url'), 'https://api2.example.com', 'servers changed';
is $schema->base_url(Mojo::URL->new('//api2.example.com')), $schema, 'set without scheme';
is $schema->get('/servers/0/url'), 'https://api2.example.com', 'servers changed';

is $schema->base_url(Mojo::URL->new('/v1')), $schema, 'set path';
is $schema->base_url->to_string, 'https://api2.example.com/v1', 'get';
is $schema->base_url(Mojo::URL->new('/v1')), $schema, 'set path';
is $schema->base_url->to_string, 'https://api2.example.com/v1', 'get';
};

subtest 'parameters_for_request' => sub {
Expand Down Expand Up @@ -143,8 +143,8 @@ subtest 'validate_response - content_type' => sub {
};

subtest add_default_response => sub {
$schema = JSON::Validator->new->schema($cwd->child(qw(spec v3-petstore.json)))->schema;
ok !$schema->get('/components/schemas/DefaultResponse'), 'default response missing';
my $schema = JSON::Validator->new->schema($cwd->child(qw(spec v3-petstore.json)))->schema;
ok !$schema->get('/components/schemas/DefaultResponse'), 'default response missing';
ok !$schema->get([paths => '/petss', 'get', 'responses', '400']), 'default response missing for 400';
$schema->add_default_response;
ok $schema->get('/components/schemas/DefaultResponse'), 'default response added';
Expand All @@ -157,10 +157,20 @@ subtest add_default_response => sub {
is_deeply $schema->errors, [], 'errors';
};

subtest 'add_default_response do not overwrite $ref' => sub {
my $schema = JSON::Validator->new->schema($cwd->child(qw(spec v3-default-response-extra.yaml)))->schema;
$schema->add_default_response;
is $schema->get([qw(paths /item/{id} get summary)]), 'get a single item', 'summary';
is $schema->get([qw(paths /item/{id} get responses 200 content application/json schema type)]), 'object',
'responses 200';
is $schema->get([qw(paths /item/{id} get responses 404 description)]), 'Custom 404', 'responses 404';
is $schema->get([qw(paths /item/{id} get responses 500 description)]), 'Custom 500', 'responses 500';
};

subtest 'v3.1.x' => sub {
my $schema = JSON::Validator->new->schema({openapi => '3.1.0', paths => {}})->schema;
is $schema->specification, 'https://spec.openapis.org/oas/3.1/schema/2021-05-20', 'specification';
is join(', ', @{$schema->errors}), '/info: Missing property.', 'errors';
is $schema->specification, 'https://spec.openapis.org/oas/3.1/schema/2021-05-20', 'specification';
is join(', ', @{$schema->errors}), '/info: Missing property.', 'errors';
};

subtest 'coerce defaults' => sub {
Expand Down
50 changes: 50 additions & 0 deletions t/spec/v3-default-response-extra.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: 3.0.0
info:
title: v3-default-response-extra
version: 0.0.1
components:
schemas:
base:
type: object
required: [status, reason]
properties:
status:
type: integer
reason:
type: string
not_found:
type: object
allOf:
- $ref: '#/components/schemas/base'
exception:
type: object
allOf:
- $ref: '#/components/schemas/base'
responses:
'404':
description: Custom 404
content:
application/json:
schema:
$ref: '#/components/schemas/not_found'
paths:
/item/{id}:
get:
summary: get a single item
description: get a single item from the database
x-mojo-name: item
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/base'
'404':
$ref: '#/components/responses/404'
'500':
description: Custom 500
content:
application/json:
schema:
$ref: '#/components/schemas/exception'

0 comments on commit db4d968

Please sign in to comment.