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