include/oa_normalize.h

filter: Statementstmt 99.0% 601 / 607 Branchbran 76.4% 544 / 712 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.8% 1145 / 1319 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_types.h ↑ Summary include/oa_route.h »
1 #ifndef OA_NORMALIZE_H
2 #define OA_NORMALIZE_H
3  
4 /* Document normalisation: everything that has to be true of a spec before the
5 * schemas reach JSON::Schema::Fast. Runs once, at load, on a copy - the
6 * caller's document is never mutated.
7 *
8 * Two jobs, and both are here rather than in oa_compile_schema for the same
9 * reason: oa_mock.h walks `a->spec` directly rather than the compiled JSF
10 * handles, so anything done only to compiled schemas would make the mock
11 * generator and the validator disagree about the same document.
12 *
13 * 1. OpenAPI 3.0 -> 3.1 (version-gated, `N->v30`).
14 *
15 * A 3.1 Schema Object IS JSON Schema 2020-12, which is what
16 * JSON::Schema::Fast validates, so a 3.1 document reaches the validator
17 * unchanged. A 3.0 Schema Object is a different dialect - an extended
18 * subset of JSON Schema Wright draft-00 - so it is rewritten: nullable,
19 * the boolean exclusive bounds, tuple items, schema-level example, the
20 * binary formats, and $ref siblings.
21 *
22 * 2. `discriminator` -> if/then (both versions).
23 *
24 * Once a schema is compiled, JSF owns the traversal - C cannot step in
25 * partway down to pick a branch. So the discriminator is expanded into
26 * constructs 2020-12 already has, and the validator needs to know
27 * nothing about OpenAPI at all.
28 *
29 * Two properties this file has to keep:
30 *
31 * Position-aware. A spec's `example`, `default` and `enum` values are
32 * arbitrary user JSON that may perfectly legitimately contain a key called
33 * `nullable` or `items`. So the walk descends only through the document
34 * positions where a Schema Object can actually appear, and inside a schema
35 * only through schema-valued keywords. It never rewrites a value.
36 *
37 * Idempotent. The normalised document keeps its original `openapi` string -
38 * a document should not lie about what it was - so
39 * Open::API->new(spec => $api->spec) runs it through here a second time.
40 * Every rule is written so that twice equals once; t/28-openapi30.t and
41 * t/30-discriminator.t pin that down.
42 *
43 * Only the spine (paths, path items, operations, content maps, components) and
44 * the schemas themselves are rebuilt - every other branch is shared with the
45 * original through newSVsv, so the copy stays cheap. */
46  
47 /* Literal nesting depth guard. $refs are not followed, so this bounds only
48 * hand-written nesting - but a spec can arrive from anywhere (Maat takes
49 * uploads), and C recursion is not a place to trust input. */
50 #define OA_CONV_MAX_DEPTH 128
51  
52 /* The marker every generated subschema carries. It makes the expansion
53 * idempotent - a later pass drops what it finds and rebuilds - and it means a
54 * reader of ->spec can see which branches the document declared and which
55 * Open::API derived. `x-` is OpenAPI's own extension spelling, and JSF ignores
56 * keywords it does not know. */
57 #define OA_DISC_MARK "x-oa-discriminator"
58 #define OA_DISC_MARK_LEN 18
59  
60 #define OA_SCHEMA_PFX "#/components/schemas/"
61 #define OA_SCHEMA_PFX_LEN 21
62  
63 typedef struct oa_norm {
64 int v30; /* apply the OpenAPI 3.0 schema rules */
65 HV *doc; /* the source document */
66 HV *kids; /* base schema name -> AV of child names (+1) */
67 } oa_norm;
68  
69 static SV *oa_conv_schema(pTHX_ oa_norm *N, SV *schema, int depth,
70 const char *nm, STRLEN nml);
71  
72 /* ---- booleans -------------------------------------------------------------- */
73  
74 /* A JSON boolean as a decoder leaves it: a blessed Boolean object
75 * (File::Raw::JSON::Boolean, JSON::PP::Boolean, ...) or the \1 / \0 idiom.
76 *
77 * A bare 1 or 0 is deliberately NOT one. It is the difference between the two
78 * forms of `exclusiveMinimum`: 3.0 says boolean, 2020-12 says number, and
79 * after conversion the key holds a number in a document that still declares
80 * 3.0. Reading a bare 1 as `true` would make the second pass eat its own
81 * output. */
82  
83 /* Truth of a JSON boolean, or of a bare scalar. Used for `nullable`, which is
84 * not a 2020-12 keyword at all and so is always consumed whatever its form. */
85 /* oa_sv_truthy and oa_sv_is_json_bool now live in oa_compile.h, which is
86 * included first: every document-sourced boolean has to be read through them,
87 * and most of those reads are in the compiler. */
88  
89 /* ---- small copy helpers ---------------------------------------------------- */
90  
91 /* Copy an HV one level deep: keys duplicated, values shared through newSVsv.
92 * The caller then overwrites whichever keys it actually converts. */
93 42522 static HV *oa_conv_copy_hv(pTHX_ HV *src) {
94 42522 HV *dst = newHV();
95 HE *he;
96 42522 hv_iterinit(src);
97 139439 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 96917 42522 139439
98 96917 I32 kl; const char *k = hv_iterkey(he, &kl);
99 96917 (void)hv_store(dst, k, kl, newSVsv(hv_iterval(src, he)), 0);
100 }
101 42522 return dst;
102 }
103  
104 typedef SV *(*oa_conv_fn)(pTHX_ oa_norm *N, SV *);
105  
106 /* Apply `fn` to every value of a keyed map (paths, components.responses, ...). */
107 /* `dst` is mortal while it is being filled: the conversion below it can croak
108 * on an unresolvable $ref, and until the container is attached to its parent
109 * nothing else owns it. The caller gets a counted reference, so the mortal
110 * copy expiring later is harmless. */
111 19288 static SV *oa_conv_map(pTHX_ oa_norm *N, SV *map, oa_conv_fn fn) {
112 19288 HV *src = oa_hv_of(map), *dst;
113 HE *he;
114 19288 ▶ if (!src) return newSVsv(map);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(map); 0 19288 19288
115 19288 dst = newHV();
116 19288 sv_2mortal((SV *)dst);
117 19288 hv_iterinit(src);
118 42503 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 23264 19239 42503
119 23264 I32 kl; const char *k = hv_iterkey(he, &kl);
120 23264 (void)hv_store(dst, k, kl, fn(aTHX_ N, hv_iterval(src, he)), 0);
121 }
122 19239 return newRV_inc((SV *)dst);
123 }
124  
125 /* A list of schemas: allOf / anyOf / oneOf / prefixItems / tuple items. */
126 5328 static SV *oa_conv_schema_list(pTHX_ oa_norm *N, SV *list, int depth) {
127 5328 AV *src = oa_av_of(list), *dst;
128 SSize_t i, n;
129 5328 ▶ if (!src) return newSVsv(list);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(list); 0 5328 5328
130 5328 dst = newAV();
131 5328 n = av_len(src) + 1;
132 15846 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10518 5328 15846
133 10518 SV **e = av_fetch(src, i, 0);
134 10518 ▶ av_push(dst, (e && *e) ? oa_conv_schema(aTHX_ N, *e, depth + 1, NULL, 0)
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, (e && *e) ? oa_conv_schema(aTHX_ N, *e, depth + 1, NULL, 0) 10518 0 10518
av_push(dst, (e && *e) ? oa_conv_schema(aTHX_ N, *e, depth + 1, NULL, 0) 10518 0 10518
135 : newSV(0));
136 }
137 5328 return newRV_noinc((SV *)dst);
138 }
139  
140 /* A map of schemas: properties / patternProperties / $defs / dependentSchemas. */
141 9347 static SV *oa_conv_schema_map(pTHX_ oa_norm *N, SV *map, int depth) {
142 9347 HV *src = oa_hv_of(map), *dst;
143 HE *he;
144 9347 ▶ if (!src) return newSVsv(map);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(map); 0 9347 9347
145 9347 dst = newHV();
146 9347 hv_iterinit(src);
147 24886 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 15539 9347 24886
148 15539 I32 kl; const char *k = hv_iterkey(he, &kl);
149 15539 (void)hv_store(dst, k, kl,
150 oa_conv_schema(aTHX_ N, hv_iterval(src, he), depth + 1,
151 NULL, 0), 0);
152 }
153 9347 return newRV_noinc((SV *)dst);
154 }
155  
156 /* ---- the schema keyword tables --------------------------------------------- *
157 *
158 * oa_kw_in and the three classifiers now live in oa_compile.h, which is
159 * included first: the readOnly/writeOnly projection runs at compile time and
160 * has to walk exactly the same positions this converter does. One copy, so
161 * the two cannot drift into disagreeing about where a Schema Object can
162 * appear - which is the property that keeps either of them from rewriting a
163 * key called `properties` that is really user JSON inside an `example`. */
164  
165 /* Under 3.0, keywords beside a `$ref` are ignored. These are pure annotations
166 * that cannot change a verdict - JSON::Schema::Fast treats every one of them as
167 * inert - so they ride along and keep the docs UI useful; every other sibling
168 * is an assertion and is dropped. */
169 3640 static int oa_ref_sibling_ok(const char *k, STRLEN l) {
170 static const char *const s[] = {
171 "$ref", "title", "description", "summary", "deprecated", "default",
172 "example", "examples", "readOnly", "writeOnly", "externalDocs", "xml",
173 "discriminator", NULL
174 };
175 3640 return oa_kw_in(k, l, s);
176 }
177  
178 /* ---- discriminator --------------------------------------------------------- */
179  
180 /* "#/components/schemas/Dog" -> "Dog". NULL when the ref is not a local
181 * component schema (a remote or otherwise-shaped ref is left alone). */
182 12118 static const char *oa_schema_ref_name(pTHX_ SV *ref, STRLEN *out) {
183 STRLEN l; const char *p;
184 12118 ▶ if (!ref || SvROK(ref) || !SvOK(ref)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 8488 3630 12118
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 8488 0 8488
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 0 8488 8488
185 8488 p = SvPV_const(ref, l);
186 8488 ▶ if (l <= OA_SCHEMA_PFX_LEN || !memEQ(p, OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN))
Branch: 2 branches
BranchTrueFalseTotal
if (l <= OA_SCHEMA_PFX_LEN || !memEQ(p, OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN)) 8488 0 8488
if (l <= OA_SCHEMA_PFX_LEN || !memEQ(p, OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN)) 0 8488 8488
187 0 return NULL;
188 8488 ▶ if (memchr(p + OA_SCHEMA_PFX_LEN, '/', l - OA_SCHEMA_PFX_LEN)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(p + OA_SCHEMA_PFX_LEN, '/', l - OA_SCHEMA_PFX_LEN)) return NULL; 0 8488 8488
189 8488 *out = l - OA_SCHEMA_PFX_LEN;
190 8488 return p + OA_SCHEMA_PFX_LEN;
191 }
192  
193 /* A mapping value is either a JSON pointer or a bare component-schema name
194 * ("Dog" and "#/components/schemas/Dog" both mean the same thing). */
195 28 static SV *oa_disc_ref(pTHX_ SV *v) {
196 STRLEN l; const char *p;
197 28 ▶ if (!v || SvROK(v)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || SvROK(v)) return NULL; 28 0 28
if (!v || SvROK(v)) return NULL; 0 28 28
198 28 p = SvPV_const(v, l);
199 28 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 0 28 28
200 28 ▶ if (p[0] == '#' || memchr(p, '/', l)) return newSVsv(v);
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == '#' || memchr(p, '/', l)) return newSVsv(v); 10 18 28
if (p[0] == '#' || memchr(p, '/', l)) return newSVsv(v); 0 10 10
201 10 { SV *r = newSVpvn(OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN);
202 10 sv_catpvn(r, p, l); return r; }
203 }
204  
205 /* components.schemas.<name> as it was written, borrowed. */
206 2435 static SV *oa_named_schema(pTHX_ oa_norm *N, const char *nm, STRLEN nl) {
207 2435 ▶ HV *comp = N->doc ? oa_hv_of(oa_get(aTHX_ N->doc, "components")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *comp = N->doc ? oa_hv_of(oa_get(aTHX_ N->doc, "components")) : NULL; 2435 0 2435
208 2435 ▶ 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; 2435 0 2435
209 2435 ▶ 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; 2435 0 2435
210 2435 ▶ return (e && *e && oa_hv_of(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 2435 0 2435
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 2435 0 2435
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 2435 0 2435
211 }
212  
213 /* base schema name -> the schemas that allOf-reference it, in document order.
214 * Built once, lazily: it is only needed by the inheritance form. */
215 1214 static HV *oa_norm_kids(pTHX_ oa_norm *N) {
216 HV *comp, *schemas;
217 HE *he;
218 1214 ▶ if (N->kids) return N->kids;
Branch: 1 branch
BranchTrueFalseTotal
if (N->kids) return N->kids; 607 607 1214
219 607 N->kids = newHV();
220 607 ▶ comp = N->doc ? oa_hv_of(oa_get(aTHX_ N->doc, "components")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
comp = N->doc ? oa_hv_of(oa_get(aTHX_ N->doc, "components")) : NULL; 607 0 607
221 607 ▶ schemas = comp ? oa_hv_of(oa_get(aTHX_ comp, "schemas")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
schemas = comp ? oa_hv_of(oa_get(aTHX_ comp, "schemas")) : NULL; 607 0 607
222 607 ▶ if (!schemas) return N->kids;
Branch: 1 branch
BranchTrueFalseTotal
if (!schemas) return N->kids; 0 607 607
223 607 hv_iterinit(schemas);
224 2424 ▶ while ((he = hv_iternext(schemas))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(schemas))) { 1817 607 2424
225 1817 I32 kl; const char *k = hv_iterkey(he, &kl);
226 1817 HV *sh = oa_hv_of(hv_iterval(schemas, he));
227 1817 ▶ AV *all = sh ? oa_av_of(oa_get(aTHX_ sh, "allOf")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *all = sh ? oa_av_of(oa_get(aTHX_ sh, "allOf")) : NULL; 1817 0 1817
228 1817 ▶ SSize_t i, n = all ? av_len(all) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = all ? av_len(all) + 1 : 0; 1208 609 1817
229 4233 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2416 1817 4233
230 2416 SV **e = av_fetch(all, i, 0);
231 2416 ▶ HV *mh = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 2416 0 2416
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 2416 0 2416
232 STRLEN bl;
233 2416 const char *base = mh ? oa_schema_ref_name(aTHX_ oa_get(aTHX_ mh, "$ref"), &bl)
234 2416 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 2416 0 2416
235 SV **slot;
236 AV *lst;
237 2416 ▶ if (!base) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!base) continue; 1208 1208 2416
238 1208 slot = hv_fetch(N->kids, base, (I32)bl, 0);
239 1208 ▶ if (slot && *slot && oa_av_of(*slot)) lst = oa_av_of(*slot);
Branch: 3 branches
BranchTrueFalseTotal
if (slot && *slot && oa_av_of(*slot)) lst = oa_av_of(*slot); 604 604 1208
if (slot && *slot && oa_av_of(*slot)) lst = oa_av_of(*slot); 604 0 604
if (slot && *slot && oa_av_of(*slot)) lst = oa_av_of(*slot); 604 0 604
240 else {
241 604 lst = newAV();
242 604 (void)hv_store(N->kids, base, (I32)bl, newRV_noinc((SV *)lst), 0);
243 }
244 1208 av_push(lst, newSVpvn(k, (STRLEN)kl));
245 }
246 }
247 607 return N->kids;
248 }
249  
250 /* Insertion-sort an AV of strings. Hash order is not order, and a document
251 * that normalises differently run to run is a document you cannot diff. */
252 1226 static void oa_sort_av(pTHX_ AV *av) {
253 1226 SSize_t i, j, n = av_len(av) + 1;
254 2452 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 1226 1226 2452
255 1226 SV *key = SvREFCNT_inc(*av_fetch(av, i, 0));
256 1236 ▶ for (j = i - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = i - 1; j >= 0; j--) { 1226 10 1236
257 1226 SV **c = av_fetch(av, j, 0);
258 1226 ▶ if (!c || !*c || sv_cmp(*c, key) <= 0) break;
Branch: 3 branches
BranchTrueFalseTotal
if (!c || !*c || sv_cmp(*c, key) <= 0) break; 1226 0 1226
if (!c || !*c || sv_cmp(*c, key) <= 0) break; 1226 0 1226
if (!c || !*c || sv_cmp(*c, key) <= 0) break; 10 1216 1226
259 10 (void)av_store(av, j + 1, SvREFCNT_inc(*c));
260 }
261 1226 (void)av_store(av, j + 1, key);
262 }
263 1226 }
264  
265 /* Collect the discriminator's (key -> $ref) entries into two parallel arrays.
266 *
267 * Three sources, in precedence order: an explicit `mapping`; the $refs of a
268 * sibling oneOf/anyOf (implicit key = the schema name); or, for a named base
269 * schema, the schemas that allOf-reference it. Returns 0 when there is nothing
270 * to expand, in which case the discriminator stays an inert annotation.
271 *
272 * `*from_union` says the entries came from oneOf/anyOf, which are then replaced
273 * by the generated chain: leaving them would reintroduce the ambiguity the
274 * discriminator exists to resolve (two branches a payload both satisfies make
275 * `oneOf` fail, however clear the document was about which one was meant). */
276 1232 static int oa_disc_entries(pTHX_ oa_norm *N, HV *src, HV *dh,
277 const char *nm, STRLEN nml,
278 AV **keys_out, AV **refs_out, int *from_union) {
279 1232 AV *keys = (AV *)sv_2mortal((SV *)newAV());
280 1232 AV *refs = (AV *)sv_2mortal((SV *)newAV());
281 1232 HV *map = oa_hv_of(oa_get(aTHX_ dh, "mapping"));
282 SSize_t i, n;
283  
284 1232 *from_union = 0;
285  
286 1246 ▶ if (map && HvUSEDKEYS(map)) {
Branch: 3 branches
BranchTrueFalseTotal
if (map && HvUSEDKEYS(map)) { 14 1218 1232
if (map && HvUSEDKEYS(map)) { 0 14 14
if (map && HvUSEDKEYS(map)) { 14 0 14
287 14 AV *mk = (AV *)sv_2mortal((SV *)newAV());
288 HE *he;
289 14 hv_iterinit(map);
290 42 ▶ while ((he = hv_iternext(map))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(map))) { 28 14 42
291 28 I32 kl; const char *k = hv_iterkey(he, &kl);
292 28 av_push(mk, newSVpvn(k, (STRLEN)kl));
293 }
294 14 oa_sort_av(aTHX_ mk);
295 14 n = av_len(mk) + 1;
296 42 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 28 14 42
297 28 SV *k = *av_fetch(mk, i, 0);
298 28 STRLEN kl; const char *kp = SvPV_const(k, kl);
299 28 SV **v = hv_fetch(map, kp, (I32)kl, 0);
300 28 ▶ SV *ref = (v && *v) ? oa_disc_ref(aTHX_ *v) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *ref = (v && *v) ? oa_disc_ref(aTHX_ *v) : NULL; 28 0 28
SV *ref = (v && *v) ? oa_disc_ref(aTHX_ *v) : NULL; 28 0 28
301 28 ▶ if (!ref) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ref) continue; 0 28 28
302 28 av_push(keys, newSVsv(k));
303 28 av_push(refs, ref);
304 }
305 }
306 else {
307 /* a sibling union, but only when every branch is a plain component
308 * $ref - a mixed union has nothing to key an implicit mapping on */
309 1218 AV *un = oa_av_of(oa_get(aTHX_ src, "oneOf"));
310 1218 ▶ if (!un) un = oa_av_of(oa_get(aTHX_ src, "anyOf"));
Branch: 1 branch
BranchTrueFalseTotal
if (!un) un = oa_av_of(oa_get(aTHX_ src, "anyOf")); 1214 4 1218
311 1218 ▶ if (un && (n = av_len(un) + 1) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (un && (n = av_len(un) + 1) > 0) { 4 1214 1218
if (un && (n = av_len(un) + 1) > 0) { 4 0 4
312 4 int all_refs = 1;
313 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7 2 9
314 7 SV **e = av_fetch(un, i, 0);
315 7 ▶ HV *mh = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 7 0 7
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 7 0 7
316 STRLEN rl;
317 7 const char *rn = mh ? oa_schema_ref_name(aTHX_ oa_get(aTHX_ mh, "$ref"), &rl)
318 7 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 7 0 7
319 7 ▶ if (!rn) { all_refs = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rn) { all_refs = 0; break; } 2 5 7
320 5 av_push(keys, newSVpvn(rn, rl));
321 5 av_push(refs, newSVsv(oa_get(aTHX_ mh, "$ref")));
322 }
323 4 ▶ if (all_refs) *from_union = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (all_refs) *from_union = 1; 2 2 4
324 2 else { av_clear(keys); av_clear(refs); }
325 }
326 /* a named base schema: the children that inherit from it */
327 1218 ▶ if (!*from_union && nm && nml) {
Branch: 3 branches
BranchTrueFalseTotal
if (!*from_union && nm && nml) { 1216 2 1218
if (!*from_union && nm && nml) { 1214 2 1216
if (!*from_union && nm && nml) { 1214 0 1214
328 1214 HV *kids = oa_norm_kids(aTHX_ N);
329 1214 SV **slot = hv_fetch(kids, nm, (I32)nml, 0);
330 1214 ▶ AV *lst = (slot && *slot) ? oa_av_of(*slot) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
AV *lst = (slot && *slot) ? oa_av_of(*slot) : NULL; 1208 6 1214
AV *lst = (slot && *slot) ? oa_av_of(*slot) : NULL; 1208 0 1208
331 1214 ▶ if (lst) {
Branch: 1 branch
BranchTrueFalseTotal
if (lst) { 1208 6 1214
332 1208 AV *sorted = (AV *)sv_2mortal((SV *)newAV());
333 1208 n = av_len(lst) + 1;
334 3624 ▶ for (i = 0; i < n; i++) av_push(sorted, newSVsv(*av_fetch(lst, i, 0)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(sorted, newSVsv(*av_fetch(lst, i, 0))); 2416 1208 3624
335 1208 oa_sort_av(aTHX_ sorted);
336 3624 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2416 1208 3624
337 2416 SV *c = *av_fetch(sorted, i, 0);
338 2416 STRLEN cl; const char *cp = SvPV_const(c, cl);
339 2416 SV *ref = newSVpvn(OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN);
340 2416 sv_catpvn(ref, cp, cl);
341 2416 av_push(keys, newSVsv(c));
342 2416 av_push(refs, ref);
343 }
344 }
345 }
346 }
347  
348 1232 ▶ if (av_len(keys) < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(keys) < 0) return 0; 8 1224 1232
349 1224 *keys_out = keys;
350 1224 *refs_out = refs;
351 1224 return 1;
352 }
353  
354 /* The `then` schema for one mapping entry.
355 *
356 * Normally {$ref: target}. But in the inheritance form the child allOf-
357 * references the very base doing the dispatching, and
358 * base -> then -> child -> base is an infinite validation loop. So when the
359 * target refers back to `nm`, that one reference is dropped and the child's
360 * own constraints are inlined instead - which is exactly what the child adds
361 * on top of the base, and terminates. */
362 2447 static SV *oa_disc_then(pTHX_ oa_norm *N, SV *ref, const char *nm, STRLEN nml,
363 int depth) {
364 2447 STRLEN tl = 0;
365 2447 const char *tn = NULL;
366 2447 SV *target = NULL;
367 2447 AV *all = NULL;
368 2447 SSize_t i, n = 0;
369 2447 int backref = 0;
370  
371 2447 ▶ if (nm && nml && (tn = oa_schema_ref_name(aTHX_ ref, &tl)) != NULL
Branch: 3 branches
BranchTrueFalseTotal
if (nm && nml && (tn = oa_schema_ref_name(aTHX_ ref, &tl)) != NULL 2435 12 2447
if (nm && nml && (tn = oa_schema_ref_name(aTHX_ ref, &tl)) != NULL 2435 0 2435
if (nm && nml && (tn = oa_schema_ref_name(aTHX_ ref, &tl)) != NULL 2435 0 2435
372 2435 ▶ && (target = oa_named_schema(aTHX_ N, tn, tl)) != NULL
Branch: 1 branch
BranchTrueFalseTotal
&& (target = oa_named_schema(aTHX_ N, tn, tl)) != NULL 2435 0 2435
373 2435 ▶ && (all = oa_av_of(oa_get(aTHX_ (HV *)SvRV(target), "allOf"))) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
&& (all = oa_av_of(oa_get(aTHX_ (HV *)SvRV(target), "allOf"))) != NULL) { 2420 15 2435
374 2420 n = av_len(all) + 1;
375 2420 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2420 0 2420
376 2420 SV **e = av_fetch(all, i, 0);
377 2420 ▶ HV *mh = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 2420 0 2420
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 2420 0 2420
378 STRLEN bl;
379 2420 const char *bn = mh
380 2420 ▶ ? oa_schema_ref_name(aTHX_ oa_get(aTHX_ mh, "$ref"), &bl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_schema_ref_name(aTHX_ oa_get(aTHX_ mh, "$ref"), &bl) : NULL; 2420 0 2420
381 2420 ▶ if (bn && bl == nml && memEQ(bn, nm, nml)) { backref = 1; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (bn && bl == nml && memEQ(bn, nm, nml)) { backref = 1; break; } 2420 0 2420
if (bn && bl == nml && memEQ(bn, nm, nml)) { backref = 1; break; } 2420 0 2420
if (bn && bl == nml && memEQ(bn, nm, nml)) { backref = 1; break; } 2420 0 2420
382 }
383 }
384  
385 2447 ▶ if (!backref) { /* no loop to break: name the target */
Branch: 1 branch
BranchTrueFalseTotal
if (!backref) { /* no loop to break: name the target */ 27 2420 2447
386 27 HV *r = newHV();
387 27 (void)hv_stores(r, "$ref", newSVsv(ref));
388 27 return newRV_noinc((SV *)r);
389 }
390  
391 /* Convert the child exactly as it is converted in its own right - passing
392 * its name so a hierarchy more than one level deep expands the same way -
393 * then drop the one member that points back at us. */
394 {
395 2420 SV *conv = oa_conv_schema(aTHX_ N, target, depth + 1, tn, tl);
396 2420 HV *out = (HV *)SvRV(conv);
397 2420 AV *ca = oa_av_of(oa_get(aTHX_ out, "allOf"));
398 AV *keep;
399 2420 ▶ if (!ca) return conv;
Branch: 1 branch
BranchTrueFalseTotal
if (!ca) return conv; 0 2420 2420
400 2420 keep = newAV();
401 2420 n = av_len(ca) + 1;
402 7260 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4840 2420 7260
403 4840 SV **e = av_fetch(ca, i, 0);
404 4840 ▶ HV *mh = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 4840 0 4840
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 4840 0 4840
405 STRLEN bl;
406 4840 const char *bn = mh
407 4840 ▶ ? oa_schema_ref_name(aTHX_ oa_get(aTHX_ mh, "$ref"), &bl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_schema_ref_name(aTHX_ oa_get(aTHX_ mh, "$ref"), &bl) : NULL; 4840 0 4840
408 4840 ▶ if (bn && bl == nml && memEQ(bn, nm, nml)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (bn && bl == nml && memEQ(bn, nm, nml)) continue; 2420 2420 4840
if (bn && bl == nml && memEQ(bn, nm, nml)) continue; 2420 0 2420
if (bn && bl == nml && memEQ(bn, nm, nml)) continue; 2420 0 2420
409 2420 ▶ av_push(keep, (e && *e) ? SvREFCNT_inc(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(keep, (e && *e) ? SvREFCNT_inc(*e) : newSV(0)); 2420 0 2420
av_push(keep, (e && *e) ? SvREFCNT_inc(*e) : newSV(0)); 2420 0 2420
410 }
411 2420 ▶ if (av_len(keep) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(keep) >= 0) 2420 0 2420
412 2420 (void)hv_stores(out, "allOf", newRV_noinc((SV *)keep));
413 else {
414 0 SvREFCNT_dec((SV *)keep);
415 0 (void)hv_delete(out, "allOf", 5, G_DISCARD);
416 }
417 2420 return conv;
418 }
419 }
420  
421 /* { <mark>: pn, required: [pn], properties: { pn: <inner> } } - the shape both
422 * generated forms share: the guard uses an enum of every known value, each
423 * branch's `if` uses a const. */
424 3671 static SV *oa_disc_probe(pTHX_ SV *pn, SV *inner) {
425 3671 HV *out = newHV(), *props = newHV();
426 3671 AV *req = newAV();
427 3671 av_push(req, newSVsv(pn));
428 3671 (void)hv_store(props, SvPVX_const(pn), (I32)SvCUR(pn), inner, 0);
429 3671 (void)hv_stores(out, "required", newRV_noinc((SV *)req));
430 3671 (void)hv_stores(out, "properties", newRV_noinc((SV *)props));
431 3671 return newRV_noinc((SV *)out);
432 }
433  
434 /* ---- a Schema Object ------------------------------------------------------- */
435  
436 53398 static SV *oa_conv_schema(pTHX_ oa_norm *N, SV *schema, int depth,
437 const char *nm, STRLEN nml) {
438 53398 HV *src = oa_hv_of(schema), *dst;
439 HE *he;
440 SV *nullable, *ref, *type, *mn, *mx, *exmn, *exmx, *items, *fmt;
441 53398 SV *disc, *pn = NULL;
442 53398 HV *dh = NULL;
443 53398 AV *dkeys = NULL, *drefs = NULL;
444 int has_ref, is_nullable, tuple;
445 int exmn_bool, exmx_bool, exmn_on, exmx_on;
446 53398 int expand = 0, from_union = 0;
447  
448 /* booleans, arrays and junk are not objects to rewrite: share them */
449 53398 ▶ if (!src || depth >= OA_CONV_MAX_DEPTH) return newSVsv(schema);
Branch: 2 branches
BranchTrueFalseTotal
if (!src || depth >= OA_CONV_MAX_DEPTH) return newSVsv(schema); 53133 265 53398
if (!src || depth >= OA_CONV_MAX_DEPTH) return newSVsv(schema); 0 53133 53133
450  
451 53133 ▶ nullable = N->v30 ? oa_get(aTHX_ src, "nullable") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
nullable = N->v30 ? oa_get(aTHX_ src, "nullable") : NULL; 16411 36722 53133
452 53133 ref = oa_get(aTHX_ src, "$ref");
453 53133 type = oa_get(aTHX_ src, "type");
454 53133 mn = oa_get(aTHX_ src, "minimum");
455 53133 mx = oa_get(aTHX_ src, "maximum");
456 53133 exmn = oa_get(aTHX_ src, "exclusiveMinimum");
457 53133 exmx = oa_get(aTHX_ src, "exclusiveMaximum");
458 53133 items = oa_get(aTHX_ src, "items");
459 53133 fmt = oa_get(aTHX_ src, "format");
460 53133 disc = oa_get(aTHX_ src, "discriminator");
461  
462 53133 ▶ has_ref = ref && !SvROK(ref);
Branch: 2 branches
BranchTrueFalseTotal
has_ref = ref && !SvROK(ref); 11163 41970 53133
has_ref = ref && !SvROK(ref); 11163 0 11163
463 53133 is_nullable = oa_sv_truthy(aTHX_ nullable);
464  
465 /* the 3.0 boolean form of the exclusive bounds, and only that form */
466 53133 ▶ exmn_bool = N->v30 && exmn && oa_sv_is_json_bool(aTHX_ exmn);
Branch: 3 branches
BranchTrueFalseTotal
exmn_bool = N->v30 && exmn && oa_sv_is_json_bool(aTHX_ exmn); 16411 36722 53133
exmn_bool = N->v30 && exmn && oa_sv_is_json_bool(aTHX_ exmn); 1223 15188 16411
exmn_bool = N->v30 && exmn && oa_sv_is_json_bool(aTHX_ exmn); 1216 7 1223
467 53133 ▶ exmx_bool = N->v30 && exmx && oa_sv_is_json_bool(aTHX_ exmx);
Branch: 3 branches
BranchTrueFalseTotal
exmx_bool = N->v30 && exmx && oa_sv_is_json_bool(aTHX_ exmx); 16411 36722 53133
exmx_bool = N->v30 && exmx && oa_sv_is_json_bool(aTHX_ exmx); 6 16405 16411
exmx_bool = N->v30 && exmx && oa_sv_is_json_bool(aTHX_ exmx); 2 4 6
468 53133 ▶ exmn_on = exmn_bool && oa_sv_truthy(aTHX_ exmn) && mn != NULL;
Branch: 3 branches
BranchTrueFalseTotal
exmn_on = exmn_bool && oa_sv_truthy(aTHX_ exmn) && mn != NULL; 1216 51917 53133
exmn_on = exmn_bool && oa_sv_truthy(aTHX_ exmn) && mn != NULL; 1214 2 1216
exmn_on = exmn_bool && oa_sv_truthy(aTHX_ exmn) && mn != NULL; 1213 1 1214
469 53133 ▶ exmx_on = exmx_bool && oa_sv_truthy(aTHX_ exmx) && mx != NULL;
Branch: 3 branches
BranchTrueFalseTotal
exmx_on = exmx_bool && oa_sv_truthy(aTHX_ exmx) && mx != NULL; 2 53131 53133
exmx_on = exmx_bool && oa_sv_truthy(aTHX_ exmx) && mx != NULL; 2 0 2
exmx_on = exmx_bool && oa_sv_truthy(aTHX_ exmx) && mx != NULL; 2 0 2
470  
471 /* draft-04 tuple `items` (not legal 3.0, but specs carry the habit) */
472 53133 ▶ tuple = N->v30 && items && oa_av_of(items) && !oa_get(aTHX_ src, "prefixItems");
Branch: 4 branches
BranchTrueFalseTotal
tuple = N->v30 && items && oa_av_of(items) && !oa_get(aTHX_ src, "prefixItems"); 16411 36722 53133
tuple = N->v30 && items && oa_av_of(items) && !oa_get(aTHX_ src, "prefixItems"); 1837 14574 16411
tuple = N->v30 && items && oa_av_of(items) && !oa_get(aTHX_ src, "prefixItems"); 608 1229 1837
tuple = N->v30 && items && oa_av_of(items) && !oa_get(aTHX_ src, "prefixItems"); 608 0 608
473  
474 /* discriminator: expandable in either version */
475 53133 ▶ dh = disc ? oa_hv_of(disc) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
dh = disc ? oa_hv_of(disc) : NULL; 1234 51899 53133
476 53133 ▶ pn = dh ? oa_get(aTHX_ dh, "propertyName") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
pn = dh ? oa_get(aTHX_ dh, "propertyName") : NULL; 1234 51899 53133
477 /* The XML Object is an annotation - nothing here serializes XML - but its
478 * `namespace` is constrained, and a relative namespace is a document bug
479 * whoever DOES serialize from this spec will inherit. */
480 {
481 53133 SV *xml = oa_get(aTHX_ src, "xml");
482 53133 ▶ HV *xh = xml ? oa_hv_of(xml) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *xh = xml ? oa_hv_of(xml) : NULL; 10 53123 53133
483 53133 ▶ SV *ns = xh ? oa_get(aTHX_ xh, "namespace") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *ns = xh ? oa_get(aTHX_ xh, "namespace") : NULL; 10 53123 53133
484 53133 ▶ if (ns && !oa_looks_like_url(aTHX_ ns)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ns && !oa_looks_like_url(aTHX_ ns)) { 5 53128 53133
if (ns && !oa_looks_like_url(aTHX_ ns)) { 2 3 5
485 2 STRLEN nl2; const char *np = SvPV_const(ns, nl2);
486 2 croak("Open::API: xml namespace '%.*s' is not an absolute URI",
487 (int)nl2, np);
488 }
489 }
490  
491 /* `propertyName` is REQUIRED. Without it there is nothing to switch on and
492 * the expansion below silently does nothing, so a document that means to
493 * discriminate validates against the bare composite instead - accepting
494 * every branch, which is the opposite of what it asked for. */
495 53131 ▶ if (dh && (!pn || SvROK(pn)))
Branch: 3 branches
BranchTrueFalseTotal
if (dh && (!pn || SvROK(pn))) 1234 51897 53131
if (dh && (!pn || SvROK(pn))) 1232 2 1234
if (dh && (!pn || SvROK(pn))) 0 1232 1232
496 2 croak("Open::API: a discriminator has no 'propertyName' "
497 "(it is required)");
498 53129 ▶ if (pn && !SvROK(pn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pn && !SvROK(pn)) { 1232 51897 53129
if (pn && !SvROK(pn)) { 1232 0 1232
499 STRLEN pl;
500 (void)SvPV_const(pn, pl); /* force POK: SvPVX/SvCUR are used below */
501 1232 ▶ if (pl) expand = oa_disc_entries(aTHX_ N, src, dh, nm, nml,
Branch: 1 branch
BranchTrueFalseTotal
if (pl) expand = oa_disc_entries(aTHX_ N, src, dh, nm, nml, 1232 0 1232
502 &dkeys, &drefs, &from_union);
503 /* "legal only when using one of the composite keywords oneOf, anyOf,
504 * allOf" - but the specification's OWN inheritance example puts a
505 * discriminator on a base that carries none of them, and finds the
506 * children by their allOf $ref back to it.
507 *
508 * So the refusal is narrowed to the case both readings condemn: an
509 * INLINE schema (nm == NULL) with no composite keyword and nothing
510 * inheriting from it. A NAMED component in that state is left alone
511 * deliberately - it is a base another document may extend, and
512 * t/30-discriminator.t pins it as "a childless base is left as an
513 * annotation". An inline schema can never be inherited from by
514 * anything, so there its discriminator is provably inert. */
515 1232 ▶ if (!expand && !nm && !oa_get(aTHX_ src, "oneOf")
Branch: 3 branches
BranchTrueFalseTotal
if (!expand && !nm && !oa_get(aTHX_ src, "oneOf") 8 1224 1232
if (!expand && !nm && !oa_get(aTHX_ src, "oneOf") 2 6 8
if (!expand && !nm && !oa_get(aTHX_ src, "oneOf") 2 0 2
516 2 ▶ && !oa_get(aTHX_ src, "anyOf")
Branch: 1 branch
BranchTrueFalseTotal
&& !oa_get(aTHX_ src, "anyOf") 2 0 2
517 2 ▶ && !oa_get(aTHX_ src, "allOf"))
Branch: 1 branch
BranchTrueFalseTotal
&& !oa_get(aTHX_ src, "allOf")) 2 0 2
518 2 croak("Open::API: discriminator '%.*s' selects nothing: it is "
519 "inline, beside no oneOf, anyOf or allOf, so nothing can "
520 "inherit from it", (int)pl, SvPVX_const(pn));
521 }
522  
523 53127 dst = newHV();
524 53127 hv_iterinit(src);
525 138802 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 85675 53127 138802
526 85675 I32 i32kl; const char *k = hv_iterkey(he, &i32kl);
527 85675 SV *v = hv_iterval(src, he);
528 85675 STRLEN kl = (STRLEN)i32kl;
529  
530 /* `nullable` never survives: it is not a 2020-12 keyword */
531 105804 ▶ if (N->v30 && kl == 8 && memEQ(k, "nullable", 8)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 8 && memEQ(k, "nullable", 8)) continue; 31398 54277 85675
if (N->v30 && kl == 8 && memEQ(k, "nullable", 8)) continue; 3744 27654 31398
if (N->v30 && kl == 8 && memEQ(k, "nullable", 8)) continue; 1829 1915 3744
532  
533 /* beside a $ref, assertions are ignored under 3.0 */
534 83846 ▶ if (N->v30 && has_ref && !oa_ref_sibling_ok(k, kl)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && has_ref && !oa_ref_sibling_ok(k, kl)) continue; 29569 54277 83846
if (N->v30 && has_ref && !oa_ref_sibling_ok(k, kl)) continue; 3640 25929 29569
if (N->v30 && has_ref && !oa_ref_sibling_ok(k, kl)) continue; 1 3639 3640
535  
536 /* $ref itself: the nullable form rebuilds it as an anyOf below */
537 83845 ▶ if (kl == 4 && memEQ(k, "$ref", 4)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "$ref", 4)) { 43286 40559 83845
if (kl == 4 && memEQ(k, "$ref", 4)) { 11163 32123 43286
538 11163 ▶ if (!(has_ref && is_nullable))
Branch: 2 branches
BranchTrueFalseTotal
if (!(has_ref && is_nullable)) 11163 0 11163
if (!(has_ref && is_nullable)) 10558 605 11163
539 10558 (void)hv_store(dst, k, i32kl, newSVsv(v), 0);
540 11163 continue;
541 }
542  
543 /* the union the discriminator replaces */
544 72682 ▶ if (expand && from_union
Branch: 2 branches
BranchTrueFalseTotal
if (expand && from_union 4872 67810 72682
if (expand && from_union 4 4868 4872
545 4 ▶ && ((kl == 5 && memEQ(k, "oneOf", 5))
Branch: 2 branches
BranchTrueFalseTotal
&& ((kl == 5 && memEQ(k, "oneOf", 5)) 2 2 4
&& ((kl == 5 && memEQ(k, "oneOf", 5)) 0 2 2
546 4 ▶ || (kl == 5 && memEQ(k, "anyOf", 5)))) continue;
Branch: 2 branches
BranchTrueFalseTotal
|| (kl == 5 && memEQ(k, "anyOf", 5)))) continue; 0 2 2
|| (kl == 5 && memEQ(k, "anyOf", 5)))) continue; 0 0 0
547  
548 /* the discriminator is rewritten below, with its mapping made explicit */
549 72680 ▶ if (expand && kl == 13 && memEQ(k, "discriminator", 13)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (expand && kl == 13 && memEQ(k, "discriminator", 13)) continue; 4870 67810 72680
if (expand && kl == 13 && memEQ(k, "discriminator", 13)) continue; 1224 3646 4870
if (expand && kl == 13 && memEQ(k, "discriminator", 13)) continue; 1224 0 1224
550  
551 /* previously generated members: dropped, then rebuilt */
552 71456 ▶ if (expand && kl == 5 && memEQ(k, "allOf", 5)) {
Branch: 3 branches
BranchTrueFalseTotal
if (expand && kl == 5 && memEQ(k, "allOf", 5)) { 3646 67810 71456
if (expand && kl == 5 && memEQ(k, "allOf", 5)) { 16 3630 3646
if (expand && kl == 5 && memEQ(k, "allOf", 5)) { 4 12 16
553 4 AV *keep = newAV();
554 4 AV *av = oa_av_of(v);
555 4 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 4 0 4
556 16 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12 4 16
557 12 SV **e = av_fetch(av, i, 0);
558 12 ▶ HV *mh = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 12 0 12
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 12 0 12
559 12 ▶ if (mh && hv_exists(mh, OA_DISC_MARK, OA_DISC_MARK_LEN)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (mh && hv_exists(mh, OA_DISC_MARK, OA_DISC_MARK_LEN)) continue; 12 0 12
if (mh && hv_exists(mh, OA_DISC_MARK, OA_DISC_MARK_LEN)) continue; 12 0 12
560 0 ▶ av_push(keep, (e && *e)
Branch: 2 branches
BranchTrueFalseTotal
av_push(keep, (e && *e) 0 0 0
av_push(keep, (e && *e) 0 0 0
561 ? oa_conv_schema(aTHX_ N, *e, depth + 1, NULL, 0) : newSV(0));
562 }
563 4 (void)hv_stores(dst, "allOf", newRV_noinc((SV *)keep));
564 4 continue;
565 }
566  
567 /* numeric bounds: the boolean exclusive* form folds into the number */
568 71452 ▶ if (N->v30 && kl == 7 && memEQ(k, "minimum", 7)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 7 && memEQ(k, "minimum", 7)) { 26532 44920 71452
if (N->v30 && kl == 7 && memEQ(k, "minimum", 7)) { 3328 23204 26532
if (N->v30 && kl == 7 && memEQ(k, "minimum", 7)) { 1225 2103 3328
569 1225 ▶ if (!exmn_on) (void)hv_store(dst, k, i32kl, newSVsv(v), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!exmn_on) (void)hv_store(dst, k, i32kl, newSVsv(v), 0); 12 1213 1225
570 1225 continue;
571 }
572 70227 ▶ if (N->v30 && kl == 7 && memEQ(k, "maximum", 7)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 7 && memEQ(k, "maximum", 7)) { 25307 44920 70227
if (N->v30 && kl == 7 && memEQ(k, "maximum", 7)) { 2103 23204 25307
if (N->v30 && kl == 7 && memEQ(k, "maximum", 7)) { 609 1494 2103
573 609 ▶ if (!exmx_on) (void)hv_store(dst, k, i32kl, newSVsv(v), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!exmx_on) (void)hv_store(dst, k, i32kl, newSVsv(v), 0); 607 2 609
574 609 continue;
575 }
576 69618 ▶ if (kl == 16 && memEQ(k, "exclusiveMinimum", 16)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 16 && memEQ(k, "exclusiveMinimum", 16)) { 1317 68301 69618
if (kl == 16 && memEQ(k, "exclusiveMinimum", 16)) { 1245 72 1317
577 1245 ▶ if (!exmn_bool) (void)hv_store(dst, k, i32kl, newSVsv(v), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!exmn_bool) (void)hv_store(dst, k, i32kl, newSVsv(v), 0); 29 1216 1245
578 1216 ▶ else if (exmn_on) (void)hv_store(dst, k, i32kl, newSVsv(mn), 0);
Branch: 1 branch
BranchTrueFalseTotal
else if (exmn_on) (void)hv_store(dst, k, i32kl, newSVsv(mn), 0); 1213 3 1216
579 1245 continue; /* `true` with no minimum, or `false`: meaningless */
580 }
581 68373 ▶ if (kl == 16 && memEQ(k, "exclusiveMaximum", 16)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 16 && memEQ(k, "exclusiveMaximum", 16)) { 72 68301 68373
if (kl == 16 && memEQ(k, "exclusiveMaximum", 16)) { 42 30 72
582 42 ▶ if (!exmx_bool) (void)hv_store(dst, k, i32kl, newSVsv(v), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!exmx_bool) (void)hv_store(dst, k, i32kl, newSVsv(v), 0); 40 2 42
583 2 ▶ else if (exmx_on) (void)hv_store(dst, k, i32kl, newSVsv(mx), 0);
Branch: 1 branch
BranchTrueFalseTotal
else if (exmx_on) (void)hv_store(dst, k, i32kl, newSVsv(mx), 0); 2 0 2
584 42 continue;
585 }
586  
587 /* tuple items -> prefixItems, and additionalItems takes over `items` */
588 68331 ▶ if (kl == 5 && memEQ(k, "items", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 5 && memEQ(k, "items", 5)) { 8145 60186 68331
if (kl == 5 && memEQ(k, "items", 5)) { 3402 4743 8145
589 3402 ▶ if (tuple)
Branch: 1 branch
BranchTrueFalseTotal
if (tuple) 608 2794 3402
590 608 (void)hv_stores(dst, "prefixItems",
591 oa_conv_schema_list(aTHX_ N, v, depth));
592 else
593 2794 (void)hv_store(dst, k, i32kl,
594 oa_conv_schema(aTHX_ N, v, depth + 1, NULL, 0), 0);
595 3402 continue;
596 }
597 64929 ▶ if (N->v30 && kl == 15 && memEQ(k, "additionalItems", 15)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 15 && memEQ(k, "additionalItems", 15)) { 21632 43297 64929
if (N->v30 && kl == 15 && memEQ(k, "additionalItems", 15)) { 607 21025 21632
if (N->v30 && kl == 15 && memEQ(k, "additionalItems", 15)) { 605 2 607
598 605 ▶ if (tuple)
Branch: 1 branch
BranchTrueFalseTotal
if (tuple) 605 0 605
599 605 (void)hv_stores(dst, "items",
600 oa_conv_schema(aTHX_ N, v, depth + 1, NULL, 0));
601 605 continue; /* without a tuple it constrains nothing */
602 }
603  
604 /* schema-level `example` is 3.1's `examples` array. The value itself is
605 * user JSON and is copied, never walked. */
606 64324 ▶ if (N->v30 && kl == 7 && memEQ(k, "example", 7)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 7 && memEQ(k, "example", 7)) { 21027 43297 64324
if (N->v30 && kl == 7 && memEQ(k, "example", 7)) { 1494 19533 21027
if (N->v30 && kl == 7 && memEQ(k, "example", 7)) { 607 887 1494
607 607 ▶ if (!oa_get(aTHX_ src, "examples")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ src, "examples")) { 607 0 607
608 607 AV *ex = newAV();
609 607 av_push(ex, newSVsv(v));
610 607 (void)hv_stores(dst, "examples", newRV_noinc((SV *)ex));
611 }
612 607 continue;
613 }
614  
615 63717 ▶ if (oa_kw_schema(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_kw_schema(k, kl)) { 201 63516 63717
616 201 (void)hv_store(dst, k, i32kl,
617 oa_conv_schema(aTHX_ N, v, depth + 1, NULL, 0), 0);
618 63516 ▶ } else if (oa_kw_schema_map(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (oa_kw_schema_map(k, kl)) { 9347 54169 63516
619 9347 (void)hv_store(dst, k, i32kl, oa_conv_schema_map(aTHX_ N, v, depth), 0);
620 54169 ▶ } else if (oa_kw_schema_list(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (oa_kw_schema_list(k, kl)) { 4720 49449 54169
621 4720 (void)hv_store(dst, k, i32kl, oa_conv_schema_list(aTHX_ N, v, depth), 0);
622 } else {
623 49449 (void)hv_store(dst, k, i32kl, newSVsv(v), 0); /* value or annotation */
624 }
625 }
626  
627 /* ---- rules that add keys ---- */
628  
629 53127 ▶ if (is_nullable) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_nullable) { 1828 51299 53127
630 1828 ▶ if (has_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_ref) { 605 1223 1828
631 /* `nullable` beside a $ref: the one sibling worth keeping, as the
632 * union the 3.1 spec would have written. */
633 605 AV *alt = newAV();
634 605 HV *rh = newHV(), *nh = newHV();
635 605 (void)hv_stores(rh, "$ref", newSVsv(ref));
636 605 (void)hv_stores(nh, "type", newSVpvs("null"));
637 605 av_push(alt, newRV_noinc((SV *)rh));
638 605 av_push(alt, newRV_noinc((SV *)nh));
639 605 (void)hv_stores(dst, "anyOf", newRV_noinc((SV *)alt));
640 1223 ▶ } else if (type) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (type) { 1222 1 1223
641 1222 AV *t = newAV();
642 1222 int seen = 0;
643 1223 ▶ if (SvROK(type) && oa_av_of(type)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(type) && oa_av_of(type)) { 1 1221 1222
if (SvROK(type) && oa_av_of(type)) { 1 0 1
644 1 AV *ta = oa_av_of(type);
645 1 SSize_t i, n = av_len(ta) + 1;
646 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 1 3
647 2 SV **e = av_fetch(ta, i, 0);
648 2 ▶ if (!e || !*e) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !*e) continue; 2 0 2
if (!e || !*e) continue; 0 2 2
649 2 { STRLEN l; const char *p = SvPV_const(*e, l);
650 2 ▶ if (l == 4 && memEQ(p, "null", 4)) seen = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "null", 4)) seen = 1; } 0 2 2
if (l == 4 && memEQ(p, "null", 4)) seen = 1; } 0 0 0
651 2 av_push(t, newSVsv(*e));
652 }
653 } else {
654 1221 STRLEN l; const char *p = SvPV_const(type, l);
655 1221 ▶ if (l == 4 && memEQ(p, "null", 4)) seen = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "null", 4)) seen = 1; 0 1221 1221
if (l == 4 && memEQ(p, "null", 4)) seen = 1; 0 0 0
656 1221 av_push(t, newSVsv(type));
657 }
658 1222 ▶ if (!seen) av_push(t, newSVpvs("null"));
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) av_push(t, newSVpvs("null")); 1222 0 1222
659 1222 (void)hv_stores(dst, "type", newRV_noinc((SV *)t));
660 } else {
661 /* no type to widen: a nullable enum admits the null member */
662 1 SV *en = oa_get(aTHX_ src, "enum");
663 1 ▶ AV *ea = en ? oa_av_of(en) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *ea = en ? oa_av_of(en) : NULL; 1 0 1
664 1 ▶ if (ea) {
Branch: 1 branch
BranchTrueFalseTotal
if (ea) { 1 0 1
665 1 AV *out = newAV();
666 1 SSize_t i, n = av_len(ea) + 1;
667 1 int seen = 0;
668 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 1 3
669 2 SV **e = av_fetch(ea, i, 0);
670 2 ▶ if (e && *e && !SvOK(*e)) seen = 1;
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && !SvOK(*e)) seen = 1; 2 0 2
if (e && *e && !SvOK(*e)) seen = 1; 2 0 2
if (e && *e && !SvOK(*e)) seen = 1; 0 2 2
671 2 ▶ av_push(out, (e && *e) ? newSVsv(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, (e && *e) ? newSVsv(*e) : newSV(0)); 2 0 2
av_push(out, (e && *e) ? newSVsv(*e) : newSV(0)); 2 0 2
672 }
673 1 ▶ if (!seen) av_push(out, newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) av_push(out, newSV(0)); 1 0 1
674 1 (void)hv_stores(dst, "enum", newRV_noinc((SV *)out));
675 }
676 /* nullable with neither type nor enum constrains nothing: drop it */
677 }
678 }
679  
680 /* 3.0 spelled binary payloads with `format`; 2020-12 has vocabulary for it.
681 * `format` stays as well - it is inert to the validator, and the docs UI
682 * keys its file picker off either idiom. */
683 53127 ▶ if (N->v30 && fmt && !SvROK(fmt)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && fmt && !SvROK(fmt)) { 16411 36716 53127
if (N->v30 && fmt && !SvROK(fmt)) { 610 15801 16411
if (N->v30 && fmt && !SvROK(fmt)) { 610 0 610
684 610 STRLEN l; const char *p = SvPV_const(fmt, l);
685 610 ▶ if (l == 4 && memEQ(p, "byte", 4) && !oa_get(aTHX_ src, "contentEncoding"))
Branch: 3 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "byte", 4) && !oa_get(aTHX_ src, "contentEncoding")) 609 1 610
if (l == 4 && memEQ(p, "byte", 4) && !oa_get(aTHX_ src, "contentEncoding")) 609 0 609
if (l == 4 && memEQ(p, "byte", 4) && !oa_get(aTHX_ src, "contentEncoding")) 607 2 609
686 607 (void)hv_stores(dst, "contentEncoding", newSVpvs("base64"));
687 3 ▶ else if (l == 6 && memEQ(p, "binary", 6)
Branch: 2 branches
BranchTrueFalseTotal
else if (l == 6 && memEQ(p, "binary", 6) 1 2 3
else if (l == 6 && memEQ(p, "binary", 6) 1 0 1
688 1 ▶ && !oa_get(aTHX_ src, "contentMediaType"))
Branch: 1 branch
BranchTrueFalseTotal
&& !oa_get(aTHX_ src, "contentMediaType")) 1 0 1
689 1 (void)hv_stores(dst, "contentMediaType",
690 newSVpvs("application/octet-stream"));
691 }
692  
693 /* the discriminator chain */
694 53127 ▶ if (expand) {
Branch: 1 branch
BranchTrueFalseTotal
if (expand) { 1224 51903 53127
695 1224 SSize_t i, n = av_len(dkeys) + 1;
696 AV *chain;
697 SV **slot;
698  
699 { /* the discriminator, with its mapping now always explicit: that is
700 * what makes a second pass rebuild exactly this same chain */
701 1224 HV *d2 = oa_conv_copy_hv(aTHX_ dh);
702 1224 HV *m2 = newHV();
703 3671 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2447 1224 3671
704 2447 SV *k = *av_fetch(dkeys, i, 0);
705 2447 (void)hv_store(m2, SvPVX_const(k), (I32)SvCUR(k),
706 newSVsv(*av_fetch(drefs, i, 0)), 0);
707 }
708 1224 (void)hv_stores(d2, "mapping", newRV_noinc((SV *)m2));
709 1224 (void)hv_stores(dst, "discriminator", newRV_noinc((SV *)d2));
710 }
711  
712 1224 slot = hv_fetchs(dst, "allOf", 0);
713 1224 ▶ chain = (slot && *slot && oa_av_of(*slot)) ? oa_av_of(*slot) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
chain = (slot && *slot && oa_av_of(*slot)) ? oa_av_of(*slot) : NULL; 4 1220 1224
chain = (slot && *slot && oa_av_of(*slot)) ? oa_av_of(*slot) : NULL; 4 0 4
chain = (slot && *slot && oa_av_of(*slot)) ? oa_av_of(*slot) : NULL; 4 0 4
714 1224 ▶ if (!chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (!chain) { 1220 4 1224
715 1220 chain = newAV();
716 1220 (void)hv_stores(dst, "allOf", newRV_noinc((SV *)chain));
717 }
718  
719 { /* the guard: the property is required, and must name a branch we
720 * know. Without it an unmapped value would match no `if` at all and
721 * so be validated against nothing. */
722 1224 AV *en = newAV();
723 1224 HV *inner = newHV();
724 SV *probe;
725 3671 ▶ for (i = 0; i < n; i++) av_push(en, newSVsv(*av_fetch(dkeys, i, 0)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(en, newSVsv(*av_fetch(dkeys, i, 0))); 2447 1224 3671
726 1224 (void)hv_stores(inner, "enum", newRV_noinc((SV *)en));
727 1224 probe = oa_disc_probe(aTHX_ pn, newRV_noinc((SV *)inner));
728 1224 (void)hv_stores((HV *)SvRV(probe), OA_DISC_MARK, newSVsv(pn));
729 1224 av_push(chain, probe);
730 }
731  
732 3671 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2447 1224 3671
733 2447 HV *branch = newHV(), *inner = newHV();
734 2447 SV *ref_i = *av_fetch(drefs, i, 0);
735 2447 (void)hv_stores(inner, "const", newSVsv(*av_fetch(dkeys, i, 0)));
736 2447 (void)hv_stores(branch, OA_DISC_MARK, newSVsv(pn));
737 2447 (void)hv_stores(branch, "if",
738 oa_disc_probe(aTHX_ pn, newRV_noinc((SV *)inner)));
739 2447 (void)hv_stores(branch, "then",
740 oa_disc_then(aTHX_ N, ref_i, nm, nml, depth));
741 2447 av_push(chain, newRV_noinc((SV *)branch));
742 }
743 }
744  
745 53127 return newRV_noinc((SV *)dst);
746 }
747  
748 /* ---- the document spine ---------------------------------------------------- */
749  
750 /* A Media Type Object: { schema: S, example: V, examples: {...} }. The
751 * `example`/`examples` here are Example Objects, not schema keywords - they are
752 * copied, not converted. */
753 /* Both are defined below, and both are needed above their definitions: media
754 * converts an `examples` map, and the deref-only helpers sit beside the
755 * section lists rather than beside oa_deref_object. Static, to match. */
756 static HV *oa_deref_object(pTHX_ oa_norm *N, HV *src, const char *const *sections);
757 static SV *oa_conv_example(pTHX_ oa_norm *N, SV *e);
758  
759 9225 static SV *oa_conv_media(pTHX_ oa_norm *N, SV *media) {
760 9225 HV *mh = oa_hv_of(media), *mc;
761 SV *schema, *examples;
762 9225 ▶ if (!mh) return newSVsv(media);
Branch: 1 branch
BranchTrueFalseTotal
if (!mh) return newSVsv(media); 0 9225 9225
763 /* `example` and `examples` are mutually exclusive on a Media Type Object.
764 * This is the MEDIA TYPE's example (an Example Object), not the schema
765 * keyword the 3.0 converter rewrites - those are different fields at
766 * different levels and only this one has the exclusion. */
767 9225 ▶ if (oa_get(aTHX_ mh, "example") && oa_get(aTHX_ mh, "examples"))
Branch: 2 branches
BranchTrueFalseTotal
if (oa_get(aTHX_ mh, "example") && oa_get(aTHX_ mh, "examples")) 11 9214 9225
if (oa_get(aTHX_ mh, "example") && oa_get(aTHX_ mh, "examples")) 1 10 11
768 1 croak("Open::API: a media type declares both 'example' and "
769 "'examples', which are mutually exclusive");
770 9224 schema = oa_get(aTHX_ mh, "schema");
771 9224 examples = oa_get(aTHX_ mh, "examples");
772 9224 ▶ if (!schema && !examples) return newSVsv(media);
Branch: 2 branches
BranchTrueFalseTotal
if (!schema && !examples) return newSVsv(media); 1305 7919 9224
if (!schema && !examples) return newSVsv(media); 1305 0 1305
773 7919 mc = oa_conv_copy_hv(aTHX_ mh);
774 7919 sv_2mortal((SV *)mc);
775 7919 ▶ if (schema)
Branch: 1 branch
BranchTrueFalseTotal
if (schema) 7919 0 7919
776 7919 (void)hv_stores(mc, "schema", oa_conv_schema(aTHX_ N, schema, 0, NULL, 0));
777 /* each entry may be a $ref in place of the Example Object */
778 7913 ▶ if (examples)
Branch: 1 branch
BranchTrueFalseTotal
if (examples) 9 7904 7913
779 9 (void)hv_stores(mc, "examples", oa_conv_map(aTHX_ N, examples, oa_conv_example));
780 7911 return newRV_inc((SV *)mc);
781 }
782  
783 /* a `content` map: { "application/json": <Media Type Object>, ... }
784 *
785 * The Encoding Object is checked HERE rather than in oa_conv_media, because
786 * both of its rules are about the media type - and the media type is the map
787 * KEY, which the per-value converter never sees. */
788 7928 static SV *oa_conv_content(pTHX_ oa_norm *N, SV *content) {
789 7928 HV *ch = oa_hv_of(content);
790 7928 ▶ if (ch) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch) { 7928 0 7928
791 HE *he;
792 7928 hv_iterinit(ch);
793 17153 ▶ while ((he = hv_iternext(ch))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(ch))) { 9229 7924 17153
794 9229 I32 kl; const char *k = hv_iterkey(he, &kl);
795 9229 HV *mh = oa_hv_of(hv_iterval(ch, he));
796 9229 ▶ HV *enc = mh ? oa_hv_of(oa_get(aTHX_ mh, "encoding")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *enc = mh ? oa_hv_of(oa_get(aTHX_ mh, "encoding")) : NULL; 9229 0 9229
797 HV *props;
798 HE *ehe;
799 9229 ▶ if (!enc) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) continue; 9212 17 9229
800 /* "MAY only be used in a Media Type Object whose media type is
801 * multipart or application/x-www-form-urlencoded". Anywhere else
802 * it is inert, and an inert encoding is a silently unenforced
803 * contentType or set of part headers. */
804 17 ▶ if (!oa_ctype_is_form(k, (STRLEN)kl)
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_ctype_is_form(k, (STRLEN)kl) 6 11 17
805 6 ▶ && !oa_ctype_is_multipart(k, (STRLEN)kl))
Branch: 1 branch
BranchTrueFalseTotal
&& !oa_ctype_is_multipart(k, (STRLEN)kl)) 2 4 6
806 2 croak("Open::API: '%.*s' declares an 'encoding', which is only "
807 "legal for multipart or x-www-form-urlencoded",
808 (int)kl, k);
809 /* "The key, being the property name, MUST exist in the schema as a
810 * property." A key that names nothing encodes nothing. */
811 15 props = oa_hv_of(oa_get(aTHX_ mh, "schema"));
812 15 ▶ props = props ? oa_hv_of(oa_get(aTHX_ props, "properties")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
props = props ? oa_hv_of(oa_get(aTHX_ props, "properties")) : NULL; 15 0 15
813 15 ▶ if (!props) continue; /* no properties to check against */
Branch: 1 branch
BranchTrueFalseTotal
if (!props) continue; /* no properties to check against */ 0 15 15
814 15 hv_iterinit(enc);
815 28 ▶ while ((ehe = hv_iternext(enc))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ehe = hv_iternext(enc))) { 15 13 28
816 15 I32 el; const char *e = hv_iterkey(ehe, &el);
817 15 ▶ if (!hv_exists(props, e, el))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(props, e, el)) 2 13 15
818 2 croak("Open::API: encoding '%.*s' on '%.*s' names no "
819 "property in the schema", (int)el, e, (int)kl, k);
820 }
821 }
822 }
823 7924 return oa_conv_map(aTHX_ N, content, oa_conv_media);
824 }
825  
826 /* ---- component references --------------------------------------------------
827 *
828 * A `$ref` used IN PLACE OF a parameter, header, requestBody or response
829 * object is legal and common OpenAPI:
830 *
831 * parameters:
832 * - $ref: '#/components/parameters/PageSize'
833 *
834 * and until now nothing here resolved one. Only schema refs were handled, by
835 * the `#/components/schemas/X` -> `#/$defs/X` rewrite. Left alone, such an
836 * object reaches the compiler as a hash with neither `in` nor `content`, and
837 * the two failures do not look alike: a parameter CROAKS the whole document on
838 * its absent `in`, while a requestBody or response returns early on its absent
839 * `content` and is left silently unvalidated and silently optional.
840 *
841 * So they are inlined here, before anything compiles.
842 *
843 * Resolution is against the SOURCE document (N->doc). oa_normalize converts
844 * paths before components, so a target read from either is the raw one, and
845 * the spliced-in object then goes through exactly the conversion it would have
846 * had written inline. That is also what makes the pass idempotent: an inlined
847 * object carries no `$ref`, so a second pass finds nothing to do.
848 *
849 * A ref that does not resolve - a remote document, a deeper JSON pointer, a
850 * name that is not there - is left EXACTLY as it was, so it fails the way it
851 * does today rather than being quietly dropped. A chain is followed to a fixed
852 * depth, which makes a cycle terminate with the ref still in place, and so
853 * refused by the compiler rather than able to hang it. */
854  
855 #define OA_COMP_MAX_HOPS 8
856  
857 static const char *const oa_sec_param[] = { "parameters", "headers", NULL };
858 static const char *const oa_sec_body[] = { "requestBodies", NULL };
859 static const char *const oa_sec_resp[] = { "responses", NULL };
860 /* 3.1 only: 3.0 has no components.pathItems, so a 3.0 path-item $ref names
861 * something outside this document and is refused like any other reference we
862 * cannot follow - loudly, where it used to make the path silently vanish. */
863 static const char *const oa_sec_pathitem[] = { "pathItems", NULL };
864 /* An Example, a Security Scheme and a Callback may each be written as a $ref
865 * in place of the object. Nothing inlined them, so the reference survived
866 * into ->spec: the mock generator found no `value` on an example and fell
867 * through to generation, and a referenced security scheme reached the
868 * compiler with no `type` and refused the document. */
869 static const char *const oa_sec_example[] = { "examples", NULL };
870 static const char *const oa_sec_scheme[] = { "securitySchemes", NULL };
871 static const char *const oa_sec_callback[] = { "callbacks", NULL };
872 static const char *const oa_sec_link[] = { "links", NULL };
873  
874 /* deref-only: these carry nothing this normaliser needs to convert, they just
875 * have to stop being a reference */
876 44 static SV *oa_conv_deref_only(pTHX_ oa_norm *N, SV *sv,
877 const char *const *sections) {
878 44 HV *h = oa_hv_of(sv), *c;
879 44 ▶ if (!h) return newSVsv(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(sv); 0 44 44
880 44 c = oa_deref_object(aTHX_ N, h, sections);
881 44 ▶ return c ? newRV_noinc((SV *)c) : newSVsv(sv);
Branch: 1 branch
BranchTrueFalseTotal
return c ? newRV_noinc((SV *)c) : newSVsv(sv); 4 40 44
882 }
883 15 static SV *oa_conv_example(pTHX_ oa_norm *N, SV *e) {
884 15 HV *eh = oa_hv_of(e);
885 /* `value` embeds the example, `externalValue` points at it. Declaring
886 * both says two different things about one example. */
887 15 ▶ if (eh && oa_get(aTHX_ eh, "value") && oa_get(aTHX_ eh, "externalValue"))
Branch: 3 branches
BranchTrueFalseTotal
if (eh && oa_get(aTHX_ eh, "value") && oa_get(aTHX_ eh, "externalValue")) 15 0 15
if (eh && oa_get(aTHX_ eh, "value") && oa_get(aTHX_ eh, "externalValue")) 12 3 15
if (eh && oa_get(aTHX_ eh, "value") && oa_get(aTHX_ eh, "externalValue")) 2 10 12
888 2 croak("Open::API: an example declares both 'value' and "
889 "'externalValue', which are mutually exclusive");
890 13 return oa_conv_deref_only(aTHX_ N, e, oa_sec_example);
891 }
892  
893 /* A Header Object is a Parameter Object without `name` and `in` - the name is
894 * the key it is filed under and the location is implicit. Declaring either
895 * says the header is something it cannot be.
896 *
897 * oa_conv_param is defined below and static, so it needs declaring here. */
898 static SV *oa_conv_param(pTHX_ oa_norm *N, SV *p);
899  
900 17 static SV *oa_conv_header(pTHX_ oa_norm *N, SV *hv) {
901 17 HV *hh = oa_hv_of(hv);
902 SV *st;
903 17 ▶ if (hh && (oa_get(aTHX_ hh, "name") || oa_get(aTHX_ hh, "in")))
Branch: 3 branches
BranchTrueFalseTotal
if (hh && (oa_get(aTHX_ hh, "name") || oa_get(aTHX_ hh, "in"))) 17 0 17
if (hh && (oa_get(aTHX_ hh, "name") || oa_get(aTHX_ hh, "in"))) 16 1 17
if (hh && (oa_get(aTHX_ hh, "name") || oa_get(aTHX_ hh, "in"))) 0 16 16
904 1 croak("Open::API: a header object declares 'name' or 'in'; a header "
905 "takes its name from its key and is always in the header");
906 /* A header is serialized the way a parameter `in: header` is, and `simple`
907 * is the only style defined for that location. Another style would be
908 * read by nobody: the value still arrives simple-encoded. */
909 16 ▶ st = hh ? oa_get(aTHX_ hh, "style") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
st = hh ? oa_get(aTHX_ hh, "style") : NULL; 16 0 16
910 16 ▶ if (st && !SvROK(st)) {
Branch: 2 branches
BranchTrueFalseTotal
if (st && !SvROK(st)) { 3 13 16
if (st && !SvROK(st)) { 3 0 3
911 3 STRLEN sl; const char *sp = SvPV_const(st, sl);
912 3 ▶ if (!(sl == 6 && memEQ(sp, "simple", 6)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(sl == 6 && memEQ(sp, "simple", 6))) 1 2 3
if (!(sl == 6 && memEQ(sp, "simple", 6))) 0 1 1
913 2 croak("Open::API: a header declares style '%.*s'; 'simple' is the "
914 "only style defined for a header", (int)sl, sp);
915 }
916 14 return oa_conv_param(aTHX_ N, hv);
917 }
918 24 static SV *oa_conv_scheme(pTHX_ oa_norm *N, SV *s) {
919 24 return oa_conv_deref_only(aTHX_ N, s, oa_sec_scheme);
920 }
921 7 static SV *oa_conv_link(pTHX_ oa_norm *N, SV *l) {
922 7 return oa_conv_deref_only(aTHX_ N, l, oa_sec_link);
923 }
924  
925 /* "#/components/<section>/<name>" -> the raw component object, borrowed, or
926 * NULL when the ref is not a local reference into one of `sections`. */
927 60 static SV *oa_component_target(pTHX_ oa_norm *N, SV *ref,
928 const char *const *sections) {
929 static const char pfx[] = "#/components/";
930 60 const STRLEN pfxl = sizeof(pfx) - 1;
931 STRLEN l, sl, nl;
932 const char *p, *sec, *slash, *nm;
933 HV *comp, *bucket;
934 SV **e;
935 60 int i, hit = -1;
936  
937 60 ▶ if (!ref || SvROK(ref) || !SvOK(ref)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 60 0 60
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 60 0 60
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 0 60 60
938 60 p = SvPV_const(ref, l);
939 60 ▶ if (l <= pfxl || !memEQ(p, pfx, pfxl)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (l <= pfxl || !memEQ(p, pfx, pfxl)) return NULL; 60 0 60
if (l <= pfxl || !memEQ(p, pfx, pfxl)) return NULL; 2 58 60
940  
941 58 sec = p + pfxl;
942 58 slash = (const char *)memchr(sec, '/', l - pfxl);
943 58 ▶ if (!slash) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!slash) return NULL; 0 58 58
944 58 sl = (STRLEN)(slash - sec);
945 58 nm = slash + 1;
946 58 nl = l - pfxl - sl - 1;
947 /* A deeper pointer addresses something inside a component, which is not a
948 * whole object and is not ours to inline. */
949 58 ▶ if (!nl || memchr(nm, '/', nl)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!nl || memchr(nm, '/', nl)) return NULL; 58 0 58
if (!nl || memchr(nm, '/', nl)) return NULL; 3 55 58
950  
951 59 ▶ for (i = 0; sections[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; sections[i]; i++) { 59 0 59
952 59 STRLEN wl = (STRLEN)strlen(sections[i]);
953 59 ▶ if (wl == sl && memEQ(sec, sections[i], sl)) { hit = i; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (wl == sl && memEQ(sec, sections[i], sl)) { hit = i; break; } 55 4 59
if (wl == sl && memEQ(sec, sections[i], sl)) { hit = i; break; } 55 0 55
954 }
955 55 ▶ if (hit < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (hit < 0) return NULL; 0 55 55
956  
957 55 ▶ comp = N->doc ? oa_hv_of(oa_get(aTHX_ N->doc, "components")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
comp = N->doc ? oa_hv_of(oa_get(aTHX_ N->doc, "components")) : NULL; 55 0 55
958 55 ▶ bucket = comp ? oa_hv_of(oa_get(aTHX_ comp, sections[hit])) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
bucket = comp ? oa_hv_of(oa_get(aTHX_ comp, sections[hit])) : NULL; 55 0 55
959 55 ▶ e = bucket ? hv_fetch(bucket, nm, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
e = bucket ? hv_fetch(bucket, nm, (I32)nl, 0) : NULL; 52 3 55
960 55 ▶ return (e && *e && oa_hv_of(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 49 6 55
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 49 0 49
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 49 0 49
961 }
962  
963 /* Follow a component reference to the object it names and return a copy the
964 * caller owns, or NULL when there was nothing to follow. Any siblings the
965 * reference itself carried win over the target's: 3.1 allows `summary` and
966 * `description` beside a `$ref` precisely so a caller can retitle one. */
967 24712 static HV *oa_deref_object(pTHX_ oa_norm *N, HV *src,
968 const char *const *sections) {
969 24712 HV *cur = src, *out;
970 HE *he;
971 int hops;
972  
973 24761 ▶ for (hops = 0; hops < OA_COMP_MAX_HOPS; hops++) {
Branch: 1 branch
BranchTrueFalseTotal
for (hops = 0; hops < OA_COMP_MAX_HOPS; hops++) { 24759 2 24761
974 24759 SV *ref = oa_get(aTHX_ cur, "$ref");
975 SV *t;
976 HV *target;
977 24759 ▶ if (!ref) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!ref) break; 24699 60 24759
978 60 t = oa_component_target(aTHX_ N, ref, sections);
979 60 ▶ if (!t) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) break; 11 49 60
980 49 target = oa_hv_of(t);
981 49 ▶ if (!target || target == cur) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!target || target == cur) break; 49 0 49
if (!target || target == cur) break; 49 0 49
982 49 cur = target;
983 }
984 /* A `$ref` still here is one we could not follow: a name that is not in
985 * components, a pointer into another section, a remote document, a deeper
986 * JSON pointer, or a chain that outran OA_COMP_MAX_HOPS. Refuse it.
987 *
988 * This used to be left in place on the theory that it would "fail the way
989 * it does today". That holds only for a PARAMETER, where oa_compile_params
990 * croaks on the absent `in`. A requestBody or response has no such
991 * backstop: oa_compile_body reads `required` off the leftover stub (absent,
992 * so 0) and returns on the absent `content`, and oa_compile_responses skips
993 * the row - so the body was neither validated NOR required, silently. That
994 * is the very failure this file's $ref support was added to remove. */
995 {
996 24712 SV *left = oa_get(aTHX_ cur, "$ref");
997 24712 ▶ if (left) {
Branch: 1 branch
BranchTrueFalseTotal
if (left) { 13 24699 24712
998 13 STRLEN rl; const char *rp = SvPV_const(left, rl);
999 13 croak("Open::API: cannot resolve $ref '%.*s'", (int)rl, rp);
1000 }
1001 }
1002 24699 ▶ if (cur == src) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (cur == src) return NULL; 24666 33 24699
1003  
1004 33 out = oa_conv_copy_hv(aTHX_ cur);
1005 33 hv_iterinit(src);
1006 68 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 35 33 68
1007 35 I32 kl; const char *k = hv_iterkey(he, &kl);
1008 35 ▶ if (kl == 4 && memEQ(k, "$ref", 4)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "$ref", 4)) continue; 33 2 35
if (kl == 4 && memEQ(k, "$ref", 4)) continue; 33 0 33
1009 2 (void)hv_store(out, k, kl, newSVsv(hv_iterval(src, he)), 0);
1010 }
1011 33 return out;
1012 }
1013  
1014 /* A Parameter or Header Object: `schema`, or `content` holding one. */
1015 7369 static SV *oa_conv_param(pTHX_ oa_norm *N, SV *p) {
1016 7369 HV *ph = oa_hv_of(p), *pc;
1017 SV *schema, *content;
1018 7369 ▶ if (!ph) return newSVsv(p);
Branch: 1 branch
BranchTrueFalseTotal
if (!ph) return newSVsv(p); 0 7369 7369
1019 7369 pc = oa_deref_object(aTHX_ N, ph, oa_sec_param);
1020 7365 ▶ if (pc) ph = pc; /* resolved, and pc is already our copy */
Branch: 1 branch
BranchTrueFalseTotal
if (pc) ph = pc; /* resolved, and pc is already our copy */ 15 7350 7365
1021 /* `example` and `examples` are mutually exclusive here too, for the same
1022 * reason they are on a Media Type Object */
1023 7365 ▶ if (oa_get(aTHX_ ph, "example") && oa_get(aTHX_ ph, "examples"))
Branch: 2 branches
BranchTrueFalseTotal
if (oa_get(aTHX_ ph, "example") && oa_get(aTHX_ ph, "examples")) 1 7364 7365
if (oa_get(aTHX_ ph, "example") && oa_get(aTHX_ ph, "examples")) 1 0 1
1024 1 croak("Open::API: a parameter declares both 'example' and "
1025 "'examples', which are mutually exclusive");
1026 7364 schema = oa_get(aTHX_ ph, "schema");
1027 7364 content = oa_get(aTHX_ ph, "content");
1028 7364 ▶ if (!schema && !content)
Branch: 2 branches
BranchTrueFalseTotal
if (!schema && !content) 610 6754 7364
if (!schema && !content) 0 610 610
1029 0 ▶ return pc ? newRV_noinc((SV *)pc) : newSVsv(p);
Branch: 1 branch
BranchTrueFalseTotal
return pc ? newRV_noinc((SV *)pc) : newSVsv(p); 0 0 0
1030 7364 ▶ if (!pc) pc = oa_conv_copy_hv(aTHX_ ph);
Branch: 1 branch
BranchTrueFalseTotal
if (!pc) pc = oa_conv_copy_hv(aTHX_ ph); 7349 15 7364
1031 7364 ▶ if (schema) (void)hv_stores(pc, "schema",
Branch: 1 branch
BranchTrueFalseTotal
if (schema) (void)hv_stores(pc, "schema", 6754 610 7364
1032 oa_conv_schema(aTHX_ N, schema, 0, NULL, 0));
1033 7364 ▶ if (content) (void)hv_stores(pc, "content", oa_conv_content(aTHX_ N, content));
Branch: 1 branch
BranchTrueFalseTotal
if (content) (void)hv_stores(pc, "content", oa_conv_content(aTHX_ N, content)); 610 6754 7364
1034 7364 return newRV_noinc((SV *)pc);
1035 }
1036  
1037 6029 static SV *oa_conv_param_list(pTHX_ oa_norm *N, SV *params) {
1038 6029 AV *src = oa_av_of(params), *dst;
1039 SSize_t i, n;
1040 6029 ▶ if (!src) return newSVsv(params);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(params); 0 6029 6029
1041 6029 dst = newAV();
1042 6029 sv_2mortal((SV *)dst); /* see oa_conv_map: oa_conv_param croaks */
1043 6029 n = av_len(src) + 1;
1044 13361 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7337 6024 13361
1045 7337 SV **e = av_fetch(src, i, 0);
1046 7337 ▶ av_push(dst, (e && *e) ? oa_conv_param(aTHX_ N, *e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, (e && *e) ? oa_conv_param(aTHX_ N, *e) : newSV(0)); 7337 0 7337
av_push(dst, (e && *e) ? oa_conv_param(aTHX_ N, *e) : newSV(0)); 7337 0 7337
1047 }
1048 6024 return newRV_inc((SV *)dst);
1049 }
1050  
1051 3347 static SV *oa_conv_body(pTHX_ oa_norm *N, SV *rb) {
1052 3347 HV *h = oa_hv_of(rb), *c;
1053 SV *content;
1054 3347 ▶ if (!h) return newSVsv(rb);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(rb); 0 3347 3347
1055 3347 c = oa_deref_object(aTHX_ N, h, oa_sec_body);
1056 3342 ▶ if (c) h = c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) h = c; 4 3338 3342
1057 3342 content = oa_get(aTHX_ h, "content");
1058 3342 ▶ if (!content)
Branch: 1 branch
BranchTrueFalseTotal
if (!content) 0 3342 3342
1059 0 ▶ return c ? newRV_noinc((SV *)c) : newSVsv(rb);
Branch: 1 branch
BranchTrueFalseTotal
return c ? newRV_noinc((SV *)c) : newSVsv(rb); 0 0 0
1060 3342 ▶ if (!c) c = oa_conv_copy_hv(aTHX_ h);
Branch: 1 branch
BranchTrueFalseTotal
if (!c) c = oa_conv_copy_hv(aTHX_ h); 3338 4 3342
1061 3342 (void)hv_stores(c, "content", oa_conv_content(aTHX_ N, content));
1062 3329 return newRV_noinc((SV *)c);
1063 }
1064  
1065 8918 static SV *oa_conv_response(pTHX_ oa_norm *N, SV *r) {
1066 8918 HV *h = oa_hv_of(r), *c;
1067 SV *content, *hdrs, *links;
1068 8918 ▶ if (!h) return newSVsv(r);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(r); 0 8918 8918
1069 8918 c = oa_deref_object(aTHX_ N, h, oa_sec_resp);
1070 8916 ▶ if (c) h = c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) h = c; 7 8909 8916
1071 8916 content = oa_get(aTHX_ h, "content");
1072 8916 hdrs = oa_get(aTHX_ h, "headers");
1073 8916 links = oa_get(aTHX_ h, "links");
1074 8916 ▶ if (!content && !hdrs && !links)
Branch: 3 branches
BranchTrueFalseTotal
if (!content && !hdrs && !links) 4940 3976 8916
if (!content && !hdrs && !links) 4928 12 4940
if (!content && !hdrs && !links) 4923 5 4928
1075 4923 ▶ return c ? newRV_noinc((SV *)c) : newSVsv(r);
Branch: 1 branch
BranchTrueFalseTotal
return c ? newRV_noinc((SV *)c) : newSVsv(r); 6 4917 4923
1076 3993 ▶ if (!c) c = oa_conv_copy_hv(aTHX_ h);
Branch: 1 branch
BranchTrueFalseTotal
if (!c) c = oa_conv_copy_hv(aTHX_ h); 3992 1 3993
1077 3993 ▶ if (content) (void)hv_stores(c, "content", oa_conv_content(aTHX_ N, content));
Branch: 1 branch
BranchTrueFalseTotal
if (content) (void)hv_stores(c, "content", oa_conv_content(aTHX_ N, content)); 3976 17 3993
1078 3993 ▶ if (hdrs) (void)hv_stores(c, "headers",
Branch: 1 branch
BranchTrueFalseTotal
if (hdrs) (void)hv_stores(c, "headers", 17 3976 3993
1079 oa_conv_map(aTHX_ N, hdrs, oa_conv_header));
1080 3990 ▶ if (links) (void)hv_stores(c, "links",
Branch: 1 branch
BranchTrueFalseTotal
if (links) (void)hv_stores(c, "links", 5 3985 3990
1081 oa_conv_map(aTHX_ N, links, oa_conv_link));
1082 3990 return newRV_noinc((SV *)c);
1083 }
1084  
1085 /* A Callback Object is a map of runtime expression => Path Item, so its
1086 * contents are converted exactly like a path: a 3.0 schema inside a callback
1087 * is a 3.0 schema, and ->spec must not end up holding two dialects at once.
1088 * Declared ahead of oa_conv_path_item, which is defined below and already
1089 * calls back into oa_conv_operation. */
1090 static SV *oa_conv_path_item(pTHX_ oa_norm *N, SV *item);
1091  
1092 8 static SV *oa_conv_callback(pTHX_ oa_norm *N, SV *cb) {
1093 8 HV *h = oa_hv_of(cb), *d;
1094 8 ▶ if (!h) return newSVsv(cb);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(cb); 0 8 8
1095 8 d = oa_deref_object(aTHX_ N, h, oa_sec_callback);
1096 8 ▶ return oa_conv_map(aTHX_ N, d ? sv_2mortal(newRV_noinc((SV *)d))
Branch: 1 branch
BranchTrueFalseTotal
return oa_conv_map(aTHX_ N, d ? sv_2mortal(newRV_noinc((SV *)d)) 1 7 8
1097 : cb, oa_conv_path_item);
1098 }
1099  
1100 7632 static SV *oa_conv_operation(pTHX_ oa_norm *N, SV *op) {
1101 7632 HV *h = oa_hv_of(op), *c;
1102 SV *params, *rb, *resps, *cbs;
1103 7632 ▶ if (!h) return newSVsv(op);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(op); 0 7632 7632
1104 7632 params = oa_get(aTHX_ h, "parameters");
1105 7632 rb = oa_get(aTHX_ h, "requestBody");
1106 7632 resps = oa_get(aTHX_ h, "responses");
1107 7632 cbs = oa_get(aTHX_ h, "callbacks");
1108 7632 ▶ if (!params && !rb && !resps && !cbs) return newSVsv(op);
Branch: 4 branches
BranchTrueFalseTotal
if (!params && !rb && !resps && !cbs) return newSVsv(op); 2906 4726 7632
if (!params && !rb && !resps && !cbs) return newSVsv(op); 166 2740 2906
if (!params && !rb && !resps && !cbs) return newSVsv(op); 0 166 166
if (!params && !rb && !resps && !cbs) return newSVsv(op); 0 0 0
1109 7632 c = oa_conv_copy_hv(aTHX_ h);
1110 /* mortal while it is filled: unlike oa_conv_param and friends, the
1111 * container exists BEFORE the conversions that can croak on a $ref we
1112 * cannot resolve, so nothing else would own it on the way out. */
1113 7632 sv_2mortal((SV *)c);
1114 7632 ▶ if (params) (void)hv_stores(c, "parameters", oa_conv_param_list(aTHX_ N, params));
Branch: 1 branch
BranchTrueFalseTotal
if (params) (void)hv_stores(c, "parameters", oa_conv_param_list(aTHX_ N, params)); 4726 2906 7632
1115 7627 ▶ if (rb) (void)hv_stores(c, "requestBody", oa_conv_body(aTHX_ N, rb));
Branch: 1 branch
BranchTrueFalseTotal
if (rb) (void)hv_stores(c, "requestBody", oa_conv_body(aTHX_ N, rb)); 3341 4286 7627
1116 7609 ▶ if (resps) (void)hv_stores(c, "responses",
Branch: 1 branch
BranchTrueFalseTotal
if (resps) (void)hv_stores(c, "responses", 7609 0 7609
1117 oa_conv_map(aTHX_ N, resps, oa_conv_response));
1118 7604 ▶ if (cbs) (void)hv_stores(c, "callbacks",
Branch: 1 branch
BranchTrueFalseTotal
if (cbs) (void)hv_stores(c, "callbacks", 6 7598 7604
1119 oa_conv_map(aTHX_ N, cbs, oa_conv_callback));
1120 7604 return newRV_inc((SV *)c);
1121 }
1122  
1123 /* A path item, which may itself BE a `$ref` (legal in 3.0 and 3.1, and common
1124 * in split documents). Nothing inlined it before, so the item kept its `$ref`
1125 * and carried no method keys: oa_compile found no operations and registered no
1126 * route, and a legally-specified path simply 404'd with nothing to report. */
1127 5026 static SV *oa_conv_path_item(pTHX_ oa_norm *N, SV *item) {
1128 5026 HV *h = oa_hv_of(item), *c, *d;
1129 SV *params;
1130 int m;
1131 5026 ▶ if (!h) return newSVsv(item);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(item); 0 5026 5026
1132 5026 d = oa_deref_object(aTHX_ N, h, oa_sec_pathitem);
1133 5024 ▶ if (d) h = d;
Branch: 1 branch
BranchTrueFalseTotal
if (d) h = d; 2 5022 5024
1134 5024 ▶ c = d ? d : oa_conv_copy_hv(aTHX_ h);
Branch: 1 branch
BranchTrueFalseTotal
c = d ? d : oa_conv_copy_hv(aTHX_ h); 5022 2 5024
1135 5024 sv_2mortal((SV *)c); /* see oa_conv_operation */
1136 5024 params = oa_get(aTHX_ h, "parameters");
1137 5024 ▶ if (params) (void)hv_stores(c, "parameters", oa_conv_param_list(aTHX_ N, params));
Branch: 1 branch
BranchTrueFalseTotal
if (params) (void)hv_stores(c, "parameters", oa_conv_param_list(aTHX_ N, params)); 1303 3721 5024
1138 45028 ▶ for (m = 0; oa_methods[m]; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; oa_methods[m]; m++) { 40032 4996 45028
1139 40032 SV *op = oa_get(aTHX_ h, oa_methods[m]);
1140 40032 ▶ if (op) (void)hv_store(c, oa_methods[m], (I32)strlen(oa_methods[m]),
Branch: 1 branch
BranchTrueFalseTotal
if (op) (void)hv_store(c, oa_methods[m], (I32)strlen(oa_methods[m]), 7632 32400 40032
1141 oa_conv_operation(aTHX_ N, op), 0);
1142 }
1143 4996 return newRV_inc((SV *)c);
1144 }
1145  
1146 /* components.schemas is the one place a schema has a name, and the name is what
1147 * the inheritance form of `discriminator` keys its children on. */
1148 2291 static SV *oa_conv_schemas(pTHX_ oa_norm *N, SV *schemas) {
1149 2291 HV *src = oa_hv_of(schemas), *dst;
1150 HE *he;
1151 2291 ▶ if (!src) return newSVsv(schemas);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(schemas); 0 2291 2291
1152 2291 dst = newHV();
1153 2291 hv_iterinit(src);
1154 8939 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 6648 2291 8939
1155 6648 I32 kl; const char *k = hv_iterkey(he, &kl);
1156 6648 (void)hv_store(dst, k, kl,
1157 oa_conv_schema(aTHX_ N, hv_iterval(src, he), 0,
1158 k, (STRLEN)kl), 0);
1159 }
1160 2291 return newRV_noinc((SV *)dst);
1161 }
1162  
1163 2335 static SV *oa_conv_components(pTHX_ oa_norm *N, SV *comp) {
1164 2335 HV *h = oa_hv_of(comp), *c;
1165 SV *v;
1166 2335 ▶ if (!h) return newSVsv(comp);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(comp); 0 2335 2335
1167 2335 c = oa_conv_copy_hv(aTHX_ h);
1168 2335 ▶ if ((v = oa_get(aTHX_ h, "schemas")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "schemas"))) 2291 44 2335
1169 2291 (void)hv_stores(c, "schemas", oa_conv_schemas(aTHX_ N, v));
1170 2335 ▶ if ((v = oa_get(aTHX_ h, "parameters")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "parameters"))) 7 2328 2335
1171 7 (void)hv_stores(c, "parameters", oa_conv_map(aTHX_ N, v, oa_conv_param));
1172 2335 ▶ if ((v = oa_get(aTHX_ h, "headers")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "headers"))) 11 2324 2335
1173 11 (void)hv_stores(c, "headers", oa_conv_map(aTHX_ N, v, oa_conv_param));
1174 2335 ▶ if ((v = oa_get(aTHX_ h, "requestBodies")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "requestBodies"))) 6 2329 2335
1175 6 (void)hv_stores(c, "requestBodies", oa_conv_map(aTHX_ N, v, oa_conv_body));
1176 2335 ▶ if ((v = oa_get(aTHX_ h, "responses")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "responses"))) 6 2329 2335
1177 6 (void)hv_stores(c, "responses", oa_conv_map(aTHX_ N, v, oa_conv_response));
1178 2335 ▶ if ((v = oa_get(aTHX_ h, "pathItems")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "pathItems"))) 4 2331 2335
1179 4 (void)hv_stores(c, "pathItems", oa_conv_map(aTHX_ N, v, oa_conv_path_item));
1180 2335 ▶ if ((v = oa_get(aTHX_ h, "examples")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "examples"))) 3 2332 2335
1181 3 (void)hv_stores(c, "examples", oa_conv_map(aTHX_ N, v, oa_conv_example));
1182 2335 ▶ if ((v = oa_get(aTHX_ h, "securitySchemes")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "securitySchemes"))) 21 2314 2335
1183 21 (void)hv_stores(c, "securitySchemes", oa_conv_map(aTHX_ N, v, oa_conv_scheme));
1184 2335 ▶ if ((v = oa_get(aTHX_ h, "callbacks")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "callbacks"))) 2 2333 2335
1185 2 (void)hv_stores(c, "callbacks", oa_conv_map(aTHX_ N, v, oa_conv_callback));
1186 2335 ▶ if ((v = oa_get(aTHX_ h, "links")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "links"))) 2 2333 2335
1187 2 (void)hv_stores(c, "links", oa_conv_map(aTHX_ N, v, oa_conv_link));
1188 2335 return newRV_noinc((SV *)c);
1189 }
1190  
1191 /* Return a normalised document, or the original SV when there is nothing to do.
1192 * `doc` is never mutated. */
1193 3644 static SV *oa_normalize(pTHX_ SV *doc, int v30) {
1194 3644 HV *h = oa_hv_of(doc), *c;
1195 SV *v;
1196 oa_norm N;
1197 3644 ▶ if (!h) return newSVsv(doc);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(doc); 0 3644 3644
1198 3644 N.v30 = v30;
1199 3644 N.doc = h;
1200 3644 N.kids = NULL;
1201 3644 c = oa_conv_copy_hv(aTHX_ h);
1202 3644 ▶ if ((v = oa_get(aTHX_ h, "paths")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "paths"))) 3644 0 3644
1203 3644 (void)hv_stores(c, "paths", oa_conv_map(aTHX_ &N, v, oa_conv_path_item));
1204 /* 3.1 webhooks are path items too. They are calls the API SENDS, so they
1205 * are deliberately not routed - compiling them to zero routes is correct.
1206 * What was wrong is that they were skipped entirely, so a 3.0-shaped
1207 * schema inside one was never converted and a $ref inside one was never
1208 * resolved, leaving ->spec holding two dialects at once. */
1209 3614 ▶ if ((v = oa_get(aTHX_ h, "webhooks")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "webhooks"))) 4 3610 3614
1210 4 (void)hv_stores(c, "webhooks", oa_conv_map(aTHX_ &N, v, oa_conv_path_item));
1211 3614 ▶ if ((v = oa_get(aTHX_ h, "components")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "components"))) 2335 1279 3614
1212 2335 (void)hv_stores(c, "components", oa_conv_components(aTHX_ &N, v));
1213 3614 ▶ if (N.kids) SvREFCNT_dec((SV *)N.kids);
Branch: 1 branch
BranchTrueFalseTotal
if (N.kids) SvREFCNT_dec((SV *)N.kids); 607 3007 3614
1214 3614 return newRV_noinc((SV *)c);
1215 }
1216  
1217 #endif /* OA_NORMALIZE_H */
« include/oa_types.h ↑ Summary include/oa_route.h »