1Mojolicious::Plugin::OpUesneArPIC(o3n)tributed Perl DocuMmoejnotlaitciioonus::Plugin::OpenAPI(3)
2
3
4
6 Mojolicious::Plugin::OpenAPI - OpenAPI / Swagger plugin for Mojolicious
7
9 use Mojolicious::Lite;
10
11 # Will be moved under "basePath", resulting in "POST /api/echo"
12 post "/echo" => sub {
13
14 # Validate input request or return an error document
15 my $c = shift->openapi->valid_input or return;
16
17 # Generate some data
18 my $data = {body => $c->req->json};
19
20 # Validate the output response and render it to the user agent
21 # using a custom "openapi" handler.
22 $c->render(openapi => $data);
23 }, "echo";
24
25 # Load specification and start web server
26 plugin OpenAPI => {url => "data:///spec.json"};
27 app->start;
28
29 __DATA__
30 @@ spec.json
31 {
32 "swagger" : "2.0",
33 "info" : { "version": "0.8", "title" : "Echo Service" },
34 "schemes" : [ "http" ],
35 "basePath" : "/api",
36 "paths" : {
37 "/echo" : {
38 "post" : {
39 "x-mojo-name" : "echo",
40 "parameters" : [
41 { "in": "body", "name": "body", "schema": { "type" : "object" } }
42 ],
43 "responses" : {
44 "200": {
45 "description": "Echo response",
46 "schema": { "type": "object" }
47 }
48 }
49 }
50 }
51 }
52 }
53
54 See Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv2 or
55 Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv3 for more information
56 about.
57
58 Looking at the documentation for "x-mojo-to" in
59 Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv2 can be especially
60 useful if you are using extensions (formats) such as ".json". The logic
61 is the same for OpenAPIv2 and OpenAPIv3.
62
64 Mojolicious::Plugin::OpenAPI is Mojolicious::Plugin that add routes and
65 input/output validation to your Mojolicious application based on a
66 OpenAPI (Swagger) specification. This plugin supports both version 2.0
67 and 3.x, though 3.x might have some missing features.
68
69 Have a look at the "SEE ALSO" for references to more documentation.
70
71 Please report in issues <https://github.com/jhthorsen/json-
72 validator/issues> or open pull requests to enhance the 3.0 support.
73
75 openapi.spec
76 $hash = $c->openapi->spec($json_pointer)
77 $hash = $c->openapi->spec("/info/title")
78 $hash = $c->openapi->spec;
79
80 Returns the OpenAPI specification. A JSON Pointer can be used to
81 extract a given section of the specification. The default value of
82 $json_pointer will be relative to the current operation. Example:
83
84 {
85 "paths": {
86 "/pets": {
87 "get": {
88 // This datastructure is returned by default
89 }
90 }
91 }
92 }
93
94 openapi.validate
95 @errors = $c->openapi->validate;
96
97 Used to validate a request. @errors holds a list of
98 JSON::Validator::Error objects or empty list on valid input.
99
100 Note that this helper is only for customization. You probably want
101 "openapi.valid_input" in most cases.
102
103 openapi.valid_input
104 $c = $c->openapi->valid_input;
105
106 Returns the Mojolicious::Controller object if the input is valid or
107 automatically render an error document if not and return false. See
108 "SYNOPSIS" for example usage.
109
111 Mojolicious::Plugin::OpenAPI will emit the following hooks on the
112 application object.
113
114 openapi_routes_added
115 Emitted after all routes have been added by this plugin.
116
117 $app->hook(openapi_routes_added => sub {
118 my ($openapi, $routes) = @_;
119
120 for my $route (@$routes) {
121 ...
122 }
123 });
124
125 This hook is EXPERIMENTAL and subject for change.
126
128 This plugin register a new handler called "openapi". The special thing
129 about this handler is that it will validate the data before sending it
130 back to the user agent. Examples:
131
132 $c->render(json => {foo => 123}); # without validation
133 $c->render(openapi => {foo => 123}); # with validation
134
135 This handler will also use "renderer" to format the output data. The
136 code below shows the default "renderer" which generates JSON data:
137
138 $app->plugin(
139 OpenAPI => {
140 renderer => sub {
141 my ($c, $data) = @_;
142 return Mojo::JSON::encode_json($data);
143 }
144 }
145 );
146
148 route
149 $route = $openapi->route;
150
151 The parent Mojolicious::Routes::Route object for all the OpenAPI
152 endpoints.
153
154 validator
155 $jv = $openapi->validator;
156
157 Holds either a JSON::Validator::Schema::OpenAPIv2 or a
158 JSON::Validator::Schema::OpenAPIv3 object.
159
161 register
162 $openapi = $openapi->register($app, \%config);
163 $openapi = $app->plugin(OpenAPI => \%config);
164
165 Loads the OpenAPI specification, validates it and add routes to $app.
166 It will also set up "HELPERS" and adds a before_render hook for auto-
167 rendering of error documents. The return value is the object instance,
168 which allow you to access the "ATTRIBUTES" after you load the plugin.
169
170 %config can have:
171
172 allow_invalid_ref
173
174 The OpenAPI specification does not allow "$ref" at every level, but
175 setting this flag to a true value will ignore the $ref check.
176
177 Note that setting this attribute is discourage.
178
179 coerce
180
181 See "coerce" in JSON::Validator for possible values that "coerce" can
182 take.
183
184 Default: booleans,numbers,strings
185
186 The default value will include "defaults" in the future, once that is
187 stable enough.
188
189 default_response
190
191 Instructions for "add_default_response_schema" in
192 JSON::Validator::Schema::OpenAPIv2. (Also used for OpenAPIv3)
193
194 format
195
196 Set this to a default list of file extensions that your API accepts.
197 This value can be overwritten by "x-mojo-to" in
198 Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv2.
199
200 This config parameter is EXPERIMENTAL and subject for change.
201
202 log_level
203
204 "log_level" is used when logging invalid request/response error
205 messages.
206
207 Default: "warn".
208
209 plugins
210
211 A list of OpenAPI classes to extend the functionality. Default is:
212 Mojolicious::Plugin::OpenAPI::Cors,
213 Mojolicious::Plugin::OpenAPI::SpecRenderer and
214 Mojolicious::Plugin::OpenAPI::Security.
215
216 $app->plugin(OpenAPI => {plugins => [qw(+Cors +SpecRenderer +Security)]});
217
218 You can load your own plugins by doing:
219
220 $app->plugin(OpenAPI => {plugins => [qw(+SpecRenderer My::Cool::OpenAPI::Plugin)]});
221
222 renderer
223
224 See "RENDERER".
225
226 route
227
228 "route" can be specified in case you want to have a protected API.
229 Example:
230
231 $app->plugin(OpenAPI => {
232 route => $app->routes->under("/api")->to("user#auth"),
233 url => $app->home->rel_file("cool.api"),
234 });
235
236 skip_validating_specification
237
238 Used to prevent calling "errors" in JSON::Validator::Schema::OpenAPIv2
239 for the specification.
240
241 spec_route_name
242
243 Name of the route that handles the "basePath" part of the specification
244 and serves the specification. Defaults to "x-mojo-name" in the
245 specification at the top level.
246
247 url
248
249 See "schema" in JSON::Validator for the different "url" formats that is
250 accepted.
251
252 "spec" is an alias for "url", which might make more sense if your
253 specification is written in perl, instead of JSON or YAML.
254
255 version_from_class
256
257 Can be used to overridden "/info/version" in the API specification,
258 from the return value from the "VERSION()" method in
259 "version_from_class".
260
261 This will only have an effect if "version" is "0".
262
263 Defaults to the current $app.
264
266 Henrik Andersen
267
268 Ilya Rassadin
269
270 Jan Henning Thorsen
271
272 Joel Berger
273
275 Copyright (C) Jan Henning Thorsen
276
277 This program is free software, you can redistribute it and/or modify it
278 under the terms of the Artistic License version 2.0.
279
281 • Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv2
282
283 Guide for how to use this plugin with OpenAPI version 2.0 spec.
284
285 • Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv3
286
287 Guide for how to use this plugin with OpenAPI version 3.0 spec.
288
289 • Mojolicious::Plugin::OpenAPI::Cors
290
291 Plugin to add Cross-Origin Resource Sharing (CORS).
292
293 • Mojolicious::Plugin::OpenAPI::Security
294
295 Plugin for handling security definitions in your schema.
296
297 • Mojolicious::Plugin::OpenAPI::SpecRenderer
298
299 Plugin for exposing your spec in human readble or JSON format.
300
301 • <https://www.openapis.org/>
302
303 Official OpenAPI website.
304
305
306
307perl v5.34.0 2021-07-22 Mojolicious::Plugin::OpenAPI(3)