| 1 | package PAGI::FastAPI::ResponseModel; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use version; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = qv('v1.7.3'); | |||||||||||||||||||||||||||||||
| 7 | 2 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 2 | our @EXPORT_OK = qw(with_response_model); | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | # There is no response_model hook in route registration itself (only 'query' | ||||||||||||||||||||||||||||||||
| 16 | # and 'body' are validated), so this isn't a route option the way 'query'/ | ||||||||||||||||||||||||||||||||
| 17 | # 'body' are, it's a plain wrapper you apply around your own handler | ||||||||||||||||||||||||||||||||
| 18 | # coderef, following the exact schema conventions 'body' validation already | ||||||||||||||||||||||||||||||||
| 19 | # uses (a Type::Tiny object validating the whole value, OR a HashRef of | ||||||||||||||||||||||||||||||||
| 20 | # field => Type::Tiny validating per-field), so it feels consistent with the | ||||||||||||||||||||||||||||||||
| 21 | # rest of the framework rather than inventing a new schema shape. | ||||||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||||||
| 23 | # Failure semantics deliberately mirror Python FastAPI: a response failing | ||||||||||||||||||||||||||||||||
| 24 | # its own declared response_model is treated as a SERVER bug (500), not a | ||||||||||||||||||||||||||||||||
| 25 | # client error (422), the client didn't do anything wrong, your handler | ||||||||||||||||||||||||||||||||
| 26 | # returned data that doesn't match what it promised. The validation detail | ||||||||||||||||||||||||||||||||
| 27 | # is logged via warn() but not leaked to the client. | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # with_response_model($schema, $handler) -> coderef suitable for the | ||||||||||||||||||||||||||||||||
| 30 | # route's `handler` option. | ||||||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||||||
| 32 | # $schema is either: | ||||||||||||||||||||||||||||||||
| 33 | # - a Type::Tiny object: validates the ENTIRE returned value. | ||||||||||||||||||||||||||||||||
| 34 | # - a HashRef of { field => Type::Tiny }: requires the returned value to | ||||||||||||||||||||||||||||||||
| 35 | # be a HashRef, validates each declared field, and FILTERS the output | ||||||||||||||||||||||||||||||||
| 36 | # to only the declared fields (undeclared keys are silently dropped, | ||||||||||||||||||||||||||||||||
| 37 | # this is the actual point: it's what gives you Pydantic-style | ||||||||||||||||||||||||||||||||
| 38 | # response filtering, not just validation). | ||||||||||||||||||||||||||||||||
| 39 | 8 | ▶ | sub with_response_model ($schema, $handler) { | ||||||||||||||||||||||||||||||
|
Subroutine
with_response_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 40 | 8 | ▶ | return async sub ($c) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.9
| |||||||||||||||||||||||||||||||||
| 41 | 8 | my $result = await $handler->($c); | |||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | # Don't try to apply a response_model to a Response object (e.g. | ||||||||||||||||||||||||||||||||
| 44 | # PAGI::FastAPI::Response::HTML, ::SSE, or a companion ::Redirect/ | ||||||||||||||||||||||||||||||||
| 45 | # ::File), those are already a fully-formed response, not raw | ||||||||||||||||||||||||||||||||
| 46 | # data for the framework to JSON-encode, so a data schema doesn't | ||||||||||||||||||||||||||||||||
| 47 | # apply to them. | ||||||||||||||||||||||||||||||||
| 48 | 8 | ▶ | return $result if blessed($result) && $result->can('prepare_headers'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($result) and $result->can("prepare_headers")
MC/DC50%1/2
builtin::blessed($result) and $result->can("prepare_headers")
builtin::blessed($result)
$result->can("prepare_headers")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->can("prepare_headers")
| |||||||||||||||||||||||||||||||||
| 49 | 7 | ▶ | return $result if blessed($result) && $result->can('dispatch'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($result) and $result->can("dispatch")
MC/DC0%0/2
builtin::blessed($result) and $result->can("dispatch")
builtin::blessed($result)
$result->can("dispatch")
Truth table33%1/3
A and B
A= builtin::blessed($result)B= $result->can("dispatch")
| |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 7 | ▶ | if (blessed($schema) && $schema->can('validate')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($schema) and $schema->can("validate")
MC/DC50%1/2
builtin::blessed($schema) and $schema->can("validate")
builtin::blessed($schema)
$schema->can("validate")
Truth table66%2/3
A and B
A= builtin::blessed($schema)B= $schema->can("validate")
| |||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | if (my $err = $schema->validate($result)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | 1 | warn "[PAGI::FastAPI::ResponseModel] response_model validation failed: $err\n"; | |||||||||||||||||||||||||||||||
| 54 | 1 | $c->status(500); | |||||||||||||||||||||||||||||||
| 55 | 1 | return { detail => 'Internal Server Error' }; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | 1 | return $result; | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | elsif (ref $schema eq 'HASH') { | ||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | unless (ref $result eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | warn "[PAGI::FastAPI::ResponseModel] response_model expected a HashRef return value, got " | ||||||||||||||||||||||||||||||
|
Condition50%1/2
ref $result || 'a non-reference'
MC/DC0%0/1
ref $result || 'a non-reference'
ref $result
Truth table50%1/2
A
A= ref $result
| |||||||||||||||||||||||||||||||||
| 62 | . (ref($result) || 'a non-reference') . "\n"; | ||||||||||||||||||||||||||||||||
| 63 | 1 | $c->status(500); | |||||||||||||||||||||||||||||||
| 64 | 1 | return { detail => 'Internal Server Error' }; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 3 | for my $field (keys %$schema) { | |||||||||||||||||||||||||||||||
| 68 | 5 | my $type = $schema->{$field}; | |||||||||||||||||||||||||||||||
| 69 | 5 | ▶ | if (my $err = $type->validate($result->{$field})) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 1 | warn "[PAGI::FastAPI::ResponseModel] response_model field '$field' invalid: $err\n"; | |||||||||||||||||||||||||||||||
| 71 | 1 | $c->status(500); | |||||||||||||||||||||||||||||||
| 72 | 1 | return { detail => 'Internal Server Error' }; | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # Filter: only declared fields survive. This is the actual | ||||||||||||||||||||||||||||||||
| 77 | # "response_model" behaviour, not just validation, extra | ||||||||||||||||||||||||||||||||
| 78 | # fields your handler happened to include (e.g. an internal | ||||||||||||||||||||||||||||||||
| 79 | # 'password_hash' column pulled straight from a DB row) are | ||||||||||||||||||||||||||||||||
| 80 | # dropped rather than leaking to the client. | ||||||||||||||||||||||||||||||||
| 81 | 2 | return { map { $_ => $result->{$_} } keys %$schema }; | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 1 | die "with_response_model: \$schema must be a Type::Tiny object or a HashRef of field => Type::Tiny\n"; | |||||||||||||||||||||||||||||||
| 85 | 8 | }; | |||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | =encoding utf-8 | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | PAGI::FastAPI::ResponseModel - Response Shape Validation and Filtering for PAGI::FastAPI | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | Version v1.7.3 | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | use PAGI::FastAPI::ResponseModel qw(with_response_model); | ||||||||||||||||||||||||||||||||
| 101 | use Types::Standard qw(Str Int); | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # HashRef form: validates AND filters to just these fields. | ||||||||||||||||||||||||||||||||
| 104 | $app->get('/users/{id}', | ||||||||||||||||||||||||||||||||
| 105 | handler => with_response_model( | ||||||||||||||||||||||||||||||||
| 106 | { id => Int, name => Str, email => Str }, # note: no 'password_hash' here | ||||||||||||||||||||||||||||||||
| 107 | async sub ($c) { | ||||||||||||||||||||||||||||||||
| 108 | my $row = My::DB->find_user($c->path_param('id')); | ||||||||||||||||||||||||||||||||
| 109 | return $row; # even if $row also has password_hash, last_login, etc., | ||||||||||||||||||||||||||||||||
| 110 | # only id/name/email reach the client | ||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | ), | ||||||||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | # Type::Tiny object form: validates the whole return value as-is. | ||||||||||||||||||||||||||||||||
| 116 | use Types::Standard qw(ArrayRef); | ||||||||||||||||||||||||||||||||
| 117 | $app->get('/tags', | ||||||||||||||||||||||||||||||||
| 118 | handler => with_response_model( | ||||||||||||||||||||||||||||||||
| 119 | ArrayRef[Str], | ||||||||||||||||||||||||||||||||
| 120 | async sub ($c) { return My::DB->all_tags } | ||||||||||||||||||||||||||||||||
| 121 | ), | ||||||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | Python FastAPI's C<response_model> does two things: validates that a | ||||||||||||||||||||||||||||||||
| 127 | handler's return value matches a declared shape, and filters the output to | ||||||||||||||||||||||||||||||||
| 128 | only the declared fields (so accidentally returning an ORM row with extra | ||||||||||||||||||||||||||||||||
| 129 | internal columns doesn't leak them). This module reproduces both, using the | ||||||||||||||||||||||||||||||||
| 130 | exact same L<Type::Tiny>-based schema conventions | ||||||||||||||||||||||||||||||||
| 131 | L<PAGI::FastAPI>'s own C<body> validation already uses, so it should feel | ||||||||||||||||||||||||||||||||
| 132 | native rather than bolted-on. | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | B<Failure semantics:> if a handler's actual return value doesn't match its | ||||||||||||||||||||||||||||||||
| 135 | declared C<response_model>, that's treated as a server bug (HTTP 500), not | ||||||||||||||||||||||||||||||||
| 136 | a client error, mirroring Python FastAPI's C<ResponseValidationError> | ||||||||||||||||||||||||||||||||
| 137 | behavior. The specific validation failure is logged via C<warn()> | ||||||||||||||||||||||||||||||||
| 138 | server-side but not exposed in the response body, to avoid leaking internal | ||||||||||||||||||||||||||||||||
| 139 | shape details to the client. | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | Response objects (anything C<isa(PAGI::FastAPI::Response)> or implementing | ||||||||||||||||||||||||||||||||
| 142 | C<dispatch> the way SSE does) pass through untouched, a data schema doesn't | ||||||||||||||||||||||||||||||||
| 143 | apply to an already-fully-formed response. | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | =head2 C<with_response_model($schema, $handler)> | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | Returns a new coderef suitable for a route's C<handler> option. | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | C<$schema> is either: | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | =over 4 | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | =item * A L<Type::Tiny> object/constraint, validates the entire returned | ||||||||||||||||||||||||||||||||
| 156 | value against it. No filtering happens in this form since there's no set | ||||||||||||||||||||||||||||||||
| 157 | of "declared fields" to filter to. | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | =item * A HashRef of C<< field => Type::Tiny >>, requires the handler to | ||||||||||||||||||||||||||||||||
| 160 | return a HashRef, validates each declared field, and returns a I<new> | ||||||||||||||||||||||||||||||||
| 161 | HashRef containing only the declared fields. Undeclared keys in the | ||||||||||||||||||||||||||||||||
| 162 | handler's actual return value are silently dropped. | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | =back | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | =head1 CAVEATS | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | Filtering is shallow, nested HashRefs/ArrayRefs inside a field's value are | ||||||||||||||||||||||||||||||||
| 169 | passed through as-is, not recursively filtered. For a field holding a | ||||||||||||||||||||||||||||||||
| 170 | nested structure, validate/filter it explicitly inside your own handler, or | ||||||||||||||||||||||||||||||||
| 171 | pass a L<Type::Tiny> constraint (e.g. from L<Types::Standard>'s C<Dict>) | ||||||||||||||||||||||||||||||||
| 172 | that itself models the nested shape. | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | L<Type::Tiny>, L<Types::Standard> | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | =head1 REPOSITORY | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | L<https://github.com/manwar/PAGI-FastAPI> | ||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | =head1 BUGS | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>. | ||||||||||||||||||||||||||||||||
| 189 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||||||
| 190 | bug as I make changes. | ||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | perldoc PAGI::FastAPI::ResponseModel | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | You can also look for information at: | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | =over 4 | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | =item * BUG Report | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | L<https://github.com/manwar/PAGI-FastAPI/issues> | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | =item * Search MetaCPAN | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | L<https://metacpan.org/dist/PAGI-FastAPI/> | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | =back | ||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 217 | the terms of the Artistic License (2.0). | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | =cut | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | 2 | 1; # End of PAGI::FastAPI::ResponseModel | |||||||||||||||||||||||||||||||