include/oa_tools.h

filter: Statementstmt 93.9% 200 / 213 Branchbran 67.9% 178 / 262 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.5% 378 / 475 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_plack.h ↑ Summary include/oa_compile.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 177 static void oa_tools_refs_hv(pTHX_ HV *h, HV *seen, int depth) {
45 HE *he;
46 177 hv_iterinit(h);
47 485 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 308 177 485
48 308 I32 kl; const char *k = hv_iterkey(he, &kl);
49 308 SV *v = hv_iterval(h, he);
50 308 ▶ 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)) { 152 156 308
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 14 138 152
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 14 0 14
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 14 0 14
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 14 0 14
51 14 STRLEN l; const char *p = SvPV_const(v, l);
52 14 ▶ 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)) { 14 0 14
if (l > OA_DEFS_PFX_LEN && memEQ(p, OA_DEFS_PFX, OA_DEFS_PFX_LEN)) { 14 0 14
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 14 const char *nm = p + OA_DEFS_PFX_LEN;
60 14 STRLEN nl = l - OA_DEFS_PFX_LEN;
61 14 const char *slash = (const char *)memchr(nm, '/', nl);
62 14 ▶ if (slash) nl = (STRLEN)(slash - nm);
Branch: 1 branch
BranchTrueFalseTotal
if (slash) nl = (STRLEN)(slash - nm); 1 13 14
63 14 ▶ 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); 14 0 14
64 }
65 14 continue;
66 }
67 294 oa_tools_refs_in(aTHX_ v, seen, depth + 1);
68 }
69 177 }
70  
71 /* Every "#/$defs/NAME" mentioned anywhere under `node`, as keys of `seen`. */
72 376 static void oa_tools_refs_in(pTHX_ SV *node, HV *seen, int depth) {
73 SV *rv;
74 376 ▶ if (!node || depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!node || depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return; 376 0 376
if (!node || depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return; 376 0 376
if (!node || depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return; 183 193 376
75 193 rv = SvRV(node);
76 193 ▶ 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; } 177 16 193
77 16 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 16 0 16
78 16 AV *av = (AV *)rv;
79 16 SSize_t i, n = av_len(av) + 1;
80 36 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 20 16 36
81 20 SV **e = av_fetch(av, i, 0);
82 20 ▶ 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); 20 0 20
if (e && *e) oa_tools_refs_in(aTHX_ *e, seen, depth + 1); 20 0 20
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 383 static SV *oa_tools_rewrite(pTHX_ SV *node, int depth) {
90 SV *rv;
91 383 ▶ if (!node) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!node) return newSV(0); 0 383 383
92 383 ▶ 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); 383 0 383
if (depth > OA_TOOLS_MAX_DEPTH || !SvROK(node)) return newSVsv(node); 186 197 383
93 197 rv = SvRV(node);
94  
95 197 ▶ if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) { 181 16 197
96 181 HV *src = (HV *)rv, *dst = newHV();
97 HE *he;
98 181 hv_iterinit(src);
99 494 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 313 181 494
100 313 I32 kl; const char *k = hv_iterkey(he, &kl);
101 313 SV *v = hv_iterval(src, he);
102 313 ▶ 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)) { 155 158 313
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 14 141 155
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 14 0 14
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 14 0 14
if (kl == 4 && memEQ(k, "$ref", 4) && v && !SvROK(v) && SvOK(v)) { 14 0 14
103 14 STRLEN l; const char *p = SvPV_const(v, l);
104 14 ▶ if (l > OA_SCHEMA_PFX_LEN
Branch: 1 branch
BranchTrueFalseTotal
if (l > OA_SCHEMA_PFX_LEN 14 0 14
105 14 ▶ && memEQ(p, OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(p, OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN)) { 14 0 14
106 14 SV *nr = newSVpvn(OA_DEFS_PFX, OA_DEFS_PFX_LEN);
107 14 sv_catpvn(nr, p + OA_SCHEMA_PFX_LEN, l - OA_SCHEMA_PFX_LEN);
108 14 (void)hv_store(dst, k, kl, nr, 0);
109 14 continue;
110 }
111 }
112 299 (void)hv_store(dst, k, kl, oa_tools_rewrite(aTHX_ v, depth + 1), 0);
113 }
114 181 return newRV_noinc((SV *)dst);
115 }
116  
117 16 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 16 0 16
118 16 AV *src = (AV *)rv, *dst = newAV();
119 16 SSize_t i, n = av_len(src) + 1;
120 36 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 20 16 36
121 20 SV **e = av_fetch(src, i, 0);
122 20 ▶ 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)); 20 0 20
av_push(dst, oa_tools_rewrite(aTHX_ (e && *e) ? *e : NULL, depth + 1)); 20 0 20
123 }
124 16 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 55 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 55 ▶ HV *spec = a ? oa_hv_of(a->spec) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *spec = a ? oa_hv_of(a->spec) : NULL; 55 0 55
137 55 ▶ 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; 55 0 55
138 HE *he;
139 STRLEN wl;
140 const char *want;
141  
142 55 ▶ if (!paths || !op_id || !SvOK(op_id)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!paths || !op_id || !SvOK(op_id)) return NULL; 55 0 55
if (!paths || !op_id || !SvOK(op_id)) return NULL; 55 0 55
if (!paths || !op_id || !SvOK(op_id)) return NULL; 0 55 55
143 55 want = SvPV_const(op_id, wl);
144  
145 55 hv_iterinit(paths);
146 85 ▶ while ((he = hv_iternext(paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(paths))) { 83 2 85
147 83 I32 pl; const char *pk = hv_iterkey(he, &pl);
148 83 HV *item = oa_hv_of(hv_iterval(paths, he));
149 int m;
150 83 ▶ if (!item) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!item) continue; 0 83 83
151 381 ▶ for (m = 0; oa_methods[m]; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; oa_methods[m]; m++) { 351 30 381
152 351 HV *op = oa_hv_of(oa_get(aTHX_ item, oa_methods[m]));
153 SV *oid;
154 STRLEN ol;
155 const char *ops;
156 422 ▶ if (!op) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!op) continue; 227 124 351
157 124 oid = oa_get(aTHX_ op, "operationId");
158 124 ▶ if (!oid || !SvOK(oid)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!oid || !SvOK(oid)) continue; 124 0 124
if (!oid || !SvOK(oid)) continue; 0 124 124
159 124 ops = SvPV_const(oid, ol);
160 124 ▶ if (ol != wl || memNE(ops, want, wl)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (ol != wl || memNE(ops, want, wl)) continue; 62 62 124
if (ol != wl || memNE(ops, want, wl)) continue; 9 53 62
161 53 ▶ if (item_out) *item_out = item;
Branch: 1 branch
BranchTrueFalseTotal
if (item_out) *item_out = item; 53 0 53
162 53 ▶ if (path_out) *path_out = newSVpvn(pk, (STRLEN)pl);
Branch: 1 branch
BranchTrueFalseTotal
if (path_out) *path_out = newSVpvn(pk, (STRLEN)pl); 3 50 53
163 53 ▶ if (method_out) *method_out = oa_methods[m];
Branch: 1 branch
BranchTrueFalseTotal
if (method_out) *method_out = oa_methods[m]; 3 50 53
164 53 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 54 static SV *oa_tools_param_schema(pTHX_ HV *p) {
173 54 SV *s = oa_get(aTHX_ p, "schema");
174 HV *content;
175 HE *he;
176 54 ▶ if (s && oa_hv_of(s)) return s;
Branch: 2 branches
BranchTrueFalseTotal
if (s && oa_hv_of(s)) return s; 54 0 54
if (s && oa_hv_of(s)) return s; 54 0 54
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 50 static SV *oa_tools_body_schema(pTHX_ HV *op) {
190 50 HV *rb = oa_hv_of(oa_get(aTHX_ op, "requestBody"));
191 50 ▶ 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; 14 36 50
192 HE *he;
193 50 ▶ if (!content) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!content) return NULL; 36 14 50
194 14 hv_iterinit(content);
195 16 ▶ while ((he = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(content))) { 16 0 16
196 16 I32 kl; const char *k = hv_iterkey(he, &kl);
197 16 HV *m = oa_hv_of(hv_iterval(content, he));
198 16 ▶ SV *ms = m ? oa_get(aTHX_ m, "schema") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *ms = m ? oa_get(aTHX_ m, "schema") : NULL; 16 0 16
199 16 ▶ 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; 14 2 16
if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms; 14 0 14
if (ms && oa_hv_of(ms) && oa_ctype_is_json(k, (STRLEN)kl)) return ms; 14 0 14
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 4 static SV *oa_tools_doc(pTHX_ oa_api *a, SV *op_id) {
208 4 HV *item = NULL, *op, *out;
209 4 SV *path = NULL, *v;
210 4 const char *method = NULL;
211 HE *he;
212  
213 4 op = oa_tools_find_op(aTHX_ a, op_id, &item, &path, &method);
214 4 ▶ if (!op) { if (path) SvREFCNT_dec(path); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!op) { if (path) SvREFCNT_dec(path); return NULL; } 1 3 4
if (!op) { if (path) SvREFCNT_dec(path); return NULL; } 0 1 1
215  
216 3 out = newHV();
217 3 (void)hv_stores(out, "operationId", newSVsv(op_id));
218 {
219 3 ▶ SV *mu = newSVpv(method ? method : "", 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *mu = newSVpv(method ? method : "", 0); 3 0 3
220 3 char *s = SvPV_nolen(mu);
221 3 STRLEN i, l = SvCUR(mu);
222 12 ▶ for (i = 0; i < l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) 9 3 12
223 9 ▶ 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'); 9 0 9
if (s[i] >= 'a' && s[i] <= 'z') s[i] = (char)(s[i] - 'a' + 'A'); 9 0 9
224 3 (void)hv_stores(out, "method", mu);
225 }
226 3 ▶ (void)hv_stores(out, "path", path ? path : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "path", path ? path : newSVpvs("")); 0 3 3
227  
228 3 v = oa_get(aTHX_ op, "summary");
229 3 ▶ (void)hv_stores(out, "summary", v ? newSVsv(v) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "summary", v ? newSVsv(v) : newSVpvs("")); 1 2 3
230 3 v = oa_get(aTHX_ op, "description");
231 3 ▶ (void)hv_stores(out, "description", v ? newSVsv(v) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "description", v ? newSVsv(v) : newSVpvs("")); 1 2 3
232 3 v = oa_get(aTHX_ op, "tags");
233 3 ▶ (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 2 3
234 3 v = oa_get(aTHX_ op, "deprecated");
235 3 ▶ (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)); 2 1 3
(void)hv_stores(out, "deprecated", newSViv(v && SvTRUE(v) ? 1 : 0)); 2 0 2
236  
237 /* Extensions ride along: they are how a document says something this
238 * library has no opinion about. */
239 3 hv_iterinit(op);
240 16 ▶ while ((he = hv_iternext(op))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(op))) { 13 3 16
241 13 I32 kl; const char *k = hv_iterkey(he, &kl);
242 13 ▶ if (kl > 2 && k[0] == 'x' && k[1] == '-')
Branch: 3 branches
BranchTrueFalseTotal
if (kl > 2 && k[0] == 'x' && k[1] == '-') 13 0 13
if (kl > 2 && k[0] == 'x' && k[1] == '-') 1 12 13
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 3 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 51 static SV *oa_tools_schema(pTHX_ oa_api *a, SV *op_id) {
252 51 HV *item = NULL, *op, *props, *out;
253 AV *required;
254 HV *merged[64];
255 51 int nm = 0, i, src;
256 SV *body;
257  
258 51 op = oa_tools_find_op(aTHX_ a, op_id, &item, NULL, NULL);
259 51 ▶ if (!op) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!op) return NULL; 1 50 51
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 150 ▶ for (src = 0; src < 2; src++) {
Branch: 1 branch
BranchTrueFalseTotal
for (src = 0; src < 2; src++) { 100 50 150
267 100 ▶ HV *from = (src == 0) ? item : op;
Branch: 1 branch
BranchTrueFalseTotal
HV *from = (src == 0) ? item : op; 50 50 100
268 100 ▶ 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; 100 0 100
269 SSize_t j, n;
270 100 ▶ if (!list) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!list) continue; 54 46 100
271 46 n = av_len(list) + 1;
272 100 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 54 46 100
273 54 SV **e = av_fetch(list, j, 0);
274 54 ▶ HV *p = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *p = (e && *e) ? oa_hv_of(*e) : NULL; 54 0 54
HV *p = (e && *e) ? oa_hv_of(*e) : NULL; 54 0 54
275 SV *pn, *pi;
276 54 int k, hit = 0;
277 54 ▶ if (!p) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) continue; 0 54 54
278 54 pn = oa_get(aTHX_ p, "name");
279 54 pi = oa_get(aTHX_ p, "in");
280 54 ▶ if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue; 54 0 54
if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue; 54 0 54
if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue; 54 0 54
if (!pn || !pi || !SvOK(pn) || !SvOK(pi)) continue; 0 54 54
281 76 ▶ for (k = 0; k < nm; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nm; k++) { 22 54 76
282 22 SV *qn = oa_get(aTHX_ merged[k], "name");
283 22 SV *qi = oa_get(aTHX_ merged[k], "in");
284 22 ▶ 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)) { 22 0 22
if (qn && qi && sv_eq(qn, pn) && sv_eq(qi, pi)) { 22 0 22
if (qn && qi && sv_eq(qn, pn) && sv_eq(qi, pi)) { 0 22 22
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 54 ▶ if (!hit && nm < 64) merged[nm++] = p;
Branch: 2 branches
BranchTrueFalseTotal
if (!hit && nm < 64) merged[nm++] = p; 54 0 54
if (!hit && nm < 64) merged[nm++] = p; 54 0 54
289 }
290 }
291  
292 50 props = newHV();
293 50 required = newAV();
294  
295 104 ▶ for (i = 0; i < nm; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nm; i++) { 54 50 104
296 54 HV *p = merged[i], *ps;
297 54 SV *pn = oa_get(aTHX_ p, "name");
298 54 SV *sch = oa_tools_param_schema(aTHX_ p);
299 54 SV *req = oa_get(aTHX_ p, "required");
300 STRLEN nl;
301 54 const char *nmp = SvPV_const(pn, nl);
302 54 ▶ int clash = (nl == 4 && memEQ(nmp, "body", 4));
Branch: 2 branches
BranchTrueFalseTotal
int clash = (nl == 4 && memEQ(nmp, "body", 4)); 12 42 54
int clash = (nl == 4 && memEQ(nmp, "body", 4)); 1 11 12
303 54 ▶ const char *key = clash ? "param_body" : nmp;
Branch: 1 branch
BranchTrueFalseTotal
const char *key = clash ? "param_body" : nmp; 53 1 54
304 54 ▶ I32 keyl = clash ? 10 : (I32)nl;
Branch: 1 branch
BranchTrueFalseTotal
I32 keyl = clash ? 10 : (I32)nl; 53 1 54
305  
306 54 ▶ 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(); 54 0 54
ps = (sch && oa_hv_of(sch)) ? oa_conv_copy_hv(aTHX_ oa_hv_of(sch)) : newHV(); 54 0 54
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 54 ▶ if (!oa_get(aTHX_ ps, "description")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ ps, "description")) { 54 0 54
311 54 SV *pd = oa_get(aTHX_ p, "description");
312 54 ▶ 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)); 22 32 54
if (pd && SvOK(pd)) (void)hv_stores(ps, "description", newSVsv(pd)); 22 0 22
313 }
314 54 ▶ if (clash) {
Branch: 1 branch
BranchTrueFalseTotal
if (clash) { 1 53 54
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 54 (void)hv_store(props, key, keyl, newRV_noinc((SV *)ps), 0);
323 54 ▶ 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)); 37 17 54
if (req && SvTRUE(req)) av_push(required, newSVpvn(key, (STRLEN)keyl)); 25 12 37
324 }
325  
326 50 body = oa_tools_body_schema(aTHX_ op);
327 50 ▶ if (body) {
Branch: 1 branch
BranchTrueFalseTotal
if (body) { 14 36 50
328 14 HV *rb = oa_hv_of(oa_get(aTHX_ op, "requestBody"));
329 14 ▶ SV *rq = rb ? oa_get(aTHX_ rb, "required") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *rq = rb ? oa_get(aTHX_ rb, "required") : NULL; 14 0 14
330 14 (void)hv_stores(props, "body", newSVsv(body));
331 14 ▶ if (rq && SvTRUE(rq)) av_push(required, newSVpvs("body"));
Branch: 2 branches
BranchTrueFalseTotal
if (rq && SvTRUE(rq)) av_push(required, newSVpvs("body")); 13 1 14
if (rq && SvTRUE(rq)) av_push(required, newSVpvs("body")); 13 0 13
332 }
333  
334 50 out = newHV();
335 50 (void)hv_stores(out, "type", newSVpvs("object"));
336 {
337 50 SV *pr = newRV_noinc((SV *)props);
338 50 (void)hv_stores(out, "properties", oa_tools_rewrite(aTHX_ pr, 0));
339 50 SvREFCNT_dec(pr);
340 }
341 50 ▶ if (av_len(required) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(required) >= 0) 32 18 50
342 32 (void)hv_stores(out, "required", newRV_noinc((SV *)required));
343 else
344 18 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 50 HV *spec = oa_hv_of(a->spec);
352 50 ▶ 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; 50 0 50
353 50 ▶ 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; 48 2 50
354 50 ▶ if (schemas) {
Branch: 1 branch
BranchTrueFalseTotal
if (schemas) { 48 2 50
355 48 HV *want = newHV(), *defs = newHV();
356 48 int added = 1;
357 48 oa_tools_refs_in(aTHX_ oa_get(aTHX_ out, "properties"), want, 0);
358 158 ▶ while (added) {
Branch: 1 branch
BranchTrueFalseTotal
while (added) { 62 48 110
359 HE *he;
360 62 added = 0;
361 62 hv_iterinit(want);
362 77 ▶ while ((he = hv_iternext(want))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(want))) { 29 48 77
363 29 I32 kl; const char *k = hv_iterkey(he, &kl);
364 SV **s;
365 SV *rw;
366 29 ▶ if (hv_fetch(defs, k, kl, 0)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_fetch(defs, k, kl, 0)) continue; 15 14 29
367 14 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 14 ▶ if (!s || !*s)
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !*s) 14 0 14
if (!s || !*s) 0 14 14
375 0 croak("Open::API: operation_schema: $ref names '%.*s', "
376 "which is not in components.schemas",
377 (int)kl, k);
378 14 rw = oa_tools_rewrite(aTHX_ *s, 0);
379 14 (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 14 oa_tools_refs_in(aTHX_ rw, want, 0);
383 14 added = 1;
384 14 break;
385 }
386 }
387 48 ▶ 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 48 48
if (HvUSEDKEYS(defs)) (void)hv_stores(out, "$defs", newRV_noinc((SV *)defs)); 13 35 48
388 35 else SvREFCNT_dec((SV *)defs);
389 48 SvREFCNT_dec((SV *)want);
390 }
391 }
392  
393 50 return newRV_noinc((SV *)out);
394 }
395  
396 #endif /* OA_TOOLS_H */
« include/oa_plack.h ↑ Summary include/oa_compile.h »