include/oa_tools.h

filter: Statementstmt 93.9% 200 / 213 Branchbran 67.5% 177 / 262 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.3% 377 / 475 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_compile.h ↑ Summary include/oa_validate.h »
1 #ifndef OA_TOOLS_H
2 #define OA_TOOLS_H
3  
4 /* Two things the compiled table cannot answer.
5 *
6 * oa_compile.h keeps a JSON::Schema::Fast handle where a schema would be, so
7 * operation_info can report only WHETHER one is attached, and it keeps no
8 * summary, description or tags at all - the compiled form is what will be
9 * checked, not what the document said. A caller that wants to describe an
10 * operation to something that is not this validator needs the document, and
11 * ->spec is the right document to read: it is normalised to 3.1 shape whatever
12 * the source declared, and OpenAPI 3.1 schemas ARE JSON Schema 2020-12, so
13 * what comes out needs no dialect translation.
14 *
15 * Both entry points run once per compile, never on a request path.
16 *
17 * Deliberately NOT emitted: `additionalProperties`. The natural spelling of
18 * JSON false here is the \0 idiom the rest of the stack uses, and
19 * JSON::Schema::Fast refuses a scalar ref as a boolean schema ("schema must be
20 * a hashref or boolean"), while a bare 0 would encode as the number zero and
21 * stop being valid JSON Schema on the wire. Leaving it out keeps the result
22 * both compilable and encodable, and a caller reading these schemas takes the
23 * parameter names it knows.
24 *
25 * Needs oa_compile.h (oa_get, oa_hv_of, oa_av_of, oa_methods, oa_ctype_is_json)
26 * and oa_normalize.h (oa_conv_copy_hv). */
27  
28 #ifndef OA_SCHEMA_PFX
29 #define OA_SCHEMA_PFX "#/components/schemas/"
30 #define OA_SCHEMA_PFX_LEN 21
31 #endif
32  
33 #define OA_DEFS_PFX "#/$defs/"
34 #define OA_DEFS_PFX_LEN 8
35  
36 /* A spec can arrive from anywhere, and C recursion is not a place to trust
37 * input; the same reasoning as OA_CONV_MAX_DEPTH next door. */
38 #define OA_TOOLS_MAX_DEPTH 128
39  
40 /* ---- walking -------------------------------------------------------------- */
41  
42 static void oa_tools_refs_in(pTHX_ SV *node, HV *seen, int depth);
43  
44 101 static void oa_tools_refs_hv(pTHX_ HV *h, HV *seen, int depth) {
45 HE *he;
46 101 hv_iterinit(h);
47 269 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 168 101 269
48 168 I32 kl; const char *k = hv_iterkey(he, &kl);
49 168 SV *v = hv_iterval(h, he);
50 168 ▶ if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) {
Branch: 5 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 84 84 168
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 8 76 84
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 8 0 8
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 8 0 8
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 8 0 8
51 8 STRLEN l; const char *p = SvPV_const(v, l);
52 8 ▶ if (l > OA_DEFS_PFX_LEN && memEQ(p, OA_DEFS_PFX, OA_DEFS_PFX_LEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (l > OA_DEFS_PFX_LEN && memEQ(p, OA_DEFS_PFX, OA_DEFS_PFX_LEN)) { 8 0 8
if (l > OA_DEFS_PFX_LEN && memEQ(p, OA_DEFS_PFX, OA_DEFS_PFX_LEN)) { 8 0 8
53 /* The DEF is the first segment; anything after it is a pointer
54 * INTO that def (`#/$defs/A/properties/b`), which resolves on
55 * its own once A is carried. Keying on the whole remainder
56 * looked for a def literally named "A/properties/b", missed,
57 * and - before 0.13 - stored an EMPTY schema under that name,
58 * which compiles and then accepts anything. */
59 8 const char *nm = p + OA_DEFS_PFX_LEN;
60 8 STRLEN nl = l - OA_DEFS_PFX_LEN;
61 8 const char *slash = (const char *)memchr(nm, '/', nl);
62 8 ▶ if (slash) nl = (STRLEN)(slash - nm);
Branch: 1 branch
BranchTrueFalseTotal
if (slash) nl = (STRLEN)(slash - nm); 1 7 8
63 8 ▶ if (nl) (void)hv_store(seen, nm, (I32)nl, newSViv(1), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (nl) (void)hv_store(seen, nm, (I32)nl, newSViv(1), 0); 8 0 8
64 }
65 8 continue;
66 }
67 160 oa_tools_refs_in(aTHX_ v, seen, depth + 1);
68 }
69 101 }
70  
71 /* Every "#/$defs/NAME" mentioned anywhere under `node`, as keys of `seen`. */
72 210 static void oa_tools_refs_in(pTHX_ SV *node, HV *seen, int depth) {
73 SV *rv;
74 210 ▶ if (!node || depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!node || depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return; 210 0 210
if (!node || depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return; 210 0 210
if (!node || depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return; 99 111 210
75 111 rv = SvRV(node);
76 111 ▶ if (SvTYPE(rv) == SVt_PVHV) { oa_tools_refs_hv(aTHX_ (HV *)rv, seen, depth); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) { oa_tools_refs_hv(aTHX_ (HV *)rv, seen, depth); return; } 101 10 111
77 10 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 10 0 10
78 10 AV *av = (AV *)rv;
79 10 SSize_t i, n = av_len(av) + 1;
80 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 14 10 24
81 14 SV **e = av_fetch(av, i, 0);
82 14 ▶ if (e && *e) oa_tools_refs_in(aTHX_ *e, seen, depth + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) oa_tools_refs_in(aTHX_ *e, seen, depth + 1); 14 0 14
if (e && *e) oa_tools_refs_in(aTHX_ *e, seen, depth + 1); 14 0 14
83 }
84 }
85 }
86  
87 /* A deep copy with every "#/components/schemas/X" rewritten to "#/$defs/X".
88 * A copy, because nothing handed to a caller may share nodes with ->spec. */
89 217 static SV *oa_tools_rewrite(pTHX_ SV *node, int depth) {
90 SV *rv;
91 217 ▶ if (!node) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!node) return newSV(0); 0 217 217
92 217 ▶ if (depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return newSVsv(node);
Branch: 2 branches
BranchTrueFalseTotal
if (depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return newSVsv(node); 217 0 217
if (depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return newSVsv(node); 102 115 217
93 115 rv = SvRV(node);
94  
95 115 ▶ if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) { 105 10 115
96 105 HV *src = (HV *)rv, *dst = newHV();
97 HE *he;
98 105 hv_iterinit(src);
99 278 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 173 105 278
100 173 I32 kl; const char *k = hv_iterkey(he, &kl);
101 173 SV *v = hv_iterval(src, he);
102 173 ▶ if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) {
Branch: 5 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 87 86 173
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 8 79 87
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 8 0 8
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 8 0 8
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 8 0 8
103 8 STRLEN l; const char *p = SvPV_const(v, l);
104 8 ▶ if (l > OA_SCHEMA_PFX_LEN
Branch: 1 branch
BranchTrueFalseTotal
if (l > OA_SCHEMA_PFX_LEN 8 0 8
105 8 ▶ && memEQ(p, OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(p, OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN)) { 8 0 8
106 8 SV *nr = newSVpvn(OA_DEFS_PFX, OA_DEFS_PFX_LEN);
107 8 sv_catpvn(nr, p + OA_SCHEMA_PFX_LEN, l - OA_SCHEMA_PFX_LEN);
108 8 (void)hv_store(dst, k, kl, nr, 0);
109 8 continue;
110 }
111 }
112 165 (void)hv_store(dst, k, kl, oa_tools_rewrite(aTHX_ v, depth + 1), 0);
113 }
114 105 return newRV_noinc((SV *)dst);
115 }
116  
117 10 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 10 0 10
118 10 AV *src = (AV *)rv, *dst = newAV();
119 10 SSize_t i, n = av_len(src) + 1;
120 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 14 10 24
121 14 SV **e = av_fetch(src, i, 0);
122 14 ▶ av_push(dst, oa_tools_rewrite(aTHX_ (e && *e) ? *e : NULL, depth + 1));
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, oa_tools_rewrite(aTHX_ (e && *e) ? *e : NULL, depth + 1)); 14 0 14
av_push(dst, oa_tools_rewrite(aTHX_ (e && *e) ? *e : NULL, depth + 1)); 14 0 14
123 }
124 10 return newRV_noinc((SV *)dst);
125 }
126  
127 0 return newSVsv(node);
128 }
129  
130 /* ---- finding --------------------------------------------------------------- */
131  
132 /* The operation object as the document wrote it. `item_out` is its path item,
133 * which carries parameters shared by every method on that path. */
134 34 static HV *oa_tools_find_op(pTHX_ oa_api *a, SV *op_id, HV **item_out,
135 SV **path_out, const char **method_out) {
136 34 ▶ HV *spec = a ? oa_hv_of(a->spec) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *spec = a ? oa_hv_of(a->spec) : NULL; 34 0 34
137 34 ▶ HV *paths = spec ? oa_hv_of(oa_get(aTHX_ spec, "paths")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *paths = spec ? oa_hv_of(oa_get(aTHX_ spec, "paths")) : NULL; 34 0 34
138 HE *he;
139 STRLEN wl;
140 const char *want;
141  
142 34 ▶ if (!paths || !op_id || !SvOK(op_id)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!paths || !op_id || !SvOK(op_id)) return NULL; 34 0 34
if (!paths || !op_id || !SvOK(op_id)) return NULL; 34 0 34
if (!paths || !op_id || !SvOK(op_id)) return NULL; 0 34 34
143 34 want = SvPV_const(op_id, wl);
144  
145 34 hv_iterinit(paths);
146 56 ▶ while ((he = hv_iternext(paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(paths))) { 54 2 56
147 54 I32 pl; const char *pk = hv_iterkey(he, &pl);
148 54 HV *item = oa_hv_of(hv_iterval(paths, he));
149 int m;
150 54 ▶ if (!item) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!item) continue; 0 54 54
151 264 ▶ for (m = 0; oa_methods[m]; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; oa_methods[m]; m++) { 242 22 264
152 242 HV *op = oa_hv_of(oa_get(aTHX_ item, oa_methods[m]));
153 SV *oid;
154 STRLEN ol;
155 const char *ops;
156 287 ▶ if (!op) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!op) continue; 165 77 242
157 77 oid = oa_get(aTHX_ op, "operationId");
158 77 ▶ if (!oid || !SvOK(oid)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!oid || !SvOK(oid)) continue; 77 0 77
if (!oid || !SvOK(oid)) continue; 0 77 77
159 77 ops = SvPV_const(oid, ol);
160 77 ▶ if (ol != wl || memNE(ops, want, wl)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (ol != wl || memNE(ops, want, wl)) continue; 41 36 77
if (ol != wl || memNE(ops, want, wl)) continue; 9 32 41
161 32 ▶ if (item_out) *item_out = item;
Branch: 1 branch
BranchTrueFalseTotal
if (item_out) *item_out = item; 32 0 32
162 32 ▶ if (path_out) *path_out = newSVpvn(pk, (STRLEN)pl);
Branch: 1 branch
BranchTrueFalseTotal
if (path_out) *path_out = newSVpvn(pk, (STRLEN)pl); 2 30 32
163 32 ▶ if (method_out) *method_out = oa_methods[m];
Branch: 1 branch
BranchTrueFalseTotal
if (method_out) *method_out = oa_methods[m]; 2 30 32
164 32 return op;
165 }
166 }
167 2 return NULL;
168 }
169  
170 /* A parameter's schema: `schema`, or the JSON media type's schema when the
171 * parameter was declared with `content` instead. Borrowed. */
172 34 static SV *oa_tools_param_schema(pTHX_ HV *p) {
173 34 SV *s = oa_get(aTHX_ p, "schema");
174 HV *content;
175 HE *he;
176 34 ▶ if (s && oa_hv_of(s)) return s;
Branch: 2 branches
BranchTrueFalseTotal
if (s && oa_hv_of(s)) return s; 34 0 34
if (s && oa_hv_of(s)) return s; 34 0 34
177 0 content = oa_hv_of(oa_get(aTHX_ p, "content"));
178 0 ▶ if (!content) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!content) return NULL; 0 0 0
179 0 hv_iterinit(content);
180 0 ▶ while ((he = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(content))) { 0 0 0
181 0 I32 kl; const char *k = hv_iterkey(he, &kl);
182 0 HV *m = oa_hv_of(hv_iterval(content, he));
183 0 ▶ SV *ms = m ? oa_get(aTHX_ m, "schema") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *ms = m ? oa_get(aTHX_ m, "schema") : NULL; 0 0 0
184 0 ▶ if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms;
Branch: 3 branches
BranchTrueFalseTotal
if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms; 0 0 0
if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms; 0 0 0
if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms; 0 0 0
185 }
186 0 return NULL;
187 }
188  
189 30 static SV *oa_tools_body_schema(pTHX_ HV *op) {
190 30 HV *rb = oa_hv_of(oa_get(aTHX_ op, "requestBody"));
191 30 ▶ HV *content = rb ? oa_hv_of(oa_get(aTHX_ rb, "content")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *content = rb ? oa_hv_of(oa_get(aTHX_ rb, "content")) : NULL; 8 22 30
192 HE *he;
193 30 ▶ if (!content) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!content) return NULL; 22 8 30
194 8 hv_iterinit(content);
195 10 ▶ while ((he = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(content))) { 10 0 10
196 10 I32 kl; const char *k = hv_iterkey(he, &kl);
197 10 HV *m = oa_hv_of(hv_iterval(content, he));
198 10 ▶ SV *ms = m ? oa_get(aTHX_ m, "schema") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *ms = m ? oa_get(aTHX_ m, "schema") : NULL; 10 0 10
199 10 ▶ if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms;
Branch: 3 branches
BranchTrueFalseTotal
if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms; 8 2 10
if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms; 8 0 8
if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms; 8 0 8
200 }
201 0 return NULL;
202 }
203  
204 /* ---- the two answers ------------------------------------------------------- */
205  
206 /* What the document SAYS about an operation. NULL for an unknown id. */
207 3 static SV *oa_tools_doc(pTHX_ oa_api *a, SV *op_id) {
208 3 HV *item = NULL, *op, *out;
209 3 SV *path = NULL, *v;
210 3 const char *method = NULL;
211 HE *he;
212  
213 3 op = oa_tools_find_op(aTHX_ a, op_id, &item, &path, &method);
214 3 ▶ if (!op) { if (path) SvREFCNT_dec(path); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!op) { if (path) SvREFCNT_dec(path); return NULL; } 1 2 3
if (!op) { if (path) SvREFCNT_dec(path); return NULL; } 0 1 1
215  
216 2 out = newHV();
217 2 (void)hv_stores(out, "operationId", newSVsv(op_id));
218 {
219 2 ▶ SV *mu = newSVpv(method ? method : "", 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *mu = newSVpv(method ? method : "", 0); 2 0 2
220 2 char *s = SvPV_nolen(mu);
221 2 STRLEN i, l = SvCUR(mu);
222 8 ▶ for (i = 0; i < l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) 6 2 8
223 6 ▶ if (s[i] >= 'a' && s[i] <= 'z') s[i] = (char)(s[i] - 'a' + 'A');
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] >= 'a' && s[i] <= 'z') s[i] = (char)(s[i] - 'a' + 'A'); 6 0 6
if (s[i] >= 'a' && s[i] <= 'z') s[i] = (char)(s[i] - 'a' + 'A'); 6 0 6
224 2 (void)hv_stores(out, "method", mu);
225 }
226 2 ▶ (void)hv_stores(out, "path", path ? path : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "path", path ? path : newSVpvs("")); 0 2 2
227  
228 2 v = oa_get(aTHX_ op, "summary");
229 2 ▶ (void)hv_stores(out, "summary", v ? newSVsv(v) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "summary", v ? newSVsv(v) : newSVpvs("")); 1 1 2
230 2 v = oa_get(aTHX_ op, "description");
231 2 ▶ (void)hv_stores(out, "description", v ? newSVsv(v) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "description", v ? newSVsv(v) : newSVpvs("")); 1 1 2
232 2 v = oa_get(aTHX_ op, "tags");
233 2 ▶ (void)hv_stores(out, "tags", v ? newSVsv(v) : newRV_noinc((SV *)newAV()));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "tags", v ? newSVsv(v) : newRV_noinc((SV *)newAV())); 1 1 2
234 2 v = oa_get(aTHX_ op, "deprecated");
235 2 ▶ (void)hv_stores(out, "deprecated", newSViv(v && SvTRUE(v) ? 1 : 0));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(out, "deprecated", newSViv(v && SvTRUE(v) ? 1 : 0)); 1 1 2
(void)hv_stores(out, "deprecated", newSViv(v && SvTRUE(v) ? 1 : 0)); 1 0 1
236  
237 /* Extensions ride along: they are how a document says something this
238 * library has no opinion about. */
239 2 hv_iterinit(op);
240 12 ▶ while ((he = hv_iternext(op))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(op))) { 10 2 12
241 10 I32 kl; const char *k = hv_iterkey(he, &kl);
242 10 ▶ if (kl > 2 && k[0] == 'x' && k[1] == '-')
Branch: 3 branches
BranchTrueFalseTotal
if (kl > 2 && k[0] == 'x' && k[1] == '-') 10 0 10
if (kl > 2 && k[0] == 'x' && k[1] == '-') 1 9 10
if (kl > 2 && k[0] == 'x' && k[1] == '-') 1 0 1
243 1 (void)hv_store(out, k, kl, newSVsv(hv_iterval(op, he)), 0);
244 }
245  
246 2 return newRV_noinc((SV *)out);
247 }
248  
249 /* Every input an operation declares, as one JSON Schema object.
250 * NULL for an unknown id. */
251 31 static SV *oa_tools_schema(pTHX_ oa_api *a, SV *op_id) {
252 31 HV *item = NULL, *op, *props, *out;
253 AV *required;
254 HV *merged[64];
255 31 int nm = 0, i, src;
256 SV *body;
257  
258 31 op = oa_tools_find_op(aTHX_ a, op_id, &item, NULL, NULL);
259 31 ▶ if (!op) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!op) return NULL; 1 30 31
260  
261 /* Path-item parameters first, then the operation's own, an operation-level
262 * one replacing a path-level one with the same (name, in). The same
263 * precedence oa_compile_params applies - a description of the inputs that
264 * disagreed with the validator about which inputs exist would be a
265 * description nothing could act on. */
266 90 ▶ for (src = 0; src < 2; src++) {
Branch: 1 branch
BranchTrueFalseTotal
for (src = 0; src < 2; src++) { 60 30 90
267 60 ▶ HV *from = (src == 0) ? item : op;
Branch: 1 branch
BranchTrueFalseTotal
HV *from = (src == 0) ? item : op; 30 30 60
268 60 ▶ AV *list = from ? oa_av_of(oa_get(aTHX_ from, "parameters")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *list = from ? oa_av_of(oa_get(aTHX_ from, "parameters")) : NULL; 60 0 60
269 SSize_t j, n;
270 60 ▶ if (!list) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!list) continue; 31 29 60
271 29 n = av_len(list) + 1;
272 63 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 34 29 63
273 34 SV **e = av_fetch(list, j, 0);
274 34 ▶ HV *p = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *p = (e && *e) ? oa_hv_of(*e) : NULL; 34 0 34
HV *p = (e && *e) ? oa_hv_of(*e) : NULL; 34 0 34
275 SV *pn, *pi;
276 34 int k, hit = 0;
277 34 ▶ if (!p) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) continue; 0 34 34
278 34 pn = oa_get(aTHX_ p, "name");
279 34 pi = oa_get(aTHX_ p, "in");
280 34 ▶ if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue; 34 0 34
if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue; 34 0 34
if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue; 34 0 34
if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue; 0 34 34
281 50 ▶ for (k = 0; k < nm; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nm; k++) { 16 34 50
282 16 SV *qn = oa_get(aTHX_ merged[k], "name");
283 16 SV *qi = oa_get(aTHX_ merged[k], "in");
284 16 ▶ if (qn && qi && sv_eq(qn, pn) && sv_eq(qi, pi)) {
Branch: 4 branches
BranchTrueFalseTotal
if (qn && qi && sv_eq(qn, pn) && sv_eq(qi, pi)) { 16 0 16
if (qn && qi && sv_eq(qn, pn) && sv_eq(qi, pi)) { 16 0 16
if (qn && qi && sv_eq(qn, pn) && sv_eq(qi, pi)) { 0 16 16
if (qn && qi && sv_eq(qn, pn) && sv_eq(qi, pi)) { 0 0 0
285 0 merged[k] = p; hit = 1; break;
286 }
287 }
288 34 ▶ if (!hit && nm < 64) merged[nm++] = p;
Branch: 2 branches
BranchTrueFalseTotal
if (!hit && nm < 64) merged[nm++] = p; 34 0 34
if (!hit && nm < 64) merged[nm++] = p; 34 0 34
289 }
290 }
291  
292 30 props = newHV();
293 30 required = newAV();
294  
295 64 ▶ for (i = 0; i < nm; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nm; i++) { 34 30 64
296 34 HV *p = merged[i], *ps;
297 34 SV *pn = oa_get(aTHX_ p, "name");
298 34 SV *sch = oa_tools_param_schema(aTHX_ p);
299 34 SV *req = oa_get(aTHX_ p, "required");
300 STRLEN nl;
301 34 const char *nmp = SvPV_const(pn, nl);
302 34 ▶ int clash = (nl == 4 && memEQ(nmp, "body", 4));
Branch: 2 branches
BranchTrueFalseTotal
int clash = (nl == 4 && memEQ(nmp, "body", 4)); 6 28 34
int clash = (nl == 4 && memEQ(nmp, "body", 4)); 1 5 6
303 34 ▶ const char *key = clash ? "param_body" : nmp;
Branch: 1 branch
BranchTrueFalseTotal
const char *key = clash ? "param_body" : nmp; 33 1 34
304 34 ▶ I32 keyl = clash ? 10 : (I32)nl;
Branch: 1 branch
BranchTrueFalseTotal
I32 keyl = clash ? 10 : (I32)nl; 33 1 34
305  
306 34 ▶ ps = (sch && oa_hv_of(sch)) ? oa_conv_copy_hv(aTHX_ oa_hv_of(sch)) : newHV();
Branch: 2 branches
BranchTrueFalseTotal
ps = (sch && oa_hv_of(sch)) ? oa_conv_copy_hv(aTHX_ oa_hv_of(sch)) : newHV(); 34 0 34
ps = (sch && oa_hv_of(sch)) ? oa_conv_copy_hv(aTHX_ oa_hv_of(sch)) : newHV(); 34 0 34
307  
308 /* The prose lives on the parameter, not on its schema, and it is the
309 * only thing telling two same-typed parameters apart. */
310 34 ▶ if (!oa_get(aTHX_ ps, "description")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ ps, "description")) { 34 0 34
311 34 SV *pd = oa_get(aTHX_ p, "description");
312 34 ▶ if (pd && SvOK(pd)) (void)hv_stores(ps, "description", newSVsv(pd));
Branch: 2 branches
BranchTrueFalseTotal
if (pd && SvOK(pd)) (void)hv_stores(ps, "description", newSVsv(pd)); 2 32 34
if (pd && SvOK(pd)) (void)hv_stores(ps, "description", newSVsv(pd)); 2 0 2
313 }
314 34 ▶ if (clash) {
Branch: 1 branch
BranchTrueFalseTotal
if (clash) { 1 33 34
315 1 SV *d = oa_get(aTHX_ ps, "description");
316 SV *t;
317 1 ▶ if (d && SvOK(d)) { t = newSVsv(d); sv_catpvs(t, " (the `body` parameter)"); }
Branch: 2 branches
BranchTrueFalseTotal
if (d && SvOK(d)) { t = newSVsv(d); sv_catpvs(t, " (the `body` parameter)"); } 0 1 1
if (d && SvOK(d)) { t = newSVsv(d); sv_catpvs(t, " (the `body` parameter)"); } 0 0 0
318 1 else t = newSVpvs("the `body` parameter");
319 1 (void)hv_stores(ps, "description", t);
320 }
321  
322 34 (void)hv_store(props, key, keyl, newRV_noinc((SV *)ps), 0);
323 34 ▶ if (req && SvTRUE(req)) av_push(required, newSVpvn(key, (STRLEN)keyl));
Branch: 2 branches
BranchTrueFalseTotal
if (req && SvTRUE(req)) av_push(required, newSVpvn(key, (STRLEN)keyl)); 17 17 34
if (req && SvTRUE(req)) av_push(required, newSVpvn(key, (STRLEN)keyl)); 17 0 17
324 }
325  
326 30 body = oa_tools_body_schema(aTHX_ op);
327 30 ▶ if (body) {
Branch: 1 branch
BranchTrueFalseTotal
if (body) { 8 22 30
328 8 HV *rb = oa_hv_of(oa_get(aTHX_ op, "requestBody"));
329 8 ▶ SV *rq = rb ? oa_get(aTHX_ rb, "required") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *rq = rb ? oa_get(aTHX_ rb, "required") : NULL; 8 0 8
330 8 (void)hv_stores(props, "body", newSVsv(body));
331 8 ▶ if (rq && SvTRUE(rq)) av_push(required, newSVpvs("body"));
Branch: 2 branches
BranchTrueFalseTotal
if (rq && SvTRUE(rq)) av_push(required, newSVpvs("body")); 7 1 8
if (rq && SvTRUE(rq)) av_push(required, newSVpvs("body")); 7 0 7
332 }
333  
334 30 out = newHV();
335 30 (void)hv_stores(out, "type", newSVpvs("object"));
336 {
337 30 SV *pr = newRV_noinc((SV *)props);
338 30 (void)hv_stores(out, "properties", oa_tools_rewrite(aTHX_ pr, 0));
339 30 SvREFCNT_dec(pr);
340 }
341 30 ▶ if (av_len(required) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(required) >= 0) 18 12 30
342 18 (void)hv_stores(out, "required", newRV_noinc((SV *)required));
343 else
344 12 SvREFCNT_dec((SV *)required);
345  
346 /* Carry only the component schemas this operation can actually reach. A
347 * document with two hundred of them would otherwise put all two hundred
348 * into every answer, and JSON::Schema::Fast throws on an unresolvable
349 * $ref - so too little here is a loud failure, not a quiet one. */
350 {
351 30 HV *spec = oa_hv_of(a->spec);
352 30 ▶ HV *comp = spec ? oa_hv_of(oa_get(aTHX_ spec, "components")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *comp = spec ? oa_hv_of(oa_get(aTHX_ spec, "components")) : NULL; 30 0 30
353 30 ▶ HV *schemas = comp ? oa_hv_of(oa_get(aTHX_ comp, "schemas")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *schemas = comp ? oa_hv_of(oa_get(aTHX_ comp, "schemas")) : NULL; 28 2 30
354 30 ▶ if (schemas) {
Branch: 1 branch
BranchTrueFalseTotal
if (schemas) { 28 2 30
355 28 HV *want = newHV(), *defs = newHV();
356 28 int added = 1;
357 28 oa_tools_refs_in(aTHX_ oa_get(aTHX_ out, "properties"), want, 0);
358 92 ▶ while (added) {
Branch: 1 branch
BranchTrueFalseTotal
while (added) { 36 28 64
359 HE *he;
360 36 added = 0;
361 36 hv_iterinit(want);
362 44 ▶ while ((he = hv_iternext(want))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(want))) { 16 28 44
363 16 I32 kl; const char *k = hv_iterkey(he, &kl);
364 SV **s;
365 SV *rw;
366 16 ▶ if (hv_fetch(defs, k, kl, 0)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_fetch(defs, k, kl, 0)) continue; 8 8 16
367 8 s = hv_fetch(schemas, k, kl, 0);
368 /* A name that is not in components.schemas cannot be
369 * carried, and an EMPTY schema is not a safe stand-in: {}
370 * is valid JSON Schema that accepts ANYTHING, so the
371 * result compiled clean and then validated nothing. Refuse
372 * instead, which is the loud failure the comment above
373 * this block is relying on. */
374 8 ▶ if (!s || !*s)
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !*s) 8 0 8
if (!s || !*s) 0 8 8
375 0 croak("Open::API: operation_schema: $ref names '%.*s', "
376 "which is not in components.schemas",
377 (int)kl, k);
378 8 rw = oa_tools_rewrite(aTHX_ *s, 0);
379 8 (void)hv_store(defs, k, kl, rw, 0);
380 /* A schema can name others, so this widens `want` - which
381 * invalidates the iterator, hence one per pass. */
382 8 oa_tools_refs_in(aTHX_ rw, want, 0);
383 8 added = 1;
384 8 break;
385 }
386 }
387 28 ▶ if (HvUSEDKEYS(defs)) (void)hv_stores(out, "$defs", newRV_noinc((SV *)defs));
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(defs)) (void)hv_stores(out, "$defs", newRV_noinc((SV *)defs)); 0 28 28
if (HvUSEDKEYS(defs)) (void)hv_stores(out, "$defs", newRV_noinc((SV *)defs)); 7 21 28
388 21 else SvREFCNT_dec((SV *)defs);
389 28 SvREFCNT_dec((SV *)want);
390 }
391 }
392  
393 30 return newRV_noinc((SV *)out);
394 }
395  
396 #endif /* OA_TOOLS_H */
« include/oa_compile.h ↑ Summary include/oa_validate.h »