File Coverage

blib/lib/Whelk/Schema/Definition/Object.pm
Criterion Covered Total %
statement 57 57 100.0
branch 30 34 88.2
condition 4 5 80.0
subroutine 6 6 100.0
pod 3 3 100.0
total 100 105 95.2


line stmt bran cond sub pod time code
1             package Whelk::Schema::Definition::Object;
2             $Whelk::Schema::Definition::Object::VERSION = '1.04';
3 23     23   16482 use Whelk::StrictBase 'Whelk::Schema::Definition';
  23         53  
  23         180  
4              
5             attr '?properties' => undef;
6             attr '?strict' => !!0;
7              
8             sub openapi_dump
9             {
10 31     31 1 85 my ($self, $openapi_obj, %hints) = @_;
11              
12 31 100       93 if ($hints{parameters}) {
13 8         52 return $self->_dump_parameters($openapi_obj, %hints);
14             }
15              
16 23         102 my $res = $self->SUPER::openapi_dump($openapi_obj, %hints);
17 23         82 $res->{type} = 'object';
18              
19 23         53 my @required;
20             my %items;
21 23   50     172 my $properties = $self->properties // {};
22 23         149 foreach my $key (sort keys %{$properties}) {
  23         95  
23 39         267 $items{$key} = $properties->{$key}->openapi_schema($openapi_obj);
24             push @required, $key
25 39 100       148 if $properties->{$key}->required;
26             }
27              
28 23 50       91 if (%items) {
29 23         57 $res->{properties} = \%items;
30             $res->{required} = \@required
31 23 50       67 if @required;
32             }
33              
34 23         170 return $res;
35             }
36              
37             sub _dump_parameters
38             {
39 8     8   20 my ($self, $openapi_obj, %hints) = @_;
40              
41             # dump an object schema which was used for parameters. This is not actually
42             # a schema but an openapi parameters object
43 8         14 my @res;
44 8         21 my $properties = $self->properties;
45 8         46 foreach my $key (sort keys %{$properties}) {
  8         33  
46 13         26 my $item = $properties->{$key};
47             push @res, {
48             name => $key,
49             in => $hints{parameters},
50 13 100       81 ($item->description ? (description => $item->description) : ()),
51             required => $self->_bool($item->required),
52             schema => $item->openapi_schema($openapi_obj),
53             };
54             }
55              
56 8         75 return \@res;
57             }
58              
59             sub _resolve
60             {
61 151     151   327 my ($self) = @_;
62              
63 151         382 my $properties = $self->properties;
64 151 100       999 if ($properties) {
65 148         227 foreach my $key (keys %{$properties}) {
  148         421  
66 246         847 $properties->{$key} = $self->_build($properties->{$key});
67             }
68             }
69             }
70              
71             sub inhale
72             {
73 156     156 1 439 my ($self, $value) = @_;
74              
75 156 50       617 return undef if $self->_valid_nullable($value);
76              
77 156 100       567 if (ref $value eq 'HASH') {
78 147         444 my $properties = $self->properties;
79 147 100       1013 if ($properties) {
80 144         472 foreach my $key (keys %$properties) {
81 207 100       507 if (!exists $value->{$key}) {
82             return "object[$key]->required"
83 52 100       287 if $properties->{$key}->required;
84              
85 39         236 next;
86             }
87              
88 155         914 my $inhaled = $properties->{$key}->inhale($value->{$key});
89 155 100       497 return "object[$key]->$inhaled" if defined $inhaled;
90             }
91              
92 110 100       376 if ($self->strict) {
93 2         50 foreach my $key (keys %$value) {
94 3 100       8 next if exists $properties->{$key};
95 1         3 return "object[$key]->redundant";
96             }
97             }
98             }
99              
100 112         1044 return $self->_inhale_extra_rules($value);
101             }
102              
103 9         37 return 'object';
104             }
105              
106             sub exhale
107             {
108 90     90 1 1331 my ($self, $value) = @_;
109              
110 90 50       271 return undef if $self->_valid_nullable($value);
111              
112 90         245 my $properties = $self->properties;
113 90 100       591 return $value unless $properties;
114              
115 87         267 foreach my $key (keys %$properties) {
116 120 100 100     431 next if !exists $value->{$key} && !$properties->{$key}->has_default;
117              
118 112         754 $value->{$key} = $properties->{$key}->exhale($value->{$key});
119             }
120              
121 87         424 return $value;
122             }
123              
124             1;
125