.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Swagger2::Guides::Tutorial 3pm" .TH Swagger2::Guides::Tutorial 3pm "2016-04-17" "perl v5.22.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Swagger2::Guides::Tutorial \- Tutorial for Mojolicious::Plugin::Swagger2 .SH "OVERVIEW" .IX Header "OVERVIEW" This tutorial will give you an introduction to how to use Mojolicious::Plugin::Swagger2. .PP You can also check out if you want to look at a complete example application. .PP Another resources is the blog post , which includes reasons for why you want to use Swagger2. .SH "TUTORIAL" .IX Header "TUTORIAL" .SS "Swagger specification" .IX Subsection "Swagger specification" The input \*(L"url\*(R" to given as argument to the plugin need to point to a valid swagger document. .PP Every operation must have \*(L"operationId\*(R" specified, so this plugin knows where to look for the controller and method. The naming convention is: .PP .Vb 1 \& "operationId": "methodControllerOptions" .Ve .PP Example specification: .PP .Vb 10 \& { \& "swagger": "2.0", \& "basePath": "/api", \& "paths": { \& "/pets/{petId}": { \& "get": { \& "operationId": "showPetById", \& "parameters": [ ... ], \& "responses": { \& "200": { ... } \& } \& } \& } \& } \& } .Ve .PP Here the \*(L"operationId\*(R" contains the method \*(L"show\*(R" and the controller \*(L"Pet\*(R". The \*(L"Pet\*(R" controller will again expand to \f(CW\*(C`MyApp::Controller::Pet\*(C'\fR or \&\f(CW\*(C`MyApp::Pet\*(C'\fR or whatever \*(L"namespaces\*(R" in Mojolicious::Routes is set to. The \&\*(L"options\*(R" part (ById) is simply ignored. .PP Note that \*(L"showPetById\*(R" and \*(L"listPets\*(R" will both result in looking for the \*(L"Pet\*(R" controller, since the plural ending (\*(L"s\*(R") is removed. .PP The table below try to illustrate how the controller/method is resolved: .PP .Vb 10 \& .\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-. \& | operationId | Controller | Method | Ignored | \& |\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-|\-\-\-\-\-\-\-\-\-\-\-\-\-\-|\-\-\-\-\-\-\-\-\-\-\-\-\-|\-\-\-\-\-\-\-\-\-| \& | childrenOfPerson | Person | children | Of | \& | designByUser | User | design | By | \& | fooWithBar | Bar | foo | With | \& | getPetById | Pet | get | ById | \& | listUsers | User | list | s | \& | peopleInConversation | Conversation | people | In | \& | searchForPets | Pet | search | For, s | \& | sendToConversation | Conversation | send | To | \& | createFileInFileSystem | FileSystem | create_file | In | \& | removeFromFileSystem | FileSystem | remove | From | \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq .Ve .PP The operationId rules will be updated \fIif\fR bugs or incompatibility is discovered. .SS "Application" .IX Subsection "Application" The application need to load the Mojolicious::Plugin::Swagger2 plugin, with a \s-1URL\s0 to the \s-1API\s0 specification. The plugin will then add all the routes defined in the \*(L"Swagger specification\*(R". .PP .Vb 2 \& package MyApp; \& use Mojo::Base "Mojolicious"; \& \& sub startup { \& my $app = shift; \& $app\->plugin(Swagger2 => { url => app\->home\->rel_file("api.json") }); \& } .Ve .SS "Controller" .IX Subsection "Controller" The method names defined in the controller will be a decamelized version of \f(CW\*(C`operationId\*(C'\fR. .PP The example \*(L"Swagger specification\*(R" above, will result in \f(CW\*(C`show()\*(C'\fR in the controller below to be called. This method will receive the current Mojolicious::Controller object, input arguments and a callback. The callback should be called with a \s-1HTTP\s0 status code, and a data structure which will be validated and serialized back to the user agent. .PP \&\f(CW$args\fR (input arguments) will be a hash, where the keys match \*(L"name\*(R" in the \&\*(L"parameters\*(R" defined in the Swagger spec, and the values are whatever input came from the client. This also goes for the \*(L"body\*(R" parameter: This means that the input \s-1JSON\s0 from \s-1HTTP\s0 body will not be flattened, but stored under the body parameter name in \f(CW$args\fR. .PP .Vb 1 \& package MyApp::Controller::Pet; \& \& sub show { \& my ($c, $args, $cb) = @_; \& \& # Example $args: \& # $args\->{body_param_name} == $c\->req\->json \& # $args\->{query_param_name} == $c\->req\->url\->query\->param("query_param_name") \& # $args\->{"X\-Header\-Name"} == $c\->req\->headers\->header("X\-Header\-Name") \& \& $c\->$cb({limit => 123}, 200); \& } .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 4 Swagger2::Guides::ProtectedApi \- Protected \s-1API\s0 Guide .SH "AUTHOR" .IX Header "AUTHOR" Jan Henning Thorsen \- \f(CW\*(C`jhthorsen@cpan.org\*(C'\fR