| 1 | package Dancer::Plugin::Swagger::Path; | ||
| 2 | our $AUTHORITY = 'cpan:YANICK'; | ||
| 3 | # ABSTRACT: Internal representation of a swagger path | ||
| 4 | $Dancer::Plugin::Swagger::Path::VERSION = '0.2.1'; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Moo; | ||
| 10 | |||
| 11 | use MooseX::MungeHas 'is_ro'; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use Hash::Merge; | ||
| 15 | use Clone 'clone'; | ||
| 16 | use List::AllUtils qw/ first any none /; | ||
| 17 | use JSON; | ||
| 18 | use Class::Load qw/ load_class /; | ||
| 19 | |||
| 20 | has route => ( handles => [ 'pattern' ] ); | ||
| 21 | |||
| 22 | has tags => ( predicate => 1 ); | ||
| 23 | |||
| 24 | has method => sub { | ||
| 25 | eval { $_[0]->route->method } | ||
| 26 | or croak "no route or explicit method provided to path"; | ||
| 27 | }; | ||
| 28 | |||
| 29 | has path => sub { | ||
| 30 | dancer_pattern_to_swagger_path( $_[0]->route->pattern ); | ||
| 31 | }; | ||
| 32 | |||
| 33 | has responses => ( predicate => 1); | ||
| 34 | |||
| 35 | has description => ( predicate => 1 ); | ||
| 36 | |||
| 37 | has parameters => | ||
| 38 | lazy => 1, | ||
| 39 | default => sub { [] }, | ||
| 40 | predicate => 1, | ||
| 41 | ; | ||
| 42 | |||
| 43 | # TODO allow to pass a hashref instead of an arrayref | ||
| 44 | sub parameter { | ||
| 45 | my( $self, $param, $args ) = @_; | ||
| 46 | |||
| 47 | $args ||= {}; | ||
| 48 | $args->{name} ||= $param; | ||
| 49 | |||
| 50 | my $p = first { $_->{name} eq $param } @{ $self->parameters }; | ||
| 51 | |||
| 52 | push @{ $self->parameters || [] }, $p = { name => $param } | ||
| 53 | unless $p; | ||
| 54 | |||
| 55 | %$p = %{Hash::Merge::merge( $p, $args )}; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub dancer_pattern_to_swagger_path { | ||
| 59 | my $pattern = shift; | ||
| 60 | $pattern =~ s#(?<=/):(\w+)(?=/|$)#{$1}#g; | ||
| 61 | return $pattern; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub add_to_doc { | ||
| 65 | my( $self, $doc ) = @_; | ||
| 66 | |||
| 67 | my $path = $self->path; | ||
| 68 | my $method = $self->method; | ||
| 69 | |||
| 70 | # already there | ||
| 71 | next if $doc->{paths}{$path}{$method}; | ||
| 72 | |||
| 73 | my $m = $doc->{paths}{$path}{$method} ||= {}; | ||
| 74 | |||
| 75 | $m->{description} = $self->description if $self->has_description; | ||
| 76 | $m->{parameters} = $self->parameters if $self->has_parameters; | ||
| 77 | $m->{tags} = $self->tags if $self->has_tags; | ||
| 78 | |||
| 79 | if( $self->has_responses ) { | ||
| 80 | $m->{responses} = clone $self->responses; | ||
| 81 | |||
| 82 | for my $r ( values %{$m->{responses}} ) { | ||
| 83 | delete $r->{template}; | ||
| 84 | |||
| 85 | if( my $example = delete $r->{example} ) { | ||
| 86 | my $serializer = Dancer::engine('serializer'); | ||
| 87 | die "Don't know content type for serializer ", ref $serializer | ||
| 88 | if none { $serializer->isa($_) } qw/ Dancer::Serializer::JSON Dancer::Serializer::YAML /; | ||
| 89 | $r->{examples}{$serializer->content_type} = $example; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | sub validate_response { | ||
| 98 | my( $self, $code, $data, $strict ) = @_; | ||
| 99 | |||
| 100 | my $schema = $self->responses->{$code}{schema}; | ||
| 101 | |||
| 102 | die "no schema found for return code $code for ", join ' ' , uc($self->method), $self->path | ||
| 103 | unless $schema or not $strict; | ||
| 104 | |||
| 105 | return unless $schema; | ||
| 106 | |||
| 107 | my $plugin = Dancer::Plugin::Swagger->instance; | ||
| 108 | |||
| 109 | $schema = { | ||
| 110 | definitions => $plugin->doc->{definitions}, | ||
| 111 | properties => { response => $schema }, | ||
| 112 | }; | ||
| 113 | |||
| 114 | my $result = load_class('JSON::Schema::AsType')->new( schema => $schema)->validate_explain({ response => $data }); | ||
| 115 | |||
| 116 | return unless $result; | ||
| 117 | |||
| 118 | die join "\n", map { "* " . $_ } @$result; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub BUILD { | ||
| 122 | my $self = shift; | ||
| 123 | |||
| 124 | for my $param ( eval { @{ $self->route->{_params} } } ) { | ||
| 125 | $self->parameter( $param => { | ||
| 126 | in => 'path', | ||
| 127 | required => JSON::true, | ||
| 128 | type => "string", | ||
| 129 | } ); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | |||
| 135 | __END__ | ||
| 136 | |||
| 137 | =pod | ||
| 138 | |||
| 139 | =encoding UTF-8 | ||
| 140 | |||
| 141 | =head1 NAME | ||
| 142 | |||
| 143 | Dancer::Plugin::Swagger::Path - Internal representation of a swagger path | ||
| 144 | |||
| 145 | =head1 VERSION | ||
| 146 | |||
| 147 | version 0.2.1 | ||
| 148 | |||
| 149 | =head1 DESCRIPTION | ||
| 150 | |||
| 151 | Objects of this class are used by L<Dancer::Plugin::Swagger> to represent | ||
| 152 | a path in the Swagger document. | ||
| 153 | |||
| 154 | =head1 AUTHOR | ||
| 155 | |||
| 156 | Yanick Champoux <yanick@cpan.org> | ||
| 157 | |||
| 158 | =head1 COPYRIGHT AND LICENSE | ||
| 159 | |||
| 160 | This software is copyright (c) 2021, 2016, 2015 by Yanick Champoux. | ||
| 161 | |||
| 162 | This is free software; you can redistribute it and/or modify it under | ||
| 163 | the same terms as the Perl 5 programming language system itself. | ||
| 164 | |||
| 165 | =cut |