-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSwagger.pm
195 lines (141 loc) · 5.47 KB
/
Swagger.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package Catalyst::Controller::Swagger;
use Moose;
BEGIN { extends 'Catalyst::Controller' };
use namespace::autoclean;
our $VERSION = 0.001;
has swagger => (is => 'ro');
use Swagger qw(meta generate_parameterized_path);
use JSON::XS;
# ABSTRACT: Swagger For Catalyst
sub api_docs : Local {
my ($self, $c) = @_;
my $swag_generator = Swagger::get_generator($self->{swagger});
my $swag_lookup = meta();
for my $controller_name ( $c->controllers ) {
my $controller = $c->controller($controller_name);
for my $action_method ($controller->get_action_methods) {
my ($swagger_attr) = grep {/swagger/i} @{$action_method->attributes};
my $action_name = $action_method->name;
my $swag_data = $swag_lookup->{$action_name};
if ($swagger_attr || $swag_data) {
my $action = $controller->action_for($action_name);
my ($method) = $action->list_extra_info->{HTTP_METHODS} ?
$action->list_extra_info->{HTTP_METHODS}[0] :
'GET';
my $dispatcher = $c->dispatcher;
my $path = $swag_data->{path};
my $expanded = $dispatcher->expand_action($action);
unless ($path) {
if ($expanded->can('chain')) {
# This is sort of crazy
# However there isn't an easy way to get full path
# from a chained action.
# Perhaps someone with more knowledge of catalyst internals
# can make this more elegant.
my @path;
for my $chain_part (@{$expanded->chain}) {
push @path, join('/',$chain_part->attributes->{PathPart}[0],
($chain_part->attributes->{CaptureArgs} ? join('/',('*') x $chain_part->attributes->{CaptureArgs}[0]) : ()),
($chain_part->attributes->{Args} ? join('/',('*') x $chain_part->attributes->{Args}[0]) : ()),
);
}
$path = generate_parameterized_path('/' . join('/', @path),'*');
} else {
$path = $c->uri_for($action)->path;
}
}
if ($path) {
$swag_generator->add_resource($path, $method, $swag_data);
}
}
}
};
$c->response->body(JSON::XS::encode_json($swag_generator->swagger_data));
}
1;
=head1 NAME
Catalyst::Controller::Swagger
=head1 SYNOPSIS
package MyApp::Controller::Root;
use base 'Catalyst::Controller::Swagger';
use Swagger qw(add_meta);
__PACKAGE__->config(
swagger => {
api_version => '2.2.3',
info => {
title => 'test project',
description => 'test description',
},
}
};
add_meta {
action => 'test_one',
params => [
{ name => 'start', type => 'integer' }
],
};
sub test_one_base :Chained('/') :PathPart('test_one') :CaptureArgs(2) {
my ( $self, $c ) = @_;
}
sub test_one :Chained('test_one_base') :PathPart('foo') :Args(1) :Swagger {
my ($self, $c) = @_;
$c->response->body("test_one");
}
# A swagger route can be flagged to be swagger with the :Swagger attribute
sub test_two :Local :Swagger {
my ($self, $c) = @_;
$c->response->body('test_two');
}
=head1 DESCRIPTION
Add swagger meta data to any Catalyst route. This module will expose an "api_docs" route
which will contain JSON that is Swagger 1.2 compatible.
=head2 :Swagger Attribute
When this attribute is applied to an action meta data that is implicit to the route will
be exposed to the api_docs route. The data that is exposed include the following: path, method,
and route nickname. Any additional meta data that would need to be exposed would need to use
the Swagger::add_meta function to associate it.
Here is an example of what the default swagger output looks like:
{
path => '/test_two',
operations => [{
method => 'GET',
summary => '',
notes => '',
type => '',
nickname => 'GET_/test_two',
summary => '',
}],
}
=head2 api_docs route
This is a route that is exposed that will output a JSON data structure that is Swagger 1.2
compatible.
=head2 Swagger::add_meta
The add_meta function allows a developer to associate other allowed swagger meta data. For example
params would specify what sort of parameters a route would accept:
add_meta {
action => 'test_one', # name of route
params => [
{ name => 'start', type => 'integer' }
],
};
=cut
=head1 Swagger
For further information on Swagger and what it is see: http://www.swagger.io
=cut
=head1 CONTRIBUTING
The code for `catalyst-controller-swagger` is hosted on GitHub at:
https://github.com/mediamath/catalyst-controller-swagger/
If you would like to contribute code, documentation, tests, or bugfixes, follow these steps:
1. Fork the project on GitHub.
2. Clone the fork to your local machine.
3. Make your changes and push them back up to your GitHub account.
4. Send a "pull request" with a brief description of your changes, and a link to a JIRA
ticket if there is one.
If you are unfamiliar with GitHub, start with their excellent documentation here:
https://help.github.com/articles/fork-a-repo
=cut
=head1 COPYRIGHT & LICENSE
Copyright 2015, Logan Bell & John Napiorkowski / MediaMath
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut