The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojolicious::Plugin::OpenAPI::SpecRenderer - Render OpenAPI specification

SYNOPSIS

With Mojolicious::Plugin::OpenAPI

  $app->plugin(OpenAPI => {
    plugins                        => [qw(+SpecRenderer)],
    render_specification           => 1,
    render_specification_for_paths => 1,
    %openapi_parameters,
  });

See "register" in Mojolicious::Plugin::OpenAPI for what %openapi_parameters might contain.

Standalone

  use Mojolicious::Lite;
  plugin "Mojolicious::Plugin::OpenAPI::SpecRenderer";

  # Some specification to render
  my $petstore = app->home->child("petstore.json");

  get "/my-spec" => sub {
    my $c = shift;

    # "openapi_spec" can also be set in...
    # - $app->defaults(openapi_spec => ...);
    # - $route->to(openapi_spec => ...);
    $c->stash(openapi_spec => JSON::Validator->new->schema($petstore->to_string)->bundle);

    # render_spec() can be called with $openapi_path
    $c->openapi->render_spec;
  };

DESCRIPTION

Mojolicious::Plugin::OpenAPI::SpecRenderer will enable Mojolicious::Plugin::OpenAPI to render the specification in both HTML and JSON format. It can also be used "Standalone" if you just want to render the specification, and not add any API routes to your application.

The human readable format focus on making the documentation printable, so you can easily share it with third parties as a PDF. If this documentation format is too basic or has missing information, then please report in suggestions for enhancements.

HELPERS

openapi.render_spec

  $c = $c->openapi->render_spec;
  $c = $c->openapi->render_spec($openapi_path);
  $c = $c->openapi->render_spec("/user/{id}");

Used to render the specification as either "html" or "json". Set the "stash" in Mojolicious variable "format" to change the format to render.

Will render the whole specification by default, but can also render documentation for a given OpenAPI path.

METHODS

register

  $doc->register($app, $openapi, \%config);

Adds the features mentioned in the "DESCRIPTION".

%config is the same as passed on to "register" in Mojolicious::Plugin::OpenAPI. The following keys are used by this plugin:

render_specification

Render the whole specification as either HTML or JSON from "/:basePath". Example if basePath in your specification is "/api":

  GET https://api.example.com/api.html
  GET https://api.example.com/api.json

Disable this feature by setting render_specification to 0.

render_specification_for_paths

Render the specification from individual routes, using the OPTIONS HTTP method. Example:

  OPTIONS https://api.example.com/api/some/path.json
  OPTIONS https://api.example.com/api/some/path.json?method=post

Disable this feature by setting render_specification_for_paths to 0.

SEE ALSO

Mojolicious::Plugin::OpenAPI