Mojolic...wagger2(3)
πΈ NAME πΈ
okay so this is Mojolicious::Plugin::OpenAPI::Guides::Swagger2(3), straight from the User Contributed Perl Documentation shelf β and it's literally the Swagger2 back compat guide π
β¨
π« OVERVIEW π«
This guide is useful if your app is already using Mojolicious::Plugin::Swagger2 β like, that's the whole vibe. π«Ά
The old plugin used to pass on $args and $cb to the action. You can emulate that with an around_action hook β yes bestie, a little compatibility magic β¨ The SYNOPSIS below contains example code that you can use to make your old controllers and actions work with Mojolicious::Plugin::OpenAPI. No cap.
β¨ SYNOPSIS β¨
here's the code, copy it exactly and let it slay:
package MyApp;
use Mojo::Base "Mojolicious";
sub startup {
my $self = shift;
# Load your specification
$self->plugin("OpenAPI" => {url => $app->home->rel_file("myapi.json")});
$self->hook(around_action => sub {
my ($next, $c, $action, $last) = @_;
# Do not call the action with ($args, $cb) unless it is an
# OpenAPI endpoint.
return $next->() unless $last;
return $next->() unless $c->openapi->spec;
# Render error document unless the input is valid
return unless $c->openapi->valid_input;
my $cb = sub {
my ($c, $data, $code) = @_;
$c->render(openapi => $data, status => $code);
};
# Call the action with ($args, $cb)
# NOTE! $c->validation->output will be removed in the future
return $c->$action($c->validation->output, $cb);
});
}
β οΈ the code itself has a neon note: $c->validation->output will be removed in the future β so don't get too attached, okay? π
π¦ MOVING FORWARD π¦
the around_action hook above does not prevent you from writing new actions using the standard Mojolicious::Plugin::OpenAPI API. In the new actions, you can simply drop using $args and $cb and it will work as expected as well. periodt. π
π SEE ALSO π
<https://github.com/jhthorsen/mojolicious-plugin-openapi/blob/master/t/swagger2.t>β the iconic test file π₯Mojolicious::Plugin::OpenAPI. β the main module, obviously π
xoxo, your fave back-compat bestie π β perl v5.42.0, 2025-08-03, and filed under Mojolic...wagger2(3) ππ¦
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious-Plugin-OpenAPI 5.110.0-bp160.1.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms