include/oa_compile.h

filter: Statementstmt 92.7% 911 / 982 Branchbran 71.9% 909 / 1264 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.0% 1820 / 2246 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_tools.h ↑ Summary include/oa_validate.h »
1 #ifndef OA_COMPILE_H
2 #define OA_COMPILE_H
3  
4 /* The spec compiler: runs once inside Open::API->new, after the 3.1 gate.
5 * Walks the decoded document and builds the operation table - every path
6 * template pre-split into segments, every parameter/body/response schema
7 * compiled through the JSF ABI - so the per-request path (oa_route.h /
8 * oa_validate.h, phase 2) never touches the document again.
9 *
10 * SV ownership: every SV stored in the table is first pushed onto the api's
11 * keepalive AV (oa_keep), which owns it; the structs hold borrowed pointers.
12 * oa_ops_free only frees the C arrays - dropping the keep AV in oa_api_free
13 * releases all SVs, including the compiled JSF handles (blessed SVs whose
14 * DESTROY frees the underlying validators). */
15  
16 enum { OA_IN_PATH = 0, OA_IN_QUERY, OA_IN_HEADER, OA_IN_COOKIE, OA_IN_N };
17  
18 /* security scheme kinds (oauth2 / openIdConnect are carried as BEARER: the
19 * credential is the access token; flow automation is out of scope) */
20 /* OA_SEC_OTHER carries a scheme the specification defines but this library
21 * cannot settle by itself: mutualTLS (decided by the transport, and carrying
22 * no credential in the request at all) and an http scheme such as digest,
23 * which needs a challenge/response exchange. Refusing the document was worse
24 * than carrying it - a valid document became unusable - so the declaration is
25 * kept and the application's checker decides, exactly as scopes already work. */
26 enum { OA_SEC_APIKEY = 0, OA_SEC_BEARER, OA_SEC_BASIC, OA_SEC_OTHER };
27  
28 typedef struct oa_seg { SV *lit; SV *pname; } oa_seg; /* lit XOR pname */
29 /* `ctype` is set only for a parameter declared with `content` instead of
30 * `schema`: the value on the wire is then a document of that media type, and
31 * has to be decoded before the schema sees it. NULL is the ordinary case. */
32 /* How a parameter's value is spelled on the wire. The default depends on
33 * where the parameter lives: `simple` in a path or header, `form` in a query
34 * or cookie. `explode` defaults to TRUE for `form` and false for every other
35 * style, which is a per-style default rather than a global one.
36 *
37 * A value is split on the RAW text and each piece percent-decoded after,
38 * never the other way round: %2C is a comma that belongs to the value, not a
39 * delimiter, and decoding first would split `a%2Cb` into two elements. */
40 enum {
41 OA_ST_SIMPLE = 0, /* path, header: a,b,c */
42 OA_ST_LABEL, /* path: .a.b.c */
43 OA_ST_MATRIX, /* path: ;name=a,b / ;name=a;name=b */
44 OA_ST_FORM, /* query, cookie: ?n=a&n=b / ?n=a,b */
45 OA_ST_SPACE, /* query: ?n=a%20b */
46 OA_ST_PIPE, /* query: ?n=a|b */
47 OA_ST_DEEP /* query: ?n[k]=v */
48 };
49  
50 typedef struct oa_param {
51 SV *name; SV *handle; SV *ctype; int required; int is_array;
52 /* An object-typed parameter is assembled from its serialized form the way
53 * an array is. `props` holds the declared property names, and is needed
54 * only by form+explode, where the members arrive as SEPARATE top-level
55 * query keys (`R=100&G=200&B=150`) with nothing but the schema to say
56 * which keys belong to this parameter. NULL when there are none. */
57 int is_object; SV *props;
58 int style; int explode; int deprecated; int allow_empty;
59 /* allowReserved: a CLIENT-side rule. The reserved set may go on the wire
60 * unencoded, so this is read by the URL builder, never by the validator -
61 * a server decodes either spelling to the same value. */
62 int allow_reserved;
63 /* A list delimiter a style cannot spell, from Swagger 2.0 through the
64 * x-oa-collectionFormat extension (plan_openapi_v2 D5): `delim` overrides
65 * the style's delimiter at the top level (0 means "by style"), `idelim`
66 * holds the delimiter of each nested array level beneath it (a 2.0 Items
67 * Object carries its own collectionFormat), `ndepth` how many there are. */
68 char delim; char idelim[4]; int ndepth;
69 } oa_param;
70 /* `kind` is OA_MT_*: which decoder the DECLARED type wants, decided once at
71 * compile time. A range defers that to the request's own type. handle NULL
72 * still means pass-through. */
73 typedef struct oa_body { SV *ctype; SV *handle; int kind; SV *encoding; } oa_body;
74 typedef struct oa_resp { SV *status; SV *ctype; SV *handle; } oa_resp;
75 /* A declared response header. Keyed by STATUS, not by media type: headers
76 * belong to the Response Object, so a status declaring two content types must
77 * not carry two copies, and a header-only status (204, a 3xx with Location)
78 * must still be checked even though it compiles no oa_resp row at all - the
79 * response row loop skips anything without JSON `content`.
80 *
81 * The value is an oa_param because a Header Object IS a Parameter Object
82 * without `name`/`in`, so oa_check_param applies unchanged. */
83 typedef struct oa_rhdr { SV *status; oa_param p; } oa_rhdr;
84  
85 /* `oauthish` is 1 for oauth2 and openIdConnect. It cannot be derived from
86 * `type`: both collapse to OA_SEC_BEARER, which http/bearer uses too, and the
87 * specification lets only those two carry scopes in a requirement. */
88 typedef struct oa_scheme { SV *name; int type; int loc; SV *pname;
89 int oauthish; } oa_scheme;
90 typedef struct oa_secitem { int scheme; SV *scopes; } oa_secitem; /* scopes AV rv or NULL */
91 typedef struct oa_secalt { oa_secitem *items; int n; } oa_secalt;
92  
93 /* Response-validation coverage, per operation. Every response either is
94 * checked or is skipped for exactly one named reason, so what was not
95 * looked at is a reported number rather than a silent gap. `seen` is the
96 * sampling counter: deterministic 1-in-N, which is cheaper than a random
97 * draw and gives a stable denominator. */
98 typedef struct oa_rvstat {
99 unsigned long total; /* responses reaching the check */
100 unsigned long seen; /* the sampling counter */
101 unsigned long sampled; /* passed the sampling gate */
102 unsigned long checked; /* decoded and validated */
103 unsigned long violations; /* validated and did not conform */
104 unsigned long skip_ctype; /* not a declared JSON content type */
105 unsigned long skip_size; /* Content-Length absent or over max_body */
106 unsigned long skip_body; /* body not a plain scalar (fh/stream) */
107 unsigned long skip_schema; /* the status declares no schema */
108 unsigned long skip_decode; /* the body is not decodable JSON */
109 unsigned long hdr_checked; /* responses whose headers were checked */
110 unsigned long hdr_missing; /* a declared required header was absent */
111 unsigned long hdr_invalid; /* a declared header failed its schema */
112 } oa_rvstat;
113  
114 typedef struct oa_op {
115 SV *op_id, *method, *path; /* method stored lowercase */
116 oa_seg *segs; int nsegs;
117 oa_param *params[OA_IN_N]; int nparams[OA_IN_N];
118 oa_body *bodies; int nbodies; int body_required;
119 oa_resp *resps; int nresps;
120 oa_rhdr *rhdrs; int nrhdrs; /* declared response headers, by status */
121 /* This operation's own server path prefix, when it or its path item
122 * declares `servers` and the caller opted in. NULL is the ordinary case,
123 * where the document-level prefix (a->prefix) applies instead. In `keep`;
124 * borrowed. */
125 SV *prefix;
126 oa_secalt *sec; int nsec; /* requirement alternatives (OR) */
127 int own_sec; /* 1 = op-level copy, 0 = shares rootsec */
128 oa_rvstat rv; /* response-validation coverage */
129 } oa_op;
130  
131 typedef struct oa_ops {
132 oa_op *ops; int n, cap;
133 /* The order the ROUTER walks `ops` in, most specific first. It is a
134 * separate index rather than a sort of `ops` itself because `by_id` holds
135 * INDICES into that array, and reordering it would silently point every
136 * operationId at the wrong operation. See oa_order_routes. */
137 int *order;
138 HV *by_id; /* operationId -> IV index */
139 oa_scheme *schemes; int nschemes; /* components.securitySchemes */
140 oa_secalt *rootsec; int nrootsec; /* document-level security */
141 int v30; /* the document is OpenAPI 3.0.x */
142 } oa_ops;
143  
144 6945 static void oa_secalts_free(oa_secalt *alts, int n) {
145 int i;
146 6945 ▶ if (!alts) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!alts) return; 5105 1840 6945
147 5496 ▶ for (i = 0; i < n; i++) free(alts[i].items);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) free(alts[i].items); 3656 1840 5496
148 1840 free(alts);
149 }
150  
151 5730 static void oa_ops_free(pTHX_ void *p) {
152 5730 oa_ops *t = (oa_ops *)p;
153 int i, loc;
154 5730 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 5730 5730
155 20983 ▶ for (i = 0; i < t->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n; i++) { 15253 5730 20983
156 15253 oa_op *o = &t->ops[i];
157 15253 free(o->segs);
158 76265 ▶ for (loc = 0; loc < OA_IN_N; loc++) free(o->params[loc]);
Branch: 1 branch
BranchTrueFalseTotal
for (loc = 0; loc < OA_IN_N; loc++) free(o->params[loc]); 61012 15253 76265
159 15253 free(o->bodies);
160 15253 free(o->resps);
161 15253 free(o->rhdrs);
162 15253 ▶ if (o->own_sec) oa_secalts_free(o->sec, o->nsec);
Branch: 1 branch
BranchTrueFalseTotal
if (o->own_sec) oa_secalts_free(o->sec, o->nsec); 1215 14038 15253
163 }
164 5730 free(t->ops);
165 5730 free(t->order);
166 5730 oa_secalts_free(t->rootsec, t->nrootsec);
167 5730 free(t->schemes);
168 5730 ▶ if (t->by_id) SvREFCNT_dec((SV *)t->by_id);
Branch: 1 branch
BranchTrueFalseTotal
if (t->by_id) SvREFCNT_dec((SV *)t->by_id); 5730 0 5730
169 5730 free(t);
170 }
171  
172 /* ---- base64 (Basic credentials, both directions) -------------------------- */
173  
174 static const char oa_b64_al[] =
175 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
176  
177 0 static SV *oa_b64_encode(pTHX_ const char *in, STRLEN len) {
178 0 SV *out = newSV((len + 2) / 3 * 4 + 1);
179 char *d;
180 STRLEN i;
181 0 SvPOK_on(out);
182 0 d = SvPVX(out);
183 0 ▶ for (i = 0; i + 2 < len; i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < len; i += 3) { 0 0 0
184 0 U32 v = ((U32)(U8)in[i] << 16) | ((U32)(U8)in[i+1] << 8) | (U8)in[i+2];
185 0 *d++ = oa_b64_al[v >> 18]; *d++ = oa_b64_al[(v >> 12) & 63];
186 0 *d++ = oa_b64_al[(v >> 6) & 63]; *d++ = oa_b64_al[v & 63];
187 }
188 0 ▶ if (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < len) { 0 0 0
189 0 U32 v = (U32)(U8)in[i] << 16;
190 0 int two = (i + 1 < len);
191 0 ▶ if (two) v |= (U32)(U8)in[i+1] << 8;
Branch: 1 branch
BranchTrueFalseTotal
if (two) v |= (U32)(U8)in[i+1] << 8; 0 0 0
192 0 *d++ = oa_b64_al[v >> 18]; *d++ = oa_b64_al[(v >> 12) & 63];
193 0 ▶ *d++ = two ? oa_b64_al[(v >> 6) & 63] : '=';
Branch: 1 branch
BranchTrueFalseTotal
*d++ = two ? oa_b64_al[(v >> 6) & 63] : '='; 0 0 0
194 0 *d++ = '=';
195 }
196 0 *d = '\0';
197 0 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
198 0 return out;
199 }
200  
201 26 static int oa_b64_val(char c) {
202 26 ▶ if (c >= 'A' && c <= 'Z') return c - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'Z') return c - 'A'; 22 4 26
if (c >= 'A' && c <= 'Z') return c - 'A'; 10 12 22
203 16 ▶ if (c >= 'a' && c <= 'z') return c - 'a' + 26;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'z') return c - 'a' + 26; 12 4 16
if (c >= 'a' && c <= 'z') return c - 'a' + 26; 12 0 12
204 4 ▶ if (c >= '0' && c <= '9') return c - '0' + 52;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0' + 52; 4 0 4
if (c >= '0' && c <= '9') return c - '0' + 52; 4 0 4
205 0 ▶ if (c == '+') return 62;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '+') return 62; 0 0 0
206 0 ▶ if (c == '/') return 63;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '/') return 63; 0 0 0
207 0 return -1;
208 }
209  
210 3 static SV *oa_b64_decode(pTHX_ const char *in, STRLEN len) { /* NULL on junk */
211 SV *out;
212 char *d;
213 3 U32 acc = 0;
214 3 int nb = 0;
215 STRLEN i;
216 5 ▶ while (len && (in[len-1] == '=' || isSPACE((U8)in[len-1]))) len--;
Branch: 3 branches
BranchTrueFalseTotal
while (len && (in[len-1] == '=' || isSPACE((U8)in[len-1]))) len--; 5 0 5
while (len && (in[len-1] == '=' || isSPACE((U8)in[len-1]))) len--; 2 3 5
while (len && (in[len-1] == '=' || isSPACE((U8)in[len-1]))) len--; 0 3 3
217 3 out = newSV(len / 4 * 3 + 4);
218 3 SvPOK_on(out);
219 3 d = SvPVX(out);
220 29 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 26 3 29
221 26 int v = oa_b64_val(in[i]);
222 26 ▶ if (v < 0) { SvREFCNT_dec(out); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) { SvREFCNT_dec(out); return NULL; } 0 26 26
223 26 acc = (acc << 6) | (U32)v;
224 26 ▶ if (++nb == 4) { *d++ = (char)(acc >> 16); *d++ = (char)(acc >> 8);
Branch: 1 branch
BranchTrueFalseTotal
if (++nb == 4) { *d++ = (char)(acc >> 16); *d++ = (char)(acc >> 8); 5 21 26
225 5 *d++ = (char)acc; acc = 0; nb = 0; }
226 }
227 3 ▶ if (nb == 3) { *d++ = (char)(acc >> 10); *d++ = (char)(acc >> 2); }
Branch: 1 branch
BranchTrueFalseTotal
if (nb == 3) { *d++ = (char)(acc >> 10); *d++ = (char)(acc >> 2); } 2 1 3
228 1 ▶ else if (nb == 2) { *d++ = (char)(acc >> 4); }
Branch: 1 branch
BranchTrueFalseTotal
else if (nb == 2) { *d++ = (char)(acc >> 4); } 0 1 1
229 1 ▶ else if (nb == 1) { SvREFCNT_dec(out); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
else if (nb == 1) { SvREFCNT_dec(out); return NULL; } 0 1 1
230 3 *d = '\0';
231 3 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
232 3 return out;
233 }
234  
235 /* ---- small helpers -------------------------------------------------------- */
236  
237 185685 static SV *oa_keep(pTHX_ oa_api *a, SV *sv) { /* keep AV takes ownership */
238 185685 av_push(a->keep, sv);
239 185685 return sv;
240 }
241 1354718 static HV *oa_hv_of(SV *sv) {
242 1354718 ▶ return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) ? (HV *)SvRV(sv) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) ? (HV *)SvRV(sv) : NULL; 1028793 325925 1354718
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) ? (HV *)SvRV(sv) : NULL; 1026377 2416 1028793
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) ? (HV *)SvRV(sv) : NULL; 1026112 265 1026377
243 }
244 291918 static AV *oa_av_of(SV *sv) {
245 291918 ▶ return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) ? (AV *)SvRV(sv) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) ? (AV *)SvRV(sv) : NULL; 138852 153066 291918
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) ? (AV *)SvRV(sv) : NULL; 115710 23142 138852
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) ? (AV *)SvRV(sv) : NULL; 94868 20842 115710
246 }
247 4542398 static SV *oa_get(pTHX_ HV *h, const char *k) {
248 4542398 ▶ SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL; 4542398 0 4542398
249 4542398 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 1344664 3197734 4542398
return (e && *e && SvOK(*e)) ? *e : NULL; 1344664 0 1344664
return (e && *e && SvOK(*e)) ? *e : NULL; 1344664 0 1344664
250 }
251  
252 /* What a reference into components.schemas looks like. Defined here rather
253 * than beside oa_rewrite_refs because oa_schema_is_array below reads it too,
254 * and the two must not drift apart on the spelling. */
255 #define OA_COMP_PFX "#/components/schemas/"
256 #define OA_COMP_PFX_LEN 21
257  
258 /* A component schema by name, from the normalised document. ->spec keeps its
259 * refs in `#/components/schemas/` form: the `#/$defs/` rewrite happens on the
260 * copy oa_compile_schema makes, not here. */
261 2 static SV *oa_comp_schema(pTHX_ oa_api *a, const char *nm, STRLEN nl) {
262 2 ▶ HV *doc = a && a->spec ? oa_hv_of(a->spec) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *doc = a && a->spec ? oa_hv_of(a->spec) : NULL; 2 0 2
HV *doc = a && a->spec ? oa_hv_of(a->spec) : NULL; 2 0 2
263 2 ▶ HV *comp = doc ? oa_hv_of(oa_get(aTHX_ doc, "components")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *comp = doc ? oa_hv_of(oa_get(aTHX_ doc, "components")) : NULL; 2 0 2
264 2 ▶ HV *sch = comp ? oa_hv_of(oa_get(aTHX_ comp, "schemas")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *sch = comp ? oa_hv_of(oa_get(aTHX_ comp, "schemas")) : NULL; 2 0 2
265 2 ▶ SV **e = sch ? hv_fetch(sch, nm, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = sch ? hv_fetch(sch, nm, (I32)nl, 0) : NULL; 2 0 2
266 2 ▶ return (e && *e && oa_hv_of(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 2 0 2
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 2 0 2
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 2 0 2
267 }
268  
269 #define OA_ARR_MAX_DEPTH 8
270  
271 /* Is this schema an array? `type` decides it directly, but the type may sit
272 * behind a $ref or inside an allOf/oneOf/anyOf - and that matters, because
273 * this is what tells the query parser to accumulate repeat keys. Reading only
274 * a literal `type` meant an array declared through a component was invisible,
275 * so ?v=a&v=b collapsed to the last value alone. Bounded, because a document
276 * can point a $ref at itself. */
277 43091 static int oa_schema_is_type(pTHX_ oa_api *a, SV *schema, int depth,
278 const char *tn, STRLEN tnl) {
279 43091 HV *h = oa_hv_of(schema);
280 SV *t, *r;
281 int k;
282 static const char *const unions[] = { "allOf", "oneOf", "anyOf", NULL };
283  
284 43091 ▶ if (!h || depth > OA_ARR_MAX_DEPTH) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || depth > OA_ARR_MAX_DEPTH) return 0; 43091 0 43091
if (!h || depth > OA_ARR_MAX_DEPTH) return 0; 0 43091 43091
285  
286 43091 t = oa_get(aTHX_ h, "type");
287 43091 ▶ if (t) {
Branch: 1 branch
BranchTrueFalseTotal
if (t) { 43089 2 43091
288 43089 ▶ if (!SvROK(t)) { STRLEN l; const char *p = SvPV_const(t, l);
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(t)) { STRLEN l; const char *p = SvPV_const(t, l); 43087 2 43089
289 43087 ▶ if (l == tnl && memEQ(p, tn, tnl)) return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (l == tnl && memEQ(p, tn, tnl)) return 1; } 11490 31597 43087
if (l == tnl && memEQ(p, tn, tnl)) return 1; } 7493 3997 11490
290 else {
291 2 AV *av = oa_av_of(t);
292 2 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 2 0 2
293 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 2 6
294 4 SV **e = av_fetch(av, i, 0);
295 4 ▶ if (e && *e) { STRLEN l; const char *p = SvPV_const(*e, l);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) { STRLEN l; const char *p = SvPV_const(*e, l); 4 0 4
if (e && *e) { STRLEN l; const char *p = SvPV_const(*e, l); 4 0 4
296 4 ▶ if (l == tnl && memEQ(p, tn, tnl)) return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (l == tnl && memEQ(p, tn, tnl)) return 1; } 1 3 4
if (l == tnl && memEQ(p, tn, tnl)) return 1; } 0 1 1
297 }
298 }
299 }
300  
301 /* through a reference into components.schemas */
302 35598 r = oa_get(aTHX_ h, "$ref");
303 35598 ▶ if (r && !SvROK(r)) {
Branch: 2 branches
BranchTrueFalseTotal
if (r && !SvROK(r)) { 2 35596 35598
if (r && !SvROK(r)) { 2 0 2
304 2 STRLEN rl; const char *rp = SvPV_const(r, rl);
305 2 ▶ if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 2 0 2
if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 2 0 2
306 2 SV *tgt = oa_comp_schema(aTHX_ a, rp + OA_COMP_PFX_LEN,
307 rl - OA_COMP_PFX_LEN);
308 2 ▶ if (tgt && oa_schema_is_type(aTHX_ a, tgt, depth + 1, tn, tnl))
Branch: 2 branches
BranchTrueFalseTotal
if (tgt && oa_schema_is_type(aTHX_ a, tgt, depth + 1, tn, tnl)) 2 0 2
if (tgt && oa_schema_is_type(aTHX_ a, tgt, depth + 1, tn, tnl)) 1 1 2
309 1 return 1;
310 }
311 }
312  
313 /* or inside a union: one member is enough to make repeat keys right */
314 142388 ▶ for (k = 0; unions[k]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; unions[k]; k++) { 106791 35597 142388
315 106791 AV *av = oa_av_of(oa_get(aTHX_ h, unions[k]));
316 106791 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 0 106791 106791
317 106791 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 106791 106791
318 0 SV **e = av_fetch(av, i, 0);
319 0 ▶ if (e && *e && oa_schema_is_type(aTHX_ a, *e, depth + 1, tn, tnl))
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && oa_schema_is_type(aTHX_ a, *e, depth + 1, tn, tnl)) 0 0 0
if (e && *e && oa_schema_is_type(aTHX_ a, *e, depth + 1, tn, tnl)) 0 0 0
if (e && *e && oa_schema_is_type(aTHX_ a, *e, depth + 1, tn, tnl)) 0 0 0
320 0 return 1;
321 }
322 }
323 35597 return 0;
324 }
325  
326 22161 static int oa_schema_is_array(pTHX_ oa_api *a, SV *schema, int depth) {
327 22161 return oa_schema_is_type(aTHX_ a, schema, depth, "array", 5);
328 }
329  
330 20928 static int oa_schema_is_object(pTHX_ oa_api *a, SV *schema, int depth) {
331 20928 return oa_schema_is_type(aTHX_ a, schema, depth, "object", 6);
332 }
333  
334 /* The declared property names, following the same $ref and union paths the
335 * type test does. Only form+explode needs them: there the members arrive as
336 * separate top-level query keys and the schema is the only thing that says
337 * which of them belong to this parameter. Returns a new AV ref, or NULL. */
338 30 static void oa_schema_props_into(pTHX_ oa_api *a, SV *schema, int depth,
339 AV *out) {
340 30 HV *h = oa_hv_of(schema);
341 HV *props;
342 SV *r;
343 int k;
344 static const char *const unions[] = { "allOf", "oneOf", "anyOf", NULL };
345  
346 30 ▶ if (!h || depth > OA_ARR_MAX_DEPTH) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || depth > OA_ARR_MAX_DEPTH) return; 30 0 30
if (!h || depth > OA_ARR_MAX_DEPTH) return; 0 30 30
347  
348 30 props = oa_hv_of(oa_get(aTHX_ h, "properties"));
349 30 ▶ if (props) {
Branch: 1 branch
BranchTrueFalseTotal
if (props) { 27 3 30
350 HE *he;
351 27 hv_iterinit(props);
352 105 ▶ while ((he = hv_iternext(props))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(props))) { 78 27 105
353 78 I32 kl; const char *kp = hv_iterkey(he, &kl);
354 78 av_push(out, newSVpvn(kp, (STRLEN)kl));
355 }
356 }
357  
358 30 r = oa_get(aTHX_ h, "$ref");
359 30 ▶ if (r && !SvROK(r)) {
Branch: 2 branches
BranchTrueFalseTotal
if (r && !SvROK(r)) { 0 30 30
if (r && !SvROK(r)) { 0 0 0
360 0 STRLEN rl; const char *rp = SvPV_const(r, rl);
361 0 ▶ if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 0 0 0
if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 0 0 0
362 0 SV *tgt = oa_comp_schema(aTHX_ a, rp + OA_COMP_PFX_LEN,
363 rl - OA_COMP_PFX_LEN);
364 0 ▶ if (tgt) oa_schema_props_into(aTHX_ a, tgt, depth + 1, out);
Branch: 1 branch
BranchTrueFalseTotal
if (tgt) oa_schema_props_into(aTHX_ a, tgt, depth + 1, out); 0 0 0
365 }
366 }
367 120 ▶ for (k = 0; unions[k]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; unions[k]; k++) { 90 30 120
368 90 AV *av = oa_av_of(oa_get(aTHX_ h, unions[k]));
369 90 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 0 90 90
370 90 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 90 90
371 0 SV **e = av_fetch(av, i, 0);
372 0 ▶ if (e && *e) oa_schema_props_into(aTHX_ a, *e, depth + 1, out);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) oa_schema_props_into(aTHX_ a, *e, depth + 1, out); 0 0 0
if (e && *e) oa_schema_props_into(aTHX_ a, *e, depth + 1, out); 0 0 0
373 }
374 }
375 }
376  
377 30 static SV *oa_schema_props(pTHX_ oa_api *a, SV *schema) {
378 30 AV *out = newAV();
379 30 oa_schema_props_into(aTHX_ a, schema, 0, out);
380 30 ▶ if (av_len(out) < 0) { SvREFCNT_dec((SV *)out); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(out) < 0) { SvREFCNT_dec((SV *)out); return NULL; } 3 27 30
381 27 return newRV_noinc((SV *)out);
382 }
383  
384 /* ---- JSON booleans ----------------------------------------------------------
385 *
386 * A document's `false` does NOT arrive as a plain 0. File::Raw::JSON yields a
387 * blessed Boolean, and the \0 idiom is a scalar ref - and a reference is TRUE
388 * to C's SvTRUE, whatever it points at. So reading `readOnly: false` or
389 * `explode: false` with SvTRUE gets the answer exactly backwards, silently,
390 * for every real specification.
391 *
392 * These two are the only correct way to read a boolean that came out of a
393 * document. Moved here from oa_normalize.h because oa_compile.h is included
394 * first and most of the reads are in the compiler. */
395 3326 static int oa_sv_is_json_bool(pTHX_ SV *sv) {
396 SV *rv;
397 PERL_UNUSED_CONTEXT;
398 3326 ▶ if (!sv || !SvROK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv)) return 0; 3326 0 3326
if (!sv || !SvROK(sv)) return 0; 14 3312 3326
399 3312 rv = SvRV(sv);
400 6624 ▶ return !(SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV
Branch: 2 branches
BranchTrueFalseTotal
return !(SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV 3312 0 3312
return !(SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV 3312 0 3312
401 3312 ▶ || SvTYPE(rv) == SVt_PVCV);
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(rv) == SVt_PVCV); 3312 0 3312
402 }
403  
404 179067 static int oa_sv_truthy(pTHX_ SV *sv) {
405 179067 ▶ if (!sv || !SvOK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 0; 26019 153048 179067
if (!sv || !SvOK(sv)) return 0; 0 26019 26019
406 26019 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 23910 2109 26019
407 23910 SV *rv = SvRV(sv);
408 23910 ▶ if (SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV) return 1; 23910 0 23910
if (SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV) return 1; 0 23910 23910
409 23910 return SvTRUE(rv) ? 1 : 0;
410 }
411 2109 return SvTRUE(sv) ? 1 : 0;
412 }
413  
414 /* The document's declared version: `openapi` for 3.0[.x] and 3.1[.x],
415 * `swagger` for 2.0. ONE reader, shared by the load-time gate and the
416 * compiler, so the two cannot disagree about which rules apply (the scopes
417 * rule reads it, and 2.0 has 3.0's sentence). Croaks on anything else. */
418 #define OA_V20 20
419 #define OA_V30 30
420 #define OA_V31 31
421  
422 13723 static int oa_doc_version(pTHX_ HV *h) {
423 13723 SV *ov = oa_get(aTHX_ h, "openapi");
424 13723 SV *sv = oa_get(aTHX_ h, "swagger");
425 STRLEN l; const char *p;
426 13723 ▶ if (ov && sv)
Branch: 2 branches
BranchTrueFalseTotal
if (ov && sv) 8011 5712 13723
if (ov && sv) 2 8009 8011
427 2 croak("Open::API: spec declares both 'openapi' and 'swagger'");
428 13721 ▶ if (ov) {
Branch: 1 branch
BranchTrueFalseTotal
if (ov) { 8009 5712 13721
429 8009 p = SvPV_const(ov, l);
430 8009 ▶ if (!(l >= 3 && memEQ(p, "3.", 2) && (l == 3 || p[3] == '.')
Branch: 4 branches
BranchTrueFalseTotal
if (!(l >= 3 && memEQ(p, "3.", 2) && (l == 3 || p[3] == '.') 8009 0 8009
if (!(l >= 3 && memEQ(p, "3.", 2) && (l == 3 || p[3] == '.') 8006 3 8009
if (!(l >= 3 && memEQ(p, "3.", 2) && (l == 3 || p[3] == '.') 8006 0 8006
if (!(l >= 3 && memEQ(p, "3.", 2) && (l == 3 || p[3] == '.') 8006 0 8006
431 8006 ▶ && (p[2] == '1' || p[2] == '0')))
Branch: 2 branches
BranchTrueFalseTotal
&& (p[2] == '1' || p[2] == '0'))) 1848 6158 8006
&& (p[2] == '1' || p[2] == '0'))) 0 1848 1848
432 3 croak("Open::API supports OpenAPI 3.0 and 3.1 (got %.*s)", (int)l, p);
433 8006 ▶ return (p[2] == '1') ? OA_V31 : OA_V30;
Branch: 1 branch
BranchTrueFalseTotal
return (p[2] == '1') ? OA_V31 : OA_V30; 6158 1848 8006
434 }
435 5712 ▶ if (sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv) { 5711 1 5712
436 /* "The value MUST be \"2.0\"": a string. A YAML `swagger: 2.0` is a
437 * number and stringifies as 2, so it is refused by name rather than
438 * read as a version that would admit 2.1 tomorrow. */
439 5711 ▶ if (SvROK(sv) || !SvPOK(sv) || SvNIOK(sv))
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(sv) || !SvPOK(sv) || SvNIOK(sv)) 5711 0 5711
if (SvROK(sv) || !SvPOK(sv) || SvNIOK(sv)) 5709 2 5711
if (SvROK(sv) || !SvPOK(sv) || SvNIOK(sv)) 0 5709 5709
440 2 croak("Open::API: 'swagger' must be the string \"2.0\" "
441 "(quote it in YAML)");
442 5709 p = SvPV_const(sv, l);
443 5709 ▶ if (!(l == 3 && memEQ(p, "2.0", 3)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(l == 3 && memEQ(p, "2.0", 3))) 5709 0 5709
if (!(l == 3 && memEQ(p, "2.0", 3))) 1 5708 5709
444 1 croak("Open::API supports Swagger 2.0 (got swagger %.*s)", (int)l, p);
445 5708 return OA_V20;
446 }
447 1 croak("Open::API: spec has no 'openapi' version field (and no 'swagger')");
448 return 0; /* not reached */
449 }
450  
451 /* A Components Object key: the specification fixes the alphabet. */
452 5549 static int oa_key_ok(const char *k, STRLEN l) {
453 STRLEN i;
454 5549 ▶ if (!l) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return 0; 0 5549 5549
455 26659 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 21113 5546 26659
456 21113 char c = k[i];
457 21116 ▶ if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 5 branches
BranchTrueFalseTotal
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 21110 3 21113
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 14253 6857 21110
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 14253 3 14256
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 14253 14253
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 3 0 3
458 3 ▶ || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_'))
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_')) 0 3 3
|| (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_')) 0 0 0
|| (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_')) 3 0 3
|| (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_')) 3 0 3
459 3 return 0;
460 }
461 5546 return 1;
462 }
463  
464 /* `MUST be in the format of a URL` (Contact) and `MUST be in the form of an
465 * absolute URI` (the XML Object's namespace) are the same test here: a scheme
466 * followed by a colon, and no whitespace anywhere.
467 *
468 * Deliberately loose. A full URI grammar would refuse documents that work, and
469 * a false refusal is worse than a missed typo: nothing in this library
470 * dereferences either field, so the only value of the check is catching a
471 * value that was never a URL at all. Anything with a scheme passes. */
472 2440 static int oa_looks_like_url(pTHX_ SV *sv) {
473 STRLEN l, i;
474 const char *p;
475 2440 ▶ if (!sv || !SvOK(sv)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 1; 2440 0 2440
if (!sv || !SvOK(sv)) return 1; 0 2440 2440
476 2440 p = SvPV_const(sv, l);
477 2440 ▶ if (!l) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return 0; 0 2440 2440
478 52322 ▶ for (i = 0; i < l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) 49884 2438 52322
479 49884 ▶ if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r')
Branch: 4 branches
BranchTrueFalseTotal
if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r') 49882 2 49884
if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r') 49882 0 49882
if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r') 49882 0 49882
if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r') 0 49882 49882
480 2 return 0;
481 /* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":" */
482 2438 ▶ if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z')))
Branch: 4 branches
BranchTrueFalseTotal
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) 2436 2 2438
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) 2436 0 2436
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) 2436 2 2438
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) 0 2436 2436
483 2 return 0;
484 11570 ▶ for (i = 1; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < l; i++) { 11570 0 11570
485 11570 char c = p[i];
486 11570 ▶ if (c == ':') return i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c == ':') return i > 0; 2436 9134 11570
487 9135 ▶ if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 5 branches
BranchTrueFalseTotal
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 9130 4 9134
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 9130 0 9130
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 9130 4 9134
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 9130 9130
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 1 1
488 4 ▶ || (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.'))
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 1 3 4
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 0 1 1
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 2 1 3
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 1 1 2
489 0 return 0;
490 }
491 0 return 0;
492 }
493  
494 /* `MUST be in the format of an email address`. Loose for the same reason: one
495 * `@` with something either side, and no whitespace. The local part of a real
496 * address can contain almost anything, so anything stricter would refuse
497 * addresses that deliver. */
498 1231 static int oa_looks_like_email(pTHX_ SV *sv) {
499 STRLEN l, i;
500 const char *p;
501 1231 STRLEN at = 0;
502 1231 int seen = 0;
503 1231 ▶ if (!sv || !SvOK(sv)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 1; 1231 0 1231
if (!sv || !SvOK(sv)) return 1; 0 1231 1231
504 1231 p = SvPV_const(sv, l);
505 1231 ▶ if (!l) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return 0; 0 1231 1231
506 20334 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 19104 1230 20334
507 19104 char c = p[i];
508 19104 ▶ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0; 19103 1 19104
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0; 19103 0 19103
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0; 19103 0 19103
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0; 0 19103 19103
509 19103 ▶ if (c == '@') { if (seen) return 0; seen = 1; at = i; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '@') { if (seen) return 0; seen = 1; at = i; } 1228 17875 19103
if (c == '@') { if (seen) return 0; seen = 1; at = i; } 0 1228 1228
510 }
511 1230 ▶ return (seen && at > 0 && at < l - 1) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
return (seen && at > 0 && at < l - 1) ? 1 : 0; 1228 2 1230
return (seen && at > 0 && at < l - 1) ? 1 : 0; 1228 0 1228
return (seen && at > 0 && at < l - 1) ? 1 : 0; 1228 0 1228
512 }
513  
514 /* An External Documentation Object requires a `url`. It can hang off the
515 * document root, an operation, a tag or a schema, so the check lives here and
516 * every one of those sites calls it rather than repeating the shape. `where`
517 * names the position for the message, because "externalDocs has no url" on a
518 * document with thirty operations is not an actionable complaint. */
519 23212 static void oa_ext_docs_ok(pTHX_ SV *xd, const char *where) {
520 HV *h;
521 23212 ▶ if (!xd || !SvROK(xd) || SvTYPE(SvRV(xd)) != SVt_PVHV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!xd || !SvROK(xd) || SvTYPE(SvRV(xd)) != SVt_PVHV) return; 621 22591 23212
if (!xd || !SvROK(xd) || SvTYPE(SvRV(xd)) != SVt_PVHV) return; 621 0 621
if (!xd || !SvROK(xd) || SvTYPE(SvRV(xd)) != SVt_PVHV) return; 0 621 621
522 621 h = (HV *)SvRV(xd);
523 621 ▶ if (!hv_exists(h, "url", 3))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(h, "url", 3)) 5 616 621
524 5 croak("Open::API: %s: externalDocs has no 'url' (it is required)",
525 where);
526 }
527  
528 /* case-insensitive equality. Defined here rather than beside its users
529 * because media types, header names and scheme names are all compared this
530 * way and this is the first header that needs one. */
531 34310 static int oa_ci_eq(const char *a, STRLEN al, const char *b, STRLEN bl) {
532 STRLEN i;
533 34310 ▶ if (al != bl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (al != bl) return 0; 60 34250 34310
534 491024 ▶ for (i = 0; i < al; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < al; i++) 463770 27254 491024
535 463770 ▶ if (toLOWER((U8)a[i]) != toLOWER((U8)b[i])) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (toLOWER((U8)a[i]) != toLOWER((U8)b[i])) return 0; 2390 461380 463770
if (toLOWER((U8)a[i]) != toLOWER((U8)b[i])) return 0; 77 463693 463770
if (toLOWER((U8)a[i]) != toLOWER((U8)b[i])) return 0; 6996 456774 463770
536 27254 return 1;
537 }
538  
539 /* A media type we have a decoder for: `application/json` itself, or any
540 * `+json`-suffixed relative (`application/vnd.api+json`, `application/hal+json`).
541 *
542 * Media types are case-insensitive (RFC 7231) and any parameters after `;`
543 * are not part of the type, so both are handled here - this is the one place
544 * that decides, for request bodies, responses and parameter `content` alike.
545 *
546 * The suffix test is a real suffix test. Until 0.13 this was a PREFIX test
547 * whose comment claimed it matched `+json` relatives; it could not, because
548 * `application/vnd.api+json` does not begin with `application/json`. The same
549 * missing delimiter check made `application/json-seq` match as JSON and then
550 * fail to decode. */
551 /* The media type proper: leading space skipped, stopped at `;`, trailing
552 * space trimmed. One place, so every predicate below agrees about where a
553 * media type ends - `application/json; charset=utf-8` is JSON, and
554 * `multipart/form-data; boundary=xx` is multipart. */
555 51910 static void oa_ctype_span(const char *p, STRLEN l, STRLEN *bo, STRLEN *eo) {
556 51910 STRLEN b = 0, e;
557 51910 ▶ while (b < l && isSPACE((U8)p[b])) b++;
Branch: 2 branches
BranchTrueFalseTotal
while (b < l && isSPACE((U8)p[b])) b++; 51910 0 51910
while (b < l && isSPACE((U8)p[b])) b++; 0 51910 51910
558 51910 e = b;
559 896675 ▶ while (e < l && p[e] != ';') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < l && p[e] != ';') e++; 844777 51898 896675
while (e < l && p[e] != ';') e++; 844765 12 844777
560 51910 ▶ while (e > b && isSPACE((U8)p[e - 1])) e--;
Branch: 2 branches
BranchTrueFalseTotal
while (e > b && isSPACE((U8)p[e - 1])) e--; 51910 0 51910
while (e > b && isSPACE((U8)p[e - 1])) e--; 0 51910 51910
561 51910 *bo = b; *eo = e;
562 51910 }
563  
564 27768 static int oa_ctype_is_json(const char *p, STRLEN l) {
565 STRLEN b, e;
566 27768 oa_ctype_span(p, l, &b, &e);
567 27768 ▶ if (e - b == 16 && oa_ci_eq(p + b, 16, "application/json", 16)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (e - b == 16 && oa_ci_eq(p + b, 16, "application/json", 16)) return 1; 20841 6927 27768
if (e - b == 16 && oa_ci_eq(p + b, 16, "application/json", 16)) return 1; 20841 0 20841
568 6927 ▶ return (e - b) > 5 && oa_ci_eq(p + e - 5, 5, "+json", 5);
Branch: 2 branches
BranchTrueFalseTotal
return (e - b) > 5 && oa_ci_eq(p + e - 5, 5, "+json", 5); 6927 0 6927
return (e - b) > 5 && oa_ci_eq(p + e - 5, 5, "+json", 5); 1 6926 6927
569 }
570  
571 4387 static int oa_ctype_is_form(const char *p, STRLEN l) {
572 STRLEN b, e;
573 4387 oa_ctype_span(p, l, &b, &e);
574 4387 return e - b == 33
575 4387 ▶ && oa_ci_eq(p + b, 33, "application/x-www-form-urlencoded", 33);
Branch: 2 branches
BranchTrueFalseTotal
&& oa_ci_eq(p + b, 33, "application/x-www-form-urlencoded", 33); 1261 3126 4387
&& oa_ci_eq(p + b, 33, "application/x-www-form-urlencoded", 33); 1261 0 1261
576 }
577  
578 3126 static int oa_ctype_is_multipart(const char *p, STRLEN l) {
579 STRLEN b, e;
580 3126 oa_ctype_span(p, l, &b, &e);
581 3126 ▶ return e - b == 19 && oa_ci_eq(p + b, 19, "multipart/form-data", 19);
Branch: 2 branches
BranchTrueFalseTotal
return e - b == 19 && oa_ci_eq(p + b, 19, "multipart/form-data", 19); 1221 1905 3126
return e - b == 19 && oa_ci_eq(p + b, 19, "multipart/form-data", 19); 1221 0 1221
582 }
583  
584 /* A media range: the catch-all, or a type followed by a wildcard subtype. */
585 8425 static int oa_ctype_is_range(const char *p, STRLEN l) {
586 STRLEN b, e;
587 8425 oa_ctype_span(p, l, &b, &e);
588 8425 ▶ return (e - b) >= 3 && p[e - 1] == '*' && p[e - 2] == '/';
Branch: 3 branches
BranchTrueFalseTotal
return (e - b) >= 3 && p[e - 1] == '*' && p[e - 2] == '/'; 8425 0 8425
return (e - b) >= 3 && p[e - 1] == '*' && p[e - 2] == '/'; 2 8423 8425
return (e - b) >= 3 && p[e - 1] == '*' && p[e - 2] == '/'; 2 0 2
589 }
590  
591 /* Which decoder a media type wants. OA_MT_RANGE is not a decoder: it says the
592 * declared entry is a range, so the decision belongs to the REQUEST's own
593 * content type at validate time. Anything else we cannot decode is OPAQUE and
594 * passes through untouched - a declared text/plain must keep behaving exactly
595 * as it always has. */
596 enum { OA_MT_OPAQUE = 0, OA_MT_JSON, OA_MT_FORM, OA_MT_MULTIPART, OA_MT_RANGE };
597  
598 8425 static int oa_ctype_kind(const char *p, STRLEN l) {
599 8425 ▶ if (oa_ctype_is_range(p, l)) return OA_MT_RANGE;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_range(p, l)) return OA_MT_RANGE; 2 8423 8425
600 8423 ▶ if (oa_ctype_is_json(p, l)) return OA_MT_JSON;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_json(p, l)) return OA_MT_JSON; 5269 3154 8423
601 3154 ▶ if (oa_ctype_is_form(p, l)) return OA_MT_FORM;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_form(p, l)) return OA_MT_FORM; 637 2517 3154
602 2517 ▶ if (oa_ctype_is_multipart(p, l)) return OA_MT_MULTIPART;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_multipart(p, l)) return OA_MT_MULTIPART; 614 1903 2517
603 1903 return OA_MT_OPAQUE;
604 }
605  
606 /* Does a DECLARED media type cover an ACTUAL one? Exact, or a range. */
607 2735 static int oa_ctype_matches(const char *d, STRLEN dl, const char *a, STRLEN al) {
608 STRLEN db, de, ab, ae;
609 2735 oa_ctype_span(d, dl, &db, &de);
610 2735 oa_ctype_span(a, al, &ab, &ae);
611 2735 ▶ if (de - db == 3 && memEQ(d + db, "*/*", 3)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (de - db == 3 && memEQ(d + db, "*/*", 3)) return 1; 0 2735 2735
if (de - db == 3 && memEQ(d + db, "*/*", 3)) return 1; 0 0 0
612 2735 ▶ if ((de - db) > 2 && d[de - 1] == '*' && d[de - 2] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if ((de - db) > 2 && d[de - 1] == '*' && d[de - 2] == '/') { 2735 0 2735
if ((de - db) > 2 && d[de - 1] == '*' && d[de - 2] == '/') { 0 2735 2735
if ((de - db) > 2 && d[de - 1] == '*' && d[de - 2] == '/') { 0 0 0
613 0 STRLEN tl = de - db - 2, i = 0;
614 0 const char *as = a + ab;
615 0 ▶ while (ab + i < ae && as[i] != '/') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (ab + i < ae && as[i] != '/') i++; 0 0 0
while (ab + i < ae && as[i] != '/') i++; 0 0 0
616 0 ▶ return i == tl && oa_ci_eq(d + db, tl, as, tl);
Branch: 2 branches
BranchTrueFalseTotal
return i == tl && oa_ci_eq(d + db, tl, as, tl); 0 0 0
return i == tl && oa_ci_eq(d + db, tl, as, tl); 0 0 0
617 }
618 2735 return oa_ci_eq(d + db, de - db, a + ab, ae - ab);
619 }
620  
621 /* Does a declared response key cover this status? Exact first, then a range
622 * key (`2XX`), then `default` - the precedence OpenAPI gives them. `want` is
623 * the decimal status as text.
624 *
625 * Both the server (oa_plack.h) and the client (oa_client.h) choose a response
626 * schema, and they used to do it with a copy each of an exact-then-`default`
627 * loop. A range key matched neither, so a document written with `2XX`/`4XX`
628 * compiled schemas that were never selected and never applied. One helper, so
629 * the two cannot disagree about which schema governs a response. */
630 13 static int oa_status_covers(const char *key, STRLEN kl,
631 const char *want, STRLEN wl) {
632 13 ▶ if (kl == wl && memEQ(key, want, kl)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == wl && memEQ(key, want, kl)) return 1; 9 4 13
if (kl == wl && memEQ(key, want, kl)) return 1; 0 9 9
633 13 ▶ if (kl == 7 && memEQ(key, "default", 7)) return 0; /* caller's last pass */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 7 && memEQ(key, "default", 7)) return 0; /* caller's last pass */ 4 9 13
if (kl == 7 && memEQ(key, "default", 7)) return 0; /* caller's last pass */ 4 0 4
634 9 ▶ if (kl == 3 && wl == 3 && key[0] == want[0]
Branch: 3 branches
BranchTrueFalseTotal
if (kl == 3 && wl == 3 && key[0] == want[0] 9 0 9
if (kl == 3 && wl == 3 && key[0] == want[0] 9 0 9
if (kl == 3 && wl == 3 && key[0] == want[0] 7 2 9
635 7 ▶ && (key[1] == 'X' || key[1] == 'x')
Branch: 2 branches
BranchTrueFalseTotal
&& (key[1] == 'X' || key[1] == 'x') 0 7 7
&& (key[1] == 'X' || key[1] == 'x') 0 0 0
636 7 ▶ && (key[2] == 'X' || key[2] == 'x')) return 1;
Branch: 2 branches
BranchTrueFalseTotal
&& (key[2] == 'X' || key[2] == 'x')) return 1; 0 7 7
&& (key[2] == 'X' || key[2] == 'x')) return 1; 0 0 0
637 2 return 0;
638 }
639  
640 /* ---- the schema keyword tables ---------------------------------------------
641 *
642 * Where a Schema Object can actually appear. Used by the 3.0 converter in
643 * oa_normalize.h and by the readOnly/writeOnly projection below, which must
644 * walk the same positions and no others: `example`, `default` and `enum` hold
645 * arbitrary user JSON that may itself contain a key called `properties`. */
646  
647 943526 static int oa_kw_in(const char *k, STRLEN l, const char *const *set) {
648 int i;
649 6603478 ▶ for (i = 0; set[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; set[i]; i++) 5732475 871003 6603478
650 5732475 ▶ if (strlen(set[i]) == l && memEQ(k, set[i], l)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(set[i]) == l && memEQ(k, set[i], l)) return 1; 497101 5235374 5732475
if (strlen(set[i]) == l && memEQ(k, set[i], l)) return 1; 72523 424578 497101
651 871003 return 0;
652 }
653  
654 /* keywords whose value is a single Schema Object */
655 317541 static int oa_kw_schema(const char *k, STRLEN l) {
656 static const char *const s[] = {
657 "additionalProperties", "not", "if", "then", "else", "propertyNames",
658 "contains", "unevaluatedItems", "unevaluatedProperties", "contentSchema",
659 NULL
660 };
661 317541 return oa_kw_in(k, l, s);
662 }
663 /* keywords whose value is a map of Schema Objects */
664 312518 static int oa_kw_schema_map(const char *k, STRLEN l) {
665 static const char *const s[] = {
666 "properties", "patternProperties", "$defs", "definitions",
667 "dependentSchemas", NULL
668 };
669 312518 return oa_kw_in(k, l, s);
670 }
671 /* keywords whose value is a list of Schema Objects */
672 287419 static int oa_kw_schema_list(const char *k, STRLEN l) {
673 static const char *const s[] = { "allOf", "anyOf", "oneOf", "prefixItems", NULL };
674 287419 return oa_kw_in(k, l, s);
675 }
676  
677 /* JSF resolves same-document $refs against schema locations it knows -
678 * `#/$defs/Name` works, arbitrary keys like `#/components/schemas/Name` do
679 * not. So at compile time every extracted schema is deep-copied with its
680 * refs REWRITTEN (#/components/schemas/X -> #/$defs/X) and the spec's
681 * components.schemas - itself rewritten once, since schemas reference each
682 * other - is attached as a shared $defs. */
683  
684 335491 static SV *oa_rewrite_refs(pTHX_ SV *sv) {
685 335491 ▶ if (sv && SvROK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvROK(sv)) { 335491 0 335491
if (sv && SvROK(sv)) { 182047 153444 335491
686 182047 SV *rv = SvRV(sv);
687 182047 ▶ if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) { 147747 34300 182047
688 147747 HV *src = (HV *)rv, *dst = newHV();
689 HE *he;
690 147747 hv_iterinit(src);
691 412128 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 264381 147747 412128
692 264381 I32 kl; const char *k = hv_iterkey(he, &kl);
693 264381 SV *v = hv_iterval(src, he);
694 264381 ▶ if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) {
Branch: 4 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) { 125464 138917 264381
if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) { 21267 104197 125464
if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) { 21267 0 21267
if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) { 21263 4 21267
695 21263 STRLEN vl; const char *vp = SvPV_const(v, vl);
696 21263 ▶ if (vl > OA_COMP_PFX_LEN
Branch: 1 branch
BranchTrueFalseTotal
if (vl > OA_COMP_PFX_LEN 20655 608 21263
697 20655 ▶ && memEQ(vp, OA_COMP_PFX, OA_COMP_PFX_LEN)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(vp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 20655 0 20655
698 20655 SV *nv = newSVpvs("#/$defs/");
699 20655 sv_catpvn(nv, vp + OA_COMP_PFX_LEN, vl - OA_COMP_PFX_LEN);
700 20655 (void)hv_store(dst, k, kl, nv, 0);
701 20655 continue;
702 }
703 }
704 243726 (void)hv_store(dst, k, kl, oa_rewrite_refs(aTHX_ v), 0);
705 }
706 147747 return newRV_noinc((SV *)dst);
707 }
708 34300 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 32380 1920 34300
709 32380 AV *src = (AV *)rv, *dst = newAV();
710 32380 SSize_t i, n = av_len(src) + 1;
711 79804 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 47424 32380 79804
712 47424 SV **e = av_fetch(src, i, 0);
713 47424 ▶ av_push(dst, (e && *e) ? oa_rewrite_refs(aTHX_ *e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, (e && *e) ? oa_rewrite_refs(aTHX_ *e) : newSV(0)); 47424 0 47424
av_push(dst, (e && *e) ? oa_rewrite_refs(aTHX_ *e) : newSV(0)); 47424 0 47424
714 }
715 32380 return newRV_noinc((SV *)dst);
716 }
717 }
718 155364 return newSVsv(sv);
719 }
720  
721 /* ---- the readOnly / writeOnly projection ------------------------------------
722 *
723 * `readOnly` says a property may come back in a response but should not be
724 * sent in a request; `writeOnly` is the mirror. And a `required` property that
725 * is readOnly is required of RESPONSES only - so the same component means two
726 * different things depending on which way the payload is travelling.
727 *
728 * None of that can be decided while validating: once a schema is compiled JSF
729 * owns the traversal, exactly as with `discriminator`. So it is expanded into
730 * 2020-12 constructs here, at compile time, per direction. A forbidden
731 * property becomes `{not:{}}` - always false, so its presence fails - spelled
732 * as a hashref because JSF refuses the \0 JSON-false idiom and a bare 0 would
733 * encode as a number and stop being valid JSON Schema on the wire.
734 *
735 * Only the positions where a Schema Object can appear are descended, for the
736 * reason the 3.0 converter has the same rule: `example`, `default` and `enum`
737 * hold user JSON that may itself contain a key called `properties`. */
738  
739 #define OA_DIR_NONE 0
740 #define OA_DIR_REQ 1 /* a request: readOnly is forbidden */
741 #define OA_DIR_RESP 2 /* a response: writeOnly is forbidden */
742 #define OA_PROJ_MAX_DEPTH 128
743  
744 16962 static int oa_flagged(pTHX_ SV *sv, const char *flag) {
745 16962 HV *h = oa_hv_of(sv);
746 16962 ▶ SV *v = h ? oa_get(aTHX_ h, flag) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = h ? oa_get(aTHX_ h, flag) : NULL; 16962 0 16962
747 16962 ▶ return (v && oa_sv_truthy(aTHX_ v)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (v && oa_sv_truthy(aTHX_ v)) ? 1 : 0; 630 16332 16962
return (v && oa_sv_truthy(aTHX_ v)) ? 1 : 0; 630 0 630
748 }
749  
750 /* Does the document mention the flag anywhere? A false positive (the word
751 * inside an `example`) only costs a projection nobody consults; a false
752 * negative would silently disable the feature, so this scan is deliberately
753 * blunt rather than position-aware. */
754 1342772 static int oa_uses_flag(pTHX_ SV *node, const char *flag, int depth) {
755 SV *rv;
756 1342772 ▶ if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return 0; 1342772 0 1342772
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return 0; 1342772 0 1342772
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return 0; 584262 758510 1342772
757 758510 rv = SvRV(node);
758 758510 ▶ if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) { 621795 136715 758510
759 621795 HV *h = (HV *)rv;
760 HE *he;
761 621795 STRLEN fl = strlen(flag);
762 621795 hv_iterinit(h);
763 1812848 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 1194835 618013 1812848
764 1194835 I32 kl; const char *k = hv_iterkey(he, &kl);
765 1194835 SV *v = hv_iterval(h, he);
766 1194835 ▶ if ((STRLEN)kl == fl && memEQ(k, flag, fl)
Branch: 2 branches
BranchTrueFalseTotal
if ((STRLEN)kl == fl && memEQ(k, flag, fl) 58052 1136783 1194835
if ((STRLEN)kl == fl && memEQ(k, flag, fl) 623 57429 58052
767 3782 ▶ && v && oa_sv_truthy(aTHX_ v)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
&& v && oa_sv_truthy(aTHX_ v)) return 1; 623 0 623
&& v && oa_sv_truthy(aTHX_ v)) return 1; 623 0 623
768 1194212 ▶ if (oa_uses_flag(aTHX_ v, flag, depth + 1)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_uses_flag(aTHX_ v, flag, depth + 1)) return 1; 3159 1191053 1194212
769 }
770 618013 return 0;
771 }
772 136715 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 98978 37737 136715
773 98978 AV *av = (AV *)rv;
774 98978 SSize_t i, n = av_len(av) + 1;
775 236030 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 137052 98978 236030
776 137052 SV **e = av_fetch(av, i, 0);
777 137052 ▶ if (e && *e && oa_uses_flag(aTHX_ *e, flag, depth + 1)) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && oa_uses_flag(aTHX_ *e, flag, depth + 1)) return 1; 137052 0 137052
if (e && *e && oa_uses_flag(aTHX_ *e, flag, depth + 1)) return 1; 137052 0 137052
if (e && *e && oa_uses_flag(aTHX_ *e, flag, depth + 1)) return 1; 0 137052 137052
778 }
779 }
780 136715 return 0;
781 }
782  
783 static SV *oa_project(pTHX_ SV *node, const char *flag, int depth);
784  
785 /* project each value of a map-of-schemas / each element of a list-of-schemas */
786 619 static SV *oa_project_map(pTHX_ SV *v, const char *flag, int depth) {
787 619 HV *src = oa_hv_of(v), *dst;
788 HE *he;
789 619 ▶ if (!src) return newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(v); 0 619 619
790 619 dst = newHV();
791 619 hv_iterinit(src);
792 4253 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 3634 619 4253
793 3634 I32 kl; const char *k = hv_iterkey(he, &kl);
794 3634 (void)hv_store(dst, k, kl,
795 oa_project(aTHX_ hv_iterval(src, he), flag, depth + 1), 0);
796 }
797 619 return newRV_noinc((SV *)dst);
798 }
799  
800 4221 static SV *oa_project_list(pTHX_ SV *v, const char *flag, int depth) {
801 4221 AV *src = oa_av_of(v), *dst;
802 SSize_t i, n;
803 4221 ▶ if (!src) return newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(v); 0 4221 4221
804 4221 dst = newAV();
805 4221 n = av_len(src) + 1;
806 12060 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7839 4221 12060
807 7839 SV **e = av_fetch(src, i, 0);
808 7839 ▶ av_push(dst, oa_project(aTHX_ (e && *e) ? *e : NULL, flag, depth + 1));
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, oa_project(aTHX_ (e && *e) ? *e : NULL, flag, depth + 1)); 7839 0 7839
av_push(dst, oa_project(aTHX_ (e && *e) ? *e : NULL, flag, depth + 1)); 7839 0 7839
809 }
810 4221 return newRV_noinc((SV *)dst);
811 }
812  
813 50143 static SV *oa_project(pTHX_ SV *node, const char *flag, int depth) {
814 50143 HV *src, *dst, *drop = NULL;
815 SV *props;
816 HE *he;
817 50143 ▶ if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return newSVsv(node);
Branch: 3 branches
BranchTrueFalseTotal
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return newSVsv(node); 50143 0 50143
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return newSVsv(node); 50143 0 50143
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return newSVsv(node); 0 50143 50143
818 50143 ▶ if (SvTYPE(SvRV(node)) != SVt_PVHV) return newSVsv(node);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(node)) != SVt_PVHV) return newSVsv(node); 0 50143 50143
819 50143 src = (HV *)SvRV(node);
820 50143 dst = newHV();
821  
822 /* `properties` first: it is the only place the flag is read, and what it
823 * finds decides which names have to come out of `required` below. */
824 50143 props = oa_get(aTHX_ src, "properties");
825 50143 ▶ if (oa_hv_of(props)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_hv_of(props)) { 7263 42880 50143
826 7263 HV *ph = (HV *)SvRV(props), *np = newHV();
827 HE *pe;
828 7263 hv_iterinit(ph);
829 24225 ▶ while ((pe = hv_iternext(ph))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((pe = hv_iternext(ph))) { 16962 7263 24225
830 16962 I32 pkl; const char *pk = hv_iterkey(pe, &pkl);
831 16962 SV *pv = hv_iterval(ph, pe);
832 16962 ▶ if (oa_flagged(aTHX_ pv, flag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_flagged(aTHX_ pv, flag)) { 630 16332 16962
833 630 HV *no = newHV();
834 630 (void)hv_stores(no, "not", newRV_noinc((SV *)newHV()));
835 630 (void)hv_store(np, pk, pkl, newRV_noinc((SV *)no), 0);
836 630 ▶ if (!drop) drop = (HV *)sv_2mortal((SV *)newHV());
Branch: 1 branch
BranchTrueFalseTotal
if (!drop) drop = (HV *)sv_2mortal((SV *)newHV()); 630 0 630
837 630 (void)hv_store(drop, pk, pkl, newSViv(1), 0);
838 }
839 16332 else (void)hv_store(np, pk, pkl,
840 oa_project(aTHX_ pv, flag, depth + 1), 0);
841 }
842 7263 (void)hv_stores(dst, "properties", newRV_noinc((SV *)np));
843 }
844  
845 50143 hv_iterinit(src);
846 145587 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 95444 50143 145587
847 95444 I32 kl; const char *k = hv_iterkey(he, &kl);
848 95444 SV *v = hv_iterval(src, he);
849 110544 ▶ if (kl == 10 && memEQ(k, "properties", 10)) continue; /* done above */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 10 && memEQ(k, "properties", 10)) continue; /* done above */ 8469 86975 95444
if (kl == 10 && memEQ(k, "properties", 10)) continue; /* done above */ 7263 1206 8469
850 88181 ▶ if (drop && kl == 8 && memEQ(k, "required", 8)) {
Branch: 3 branches
BranchTrueFalseTotal
if (drop && kl == 8 && memEQ(k, "required", 8)) { 3067 85114 88181
if (drop && kl == 8 && memEQ(k, "required", 8)) { 1231 1836 3067
if (drop && kl == 8 && memEQ(k, "required", 8)) { 628 603 1231
851 628 AV *ra = oa_av_of(v), *keep = newAV();
852 628 ▶ SSize_t j, rn = ra ? av_len(ra) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, rn = ra ? av_len(ra) + 1 : 0; 628 0 628
853 2487 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 1859 628 2487
854 1859 SV **e = av_fetch(ra, j, 0);
855 STRLEN nl; const char *np2;
856 2475 ▶ if (!e || !*e) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !*e) continue; 1859 0 1859
if (!e || !*e) continue; 0 1859 1859
857 1859 np2 = SvPV_const(*e, nl);
858 1859 ▶ if (hv_exists(drop, np2, (I32)nl)) continue; /* other direction */
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(drop, np2, (I32)nl)) continue; /* other direction */ 616 1243 1859
859 1243 av_push(keep, newSVsv(*e));
860 }
861 /* an emptied `required` is deleted, not left as [] - JSON Schema
862 * allows the empty array but it is noise in ->spec and in errors */
863 628 ▶ if (av_len(keep) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(keep) >= 0) 628 0 628
864 628 (void)hv_store(dst, k, kl, newRV_noinc((SV *)keep), 0);
865 0 else SvREFCNT_dec((SV *)keep);
866 628 continue;
867 }
868 /* `items` is a Schema Object but appears in none of the three tables:
869 * the converter special-cases it for the 3.0 tuple form, which by now
870 * has already become prefixItems. Both shapes are handled anyway. */
871 87553 ▶ if (kl == 5 && memEQ(k, "items", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 5 && memEQ(k, "items", 5)) { 12060 75493 87553
if (kl == 5 && memEQ(k, "items", 5)) { 7236 4824 12060
872 7236 ▶ (void)hv_store(dst, k, kl,
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(dst, k, kl, 0 7236 7236
873 oa_av_of(v) ? oa_project_list(aTHX_ v, flag, depth)
874 : oa_project(aTHX_ v, flag, depth + 1), 0);
875 7236 continue;
876 }
877 80317 ▶ if (oa_kw_schema(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_kw_schema(k, kl)) { 3015 77302 80317
878 3015 (void)hv_store(dst, k, kl, oa_project(aTHX_ v, flag, depth + 1), 0);
879 3015 continue;
880 }
881 77302 ▶ if (oa_kw_schema_map(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_kw_schema_map(k, kl)) { 0 77302 77302
882 0 (void)hv_store(dst, k, kl, oa_project_map(aTHX_ v, flag, depth), 0);
883 0 continue;
884 }
885 77302 ▶ if (oa_kw_schema_list(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_kw_schema_list(k, kl)) { 4221 73081 77302
886 4221 (void)hv_store(dst, k, kl, oa_project_list(aTHX_ v, flag, depth), 0);
887 4221 continue;
888 }
889 73081 (void)hv_store(dst, k, kl, newSVsv(v), 0); /* user JSON: untouched */
890 }
891 50143 return newRV_noinc((SV *)dst);
892 }
893  
894 /* Build the shared $defs (rewritten components.schemas) once per api, plus a
895 * projection of it per direction when the document uses readOnly/writeOnly. */
896 5754 static void oa_build_defs(pTHX_ oa_api *a) {
897 5754 HV *doc = (HV *)SvRV(a->spec);
898 5754 HV *comp = oa_hv_of(oa_get(aTHX_ doc, "components"));
899 5754 ▶ SV *schemas = comp ? oa_get(aTHX_ comp, "schemas") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *schemas = comp ? oa_get(aTHX_ comp, "schemas") : NULL; 3795 1959 5754
900  
901 5754 a->proj_req = oa_uses_flag(aTHX_ a->spec, "readOnly", 0);
902 5754 a->proj_resp = oa_uses_flag(aTHX_ a->spec, "writeOnly", 0);
903  
904 5754 ▶ if (schemas && oa_hv_of(schemas)) {
Branch: 2 branches
BranchTrueFalseTotal
if (schemas && oa_hv_of(schemas)) { 3728 2026 5754
if (schemas && oa_hv_of(schemas)) { 3728 0 3728
905 3728 a->defs = oa_keep(aTHX_ a, oa_rewrite_refs(aTHX_ schemas));
906 /* projected from the REWRITTEN defs, so a $ref from a request body
907 * reaches the request-projected component transitively.
908 *
909 * oa_project_MAP, not oa_project: a defs block is `name => schema`,
910 * not a schema. Handed to oa_project it finds no `properties` on the
911 * map, treats every component name as an unknown keyword and copies
912 * it through untouched - so the projection silently did nothing and
913 * only inline schemas were ever direction-aware. */
914 3728 ▶ if (a->proj_req)
Branch: 1 branch
BranchTrueFalseTotal
if (a->proj_req) 611 3117 3728
915 611 a->defs_req = oa_keep(aTHX_ a,
916 oa_project_map(aTHX_ a->defs, "readOnly", 0));
917 3728 ▶ if (a->proj_resp)
Branch: 1 branch
BranchTrueFalseTotal
if (a->proj_resp) 8 3720 3728
918 8 a->defs_resp = oa_keep(aTHX_ a,
919 oa_project_map(aTHX_ a->defs, "writeOnly", 0));
920 }
921 5754 }
922  
923 /* Rewrite a schema's refs, attach the shared $defs, compile via the JSF ABI.
924 * The returned handle is owned by the keep AV. */
925 42569 static SV *oa_compile_schema(pTHX_ oa_api *a, SV *schema, int coerce, int dir) {
926 42569 SV *to = schema;
927 42569 SV *defs = a->defs;
928 /* Which way is this payload travelling? A request forbids readOnly, a
929 * response forbids writeOnly, and each reaches its own projection of the
930 * components so a $ref lands on the right variant. OA_DIR_NONE, and a
931 * document that never uses either flag, compile down the original path. */
932 42569 ▶ if (dir == OA_DIR_REQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (dir == OA_DIR_REQ) { 28054 14515 42569
933 28054 ▶ if (a->defs_req) defs = a->defs_req;
Branch: 1 branch
BranchTrueFalseTotal
if (a->defs_req) defs = a->defs_req; 12076 15978 28054
934 28054 ▶ if (a->proj_req) schema = sv_2mortal(oa_project(aTHX_ schema, "readOnly", 0));
Branch: 1 branch
BranchTrueFalseTotal
if (a->proj_req) schema = sv_2mortal(oa_project(aTHX_ schema, "readOnly", 0)); 12079 15975 28054
935 }
936 14515 ▶ else if (dir == OA_DIR_RESP) {
Branch: 1 branch
BranchTrueFalseTotal
else if (dir == OA_DIR_RESP) { 14515 0 14515
937 14515 ▶ if (a->defs_resp) defs = a->defs_resp;
Branch: 1 branch
BranchTrueFalseTotal
if (a->defs_resp) defs = a->defs_resp; 8 14507 14515
938 14515 ▶ if (a->proj_resp) schema = sv_2mortal(oa_project(aTHX_ schema, "writeOnly", 0));
Branch: 1 branch
BranchTrueFalseTotal
if (a->proj_resp) schema = sv_2mortal(oa_project(aTHX_ schema, "writeOnly", 0)); 8 14507 14515
939 }
940 42569 to = schema;
941 42569 ▶ if (defs && oa_hv_of(schema)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defs && oa_hv_of(schema)) { 40613 1956 42569
if (defs && oa_hv_of(schema)) { 40613 0 40613
942 40613 SV *rw = sv_2mortal(oa_rewrite_refs(aTHX_ schema));
943 40613 HV *w = (HV *)SvRV(rw);
944 40613 SV **own = hv_fetchs(w, "$defs", 0);
945 40613 ▶ if (own && *own && oa_hv_of(*own)) {
Branch: 3 branches
BranchTrueFalseTotal
if (own && *own && oa_hv_of(*own)) { 0 40613 40613
if (own && *own && oa_hv_of(*own)) { 0 0 0
if (own && *own && oa_hv_of(*own)) { 0 0 0
946 /* schema carries its own $defs: overlay ours underneath */
947 0 HV *merged = newHV(), *shared = (HV *)SvRV(defs), *mine;
948 HE *he;
949 0 hv_iterinit(shared);
950 0 ▶ while ((he = hv_iternext(shared))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(shared))) { 0 0 0
951 0 I32 kl; const char *k = hv_iterkey(he, &kl);
952 0 (void)hv_store(merged, k, kl,
953 SvREFCNT_inc(hv_iterval(shared, he)), 0);
954 }
955 0 mine = (HV *)SvRV(*own);
956 0 hv_iterinit(mine);
957 0 ▶ while ((he = hv_iternext(mine))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(mine))) { 0 0 0
958 0 I32 kl; const char *k = hv_iterkey(he, &kl);
959 0 (void)hv_store(merged, k, kl,
960 SvREFCNT_inc(hv_iterval(mine, he)), 0);
961 }
962 0 (void)hv_stores(w, "$defs", newRV_noinc((SV *)merged));
963 } else {
964 40613 (void)hv_stores(w, "$defs", SvREFCNT_inc(defs));
965 }
966 40613 to = rw;
967 }
968 42569 return oa_keep(aTHX_ a, JSF->compile(aTHX_ to, &PL_sv_undef, coerce, 1));
969 }
970  
971 /* ---- path template -> segments -------------------------------------------- */
972  
973 15262 static void oa_compile_segs(pTHX_ oa_api *a, oa_op *o, const char *tpl, STRLEN tl) {
974 /* count segments */
975 15262 int n = 0, i;
976 15262 STRLEN s = 0;
977 38855 ▶ while (s < tl) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < tl) { 23593 15262 38855
978 47186 ▶ while (s < tl && tpl[s] == '/') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < tl && tpl[s] == '/') s++; 47180 6 47186
while (s < tl && tpl[s] == '/') s++; 23593 23587 47180
979 23593 ▶ if (s < tl) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (s < tl) n++; 23587 6 23593
980 126809 ▶ while (s < tl && tpl[s] != '/') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < tl && tpl[s] != '/') s++; 111547 15262 126809
while (s < tl && tpl[s] != '/') s++; 103216 8331 111547
981 }
982 15262 o->nsegs = n;
983 15262 ▶ o->segs = n ? (oa_seg *)calloc((size_t)n, sizeof(oa_seg)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
o->segs = n ? (oa_seg *)calloc((size_t)n, sizeof(oa_seg)) : NULL; 15256 6 15262
984 15262 ▶ if (n && !o->segs) croak("Open::API: out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (n && !o->segs) croak("Open::API: out of memory"); 15256 6 15262
if (n && !o->segs) croak("Open::API: out of memory"); 0 15256 15256
985 15262 s = 0; i = 0;
986 38849 ▶ while (s < tl) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < tl) { 23593 15256 38849
987 STRLEN e;
988 47186 ▶ while (s < tl && tpl[s] == '/') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < tl && tpl[s] == '/') s++; 47180 6 47186
while (s < tl && tpl[s] == '/') s++; 23593 23587 47180
989 23593 ▶ if (s >= tl) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s >= tl) break; 6 23587 23593
990 23587 e = s;
991 126803 ▶ while (e < tl && tpl[e] != '/') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < tl && tpl[e] != '/') e++; 111547 15256 126803
while (e < tl && tpl[e] != '/') e++; 103216 8331 111547
992 23587 ▶ if (tpl[s] == '{' && tpl[e - 1] == '}' && e - s > 2) {
Branch: 3 branches
BranchTrueFalseTotal
if (tpl[s] == '{' && tpl[e - 1] == '}' && e - s > 2) { 7012 16575 23587
if (tpl[s] == '{' && tpl[e - 1] == '}' && e - s > 2) { 7012 0 7012
if (tpl[s] == '{' && tpl[e - 1] == '}' && e - s > 2) { 7012 0 7012
993 7012 o->segs[i].pname = oa_keep(aTHX_ a, newSVpvn(tpl + s + 1, e - s - 2));
994 } else {
995 16575 o->segs[i].lit = oa_keep(aTHX_ a, newSVpvn(tpl + s, e - s));
996 }
997 23587 i++;
998 23587 s = e;
999 }
1000 15262 }
1001  
1002 /* ---- parameters ------------------------------------------------------------ */
1003  
1004 44345 static int oa_param_loc(pTHX_ SV *in) {
1005 STRLEN l; const char *p;
1006 44345 ▶ if (!in) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!in) return -1; 0 44345 44345
1007 44345 p = SvPV_const(in, l);
1008 44345 ▶ if (l == 4 && memEQ(p, "path", 4)) return OA_IN_PATH;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "path", 4)) return OA_IN_PATH; 14044 30301 44345
if (l == 4 && memEQ(p, "path", 4)) return OA_IN_PATH; 14044 0 14044
1009 30301 ▶ if (l == 5 && memEQ(p, "query", 5)) return OA_IN_QUERY;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && memEQ(p, "query", 5)) return OA_IN_QUERY; 21998 8303 30301
if (l == 5 && memEQ(p, "query", 5)) return OA_IN_QUERY; 21998 0 21998
1010 8303 ▶ if (l == 6 && memEQ(p, "header", 6)) return OA_IN_HEADER;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 6 && memEQ(p, "header", 6)) return OA_IN_HEADER; 8303 0 8303
if (l == 6 && memEQ(p, "header", 6)) return OA_IN_HEADER; 5695 2608 8303
1011 2608 ▶ if (l == 6 && memEQ(p, "cookie", 6)) return OA_IN_COOKIE;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 6 && memEQ(p, "cookie", 6)) return OA_IN_COOKIE; 2608 0 2608
if (l == 6 && memEQ(p, "cookie", 6)) return OA_IN_COOKIE; 2608 0 2608
1012 0 return -1;
1013 }
1014  
1015 /* same (name, in)? - for the path-item / operation parameter merge */
1016 32375 static int oa_param_same(pTHX_ HV *x, HV *y) {
1017 32375 SV *xn = oa_get(aTHX_ x, "name"), *yn = oa_get(aTHX_ y, "name");
1018 32375 SV *xi = oa_get(aTHX_ x, "in"), *yi = oa_get(aTHX_ y, "in");
1019 32375 ▶ if (!xn || !yn || !xi || !yi) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!xn || !yn || !xi || !yi) return 0; 32375 0 32375
if (!xn || !yn || !xi || !yi) return 0; 32375 0 32375
if (!xn || !yn || !xi || !yi) return 0; 32375 0 32375
if (!xn || !yn || !xi || !yi) return 0; 0 32375 32375
1020 32375 ▶ return sv_eq(xn, yn) && sv_eq(xi, yi);
Branch: 2 branches
BranchTrueFalseTotal
return sv_eq(xn, yn) && sv_eq(xi, yi); 3 32372 32375
return sv_eq(xn, yn) && sv_eq(xi, yi); 3 0 3
1021 }
1022  
1023 /* The delimiter an x-oa-collectionFormat value names, or 0 when absent or
1024 * not one of the four. csv is a comma, the style's own default. */
1025 23411 static char oa_cf_delim(pTHX_ SV *cf) {
1026 STRLEN l; const char *p;
1027 23411 ▶ if (!cf || SvROK(cf) || !SvOK(cf)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!cf || SvROK(cf) || !SvOK(cf)) return 0; 2442 20969 23411
if (!cf || SvROK(cf) || !SvOK(cf)) return 0; 2442 0 2442
if (!cf || SvROK(cf) || !SvOK(cf)) return 0; 0 2442 2442
1028 2442 p = SvPV_const(cf, l);
1029 2442 ▶ if (l == 3 && memEQ(p, "ssv", 3)) return ' ';
Branch: 2 branches
BranchTrueFalseTotal
if (l == 3 && memEQ(p, "ssv", 3)) return ' '; 621 1821 2442
if (l == 3 && memEQ(p, "ssv", 3)) return ' '; 5 616 621
1030 2437 ▶ if (l == 3 && memEQ(p, "tsv", 3)) return '\t';
Branch: 2 branches
BranchTrueFalseTotal
if (l == 3 && memEQ(p, "tsv", 3)) return '\t'; 616 1821 2437
if (l == 3 && memEQ(p, "tsv", 3)) return '\t'; 616 0 616
1031 1821 ▶ if (l == 5 && memEQ(p, "pipes", 5)) return '|';
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && memEQ(p, "pipes", 5)) return '|'; 1821 0 1821
if (l == 5 && memEQ(p, "pipes", 5)) return '|'; 1821 0 1821
1032 0 ▶ if (l == 3 && memEQ(p, "csv", 3)) return ',';
Branch: 2 branches
BranchTrueFalseTotal
if (l == 3 && memEQ(p, "csv", 3)) return ','; 0 0 0
if (l == 3 && memEQ(p, "csv", 3)) return ','; 0 0 0
1033 0 return 0;
1034 }
1035  
1036 /* The nested array levels under an array schema: each `items` that is itself
1037 * an array adds a level whose delimiter is its own x-oa-collectionFormat, or
1038 * a comma. Bounded by the size of idelim, which matches the Items nesting the
1039 * pre-pass accepts. */
1040 22773 static void oa_nested_delims(pTHX_ oa_param *pp, SV *schema) {
1041 22773 HV *h = oa_hv_of(schema);
1042 22773 pp->ndepth = 0;
1043 23383 ▶ while (h && pp->ndepth < 4) {
Branch: 2 branches
BranchTrueFalseTotal
while (h && pp->ndepth < 4) { 22771 612 23383
while (h && pp->ndepth < 4) { 22771 0 22771
1044 22771 HV *ih = oa_hv_of(oa_get(aTHX_ h, "items"));
1045 SV *t;
1046 STRLEN tl; const char *tp;
1047 char d;
1048 30233 ▶ if (!ih) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!ih) break; 14699 8072 22771
1049 8072 t = oa_get(aTHX_ ih, "type");
1050 8072 ▶ if (!t || SvROK(t)) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!t || SvROK(t)) break; 8072 0 8072
if (!t || SvROK(t)) break; 8072 0 8072
1051 8072 tp = SvPV_const(t, tl);
1052 8072 ▶ if (!(tl == 5 && memEQ(tp, "array", 5))) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!(tl == 5 && memEQ(tp, "array", 5))) break; 610 7462 8072
if (!(tl == 5 && memEQ(tp, "array", 5))) break; 610 0 610
1053 610 d = oa_cf_delim(aTHX_ oa_get(aTHX_ ih, "x-oa-collectionFormat"));
1054 610 ▶ pp->idelim[pp->ndepth++] = d ? d : ',';
Branch: 1 branch
BranchTrueFalseTotal
pp->idelim[pp->ndepth++] = d ? d : ','; 607 3 610
1055 610 h = ih;
1056 }
1057 22773 }
1058  
1059 /* Merge path-item + operation parameter lists (operation wins on name+in),
1060 * then compile each into the op's per-location arrays. */
1061 15262 static void oa_compile_params(pTHX_ oa_api *a, oa_op *o, AV *item_params,
1062 AV *op_params) {
1063 HV *merged[64];
1064 15262 int nm = 0, i, loc;
1065 15262 int counts[OA_IN_N] = { 0, 0, 0, 0 };
1066 15262 int fill[OA_IN_N] = { 0, 0, 0, 0 };
1067  
1068 /* path-item level first. "The list MUST NOT include duplicated
1069 * parameters. A unique parameter is defined by a combination of a name and
1070 * location" - the same sentence in every version. The merge below used to
1071 * let a repeat within ONE list win silently. */
1072 15262 ▶ if (item_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (item_params) { 3812 11450 15262
1073 3812 SSize_t n = av_len(item_params) + 1, j;
1074 7623 ▶ for (j = 0; j < n && nm < 64; j++) {
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < n && nm < 64; j++) { 3811 3812 7623
for (j = 0; j < n && nm < 64; j++) { 3811 0 3811
1075 3811 SV **e = av_fetch(item_params, j, 0);
1076 3811 ▶ HV *ph = e ? oa_hv_of(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *ph = e ? oa_hv_of(*e) : NULL; 3811 0 3811
1077 int k;
1078 3811 ▶ if (!ph) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ph) continue; 0 3811 3811
1079 3811 ▶ for (k = 0; k < nm; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nm; k++) 0 3811 3811
1080 0 ▶ if (oa_param_same(aTHX_ merged[k], ph))
Branch: 1 branch
BranchTrueFalseTotal
if (oa_param_same(aTHX_ merged[k], ph)) 0 0 0
1081 0 ▶ croak("Open::API: %s: path item parameter '%s' (in %s) is "
Branch: 1 branch
BranchTrueFalseTotal
croak("Open::API: %s: path item parameter '%s' (in %s) is " 0 0 0
1082 "declared twice in one list", SvPV_nolen(o->op_id),
1083 SvPV_nolen(oa_get(aTHX_ ph, "name")),
1084 SvPV_nolen(oa_get(aTHX_ ph, "in")));
1085 3811 merged[nm++] = ph;
1086 }
1087 }
1088 /* operation level: replace same (name,in), else append */
1089 15262 ▶ if (op_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (op_params) { 11956 3306 15262
1090 11956 SSize_t n = av_len(op_params) + 1, j;
1091 11956 int from_item = nm;
1092 29688 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 17733 11955 29688
1093 17733 SV **e = av_fetch(op_params, j, 0);
1094 17733 ▶ HV *ph = e ? oa_hv_of(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *ph = e ? oa_hv_of(*e) : NULL; 17733 0 17733
1095 17733 int k, hit = 0;
1096 17733 ▶ if (!ph) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ph) continue; 0 17733 17733
1097 50105 ▶ for (k = 0; k < nm; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nm; k++) 32375 17730 50105
1098 32375 ▶ if (oa_param_same(aTHX_ merged[k], ph)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_param_same(aTHX_ merged[k], ph)) { 3 32372 32375
1099 3 ▶ if (k >= from_item)
Branch: 1 branch
BranchTrueFalseTotal
if (k >= from_item) 1 2 3
1100 4 ▶ croak("Open::API: %s: parameter '%s' (in %s) is declared "
Branch: 1 branch
BranchTrueFalseTotal
croak("Open::API: %s: parameter '%s' (in %s) is declared " 1 0 1
1101 "twice in one list", SvPV_nolen(o->op_id),
1102 SvPV_nolen(oa_get(aTHX_ ph, "name")),
1103 SvPV_nolen(oa_get(aTHX_ ph, "in")));
1104 2 merged[k] = ph; hit = 1; break;
1105 }
1106 17732 ▶ if (!hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hit) { 17730 2 17732
1107 17730 ▶ if (nm >= 64) croak("Open::API: more than 64 parameters on %s",
Branch: 1 branch
BranchTrueFalseTotal
if (nm >= 64) croak("Open::API: more than 64 parameters on %s", 0 17730 17730
1108 SvPV_nolen(o->op_id));
1109 17730 merged[nm++] = ph;
1110 }
1111 }
1112 }
1113  
1114 36801 ▶ for (i = 0; i < nm; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nm; i++) { 21540 15261 36801
1115 21540 int l = oa_param_loc(aTHX_ oa_get(aTHX_ merged[i], "in"));
1116 21540 ▶ if (l < 0) croak("Open::API: parameter with bad 'in' on %s",
Branch: 1 branch
BranchTrueFalseTotal
if (l < 0) croak("Open::API: parameter with bad 'in' on %s", 0 21540 21540
1117 SvPV_nolen(o->op_id));
1118 21540 counts[l]++;
1119 }
1120 76305 ▶ for (loc = 0; loc < OA_IN_N; loc++) {
Branch: 1 branch
BranchTrueFalseTotal
for (loc = 0; loc < OA_IN_N; loc++) { 61044 15261 76305
1121 61044 o->nparams[loc] = counts[loc];
1122 122088 o->params[loc] = counts[loc]
1123 61044 ▶ ? (oa_param *)calloc((size_t)counts[loc], sizeof(oa_param)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (oa_param *)calloc((size_t)counts[loc], sizeof(oa_param)) : NULL; 14734 46310 61044
1124 61044 ▶ if (counts[loc] && !o->params[loc]) croak("Open::API: out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (counts[loc] && !o->params[loc]) croak("Open::API: out of memory"); 14734 46310 61044
if (counts[loc] && !o->params[loc]) croak("Open::API: out of memory"); 0 14734 14734
1125 }
1126 36800 ▶ for (i = 0; i < nm; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nm; i++) { 21540 15260 36800
1127 21540 HV *ph = merged[i];
1128 21540 SV *name = oa_get(aTHX_ ph, "name");
1129 21540 SV *schema = oa_get(aTHX_ ph, "schema");
1130 21540 SV *req = oa_get(aTHX_ ph, "required");
1131 21540 int l = oa_param_loc(aTHX_ oa_get(aTHX_ ph, "in"));
1132 oa_param *pp;
1133 21540 ▶ if (!name) croak("Open::API: parameter without a name on %s",
Branch: 1 branch
BranchTrueFalseTotal
if (!name) croak("Open::API: parameter without a name on %s", 0 21540 21540
1134 SvPV_nolen(o->op_id));
1135 21540 pp = &o->params[l][fill[l]++];
1136 21540 pp->name = oa_keep(aTHX_ a, newSVsv(name));
1137 21540 pp->required = (l == OA_IN_PATH) ? 1
1138 21540 ▶ : (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
: (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 14525 7015 21540
: (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 2622 11903 14525
: (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 1361 1261 2622
1139 21540 pp->ctype = NULL;
1140 21540 ▶ pp->is_array = schema ? oa_schema_is_array(aTHX_ a, schema, 0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
pp->is_array = schema ? oa_schema_is_array(aTHX_ a, schema, 0) : 0; 20928 612 21540
1141 21540 ▶ pp->is_object = schema ? oa_schema_is_object(aTHX_ a, schema, 0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
pp->is_object = schema ? oa_schema_is_object(aTHX_ a, schema, 0) : 0; 20928 612 21540
1142 /* only form+explode reads these, but the schema is here and the
1143 * request path is not the place to walk it */
1144 30 ▶ pp->props = (pp->is_object && schema)
Branch: 1 branch
BranchTrueFalseTotal
pp->props = (pp->is_object && schema) 30 0 30
1145 21570 ▶ ? oa_schema_props(aTHX_ a, schema) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_schema_props(aTHX_ a, schema) : NULL; 30 21510 21540
1146 21540 pp->handle = schema
1147 21540 ▶ ? oa_compile_schema(aTHX_ a, schema, 1, OA_DIR_REQ) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_compile_schema(aTHX_ a, schema, 1, OA_DIR_REQ) : NULL; 20928 612 21540
1148  
1149 /* How the value is spelled on the wire. The default follows the
1150 * location: `simple` in a path or header, `form` in a query or
1151 * cookie; `explode` defaults to true for `form` alone. An unknown
1152 * style is left at the default rather than refused - the document is
1153 * still a valid document, and refusing here would reject specs that
1154 * work against other tools. */
1155 {
1156 21540 SV *st = oa_get(aTHX_ ph, "style");
1157 21540 SV *ex = oa_get(aTHX_ ph, "explode");
1158 14525 ▶ pp->style = (l == OA_IN_PATH || l == OA_IN_HEADER)
Branch: 1 branch
BranchTrueFalseTotal
pp->style = (l == OA_IN_PATH || l == OA_IN_HEADER) 2531 11994 14525
1159 36065 ▶ ? OA_ST_SIMPLE : OA_ST_FORM;
Branch: 1 branch
BranchTrueFalseTotal
? OA_ST_SIMPLE : OA_ST_FORM; 14525 7015 21540
1160 21540 ▶ if (st) {
Branch: 1 branch
BranchTrueFalseTotal
if (st) { 4992 16548 21540
1161 4992 STRLEN sl; const char *sp = SvPV_const(st, sl);
1162 4992 ▶ if (sl == 6 && memEQ(sp, "simple", 6)) pp->style = OA_ST_SIMPLE;
Branch: 2 branches
BranchTrueFalseTotal
if (sl == 6 && memEQ(sp, "simple", 6)) pp->style = OA_ST_SIMPLE; 50 4942 4992
if (sl == 6 && memEQ(sp, "simple", 6)) pp->style = OA_ST_SIMPLE; 28 22 50
1163 4964 ▶ else if (sl == 5 && memEQ(sp, "label", 5)) pp->style = OA_ST_LABEL;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 5 && memEQ(sp, "label", 5)) pp->style = OA_ST_LABEL; 22 4942 4964
else if (sl == 5 && memEQ(sp, "label", 5)) pp->style = OA_ST_LABEL; 22 0 22
1164 4942 ▶ else if (sl == 6 && memEQ(sp, "matrix", 6)) pp->style = OA_ST_MATRIX;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 6 && memEQ(sp, "matrix", 6)) pp->style = OA_ST_MATRIX; 22 4920 4942
else if (sl == 6 && memEQ(sp, "matrix", 6)) pp->style = OA_ST_MATRIX; 22 0 22
1165 4920 ▶ else if (sl == 4 && memEQ(sp, "form", 4)) pp->style = OA_ST_FORM;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 4 && memEQ(sp, "form", 4)) pp->style = OA_ST_FORM; 3687 1233 4920
else if (sl == 4 && memEQ(sp, "form", 4)) pp->style = OA_ST_FORM; 3687 0 3687
1166 1233 ▶ else if (sl == 14 && memEQ(sp, "spaceDelimited", 14)) pp->style = OA_ST_SPACE;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 14 && memEQ(sp, "spaceDelimited", 14)) pp->style = OA_ST_SPACE; 614 619 1233
else if (sl == 14 && memEQ(sp, "spaceDelimited", 14)) pp->style = OA_ST_SPACE; 614 0 614
1167 619 ▶ else if (sl == 13 && memEQ(sp, "pipeDelimited", 13)) pp->style = OA_ST_PIPE;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 13 && memEQ(sp, "pipeDelimited", 13)) pp->style = OA_ST_PIPE; 614 5 619
else if (sl == 13 && memEQ(sp, "pipeDelimited", 13)) pp->style = OA_ST_PIPE; 614 0 614
1168 5 ▶ else if (sl == 10 && memEQ(sp, "deepObject", 10)) pp->style = OA_ST_DEEP;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 10 && memEQ(sp, "deepObject", 10)) pp->style = OA_ST_DEEP; 5 0 5
else if (sl == 10 && memEQ(sp, "deepObject", 10)) pp->style = OA_ST_DEEP; 5 0 5
1169 }
1170 3777 pp->explode = ex ? (oa_sv_truthy(aTHX_ ex) ? 1 : 0)
1171 25317 ▶ : (pp->style == OA_ST_FORM ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
: (pp->style == OA_ST_FORM ? 1 : 0); 3777 17763 21540
1172 }
1173 { /* the delimiters a style cannot spell (D5): a tab anywhere, a
1174 * space or pipe in a path or header, and each nested level's */
1175 21540 pp->delim = oa_cf_delim(aTHX_ oa_get(aTHX_ ph, "x-oa-collectionFormat"));
1176 21540 oa_nested_delims(aTHX_ pp, schema);
1177 }
1178 { /* reported to consumers; the specification gives it no effect on
1179 * validation, so it is carried rather than enforced */
1180 21540 SV *dep = oa_get(aTHX_ ph, "deprecated");
1181 21540 ▶ pp->deprecated = (dep && oa_sv_truthy(aTHX_ dep)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
pp->deprecated = (dep && oa_sv_truthy(aTHX_ dep)) ? 1 : 0; 2 21538 21540
pp->deprecated = (dep && oa_sv_truthy(aTHX_ dep)) ? 1 : 0; 2 0 2
1182 }
1183 { /* allowEmptyValue: `?flag=` is present with no value. 3.1
1184 * deprecates the keyword and its semantics are under-specified,
1185 * so the reading here is the narrow one that can be defended:
1186 * a QUERY parameter whose value is the empty string is permitted
1187 * rather than pushed at its schema. Everywhere else it is
1188 * ignored, which is what the specification asks for. */
1189 21540 SV *ae = oa_get(aTHX_ ph, "allowEmptyValue");
1190 21540 pp->allow_empty = (l == OA_IN_QUERY
1191 21540 ▶ && ae && oa_sv_truthy(aTHX_ ae)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
&& ae && oa_sv_truthy(aTHX_ ae)) ? 1 : 0; 10690 10850 21540
&& ae && oa_sv_truthy(aTHX_ ae)) ? 1 : 0; 606 10084 10690
&& ae && oa_sv_truthy(aTHX_ ae)) ? 1 : 0; 606 0 606
1192 }
1193 { /* allowReserved: reserved characters MAY be sent without
1194 * percent-encoding. Query only. The specification also allows it
1195 * on a path parameter, but a reserved character in a path segment
1196 * changes the SHAPE of the path, so honouring it there would be a
1197 * bug wearing a feature's clothes. */
1198 21540 SV *ar = oa_get(aTHX_ ph, "allowReserved");
1199 21540 pp->allow_reserved = (l == OA_IN_QUERY
1200 21540 ▶ && ar && oa_sv_truthy(aTHX_ ar)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
&& ar && oa_sv_truthy(aTHX_ ar)) ? 1 : 0; 10690 10850 21540
&& ar && oa_sv_truthy(aTHX_ ar)) ? 1 : 0; 4 10686 10690
&& ar && oa_sv_truthy(aTHX_ ar)) ? 1 : 0; 4 0 4
1201 }
1202  
1203 /* `content` instead of `schema`: the value is a document, not a
1204 * string standing in for one. The spec allows exactly one entry, so
1205 * the first is the one - and it is compiled WITHOUT coercion, unlike
1206 * an ordinary parameter, because a decoded JSON document already has
1207 * the types the schema is talking about. */
1208 21540 ▶ if (!schema) {
Branch: 1 branch
BranchTrueFalseTotal
if (!schema) { 612 20928 21540
1209 612 HV *content = oa_hv_of(oa_get(aTHX_ ph, "content"));
1210 HE *he;
1211 /* the map MUST contain exactly one entry: it says what the single
1212 * value on the wire is, and two answers is no answer */
1213 612 ▶ if (content && HvUSEDKEYS(content) > 1)
Branch: 3 branches
BranchTrueFalseTotal
if (content && HvUSEDKEYS(content) > 1) 610 2 612
if (content && HvUSEDKEYS(content) > 1) 0 610 610
if (content && HvUSEDKEYS(content) > 1) 1 609 610
1214 3 ▶ croak("Open::API: parameter '%s' on %s declares %d content "
Branch: 2 branches
BranchTrueFalseTotal
croak("Open::API: parameter '%s' on %s declares %d content " 0 1 1
croak("Open::API: parameter '%s' on %s declares %d content " 1 0 1
1215 "entries; the map must contain only one",
1216 SvPV_nolen(name), SvPV_nolen(o->op_id),
1217 (int)HvUSEDKEYS(content));
1218 611 ▶ if (content && HvUSEDKEYS(content)) {
Branch: 3 branches
BranchTrueFalseTotal
if (content && HvUSEDKEYS(content)) { 609 2 611
if (content && HvUSEDKEYS(content)) { 0 609 609
if (content && HvUSEDKEYS(content)) { 609 0 609
1219 609 hv_iterinit(content);
1220 609 ▶ if ((he = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((he = hv_iternext(content))) { 609 0 609
1221 609 I32 ckl; const char *ck = hv_iterkey(he, &ckl);
1222 609 HV *media = oa_hv_of(hv_iterval(content, he));
1223 609 ▶ SV *cs = media ? oa_get(aTHX_ media, "schema") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *cs = media ? oa_get(aTHX_ media, "schema") : NULL; 609 0 609
1224 609 pp->ctype = oa_keep(aTHX_ a, newSVpvn(ck, (STRLEN)ckl));
1225 609 ▶ if (cs && oa_ctype_is_json(ck, (STRLEN)ckl))
Branch: 2 branches
BranchTrueFalseTotal
if (cs && oa_ctype_is_json(ck, (STRLEN)ckl)) 609 0 609
if (cs && oa_ctype_is_json(ck, (STRLEN)ckl)) 607 2 609
1226 607 pp->handle = oa_compile_schema(aTHX_ a, cs, 0, OA_DIR_REQ);
1227 }
1228 }
1229 }
1230 }
1231 15260 }
1232  
1233 /* ---- requestBody / responses ----------------------------------------------- */
1234  
1235 15260 static void oa_compile_body(pTHX_ oa_api *a, oa_op *o, SV *rb) {
1236 15260 HV *rbh = oa_hv_of(rb);
1237 HV *content;
1238 HE *he;
1239 15260 int n = 0, i = 0;
1240 15260 ▶ if (!rbh) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rbh) return; 8741 6519 15260
1241 {
1242 6519 SV *req = oa_get(aTHX_ rbh, "required");
1243 6519 ▶ o->body_required = (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
o->body_required = (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 6493 26 6519
o->body_required = (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 6493 0 6493
1244 }
1245 6519 content = oa_hv_of(oa_get(aTHX_ rbh, "content"));
1246 6519 ▶ if (!content) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!content) return; 0 6519 6519
1247 6519 ▶ n = (int)HvUSEDKEYS(content);
Branch: 1 branch
BranchTrueFalseTotal
n = (int)HvUSEDKEYS(content); 0 6519 6519
1248 6519 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 6519 6519
1249 6519 o->bodies = (oa_body *)calloc((size_t)n, sizeof(oa_body));
1250 6519 ▶ if (!o->bodies) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!o->bodies) croak("Open::API: out of memory"); 0 6519 6519
1251 6519 o->nbodies = n;
1252 6519 hv_iterinit(content);
1253 14940 ▶ while ((he = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(content))) { 8422 6518 14940
1254 8422 I32 kl; const char *k = hv_iterkey(he, &kl);
1255 8422 HV *media = oa_hv_of(hv_iterval(content, he));
1256 8422 ▶ SV *schema = media ? oa_get(aTHX_ media, "schema") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *schema = media ? oa_get(aTHX_ media, "schema") : NULL; 8422 0 8422
1257 8422 int kind = oa_ctype_kind(k, (STRLEN)kl);
1258 8422 o->bodies[i].ctype = oa_keep(aTHX_ a, newSVpvn(k, (STRLEN)kl));
1259 8422 o->bodies[i].kind = kind;
1260 /* A handle is compiled for every type we can decode, and for a range,
1261 * whose decoder is chosen from the request. OPAQUE gets none, so a
1262 * declared text/plain keeps passing through with its body preserved -
1263 * the behaviour t/06-body.t pins and the petstore fixtures rely on.
1264 *
1265 * Form bodies are coerced, like parameters: every value arrives as
1266 * text, so `count=3` has to satisfy an integer the way `?count=3`
1267 * does. JSON already carries its own types. */
1268 7123 ▶ o->bodies[i].handle = (schema && kind != OA_MT_OPAQUE)
Branch: 1 branch
BranchTrueFalseTotal
o->bodies[i].handle = (schema && kind != OA_MT_OPAQUE) 6519 604 7123
1269 6519 ▶ ? oa_compile_schema(aTHX_ a, schema,
Branch: 2 branches
BranchTrueFalseTotal
? oa_compile_schema(aTHX_ a, schema, 5883 636 6519
? oa_compile_schema(aTHX_ a, schema, 614 5269 5883
1270 (kind == OA_MT_FORM || kind == OA_MT_MULTIPART),
1271 OA_DIR_REQ)
1272 9026 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 7123 1299 8422
1273 /* An Encoding Object says how each PROPERTY of a form body is
1274 * serialized. It has meaning only where a form body is parsed into
1275 * properties in the first place, so it is kept for form and multipart
1276 * and ignored elsewhere - a JSON body already carries its own types. */
1277 {
1278 8421 ▶ SV *enc = media ? oa_get(aTHX_ media, "encoding") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *enc = media ? oa_get(aTHX_ media, "encoding") : NULL; 8421 0 8421
1279 8421 o->bodies[i].encoding =
1280 1229 ▶ (enc && oa_hv_of(enc)
Branch: 1 branch
BranchTrueFalseTotal
(enc && oa_hv_of(enc) 1229 0 1229
1281 1229 ▶ && (kind == OA_MT_FORM || kind == OA_MT_MULTIPART))
Branch: 2 branches
BranchTrueFalseTotal
&& (kind == OA_MT_FORM || kind == OA_MT_MULTIPART)) 607 622 1229
&& (kind == OA_MT_FORM || kind == OA_MT_MULTIPART)) 607 0 607
1282 9650 ▶ ? oa_keep(aTHX_ a, newSVsv(enc)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_keep(aTHX_ a, newSVsv(enc)) : NULL; 1229 7192 8421
1283 }
1284 8421 i++;
1285 }
1286 }
1287  
1288 15259 static void oa_compile_responses(pTHX_ oa_api *a, oa_op *o, SV *resps) {
1289 15259 HV *rh = oa_hv_of(resps);
1290 HE *she;
1291 15259 int cap = 0, n = 0;
1292 15259 oa_resp *rows = NULL;
1293 15259 ▶ if (!rh) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rh) return; 0 15259 15259
1294 15259 hv_iterinit(rh);
1295 37405 ▶ while ((she = hv_iternext(rh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((she = hv_iternext(rh))) { 22148 15257 37405
1296 22148 I32 skl; const char *sk = hv_iterkey(she, &skl);
1297 22148 HV *robj = oa_hv_of(hv_iterval(rh, she));
1298 22148 ▶ HV *content = robj ? oa_hv_of(oa_get(aTHX_ robj, "content")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *content = robj ? oa_hv_of(oa_get(aTHX_ robj, "content")) : NULL; 21544 604 22148
1299 HE *che;
1300 /* description is REQUIRED on a Response Object. Checked BEFORE the
1301 * `content` early-return below, or a response with neither would be
1302 * skipped rather than refused. */
1303 22148 ▶ if (robj && !oa_get(aTHX_ robj, "description"))
Branch: 2 branches
BranchTrueFalseTotal
if (robj && !oa_get(aTHX_ robj, "description")) 21544 604 22148
if (robj && !oa_get(aTHX_ robj, "description")) 2 21542 21544
1304 4 croak("Open::API: %s: response '%.*s' has no description",
1305 SvPV_nolen(o->op_id), (int)skl, sk);
1306 22146 ▶ if (!content) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!content) continue; 8224 13922 22146
1307 13922 hv_iterinit(content);
1308 31004 ▶ while ((che = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((che = hv_iternext(content))) { 17082 13922 31004
1309 17082 I32 ckl; const char *ck = hv_iterkey(che, &ckl);
1310 17082 HV *media = oa_hv_of(hv_iterval(content, che));
1311 17082 ▶ SV *schema = media ? oa_get(aTHX_ media, "schema") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *schema = media ? oa_get(aTHX_ media, "schema") : NULL; 17082 0 17082
1312 17082 ▶ if (!schema || !oa_ctype_is_json(ck, (STRLEN)ckl)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!schema || !oa_ctype_is_json(ck, (STRLEN)ckl)) continue; 17051 31 17082
if (!schema || !oa_ctype_is_json(ck, (STRLEN)ckl)) continue; 3769 13282 17051
1313 13282 ▶ if (n == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == cap) { 8946 4336 13282
1314 8946 ▶ cap = cap ? cap * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
cap = cap ? cap * 2 : 4; 0 8946 8946
1315 8946 rows = (oa_resp *)realloc(rows, (size_t)cap * sizeof(oa_resp));
1316 8946 ▶ if (!rows) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!rows) croak("Open::API: out of memory"); 0 8946 8946
1317 }
1318 13282 rows[n].status = oa_keep(aTHX_ a, newSVpvn(sk, (STRLEN)skl));
1319 13282 rows[n].ctype = oa_keep(aTHX_ a, newSVpvn(ck, (STRLEN)ckl));
1320 13282 rows[n].handle = oa_compile_schema(aTHX_ a, schema, 0, OA_DIR_RESP);
1321 13282 n++;
1322 }
1323 }
1324 15257 o->resps = rows;
1325 15257 o->nresps = n;
1326  
1327 /* Headers, in a second pass, because they hang off the STATUS and not the
1328 * media type. Two consequences the loop above cannot express: a status
1329 * declaring two content types must not carry two copies of its headers,
1330 * and a status with headers but no JSON body - 204, or a 3xx carrying
1331 * Location - compiles no row above at all yet still has headers to check.
1332 *
1333 * A Header Object is a Parameter Object without `name`/`in`, so each one
1334 * fills an oa_param and oa_check_param applies unchanged. Compiled with
1335 * coercion on, as parameters are: a header value arrives as text. */
1336 {
1337 15257 int hcap = 0, hn = 0;
1338 15257 oa_rhdr *hrows = NULL;
1339 15257 hv_iterinit(rh);
1340 37403 ▶ while ((she = hv_iternext(rh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((she = hv_iternext(rh))) { 22146 15257 37403
1341 22146 I32 skl; const char *sk = hv_iterkey(she, &skl);
1342 22146 HV *robj = oa_hv_of(hv_iterval(rh, she));
1343 22146 ▶ HV *hdrs = robj ? oa_hv_of(oa_get(aTHX_ robj, "headers")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *hdrs = robj ? oa_hv_of(oa_get(aTHX_ robj, "headers")) : NULL; 21542 604 22146
1344 HE *hhe;
1345 22146 ▶ if (!hdrs) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hdrs) continue; 21517 629 22146
1346 629 hv_iterinit(hdrs);
1347 1862 ▶ while ((hhe = hv_iternext(hdrs))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((hhe = hv_iternext(hdrs))) { 1233 629 1862
1348 1233 I32 hkl; const char *hk = hv_iterkey(hhe, &hkl);
1349 1233 HV *hobj = oa_hv_of(hv_iterval(hdrs, hhe));
1350 SV *hs, *hr;
1351 1233 ▶ if (!hobj) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hobj) continue; 0 1233 1233
1352 1233 hs = oa_get(aTHX_ hobj, "schema");
1353 1233 hr = oa_get(aTHX_ hobj, "required");
1354 1233 ▶ if (hn == hcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (hn == hcap) { 629 604 1233
1355 629 ▶ hcap = hcap ? hcap * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
hcap = hcap ? hcap * 2 : 4; 0 629 629
1356 629 hrows = (oa_rhdr *)realloc(hrows,
1357 629 (size_t)hcap * sizeof(oa_rhdr));
1358 629 ▶ if (!hrows) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!hrows) croak("Open::API: out of memory"); 0 629 629
1359 }
1360 1233 hrows[hn].status = oa_keep(aTHX_ a, newSVpvn(sk, (STRLEN)skl));
1361 1233 hrows[hn].p.name = oa_keep(aTHX_ a, newSVpvn(hk, (STRLEN)hkl));
1362 1233 ▶ hrows[hn].p.required = (hr && oa_sv_truthy(aTHX_ hr)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
hrows[hn].p.required = (hr && oa_sv_truthy(aTHX_ hr)) ? 1 : 0; 10 1223 1233
hrows[hn].p.required = (hr && oa_sv_truthy(aTHX_ hr)) ? 1 : 0; 10 0 10
1363 1233 hrows[hn].p.ctype = NULL;
1364 /* an array header is split before the check, like a request
1365 * header; until this was read the row said "not an array" and
1366 * a schema of type array failed on every value */
1367 1233 ▶ hrows[hn].p.is_array = hs ? oa_schema_is_array(aTHX_ a, hs, 0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
hrows[hn].p.is_array = hs ? oa_schema_is_array(aTHX_ a, hs, 0) : 0; 1233 0 1233
1368 /* these rows come from realloc, NOT calloc: every field has
1369 * to be assigned or it reads uninitialised memory */
1370 1233 hrows[hn].p.is_object = 0;
1371 1233 hrows[hn].p.props = NULL;
1372 1233 hrows[hn].p.style = OA_ST_SIMPLE;
1373 1233 hrows[hn].p.explode = 0;
1374 1233 hrows[hn].p.deprecated = 0;
1375 1233 hrows[hn].p.allow_empty = 0;
1376 1233 hrows[hn].p.allow_reserved = 0;
1377 1233 hrows[hn].p.delim = oa_cf_delim(aTHX_ oa_get(aTHX_ hobj, "x-oa-collectionFormat"));
1378 1233 oa_nested_delims(aTHX_ &hrows[hn].p, hs);
1379 1233 hrows[hn].p.handle = hs
1380 1233 ▶ ? oa_compile_schema(aTHX_ a, hs, 1, OA_DIR_RESP) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_compile_schema(aTHX_ a, hs, 1, OA_DIR_RESP) : NULL; 1233 0 1233
1381 1233 hn++;
1382 }
1383 }
1384 15257 o->rhdrs = hrows;
1385 15257 o->nrhdrs = hn;
1386 }
1387 }
1388  
1389 /* ---- security: schemes + requirements --------------------------------------- */
1390  
1391 /* components.securitySchemes -> the schemes table. Unsupported types croak
1392 * only when a requirement actually references them (checked at lookup). */
1393 5754 static void oa_compile_schemes(pTHX_ oa_api *a, oa_ops *t) {
1394 5754 HV *doc = (HV *)SvRV(a->spec);
1395 5754 HV *comp = oa_hv_of(oa_get(aTHX_ doc, "components"));
1396 5754 ▶ HV *ss = comp ? oa_hv_of(oa_get(aTHX_ comp, "securitySchemes")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *ss = comp ? oa_hv_of(oa_get(aTHX_ comp, "securitySchemes")) : NULL; 3795 1959 5754
1397 HE *he;
1398 5754 int i = 0, n;
1399 5754 ▶ if (!ss) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ss) return; 5109 645 5754
1400 645 ▶ n = (int)HvUSEDKEYS(ss);
Branch: 1 branch
BranchTrueFalseTotal
n = (int)HvUSEDKEYS(ss); 0 645 645
1401 645 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 645 645
1402 645 t->schemes = (oa_scheme *)calloc((size_t)n, sizeof(oa_scheme));
1403 645 ▶ if (!t->schemes) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!t->schemes) croak("Open::API: out of memory"); 0 645 645
1404 645 hv_iterinit(ss);
1405 4912 ▶ while ((he = hv_iternext(ss))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(ss))) { 4271 641 4912
1406 4271 I32 kl; const char *k = hv_iterkey(he, &kl);
1407 4271 HV *sh = oa_hv_of(hv_iterval(ss, he));
1408 4271 ▶ SV *tv = sh ? oa_get(aTHX_ sh, "type") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *tv = sh ? oa_get(aTHX_ sh, "type") : NULL; 4271 0 4271
1409 4271 oa_scheme *s = &t->schemes[i];
1410 STRLEN tl; const char *tp;
1411 4271 ▶ if (!tv) croak("Open::API: securityScheme '%.*s' has no type", (int)kl, k);
Branch: 1 branch
BranchTrueFalseTotal
if (!tv) croak("Open::API: securityScheme '%.*s' has no type", (int)kl, k); 0 4271 4271
1412 4271 tp = SvPV_const(tv, tl);
1413 4271 s->name = oa_keep(aTHX_ a, newSVpvn(k, (STRLEN)kl));
1414 4271 s->loc = -1;
1415 4271 ▶ if (tl == 6 && memEQ(tp, "apiKey", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(tp, "apiKey", 6)) { 3657 614 4271
if (tl == 6 && memEQ(tp, "apiKey", 6)) { 1235 2422 3657
1416 1235 SV *nm = oa_get(aTHX_ sh, "name");
1417 1235 int loc = oa_param_loc(aTHX_ oa_get(aTHX_ sh, "in"));
1418 1235 ▶ if (!nm || loc < 0 || loc == OA_IN_PATH)
Branch: 3 branches
BranchTrueFalseTotal
if (!nm || loc < 0 || loc == OA_IN_PATH) 1235 0 1235
if (!nm || loc < 0 || loc == OA_IN_PATH) 1235 0 1235
if (!nm || loc < 0 || loc == OA_IN_PATH) 0 1235 1235
1419 0 croak("Open::API: apiKey scheme '%.*s' needs name and "
1420 "in (query/header/cookie)", (int)kl, k);
1421 1235 s->type = OA_SEC_APIKEY;
1422 1235 s->loc = loc;
1423 1235 s->pname = oa_keep(aTHX_ a, newSVsv(nm));
1424 3647 ▶ } else if (tl == 4 && memEQ(tp, "http", 4)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (tl == 4 && memEQ(tp, "http", 4)) { 611 2425 3036
} else if (tl == 4 && memEQ(tp, "http", 4)) { 611 0 611
1425 611 SV *sch = oa_get(aTHX_ sh, "scheme");
1426 1222 ▶ STRLEN sl; const char *sp = sch ? SvPV_const(sch, sl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN sl; const char *sp = sch ? SvPV_const(sch, sl) : NULL; 611 0 611
1427 611 ▶ if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6)))
Branch: 4 branches
BranchTrueFalseTotal
if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6))) 611 0 611
if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6))) 2 609 611
if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6))) 1 1 2
if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6))) 0 1 1
1428 1 s->type = OA_SEC_BEARER;
1429 610 ▶ else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5)))
Branch: 4 branches
BranchTrueFalseTotal
else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5))) 610 0 610
else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5))) 609 1 610
else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5))) 0 609 609
else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5))) 0 0 0
1430 609 s->type = OA_SEC_BASIC;
1431 else
1432 1 s->type = OA_SEC_OTHER; /* digest and friends: the checker's */
1433 2425 ▶ } else if ((tl == 6 && memEQ(tp, "oauth2", 6))
Branch: 2 branches
BranchTrueFalseTotal
} else if ((tl == 6 && memEQ(tp, "oauth2", 6)) 2422 3 2425
} else if ((tl == 6 && memEQ(tp, "oauth2", 6)) 0 2422 2422
1434 3 ▶ || (tl == 13 && memEQ(tp, "openIdConnect", 13))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (tl == 13 && memEQ(tp, "openIdConnect", 13))) { 2 1 3
|| (tl == 13 && memEQ(tp, "openIdConnect", 13))) { 2 0 2
1435 2424 s->type = OA_SEC_BEARER; /* the credential is the access token */
1436 2424 s->oauthish = 1; /* only these two may carry scopes */
1437 /* Flow automation is out of scope, but the DECLARATION still has
1438 * required fields, and a document that omits them is not a valid
1439 * document. Accepting it means trusting a description nobody
1440 * checked. */
1441 2424 ▶ if (tl == 13) {
Branch: 1 branch
BranchTrueFalseTotal
if (tl == 13) { 2 2422 2424
1442 2 ▶ if (!oa_get(aTHX_ sh, "openIdConnectUrl"))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ sh, "openIdConnectUrl")) 1 1 2
1443 1 croak("Open::API: openIdConnect scheme '%.*s' has no "
1444 "openIdConnectUrl", (int)kl, k);
1445 } else {
1446 2422 HV *flows = oa_hv_of(oa_get(aTHX_ sh, "flows"));
1447 HE *fhe;
1448 2422 ▶ if (!flows)
Branch: 1 branch
BranchTrueFalseTotal
if (!flows) 1 2421 2422
1449 1 croak("Open::API: oauth2 scheme '%.*s' has no flows",
1450 (int)kl, k);
1451 2421 hv_iterinit(flows);
1452 4839 ▶ while ((fhe = hv_iternext(flows))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((fhe = hv_iternext(flows))) { 2420 2419 4839
1453 2420 I32 fkl; const char *fk = hv_iterkey(fhe, &fkl);
1454 2420 HV *fl = oa_hv_of(hv_iterval(flows, fhe));
1455 1208 ▶ int wants_auth = (fkl == 8 && memEQ(fk, "implicit", 8))
Branch: 1 branch
BranchTrueFalseTotal
int wants_auth = (fkl == 8 && memEQ(fk, "implicit", 8)) 604 604 1208
1456 3628 ▶ || (fkl == 17 && memEQ(fk, "authorizationCode", 17));
Branch: 3 branches
BranchTrueFalseTotal
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 1208 1212 2420
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 1212 604 1816
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 605 607 1212
1457 1208 ▶ int wants_token = (fkl == 8 && memEQ(fk, "password", 8))
Branch: 1 branch
BranchTrueFalseTotal
int wants_token = (fkl == 8 && memEQ(fk, "password", 8)) 604 604 1208
1458 1816 ▶ || (fkl == 17 && memEQ(fk, "clientCredentials", 17))
Branch: 2 branches
BranchTrueFalseTotal
|| (fkl == 17 && memEQ(fk, "clientCredentials", 17)) 1212 604 1816
|| (fkl == 17 && memEQ(fk, "clientCredentials", 17)) 605 607 1212
1459 4840 ▶ || (fkl == 17 && memEQ(fk, "authorizationCode", 17));
Branch: 3 branches
BranchTrueFalseTotal
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 1208 1212 2420
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 605 604 1209
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 605 0 605
1460 2420 ▶ if (!fl)
Branch: 1 branch
BranchTrueFalseTotal
if (!fl) 0 2420 2420
1461 0 croak("Open::API: oauth2 scheme '%.*s': flow '%.*s' is "
1462 "not an object", (int)kl, k, (int)fkl, fk);
1463 2420 ▶ if (!oa_get(aTHX_ fl, "scopes"))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ fl, "scopes")) 1 2419 2420
1464 1 croak("Open::API: oauth2 scheme '%.*s': flow '%.*s' has "
1465 "no scopes", (int)kl, k, (int)fkl, fk);
1466 2419 ▶ if (wants_auth && !oa_get(aTHX_ fl, "authorizationUrl"))
Branch: 2 branches
BranchTrueFalseTotal
if (wants_auth && !oa_get(aTHX_ fl, "authorizationUrl")) 1209 1210 2419
if (wants_auth && !oa_get(aTHX_ fl, "authorizationUrl")) 0 1209 1209
1467 0 croak("Open::API: oauth2 scheme '%.*s': flow '%.*s' has "
1468 "no authorizationUrl", (int)kl, k, (int)fkl, fk);
1469 2419 ▶ if (wants_token && !oa_get(aTHX_ fl, "tokenUrl"))
Branch: 2 branches
BranchTrueFalseTotal
if (wants_token && !oa_get(aTHX_ fl, "tokenUrl")) 1815 604 2419
if (wants_token && !oa_get(aTHX_ fl, "tokenUrl")) 1 1814 1815
1470 1 croak("Open::API: oauth2 scheme '%.*s': flow '%.*s' has "
1471 "no tokenUrl", (int)kl, k, (int)fkl, fk);
1472 }
1473 }
1474 1 ▶ } else if (tl == 9 && memEQ(tp, "mutualTLS", 9)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (tl == 9 && memEQ(tp, "mutualTLS", 9)) { 1 0 1
} else if (tl == 9 && memEQ(tp, "mutualTLS", 9)) { 1 0 1
1475 1 s->type = OA_SEC_OTHER; /* settled by the transport */
1476 } else {
1477 0 croak("Open::API: securityScheme '%.*s': unsupported type '%.*s'",
1478 (int)kl, k, (int)tl, tp);
1479 }
1480 4267 i++;
1481 }
1482 641 t->nschemes = i;
1483 }
1484  
1485 3058 static int oa_scheme_index(pTHX_ oa_ops *t, SV *name) {
1486 int i;
1487 12186 ▶ for (i = 0; i < t->nschemes; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->nschemes; i++) 12184 2 12186
1488 12184 ▶ if (sv_eq(t->schemes[i].name, name)) return i;
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(t->schemes[i].name, name)) return i; 3056 9128 12184
1489 2 return -1;
1490 }
1491  
1492 /* a `security` AV -> malloc'd alternative list (each element an object whose
1493 * keys are ANDed schemes; an empty object means "no auth is acceptable") */
1494 1846 static oa_secalt *oa_compile_security(pTHX_ oa_api *a, oa_ops *t, AV *sec,
1495 int *nout, const char *where) {
1496 1846 SSize_t n = av_len(sec) + 1, i;
1497 oa_secalt *alts;
1498 1846 *nout = 0;
1499 1846 ▶ if (n <= 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return NULL; 1 1845 1846
1500 1845 alts = (oa_secalt *)calloc((size_t)n, sizeof(oa_secalt));
1501 1845 ▶ if (!alts) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!alts) croak("Open::API: out of memory"); 0 1845 1845
1502 5501 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3661 1840 5501
1503 3661 SV **e = av_fetch(sec, i, 0);
1504 3661 ▶ HV *req = e ? oa_hv_of(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *req = e ? oa_hv_of(*e) : NULL; 3661 0 3661
1505 HE *he;
1506 3661 int m, j = 0;
1507 3661 ▶ if (!req) croak("Open::API: %s: security entries must be objects", where);
Branch: 1 branch
BranchTrueFalseTotal
if (!req) croak("Open::API: %s: security entries must be objects", where); 0 3661 3661
1508 3661 ▶ m = (int)HvUSEDKEYS(req);
Branch: 1 branch
BranchTrueFalseTotal
m = (int)HvUSEDKEYS(req); 0 3661 3661
1509 3661 alts[i].n = m;
1510 3056 alts[i].items = m ? (oa_secitem *)calloc((size_t)m, sizeof(oa_secitem))
1511 3661 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 3056 605 3661
1512 3661 ▶ if (m && !alts[i].items) croak("Open::API: out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (m && !alts[i].items) croak("Open::API: out of memory"); 3056 605 3661
if (m && !alts[i].items) croak("Open::API: out of memory"); 0 3056 3056
1513 3661 hv_iterinit(req);
1514 6714 ▶ while ((he = hv_iternext(req))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(req))) { 3058 3656 6714
1515 3058 I32 kl; const char *k = hv_iterkey(he, &kl);
1516 3058 SV *nm = sv_2mortal(newSVpvn(k, (STRLEN)kl));
1517 3058 SV *scopes = hv_iterval(req, he);
1518 3058 int idx = oa_scheme_index(aTHX_ t, nm);
1519 3058 ▶ if (idx < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) 2 3056 3058
1520 2 croak("Open::API: %s references unknown securityScheme '%.*s'",
1521 where, (int)kl, k);
1522 3056 alts[i].items[j].scheme = idx;
1523 /* Only oauth2 and openIdConnect may name scopes. Under 3.0 the
1524 * specification says the list for any other scheme MUST be empty,
1525 * and a document asking for scopes on an apiKey is describing
1526 * something that cannot happen. 3.1 relaxed that: the array MAY
1527 * carry role names, which are not defined or exchanged in-band, so
1528 * under 3.1 the list is passed through to the caller untouched. */
1529 {
1530 3056 AV *sc = oa_av_of(scopes);
1531 3056 ▶ if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish)
Branch: 4 branches
BranchTrueFalseTotal
if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish) 3039 17 3056
if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish) 3039 0 3039
if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish) 1214 1825 3039
if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish) 3 1211 1214
1532 /* "named" matters: a scheme's NAME is free, so one called
1533 * "apiKey" can be declared `type: http`, and a message
1534 * saying only "scheme 'apiKey'" reads like the type */
1535 3 croak("Open::API: %s: the security scheme NAMED '%.*s' is "
1536 "declared as neither oauth2 nor openIdConnect, so "
1537 "the scopes listed for it here must be an empty list",
1538 where, (int)kl, k);
1539 }
1540 3053 alts[i].items[j].scopes = oa_av_of(scopes)
1541 3053 ▶ ? oa_keep(aTHX_ a, newSVsv(scopes)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_keep(aTHX_ a, newSVsv(scopes)) : NULL; 3053 0 3053
1542 3053 j++;
1543 }
1544 }
1545 1840 *nout = (int)n;
1546 1840 return alts;
1547 }
1548  
1549 /* ---- the walk --------------------------------------------------------------- */
1550  
1551 static const char *oa_methods[] = {
1552 "get", "put", "post", "delete", "options", "head", "patch", "trace", NULL
1553 };
1554  
1555 /* The path prefix the first Server Object's URL carries, with {variables}
1556 * expanded from their declared defaults: `https://h.test/api/v1` yields
1557 * `/api/v1`, and a URL with no path (or a bare `/`) yields nothing to strip.
1558 * A relative URL is all path. Called only when the caller opted in. */
1559 /* The prefix a `servers` array's first entry carries, as a kept SV, or NULL.
1560 * Shared by the document level and by a path item or operation declaring its
1561 * own - the specification lets any of the three, most specific winning. */
1562 10 static SV *oa_server_prefix_of(pTHX_ oa_api *a, AV *srv) {
1563 10 ▶ SV **e = srv ? av_fetch(srv, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = srv ? av_fetch(srv, 0, 0) : NULL; 8 2 10
1564 10 ▶ HV *s0 = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *s0 = (e && *e) ? oa_hv_of(*e) : NULL; 8 2 10
HV *s0 = (e && *e) ? oa_hv_of(*e) : NULL; 8 0 8
1565 10 ▶ SV *url = s0 ? oa_get(aTHX_ s0, "url") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *url = s0 ? oa_get(aTHX_ s0, "url") : NULL; 8 2 10
1566 10 ▶ HV *vars = s0 ? oa_hv_of(oa_get(aTHX_ s0, "variables")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *vars = s0 ? oa_hv_of(oa_get(aTHX_ s0, "variables")) : NULL; 8 2 10
1567 10 STRLEN ul, i, pathstart = 0;
1568 const char *up;
1569 SV *out;
1570 10 ▶ if (!url) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!url) return NULL; 2 8 10
1571 8 up = SvPV_const(url, ul);
1572 /* past scheme://host, or past //host: a 2.0 document with a host and no
1573 * schemes converts to a scheme-relative server URL, and scanning only for
1574 * `://` would take the whole of `//host/v2` as the prefix */
1575 8 ▶ if (ul >= 2 && up[0] == '/' && up[1] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (ul >= 2 && up[0] == '/' && up[1] == '/') { 8 0 8
if (ul >= 2 && up[0] == '/' && up[1] == '/') { 1 7 8
if (ul >= 2 && up[0] == '/' && up[1] == '/') { 1 0 1
1576 1 STRLEN j = 2;
1577 7 ▶ while (j < ul && up[j] != '/') j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < ul && up[j] != '/') j++; 7 0 7
while (j < ul && up[j] != '/') j++; 6 1 7
1578 1 pathstart = j;
1579 } else {
1580 42 ▶ for (i = 0; i + 2 < ul; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < ul; i++) { 42 0 42
1581 42 ▶ if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') { 7 35 42
if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') { 7 0 7
if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') { 7 0 7
1582 7 STRLEN j = i + 3;
1583 49 ▶ while (j < ul && up[j] != '/') j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < ul && up[j] != '/') j++; 49 0 49
while (j < ul && up[j] != '/') j++; 42 7 49
1584 7 pathstart = j;
1585 7 break;
1586 }
1587 }
1588 }
1589 8 out = sv_2mortal(newSVpvs(""));
1590 46 ▶ for (i = pathstart; i < ul; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = pathstart; i < ul; i++) { 38 8 46
1591 38 ▶ if (up[i] == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (up[i] == '{') { 0 38 38
1592 0 STRLEN j = i + 1;
1593 0 ▶ while (j < ul && up[j] != '}') j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < ul && up[j] != '}') j++; 0 0 0
while (j < ul && up[j] != '}') j++; 0 0 0
1594 0 ▶ if (j < ul) {
Branch: 1 branch
BranchTrueFalseTotal
if (j < ul) { 0 0 0
1595 0 SV **dv = vars ? hv_fetch(vars, up + i + 1,
1596 0 ▶ (I32)(j - i - 1), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
(I32)(j - i - 1), 0) : NULL; 0 0 0
1597 0 ▶ HV *vh = (dv && *dv) ? oa_hv_of(*dv) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *vh = (dv && *dv) ? oa_hv_of(*dv) : NULL; 0 0 0
HV *vh = (dv && *dv) ? oa_hv_of(*dv) : NULL; 0 0 0
1598 0 ▶ SV *def = vh ? oa_get(aTHX_ vh, "default") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *def = vh ? oa_get(aTHX_ vh, "default") : NULL; 0 0 0
1599 0 ▶ if (def) sv_catsv(out, def);
Branch: 1 branch
BranchTrueFalseTotal
if (def) sv_catsv(out, def); 0 0 0
1600 0 i = j;
1601 0 continue;
1602 }
1603 }
1604 38 sv_catpvn(out, up + i, 1);
1605 }
1606 8 ▶ while (SvCUR(out) && SvPVX(out)[SvCUR(out) - 1] == '/')
Branch: 2 branches
BranchTrueFalseTotal
while (SvCUR(out) && SvPVX(out)[SvCUR(out) - 1] == '/') 8 0 8
while (SvCUR(out) && SvPVX(out)[SvCUR(out) - 1] == '/') 0 8 8
1607 0 SvCUR_set(out, SvCUR(out) - 1); /* a trailing / is not prefix */
1608 8 ▶ return SvCUR(out) ? oa_keep(aTHX_ a, newSVsv(out)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvCUR(out) ? oa_keep(aTHX_ a, newSVsv(out)) : NULL; 8 0 8
1609 }
1610  
1611 8 static void oa_set_server_prefix(pTHX_ oa_api *a) {
1612 8 HV *doc = oa_hv_of(a->spec);
1613 8 ▶ AV *srv = doc ? oa_av_of(oa_get(aTHX_ doc, "servers")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *srv = doc ? oa_av_of(oa_get(aTHX_ doc, "servers")) : NULL; 8 0 8
1614 8 SV *p = oa_server_prefix_of(aTHX_ a, srv);
1615 8 ▶ if (p) a->prefix = p;
Branch: 1 branch
BranchTrueFalseTotal
if (p) a->prefix = p; 6 2 8
1616 8 }
1617  
1618 /* The order the router walks the operation table in.
1619 *
1620 * Without one, the table is in `paths` hash order, which perl randomises per
1621 * process - so a request that two templates both fit routed to a DIFFERENT
1622 * operation between runs of the same document. `/t/fixed` against `/t/fixed`
1623 * and `/t/{id}` picked the template roughly one run in five.
1624 *
1625 * Ordering rule, applied left to right: at the first segment where two
1626 * templates differ in kind, the one with a LITERAL there sorts first, so a
1627 * concrete path is always preferred over a template that also fits. Ties are
1628 * broken on the path string, which makes the order total and reproducible
1629 * even between two templates of identical shape.
1630 *
1631 * A separate index, because by_id holds indices into `ops`. Insertion sort:
1632 * a document has tens of routes and this runs once, at compile. */
1633 18707 static int oa_route_before(pTHX_ oa_op *x, oa_op *y) {
1634 18707 int i, n = x->nsegs < y->nsegs ? x->nsegs : y->nsegs;
1635 STRLEN xl, yl;
1636 const char *xp, *yp;
1637 int c;
1638 44415 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 25786 18629 44415
1639 25786 int xlit = x->segs[i].lit ? 1 : 0;
1640 25786 int ylit = y->segs[i].lit ? 1 : 0;
1641 25786 ▶ if (xlit != ylit) return xlit; /* literal first */
Branch: 1 branch
BranchTrueFalseTotal
if (xlit != ylit) return xlit; /* literal first */ 78 25708 25786
1642 }
1643 18629 ▶ if (x->nsegs != y->nsegs) return x->nsegs < y->nsegs;
Branch: 1 branch
BranchTrueFalseTotal
if (x->nsegs != y->nsegs) return x->nsegs < y->nsegs; 12005 6624 18629
1644 /* identical shape: order on the path, then the method, so the result does
1645 * not depend on which the hash happened to hand over first */
1646 13248 ▶ xp = x->path ? SvPV_const(x->path, xl) : (xl = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
xp = x->path ? SvPV_const(x->path, xl) : (xl = 0, ""); 6624 0 6624
1647 13248 ▶ yp = y->path ? SvPV_const(y->path, yl) : (yl = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
yp = y->path ? SvPV_const(y->path, yl) : (yl = 0, ""); 6624 0 6624
1648 6624 c = memcmp(xp, yp, xl < yl ? xl : yl);
1649 6624 ▶ if (c) return c < 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c < 0; 1553 5071 6624
1650 5071 ▶ if (xl != yl) return xl < yl;
Branch: 1 branch
BranchTrueFalseTotal
if (xl != yl) return xl < yl; 0 5071 5071
1651 10142 ▶ xp = x->method ? SvPV_const(x->method, xl) : (xl = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
xp = x->method ? SvPV_const(x->method, xl) : (xl = 0, ""); 5071 0 5071
1652 10142 ▶ yp = y->method ? SvPV_const(y->method, yl) : (yl = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
yp = y->method ? SvPV_const(y->method, yl) : (yl = 0, ""); 5071 0 5071
1653 5071 c = memcmp(xp, yp, xl < yl ? xl : yl);
1654 5071 ▶ if (c) return c < 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c < 0; 5071 0 5071
1655 0 return xl <= yl;
1656 }
1657  
1658 5732 static void oa_order_routes(pTHX_ oa_ops *t) {
1659 int i;
1660 5732 free(t->order);
1661 5732 t->order = NULL;
1662 5732 ▶ if (t->n <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (t->n <= 0) return; 32 5700 5732
1663 5700 t->order = (int *)malloc((size_t)t->n * sizeof(int));
1664 5700 ▶ if (!t->order) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!t->order) croak("Open::API: out of memory"); 0 5700 5700
1665 20955 ▶ for (i = 0; i < t->n; i++) t->order[i] = i;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n; i++) t->order[i] = i; 15255 5700 20955
1666 15255 ▶ for (i = 1; i < t->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < t->n; i++) { 9555 5700 15255
1667 9555 int cur = t->order[i];
1668 9555 int j = i - 1;
1669 21750 ▶ while (j >= 0 && oa_route_before(aTHX_ &t->ops[cur], &t->ops[t->order[j]])) {
Branch: 2 branches
BranchTrueFalseTotal
while (j >= 0 && oa_route_before(aTHX_ &t->ops[cur], &t->ops[t->order[j]])) { 18707 3043 21750
while (j >= 0 && oa_route_before(aTHX_ &t->ops[cur], &t->ops[t->order[j]])) { 12195 6512 18707
1670 12195 t->order[j + 1] = t->order[j];
1671 12195 j--;
1672 }
1673 9555 t->order[j + 1] = cur;
1674 }
1675 }
1676  
1677 5754 static void oa_compile(pTHX_ oa_api *a) {
1678 5754 HV *doc = (HV *)SvRV(a->spec);
1679 5754 HV *paths = oa_hv_of(oa_get(aTHX_ doc, "paths"));
1680 oa_ops *t;
1681 HE *phe;
1682  
1683 5754 oa_build_defs(aTHX_ a); /* shared $defs from components.schemas */
1684  
1685 5754 t = (oa_ops *)calloc(1, sizeof(oa_ops));
1686 5754 ▶ if (!t) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!t) croak("Open::API: out of memory"); 0 5754 5754
1687 5754 t->by_id = newHV();
1688 5754 a->ops = t;
1689  
1690 /* The normaliser converts a 2.0 or 3.0 document's schemas but leaves its
1691 * version string alone, so the version is still readable here - and the
1692 * security requirement rules differ between 3.0 and 3.1, with 2.0 on
1693 * 3.0's side. Read through the gate's own reader so they cannot drift. */
1694 5754 t->v30 = (oa_doc_version(aTHX_ doc) <= OA_V30);
1695  
1696 5754 oa_compile_schemes(aTHX_ a, t);
1697 {
1698 5750 AV *rs = oa_av_of(oa_get(aTHX_ doc, "security"));
1699 5750 ▶ if (rs) t->rootsec = oa_compile_security(aTHX_ a, t, rs,
Branch: 1 branch
BranchTrueFalseTotal
if (rs) t->rootsec = oa_compile_security(aTHX_ a, t, rs, 631 5119 5750
1700 &t->nrootsec, "document security");
1701 }
1702  
1703 5745 ▶ if (!paths) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!paths) return; 0 5745 5745
1704  
1705 /* Two templated paths with the same hierarchy but different variable
1706 * names are IDENTICAL and must not both exist - /t/{a} and /t/{b} are one
1707 * path, and a document declaring both is asking for two routes that can
1708 * never be told apart. Each template is canonicalised by replacing every
1709 * {...} with a single placeholder, and a repeat is refused. */
1710 {
1711 5745 HV *seen = (HV *)sv_2mortal((SV *)newHV());
1712 5745 hv_iterinit(paths);
1713 16546 ▶ while ((phe = hv_iternext(paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((phe = hv_iternext(paths))) { 10805 5741 16546
1714 10805 I32 tkl; const char *tpl = hv_iterkey(phe, &tkl);
1715 10805 SV *canon = sv_2mortal(newSVpvs(""));
1716 STRLEN q;
1717 /* a Paths Object may carry ^x- extensions beside its paths, in
1718 * every version of the specification; they are not paths */
1719 10805 ▶ if (tkl >= 2 && tpl[0] == 'x' && tpl[1] == '-') continue;
Branch: 3 branches
BranchTrueFalseTotal
if (tkl >= 2 && tpl[0] == 'x' && tpl[1] == '-') continue; 10798 7 10805
if (tkl >= 2 && tpl[0] == 'x' && tpl[1] == '-') continue; 604 10194 10798
if (tkl >= 2 && tpl[0] == 'x' && tpl[1] == '-') continue; 604 0 604
1720 /* a path field name MUST begin with a forward slash: it is
1721 * appended to a server URL, and one that does not would silently
1722 * join onto the host instead */
1723 10201 ▶ if (tkl < 1 || tpl[0] != '/')
Branch: 2 branches
BranchTrueFalseTotal
if (tkl < 1 || tpl[0] != '/') 10201 0 10201
if (tkl < 1 || tpl[0] != '/') 2 10199 10201
1724 2 croak("Open::API: path '%.*s' does not begin with '/'",
1725 (int)tkl, tpl);
1726 69729 ▶ for (q = 0; q < (STRLEN)tkl; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = 0; q < (STRLEN)tkl; q++) { 59530 10199 69729
1727 59530 ▶ if (tpl[q] == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (tpl[q] == '{') { 4486 55044 59530
1728 28073 ▶ while (q < (STRLEN)tkl && tpl[q] != '}') q++;
Branch: 2 branches
BranchTrueFalseTotal
while (q < (STRLEN)tkl && tpl[q] != '}') q++; 28073 0 28073
while (q < (STRLEN)tkl && tpl[q] != '}') q++; 23587 4486 28073
1729 4486 sv_catpvs(canon, "\1"); /* one placeholder */
1730 4486 continue;
1731 }
1732 55044 sv_catpvn(canon, tpl + q, 1);
1733 }
1734 {
1735 10199 STRLEN cl; const char *cp = SvPV_const(canon, cl);
1736 10199 ▶ if (hv_exists(seen, cp, (I32)cl))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, cp, (I32)cl)) 2 10197 10199
1737 2 croak("Open::API: paths '%.*s' duplicates another path "
1738 "that differs only in its template variable names",
1739 (int)tkl, tpl);
1740 10197 (void)hv_store(seen, cp, (I32)cl, newSViv(1), 0);
1741 }
1742 }
1743 }
1744  
1745 5741 hv_iterinit(paths);
1746 16531 ▶ while ((phe = hv_iternext(paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((phe = hv_iternext(paths))) { 10799 5732 16531
1747 10799 I32 tkl; const char *tpl = hv_iterkey(phe, &tkl);
1748 10799 HV *item = oa_hv_of(hv_iterval(paths, phe));
1749 AV *item_params;
1750 int m;
1751 11403 ▶ if (!item) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!item) continue; 0 10799 10799
1752 10799 ▶ if (tkl >= 2 && tpl[0] == 'x' && tpl[1] == '-') continue; /* an extension */
Branch: 3 branches
BranchTrueFalseTotal
if (tkl >= 2 && tpl[0] == 'x' && tpl[1] == '-') continue; /* an extension */ 10793 6 10799
if (tkl >= 2 && tpl[0] == 'x' && tpl[1] == '-') continue; /* an extension */ 604 10189 10793
if (tkl >= 2 && tpl[0] == 'x' && tpl[1] == '-') continue; /* an extension */ 604 0 604
1753 10195 item_params = oa_av_of(oa_get(aTHX_ item, "parameters"));
1754  
1755 91685 ▶ for (m = 0; oa_methods[m]; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; oa_methods[m]; m++) { 81499 10186 91685
1756 81499 HV *oph = oa_hv_of(oa_get(aTHX_ item, oa_methods[m]));
1757 SV *op_id;
1758 oa_op *o;
1759 81499 ▶ if (!oph) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!oph) continue; 66233 15266 81499
1760 15266 oa_ext_docs_ok(aTHX_ oa_get(aTHX_ oph, "externalDocs"), "operation");
1761 15264 op_id = oa_get(aTHX_ oph, "operationId");
1762 15264 ▶ if (!op_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (!op_id) { 38 15226 15264
1763 /* operationId is OPTIONAL per the specification, so a document
1764 * without one is still valid and must still compile. It is the
1765 * dispatch key here, so one is derived: <method>_<path>, with
1766 * the template braces dropped and separators flattened. Same
1767 * rule as plan_maat_mcp/01, so the MCP tool names and the core
1768 * agree on what an unnamed operation is called. A collision
1769 * with a real operationId is still caught below, as it must
1770 * be - two operations cannot share a dispatch key. */
1771 38 SV *d = sv_2mortal(newSVpv(oa_methods[m], 0));
1772 STRLEN q;
1773 38 int owed = 1; /* a separator is owed before the next char */
1774 378 ▶ for (q = 0; q < (STRLEN)tkl; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = 0; q < (STRLEN)tkl; q++) { 340 38 378
1775 340 char ch = tpl[q];
1776 390 ▶ if (ch == '{' || ch == '}') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (ch == '{' || ch == '}') continue; 340 0 340
if (ch == '{' || ch == '}') continue; 0 340 340
1777 /* a slash OWES a separator rather than emitting one, so a
1778 * leading slash does not double the underscore already
1779 * between method and path, and a trailing one leaves none
1780 * dangling: /a -> get_a, /pets/{petId} -> get_pets_petId */
1781 340 ▶ if (ch == '/') { owed = 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '/') { owed = 1; continue; } 50 290 340
1782 290 ▶ if (owed) { sv_catpvs(d, "_"); owed = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (owed) { sv_catpvs(d, "_"); owed = 0; } 50 240 290
1783 290 sv_catpvn(d, &ch, 1);
1784 }
1785 38 op_id = d;
1786 }
1787 {
1788 15264 STRLEN il; const char *ip = SvPV_const(op_id, il);
1789 15264 ▶ if (hv_exists(t->by_id, ip, (I32)il))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(t->by_id, ip, (I32)il)) 2 15262 15264
1790 2 croak("Open::API: duplicate operationId '%.*s'", (int)il, ip);
1791 15262 ▶ if (t->n == t->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->n == t->cap) { 5707 9555 15262
1792 5707 ▶ t->cap = t->cap ? t->cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
t->cap = t->cap ? t->cap * 2 : 8; 0 5707 5707
1793 5707 t->ops = (oa_op *)realloc(t->ops,
1794 5707 (size_t)t->cap * sizeof(oa_op));
1795 5707 ▶ if (!t->ops) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!t->ops) croak("Open::API: out of memory"); 0 5707 5707
1796 }
1797 15262 o = &t->ops[t->n];
1798 15262 memset(o, 0, sizeof(*o));
1799 15262 o->op_id = oa_keep(aTHX_ a, newSVsv(op_id));
1800 15262 o->method = oa_keep(aTHX_ a, newSVpv(oa_methods[m], 0));
1801 15262 o->path = oa_keep(aTHX_ a, newSVpvn(tpl, (STRLEN)tkl));
1802 15262 (void)hv_store(t->by_id, ip, (I32)il, newSViv(t->n), 0);
1803 15262 t->n++;
1804 }
1805 /* A path item or an operation may declare its own `servers`, and
1806 * the more specific wins. Only consulted when the caller opted
1807 * in, exactly as the document-level prefix is: honouring a prefix
1808 * re-routes an application that already mounts without it. */
1809 15262 ▶ if (a->prefix || a->want_servers) {
Branch: 2 branches
BranchTrueFalseTotal
if (a->prefix || a->want_servers) { 15256 6 15262
if (a->prefix || a->want_servers) { 2 15254 15256
1810 8 AV *osrv = oa_av_of(oa_get(aTHX_ oph, "servers"));
1811 8 AV *psrv = oa_av_of(oa_get(aTHX_ item, "servers"));
1812 8 ▶ if (osrv) o->prefix = oa_server_prefix_of(aTHX_ a, osrv);
Branch: 1 branch
BranchTrueFalseTotal
if (osrv) o->prefix = oa_server_prefix_of(aTHX_ a, osrv); 1 7 8
1813 7 ▶ else if (psrv) o->prefix = oa_server_prefix_of(aTHX_ a, psrv);
Branch: 1 branch
BranchTrueFalseTotal
else if (psrv) o->prefix = oa_server_prefix_of(aTHX_ a, psrv); 1 6 7
1814 }
1815 15262 oa_compile_segs(aTHX_ a, o, tpl, (STRLEN)tkl);
1816 15262 oa_compile_params(aTHX_ a, o, item_params,
1817 oa_av_of(oa_get(aTHX_ oph, "parameters")));
1818 15260 oa_compile_body(aTHX_ a, o, oa_get(aTHX_ oph, "requestBody"));
1819 15259 oa_compile_responses(aTHX_ a, o, oa_get(aTHX_ oph, "responses"));
1820 { /* security: operation-level overrides (an explicit empty
1821 * array disables auth); otherwise inherit the document's */
1822 15257 SV *osec = oa_get(aTHX_ oph, "security");
1823 15257 ▶ if (osec) {
Branch: 1 branch
BranchTrueFalseTotal
if (osec) { 1215 14042 15257
1824 1215 AV *sa = oa_av_of(osec);
1825 1215 ▶ if (!sa) croak("Open::API: %s: security must be an array",
Branch: 1 branch
BranchTrueFalseTotal
if (!sa) croak("Open::API: %s: security must be an array", 0 1215 1215
1826 SvPV_nolen(o->op_id));
1827 2430 o->sec = oa_compile_security(aTHX_ a, t, sa, &o->nsec,
1828 2430 SvPV_nolen(o->op_id));
1829 1215 o->own_sec = 1;
1830 } else {
1831 14042 o->sec = t->rootsec;
1832 14042 o->nsec = t->nrootsec;
1833 14042 o->own_sec = 0;
1834 }
1835 }
1836 }
1837 }
1838  
1839 /* A Link's operationId names "an existing, resolvable OAS operation".
1840 * Links are carried for consumers rather than compiled, so nothing else
1841 * would ever catch a dangling one.
1842 *
1843 * This runs AFTER the walk above, not inside it: a Link may point at an
1844 * operation declared later in the document, and refusing that would be
1845 * wrong. By here every operationId - declared or derived - is in by_id. */
1846 5732 oa_order_routes(aTHX_ t);
1847 {
1848 HE *lphe;
1849 5732 hv_iterinit(paths);
1850 16518 ▶ while ((lphe = hv_iternext(paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((lphe = hv_iternext(paths))) { 10788 5730 16518
1851 10788 HV *item = oa_hv_of(hv_iterval(paths, lphe));
1852 int m;
1853 10788 ▶ if (!item) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!item) continue; 0 10788 10788
1854 97076 ▶ for (m = 0; oa_methods[m]; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; oa_methods[m]; m++) { 86290 10786 97076
1855 86290 HV *oph = oa_hv_of(oa_get(aTHX_ item, oa_methods[m]));
1856 86290 ▶ HV *resps = oph ? oa_hv_of(oa_get(aTHX_ oph, "responses")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *resps = oph ? oa_hv_of(oa_get(aTHX_ oph, "responses")) : NULL; 15255 71035 86290
1857 HE *rhe;
1858 86290 ▶ if (!resps) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!resps) continue; 71035 15255 86290
1859 15255 hv_iterinit(resps);
1860 37398 ▶ while ((rhe = hv_iternext(resps))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((rhe = hv_iternext(resps))) { 22145 15253 37398
1861 22145 HV *robj = oa_hv_of(hv_iterval(resps, rhe));
1862 22145 ▶ HV *links = robj ? oa_hv_of(oa_get(aTHX_ robj, "links")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *links = robj ? oa_hv_of(oa_get(aTHX_ robj, "links")) : NULL; 21541 604 22145
1863 HE *lhe;
1864 22145 ▶ if (!links) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!links) continue; 22140 5 22145
1865 5 hv_iterinit(links);
1866 8 ▶ while ((lhe = hv_iternext(links))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((lhe = hv_iternext(links))) { 5 3 8
1867 5 I32 lkl; const char *lk = hv_iterkey(lhe, &lkl);
1868 5 HV *lo = oa_hv_of(hv_iterval(links, lhe));
1869 5 ▶ SV *oid = lo ? oa_get(aTHX_ lo, "operationId") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *oid = lo ? oa_get(aTHX_ lo, "operationId") : NULL; 5 0 5
1870 5 ▶ SV *oref = lo ? oa_get(aTHX_ lo, "operationRef") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *oref = lo ? oa_get(aTHX_ lo, "operationRef") : NULL; 5 0 5
1871 STRLEN il; const char *ip;
1872 5 ▶ if (oid && oref)
Branch: 2 branches
BranchTrueFalseTotal
if (oid && oref) 5 0 5
if (oid && oref) 1 4 5
1873 1 croak("Open::API: link '%.*s' declares both "
1874 "operationId and operationRef, which are "
1875 "mutually exclusive", (int)lkl, lk);
1876 /* operationRef is the other spelling, and names a
1877 * location rather than an id: not ours to resolve */
1878 4 ▶ if (!oid) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!oid) continue; 0 4 4
1879 4 ip = SvPV_const(oid, il);
1880 4 ▶ if (!hv_exists(t->by_id, ip, (I32)il))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(t->by_id, ip, (I32)il)) 1 3 4
1881 1 croak("Open::API: link '%.*s' names operationId "
1882 "'%.*s', which no operation declares",
1883 (int)lkl, lk, (int)il, ip);
1884 }
1885 }
1886 }
1887 }
1888 }
1889 }
1890  
1891 /* op by id, or NULL */
1892 8048 static oa_op *oa_op_by_id(pTHX_ oa_api *a, SV *id) {
1893 8048 oa_ops *t = (oa_ops *)a->ops;
1894 STRLEN l; const char *p;
1895 SV **e;
1896 8048 ▶ if (!t) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return NULL; 0 8048 8048
1897 8048 p = SvPV_const(id, l);
1898 8048 e = hv_fetch(t->by_id, p, (I32)l, 0);
1899 8048 ▶ return (e && *e) ? &t->ops[SvIV(*e)] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? &t->ops[SvIV(*e)] : NULL; 8042 6 8048
return (e && *e) ? &t->ops[SvIV(*e)] : NULL; 8042 0 8042
1900 }
1901  
1902 #endif /* OA_COMPILE_H */
« include/oa_tools.h ↑ Summary include/oa_validate.h »