Skip to content

Commit fa69e8f

Browse files
include the intended type(s) in the error message when deserialization fails
1 parent e3f3d89 commit fa69e8f

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

lib/OpenAPI/Modern.pm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,14 +850,16 @@ sub _validate_query_parameter ($self, $state, $param_obj, $uri) {
850850
my @types = $self->_type_in_schema($param_obj->{schema}, { %$state, keyword_path => $state->{keyword_path}.'/schema' });
851851

852852
if (any { $_ eq 'string' || $_ eq 'number' || $_ eq 'boolean' || $_ eq 'null' } @types) {
853-
return E($state, 'cannot deserialize to %s type%s', 'requested', @types > 1 ? 's' : '')
853+
return E($state,
854+
'cannot deserialize to %s type%s%s', !@types ? 'any' : 'requested', @types > 1 ? 's' : '',
855+
@types ? ' ('.join(', ', @types).')' : '')
854856
if not coerce_primitive(\$data, \@types);
855857
}
856858
elsif (any { $_ eq 'array' || $_ eq 'object' } @types) {
857859
return E($state, 'deserializing query parameters to arrays or objects is not currently supported');
858860
}
859861
else {
860-
return E($state, 'cannot deserialize to %s type%s', !@types ? 'any' : 'requested', @types > 1 ? 's' : '');
862+
return E($state, 'cannot deserialize to any types');
861863
}
862864

863865
$self->_evaluate_subschema(\$data, $param_obj->{schema},
@@ -1116,7 +1118,8 @@ sub _deserialize_style ($self, $data, $state, %opt) {
11161118
}
11171119

11181120
return E({ %$state, data_path => jsonp($state->{data_path}, $name) },
1119-
'cannot deserialize to %s type%s', !@types ? 'any' : 'requested', @types > 1 ? 's' : '');
1121+
'cannot deserialize to %s type%s%s', !@types ? 'any' : 'requested', @types > 1 ? 's' : '',
1122+
@types ? ' ('.join(', ', @types).')' : '');
11201123
}
11211124

11221125
sub _validate_parameter_content ($self, $state, $param_obj, $content_ref) {

t/parameters.t

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ subtest 'path parameters' => sub {
199199
instanceLocation => '/request/uri/path/color',
200200
keywordLocation => $keyword_path,
201201
absoluteKeywordLocation => $openapi->openapi_uri.'#'.$keyword_path,
202-
error => 'cannot deserialize to requested type',
202+
error => 'cannot deserialize to requested type (number)',
203203
},
204204
],
205205
},
@@ -264,7 +264,7 @@ subtest 'path parameters' => sub {
264264
instanceLocation => '/request/uri/path/color',
265265
keywordLocation => $keyword_path,
266266
absoluteKeywordLocation => $openapi->openapi_uri.'#'.$keyword_path,
267-
error => 'cannot deserialize to requested type',
267+
error => 'cannot deserialize to requested type (object)',
268268
},
269269
],
270270
},
@@ -548,7 +548,7 @@ subtest 'path parameters' => sub {
548548
instanceLocation => '/request/uri/path/color',
549549
keywordLocation => $keyword_path,
550550
absoluteKeywordLocation => $openapi->openapi_uri.'#'.$keyword_path,
551-
error => 'cannot deserialize to requested type',
551+
error => 'cannot deserialize to requested type (object)',
552552
},
553553
],
554554
},
@@ -749,7 +749,7 @@ subtest 'path parameters' => sub {
749749
instanceLocation => '/request/uri/path/color',
750750
keywordLocation => $keyword_path,
751751
absoluteKeywordLocation => $openapi->openapi_uri.'#'.$keyword_path,
752-
error => 'cannot deserialize to requested type',
752+
error => 'cannot deserialize to requested type (object)',
753753
},
754754
],
755755
},
@@ -1260,7 +1260,7 @@ subtest 'header parameters' => sub {
12601260
instanceLocation => '/response/header/My-Header',
12611261
keywordLocation => $keyword_path,
12621262
absoluteKeywordLocation => $openapi->openapi_uri.'#'.$keyword_path,
1263-
error => 'cannot deserialize to requested type',
1263+
error => 'cannot deserialize to requested type (object)',
12641264
},
12651265
],
12661266
},

t/validate_request.t

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -766,37 +766,37 @@ YAML
766766
instanceLocation => '/request/uri/query/null_query',
767767
keywordLocation => jsonp(qw(/paths /foo/{null_path}/{boolean_path} get parameters 0)),
768768
absoluteKeywordLocation => $doc_uri->clone->fragment(jsonp(qw(/paths /foo/{null_path}/{boolean_path} get parameters 0)))->to_string,
769-
error => 'cannot deserialize to requested type',
769+
error => 'cannot deserialize to requested type (null)',
770770
},
771771
{
772772
instanceLocation => '/request/uri/query/boolean_query',
773773
keywordLocation => jsonp(qw(/paths /foo/{null_path}/{boolean_path} get parameters 1)),
774774
absoluteKeywordLocation => $doc_uri->clone->fragment(jsonp(qw(/paths /foo/{null_path}/{boolean_path} get parameters 1)))->to_string,
775-
error => 'cannot deserialize to requested type',
775+
error => 'cannot deserialize to requested type (boolean)',
776776
},
777777
{
778778
instanceLocation => '/request/header/NullHeader',
779779
keywordLocation => jsonp(qw(/paths /foo/{null_path}/{boolean_path} get parameters 2)),
780780
absoluteKeywordLocation => $doc_uri->clone->fragment(jsonp(qw(/paths /foo/{null_path}/{boolean_path} get parameters 2)))->to_string,
781-
error => 'cannot deserialize to requested type',
781+
error => 'cannot deserialize to requested type (null)',
782782
},
783783
{
784784
instanceLocation => '/request/header/BooleanHeader',
785785
keywordLocation => jsonp(qw(/paths /foo/{null_path}/{boolean_path} get parameters 3)),
786786
absoluteKeywordLocation => $doc_uri->clone->fragment(jsonp(qw(/paths /foo/{null_path}/{boolean_path} get parameters 3)))->to_string,
787-
error => 'cannot deserialize to requested type',
787+
error => 'cannot deserialize to requested type (boolean)',
788788
},
789789
{
790790
instanceLocation => '/request/uri/path/null_path',
791791
keywordLocation => jsonp(qw(/paths /foo/{null_path}/{boolean_path} parameters 0)),
792792
absoluteKeywordLocation => $doc_uri->clone->fragment(jsonp(qw(/paths /foo/{null_path}/{boolean_path} parameters 0)))->to_string,
793-
error => 'cannot deserialize to requested type',
793+
error => 'cannot deserialize to requested type (null)',
794794
},
795795
{
796796
instanceLocation => '/request/uri/path/boolean_path',
797797
keywordLocation => jsonp(qw(/paths /foo/{null_path}/{boolean_path} parameters 1)),
798798
absoluteKeywordLocation => $doc_uri->clone->fragment(jsonp(qw(/paths /foo/{null_path}/{boolean_path} parameters 1)))->to_string,
799-
error => 'cannot deserialize to requested type',
799+
error => 'cannot deserialize to requested type (boolean)',
800800
},
801801
],
802802
},

0 commit comments

Comments
 (0)