| 1 | package APISchema::Generator::Markdown::ResourceResolver; | ||
| 2 | use 5.014; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # cpan | ||
| 7 | use JSON::Pointer; | ||
| 8 | use Class::Accessor::Lite ( | ||
| 9 | new => 1, | ||
| 10 | ro => [qw(schema)], | ||
| 11 | ); | ||
| 12 | |||
| 13 | sub _foreach_properties($$&) { | ||
| 14 | my ($name_path, $definition, $callback) = @_; | ||
| 15 | return unless (ref $definition || '') eq 'HASH'; | ||
| 16 | |||
| 17 | if ($definition->{items}) { | ||
| 18 | my $items = $definition->{items}; | ||
| 19 | my $type = ref $items || ''; | ||
| 20 | if ($type eq 'HASH') { | ||
| 21 | $callback->([@$name_path, '[]'], $items); | ||
| 22 | } elsif ($type eq 'ARRAY') { | ||
| 23 | $callback->([@$name_path, "[$_]"], $items->{$_}) for (0..$#$items); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | if ($definition->{properties}) { | ||
| 28 | my $items = $definition->{properties}; | ||
| 29 | my $type = ref $items || ''; | ||
| 30 | if ($type eq 'HASH') { | ||
| 31 | $callback->([@$name_path, $_], $items->{$_}) for keys %$items; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _property_name (@) { | ||
| 37 | my @name_path = @_; | ||
| 38 | return '.' . join '.', @name_path; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _collect_properties { | ||
| 42 | my ($self, $path, $definition) = @_; | ||
| 43 | return {} unless (ref $definition || '') eq 'HASH'; | ||
| 44 | |||
| 45 | my $ref = $definition->{'$ref'}; | ||
| 46 | if ($ref) { | ||
| 47 | $ref = $ref =~ s/^#//r; | ||
| 48 | my $def = JSON::Pointer->get($self->schema, $ref); | ||
| 49 | return $self->_collect_properties($path, $def) | ||
| 50 | if $def && $ref !~ qr!^/resource/[^/]+$!; | ||
| 51 | |||
| 52 | $definition = +{ | ||
| 53 | %$definition, | ||
| 54 | description => $definition->{description} // $def->{description}, | ||
| 55 | }; | ||
| 56 | } | ||
| 57 | |||
| 58 | my $result = { _property_name(@$path) => $definition }; | ||
| 59 | _foreach_properties($path, $definition, sub { | ||
| 60 | $result = +{ | ||
| 61 | %$result, | ||
| 62 | %{$self->_collect_properties(@_)}, | ||
| 63 | }; | ||
| 64 | }); | ||
| 65 | return $result; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _collect_example { | ||
| 69 | my ($self, $path, $definition) = @_; | ||
| 70 | return $definition->{example} if defined $definition->{example}; | ||
| 71 | |||
| 72 | my $ref = $definition->{'$ref'}; | ||
| 73 | if ($ref) { | ||
| 74 | $ref = $ref =~ s/^#//r; | ||
| 75 | my $def = JSON::Pointer->get($self->schema, $ref); | ||
| 76 | return $self->_collect_example($path, $def) if $def; | ||
| 77 | } | ||
| 78 | |||
| 79 | my %result; | ||
| 80 | my $type = $definition->{type} || ''; | ||
| 81 | _foreach_properties($path, $definition, sub { | ||
| 82 | my $example = $self->_collect_example(@_) // $_[1]->{default}; | ||
| 83 | $result{$_[0]->[-1]} = $example if defined $example; | ||
| 84 | }); | ||
| 85 | |||
| 86 | return \%result if $type eq 'object'; | ||
| 87 | |||
| 88 | if ($type eq 'array') { | ||
| 89 | return [ $result{'[]'} ] if $result{'[]'}; | ||
| 90 | |||
| 91 | my @result; | ||
| 92 | for (keys %result) { | ||
| 93 | next unless $_ =~ /\A\[([0-9]+)\]\z/; | ||
| 94 | $result[$1] = $result{$_}; | ||
| 95 | } | ||
| 96 | return \@result; | ||
| 97 | } | ||
| 98 | |||
| 99 | return undef; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub properties { | ||
| 103 | my ($self, $resource) = @_; | ||
| 104 | return $self->_collect_properties([], $resource); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub example { | ||
| 108 | my ($self, $resource) = @_; | ||
| 109 | return $self->_collect_example([], $resource); | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; |