File Coverage

blib/lib/OpenAPI/Generator/Util.pm
Criterion Covered Total %
statement 37 41 90.2
branch 7 12 58.3
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 50 59 84.7


line stmt bran cond sub pod time code
1             package OpenAPI::Generator::Util;
2              
3 3     3   21 use strict;
  3         7  
  3         88  
4 3     3   15 use warnings;
  3         49  
  3         81  
5              
6 3     3   14 use Carp;
  3         6  
  3         163  
7 3     3   17 use Exporter qw(import);
  3         29  
  3         1342  
8              
9             our @EXPORT_OK = qw(
10             merge_definitions
11             );
12              
13             sub merge_definitions {
14              
15 3     3 1 13 my(@definitions) = @_;
16              
17 3 50       11 croak 'no definitions to merge' unless @definitions;
18              
19 3 100       13 if (scalar @definitions == 1) {
20 1         7 return $definitions[0];
21             }
22              
23 2         16 my %common_def = (
24             paths => {},
25             components => {
26             schemas => {},
27             securitySchemes => {},
28             parameters => {}
29             },
30             );
31              
32 2         9 for my $def (@definitions) {
33              
34             # paths
35 6         9 while (my($path, $path_schema) = each %{$def->{paths}}) {
  14         60  
36 8         13 while (my($method, $method_schema) = each %{$path_schema}) {
  16         67  
37 8 50       28 if (exists $common_def{paths}{$path}{$method}) {
38 0         0 croak "$method $path duplicates";
39             }
40 8         25 $common_def{paths}{$path}{$method} = $method_schema;
41             }
42             }
43              
44             # comp param
45 6         11 while (my($param, $param_schema) = each %{$def->{components}{parameters}}) {
  11         40  
46 5 50       17 if (exists $common_def{components}{parameters}{$param}) {
47 0         0 croak "param $param duplicates"
48             }
49 5         13 $common_def{components}{parameters}{$param} = $param_schema;
50             }
51              
52             # comp schemas
53 6         14 while (my($comp, $comp_schema) = each %{$def->{components}{schemas}}) {
  11         38  
54 5 50       19 if (exists $common_def{components}{schemas}{$comp}) {
55 0         0 croak "schema $comp duplicates"
56             }
57 5         13 $common_def{components}{schemas}{$comp} = $comp_schema;
58             }
59              
60             # comp security
61 6         11 while (my($security, $security_schema) = each %{$def->{components}{securitySchemes}}) {
  11         40  
62 5 50       14 if (exists $common_def{components}{securitySchemes}{$security}) {
63 0         0 croak "security $security duplicates"
64             }
65 5         28 $common_def{components}{securitySchemes}{$security} = $security_schema;
66             }
67             }
68              
69 2         27 return \%common_def;
70             }
71              
72             1
73              
74             __END__