include/oa_normalize.h

filter: Statementstmt 99.1% 602 / 607 Branchbran 77.6% 553 / 712 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.5% 1155 / 1319 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_swagger2.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 108687 static HV *oa_conv_copy_hv(pTHX_ HV *src) {
94 108687 HV *dst = newHV();
95 HE *he;
96 108687 hv_iterinit(src);
97 376548 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 267861 108687 376548
98 267861 I32 kl; const char *k = hv_iterkey(he, &kl);
99 267861 (void)hv_store(dst, k, kl, newSVsv(hv_iterval(src, he)), 0);
100 }
101 108687 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 46535 static SV *oa_conv_map(pTHX_ oa_norm *N, SV *map, oa_conv_fn fn) {
112 46535 HV *src = oa_hv_of(map), *dst;
113 HE *he;
114 46535 ▶ if (!src) return newSVsv(map);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(map); 0 46535 46535
115 46535 dst = newHV();
116 46535 sv_2mortal((SV *)dst);
117 46535 hv_iterinit(src);
118 115467 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 68983 46484 115467
119 68983 I32 kl; const char *k = hv_iterkey(he, &kl);
120 68983 (void)hv_store(dst, k, kl, fn(aTHX_ N, hv_iterval(src, he)), 0);
121 }
122 46484 return newRV_inc((SV *)dst);
123 }
124  
125 /* A list of schemas: allOf / anyOf / oneOf / prefixItems / tuple items. */
126 12079 static SV *oa_conv_schema_list(pTHX_ oa_norm *N, SV *list, int depth) {
127 12079 AV *src = oa_av_of(list), *dst;
128 SSize_t i, n;
129 12079 ▶ if (!src) return newSVsv(list);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(list); 0 12079 12079
130 12079 dst = newAV();
131 12079 n = av_len(src) + 1;
132 36075 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 23996 12079 36075
133 23996 SV **e = av_fetch(src, i, 0);
134 23996 ▶ 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) 23996 0 23996
av_push(dst, (e && *e) ? oa_conv_schema(aTHX_ N, *e, depth + 1, NULL, 0) 23996 0 23996
135 : newSV(0));
136 }
137 12079 return newRV_noinc((SV *)dst);
138 }
139  
140 /* A map of schemas: properties / patternProperties / $defs / dependentSchemas. */
141 20026 static SV *oa_conv_schema_map(pTHX_ oa_norm *N, SV *map, int depth) {
142 20026 HV *src = oa_hv_of(map), *dst;
143 HE *he;
144 20026 ▶ if (!src) return newSVsv(map);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(map); 0 20026 20026
145 20026 dst = newHV();
146 20026 hv_iterinit(src);
147 63547 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 43521 20026 63547
148 43521 I32 kl; const char *k = hv_iterkey(he, &kl);
149 43521 (void)hv_store(dst, k, kl,
150 oa_conv_schema(aTHX_ N, hv_iterval(src, he), depth + 1,
151 NULL, 0), 0);
152 }
153 20026 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 26048 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 26048 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 24214 static const char *oa_schema_ref_name(pTHX_ SV *ref, STRLEN *out) {
183 STRLEN l; const char *p;
184 24214 ▶ if (!ref || SvROK(ref) || !SvOK(ref)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 16956 7258 24214
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 16956 0 16956
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 0 16956 16956
185 16956 p = SvPV_const(ref, l);
186 16956 ▶ 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)) 16956 0 16956
if (l <= OA_SCHEMA_PFX_LEN || !memEQ(p, OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN)) 0 16956 16956
187 0 return NULL;
188 16956 ▶ 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 16956 16956
189 16956 *out = l - OA_SCHEMA_PFX_LEN;
190 16956 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 32 static SV *oa_disc_ref(pTHX_ SV *v) {
196 STRLEN l; const char *p;
197 32 ▶ if (!v || SvROK(v)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || SvROK(v)) return NULL; 32 0 32
if (!v || SvROK(v)) return NULL; 0 32 32
198 32 p = SvPV_const(v, l);
199 32 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 0 32 32
200 32 ▶ if (p[0] == '#' || memchr(p, '/', l)) return newSVsv(v);
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == '#' || memchr(p, '/', l)) return newSVsv(v); 10 22 32
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 4855 static SV *oa_named_schema(pTHX_ oa_norm *N, const char *nm, STRLEN nl) {
207 4855 ▶ 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; 4855 0 4855
208 4855 ▶ 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; 4855 0 4855
209 4855 ▶ 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; 4855 0 4855
210 4855 ▶ return (e && *e && oa_hv_of(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 4855 0 4855
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 4855 0 4855
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 4855 0 4855
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 2424 static HV *oa_norm_kids(pTHX_ oa_norm *N) {
216 HV *comp, *schemas;
217 HE *he;
218 2424 ▶ if (N->kids) return N->kids;
Branch: 1 branch
BranchTrueFalseTotal
if (N->kids) return N->kids; 1212 1212 2424
219 1212 N->kids = newHV();
220 1212 ▶ 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; 1212 0 1212
221 1212 ▶ 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; 1212 0 1212
222 1212 ▶ if (!schemas) return N->kids;
Branch: 1 branch
BranchTrueFalseTotal
if (!schemas) return N->kids; 0 1212 1212
223 1212 hv_iterinit(schemas);
224 6649 ▶ while ((he = hv_iternext(schemas))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(schemas))) { 5437 1212 6649
225 5437 I32 kl; const char *k = hv_iterkey(he, &kl);
226 5437 HV *sh = oa_hv_of(hv_iterval(schemas, he));
227 5437 ▶ 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; 5437 0 5437
228 5437 ▶ SSize_t i, n = all ? av_len(all) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = all ? av_len(all) + 1 : 0; 2416 3021 5437
229 10269 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4832 5437 10269
230 4832 SV **e = av_fetch(all, i, 0);
231 4832 ▶ HV *mh = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 4832 0 4832
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 4832 0 4832
232 STRLEN bl;
233 4832 const char *base = mh ? oa_schema_ref_name(aTHX_ oa_get(aTHX_ mh, "$ref"), &bl)
234 4832 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 4832 0 4832
235 SV **slot;
236 AV *lst;
237 4832 ▶ if (!base) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!base) continue; 2416 2416 4832
238 2416 slot = hv_fetch(N->kids, base, (I32)bl, 0);
239 2416 ▶ 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); 1207 1209 2416
if (slot && *slot && oa_av_of(*slot)) lst = oa_av_of(*slot); 1207 0 1207
if (slot && *slot && oa_av_of(*slot)) lst = oa_av_of(*slot); 1207 0 1207
240 else {
241 1209 lst = newAV();
242 1209 (void)hv_store(N->kids, base, (I32)bl, newRV_noinc((SV *)lst), 0);
243 }
244 2416 av_push(lst, newSVpvn(k, (STRLEN)kl));
245 }
246 }
247 1212 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 2439 static void oa_sort_av(pTHX_ AV *av) {
253 2439 SSize_t i, j, n = av_len(av) + 1;
254 4872 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 2433 2439 4872
255 2433 SV *key = SvREFCNT_inc(*av_fetch(av, i, 0));
256 4215 ▶ for (j = i - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = i - 1; j >= 0; j--) { 2433 1782 4215
257 2433 SV **c = av_fetch(av, j, 0);
258 2433 ▶ if (!c || !*c || sv_cmp(*c, key) <= 0) break;
Branch: 3 branches
BranchTrueFalseTotal
if (!c || !*c || sv_cmp(*c, key) <= 0) break; 2433 0 2433
if (!c || !*c || sv_cmp(*c, key) <= 0) break; 2433 0 2433
if (!c || !*c || sv_cmp(*c, key) <= 0) break; 1782 651 2433
259 1782 (void)av_store(av, j + 1, SvREFCNT_inc(*c));
260 }
261 2433 (void)av_store(av, j + 1, key);
262 }
263 2439 }
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 2445 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 2445 AV *keys = (AV *)sv_2mortal((SV *)newAV());
280 2445 AV *refs = (AV *)sv_2mortal((SV *)newAV());
281 2445 HV *map = oa_hv_of(oa_get(aTHX_ dh, "mapping"));
282 SSize_t i, n;
283  
284 2445 *from_union = 0;
285  
286 2462 ▶ if (map && HvUSEDKEYS(map)) {
Branch: 3 branches
BranchTrueFalseTotal
if (map && HvUSEDKEYS(map)) { 17 2428 2445
if (map && HvUSEDKEYS(map)) { 0 17 17
if (map && HvUSEDKEYS(map)) { 17 0 17
287 17 AV *mk = (AV *)sv_2mortal((SV *)newAV());
288 HE *he;
289 17 hv_iterinit(map);
290 49 ▶ while ((he = hv_iternext(map))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(map))) { 32 17 49
291 32 I32 kl; const char *k = hv_iterkey(he, &kl);
292 32 av_push(mk, newSVpvn(k, (STRLEN)kl));
293 }
294 17 oa_sort_av(aTHX_ mk);
295 17 n = av_len(mk) + 1;
296 49 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 32 17 49
297 32 SV *k = *av_fetch(mk, i, 0);
298 32 STRLEN kl; const char *kp = SvPV_const(k, kl);
299 32 SV **v = hv_fetch(map, kp, (I32)kl, 0);
300 32 ▶ SV *ref = (v && *v) ? oa_disc_ref(aTHX_ *v) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *ref = (v && *v) ? oa_disc_ref(aTHX_ *v) : NULL; 32 0 32
SV *ref = (v && *v) ? oa_disc_ref(aTHX_ *v) : NULL; 32 0 32
301 32 ▶ if (!ref) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ref) continue; 0 32 32
302 32 av_push(keys, newSVsv(k));
303 32 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 2428 AV *un = oa_av_of(oa_get(aTHX_ src, "oneOf"));
310 2428 ▶ 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")); 2424 4 2428
311 2428 ▶ if (un && (n = av_len(un) + 1) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (un && (n = av_len(un) + 1) > 0) { 4 2424 2428
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 2428 ▶ if (!*from_union && nm && nml) {
Branch: 3 branches
BranchTrueFalseTotal
if (!*from_union && nm && nml) { 2426 2 2428
if (!*from_union && nm && nml) { 2424 2 2426
if (!*from_union && nm && nml) { 2424 0 2424
328 2424 HV *kids = oa_norm_kids(aTHX_ N);
329 2424 SV **slot = hv_fetch(kids, nm, (I32)nml, 0);
330 2424 ▶ AV *lst = (slot && *slot) ? oa_av_of(*slot) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
AV *lst = (slot && *slot) ? oa_av_of(*slot) : NULL; 2418 6 2424
AV *lst = (slot && *slot) ? oa_av_of(*slot) : NULL; 2418 0 2418
331 2424 ▶ if (lst) {
Branch: 1 branch
BranchTrueFalseTotal
if (lst) { 2418 6 2424
332 2418 AV *sorted = (AV *)sv_2mortal((SV *)newAV());
333 2418 n = av_len(lst) + 1;
334 7250 ▶ 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))); 4832 2418 7250
335 2418 oa_sort_av(aTHX_ sorted);
336 7250 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4832 2418 7250
337 4832 SV *c = *av_fetch(sorted, i, 0);
338 4832 STRLEN cl; const char *cp = SvPV_const(c, cl);
339 4832 SV *ref = newSVpvn(OA_SCHEMA_PFX, OA_SCHEMA_PFX_LEN);
340 4832 sv_catpvn(ref, cp, cl);
341 4832 av_push(keys, newSVsv(c));
342 4832 av_push(refs, ref);
343 }
344 }
345 }
346 }
347  
348 2445 ▶ if (av_len(keys) < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(keys) < 0) return 0; 8 2437 2445
349 2437 *keys_out = keys;
350 2437 *refs_out = refs;
351 2437 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 4867 static SV *oa_disc_then(pTHX_ oa_norm *N, SV *ref, const char *nm, STRLEN nml,
363 int depth) {
364 4867 STRLEN tl = 0;
365 4867 const char *tn = NULL;
366 4867 SV *target = NULL;
367 4867 AV *all = NULL;
368 4867 SSize_t i, n = 0;
369 4867 int backref = 0;
370  
371 4867 ▶ 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 4855 12 4867
if (nm && nml && (tn = oa_schema_ref_name(aTHX_ ref, &tl)) != NULL 4855 0 4855
if (nm && nml && (tn = oa_schema_ref_name(aTHX_ ref, &tl)) != NULL 4855 0 4855
372 4855 ▶ && (target = oa_named_schema(aTHX_ N, tn, tl)) != NULL
Branch: 1 branch
BranchTrueFalseTotal
&& (target = oa_named_schema(aTHX_ N, tn, tl)) != NULL 4855 0 4855
373 4855 ▶ && (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) { 4840 15 4855
374 4840 n = av_len(all) + 1;
375 4840 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4840 0 4840
376 4840 SV **e = av_fetch(all, i, 0);
377 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
378 STRLEN bl;
379 4840 const char *bn = mh
380 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
381 4840 ▶ 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; } 4840 0 4840
if (bn && bl == nml && memEQ(bn, nm, nml)) { backref = 1; break; } 4840 0 4840
if (bn && bl == nml && memEQ(bn, nm, nml)) { backref = 1; break; } 4840 0 4840
382 }
383 }
384  
385 4867 ▶ if (!backref) { /* no loop to break: name the target */
Branch: 1 branch
BranchTrueFalseTotal
if (!backref) { /* no loop to break: name the target */ 27 4840 4867
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 4840 SV *conv = oa_conv_schema(aTHX_ N, target, depth + 1, tn, tl);
396 4840 HV *out = (HV *)SvRV(conv);
397 4840 AV *ca = oa_av_of(oa_get(aTHX_ out, "allOf"));
398 AV *keep;
399 4840 ▶ if (!ca) return conv;
Branch: 1 branch
BranchTrueFalseTotal
if (!ca) return conv; 0 4840 4840
400 4840 keep = newAV();
401 4840 n = av_len(ca) + 1;
402 14520 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9680 4840 14520
403 9680 SV **e = av_fetch(ca, i, 0);
404 9680 ▶ HV *mh = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 9680 0 9680
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 9680 0 9680
405 STRLEN bl;
406 9680 const char *bn = mh
407 9680 ▶ ? 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; 9680 0 9680
408 9680 ▶ if (bn && bl == nml && memEQ(bn, nm, nml)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (bn && bl == nml && memEQ(bn, nm, nml)) continue; 4840 4840 9680
if (bn && bl == nml && memEQ(bn, nm, nml)) continue; 4840 0 4840
if (bn && bl == nml && memEQ(bn, nm, nml)) continue; 4840 0 4840
409 4840 ▶ av_push(keep, (e && *e) ? SvREFCNT_inc(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(keep, (e && *e) ? SvREFCNT_inc(*e) : newSV(0)); 4840 0 4840
av_push(keep, (e && *e) ? SvREFCNT_inc(*e) : newSV(0)); 4840 0 4840
410 }
411 4840 ▶ if (av_len(keep) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(keep) >= 0) 4840 0 4840
412 4840 (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 4840 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 7304 static SV *oa_disc_probe(pTHX_ SV *pn, SV *inner) {
425 7304 HV *out = newHV(), *props = newHV();
426 7304 AV *req = newAV();
427 7304 av_push(req, newSVsv(pn));
428 7304 (void)hv_store(props, SvPVX_const(pn), (I32)SvCUR(pn), inner, 0);
429 7304 (void)hv_stores(out, "required", newRV_noinc((SV *)req));
430 7304 (void)hv_stores(out, "properties", newRV_noinc((SV *)props));
431 7304 return newRV_noinc((SV *)out);
432 }
433  
434 /* ---- a Schema Object ------------------------------------------------------- */
435  
436 149686 static SV *oa_conv_schema(pTHX_ oa_norm *N, SV *schema, int depth,
437 const char *nm, STRLEN nml) {
438 149686 HV *src = oa_hv_of(schema), *dst;
439 HE *he;
440 SV *nullable, *ref, *type, *mn, *mx, *exmn, *exmx, *items, *fmt;
441 149686 SV *disc, *pn = NULL;
442 149686 HV *dh = NULL;
443 149686 AV *dkeys = NULL, *drefs = NULL;
444 int has_ref, is_nullable, tuple;
445 int exmn_bool, exmx_bool, exmn_on, exmx_on;
446 149686 int expand = 0, from_union = 0;
447  
448 /* booleans, arrays and junk are not objects to rewrite: share them */
449 149686 ▶ if (!src || depth >= OA_CONV_MAX_DEPTH) return newSVsv(schema);
Branch: 2 branches
BranchTrueFalseTotal
if (!src || depth >= OA_CONV_MAX_DEPTH) return newSVsv(schema); 149421 265 149686
if (!src || depth >= OA_CONV_MAX_DEPTH) return newSVsv(schema); 0 149421 149421
450  
451 149421 ▶ nullable = N->v30 ? oa_get(aTHX_ src, "nullable") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
nullable = N->v30 ? oa_get(aTHX_ src, "nullable") : NULL; 112008 37413 149421
452 149421 ref = oa_get(aTHX_ src, "$ref");
453 149421 type = oa_get(aTHX_ src, "type");
454 149421 mn = oa_get(aTHX_ src, "minimum");
455 149421 mx = oa_get(aTHX_ src, "maximum");
456 149421 exmn = oa_get(aTHX_ src, "exclusiveMinimum");
457 149421 exmx = oa_get(aTHX_ src, "exclusiveMaximum");
458 149421 items = oa_get(aTHX_ src, "items");
459 149421 fmt = oa_get(aTHX_ src, "format");
460 149421 disc = oa_get(aTHX_ src, "discriminator");
461  
462 149421 ▶ has_ref = ref && !SvROK(ref);
Branch: 2 branches
BranchTrueFalseTotal
has_ref = ref && !SvROK(ref); 32649 116772 149421
has_ref = ref && !SvROK(ref); 32649 0 32649
463 149421 is_nullable = oa_sv_truthy(aTHX_ nullable);
464  
465 /* the 3.0 boolean form of the exclusive bounds, and only that form */
466 149421 ▶ 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); 112008 37413 149421
exmn_bool = N->v30 && exmn && oa_sv_is_json_bool(aTHX_ exmn); 2435 109573 112008
exmn_bool = N->v30 && exmn && oa_sv_is_json_bool(aTHX_ exmn); 2425 10 2435
467 149421 ▶ 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); 112008 37413 149421
exmx_bool = N->v30 && exmx && oa_sv_is_json_bool(aTHX_ exmx); 891 111117 112008
exmx_bool = N->v30 && exmx && oa_sv_is_json_bool(aTHX_ exmx); 887 4 891
468 149421 ▶ 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; 2425 146996 149421
exmn_on = exmn_bool && oa_sv_truthy(aTHX_ exmn) && mn != NULL; 2423 2 2425
exmn_on = exmn_bool && oa_sv_truthy(aTHX_ exmn) && mn != NULL; 2422 1 2423
469 149421 ▶ 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; 887 148534 149421
exmx_on = exmx_bool && oa_sv_truthy(aTHX_ exmx) && mx != NULL; 3 884 887
exmx_on = exmx_bool && oa_sv_truthy(aTHX_ exmx) && mx != NULL; 3 0 3
470  
471 /* draft-04 tuple `items` (not legal 3.0, but specs carry the habit) */
472 149421 ▶ 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"); 112008 37413 149421
tuple = N->v30 && items && oa_av_of(items) && !oa_get(aTHX_ src, "prefixItems"); 13508 98500 112008
tuple = N->v30 && items && oa_av_of(items) && !oa_get(aTHX_ src, "prefixItems"); 1816 11692 13508
tuple = N->v30 && items && oa_av_of(items) && !oa_get(aTHX_ src, "prefixItems"); 1816 0 1816
473  
474 /* discriminator: expandable in either version */
475 149421 ▶ dh = disc ? oa_hv_of(disc) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
dh = disc ? oa_hv_of(disc) : NULL; 2447 146974 149421
476 149421 ▶ pn = dh ? oa_get(aTHX_ dh, "propertyName") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
pn = dh ? oa_get(aTHX_ dh, "propertyName") : NULL; 2447 146974 149421
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 149421 SV *xml = oa_get(aTHX_ src, "xml");
482 149421 ▶ HV *xh = xml ? oa_hv_of(xml) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *xh = xml ? oa_hv_of(xml) : NULL; 1216 148205 149421
483 149421 ▶ SV *ns = xh ? oa_get(aTHX_ xh, "namespace") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *ns = xh ? oa_get(aTHX_ xh, "namespace") : NULL; 1216 148205 149421
484 149421 ▶ if (ns && !oa_looks_like_url(aTHX_ ns)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ns && !oa_looks_like_url(aTHX_ ns)) { 1211 148210 149421
if (ns && !oa_looks_like_url(aTHX_ ns)) { 2 1209 1211
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 149419 ▶ if (dh && (!pn || SvROK(pn)))
Branch: 3 branches
BranchTrueFalseTotal
if (dh && (!pn || SvROK(pn))) 2447 146972 149419
if (dh && (!pn || SvROK(pn))) 2445 2 2447
if (dh && (!pn || SvROK(pn))) 0 2445 2445
496 2 croak("Open::API: a discriminator has no 'propertyName' "
497 "(it is required)");
498 149417 ▶ if (pn && !SvROK(pn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pn && !SvROK(pn)) { 2445 146972 149417
if (pn && !SvROK(pn)) { 2445 0 2445
499 STRLEN pl;
500 (void)SvPV_const(pn, pl); /* force POK: SvPVX/SvCUR are used below */
501 2445 ▶ 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, 2445 0 2445
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 2445 ▶ if (!expand && !nm && !oa_get(aTHX_ src, "oneOf")
Branch: 3 branches
BranchTrueFalseTotal
if (!expand && !nm && !oa_get(aTHX_ src, "oneOf") 8 2437 2445
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 149415 dst = newHV();
524 149415 hv_iterinit(src);
525 402426 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 253011 149415 402426
526 253011 I32 i32kl; const char *k = hv_iterkey(he, &i32kl);
527 253011 SV *v = hv_iterval(src, he);
528 253011 STRLEN kl = (STRLEN)i32kl;
529  
530 /* `nullable` never survives: it is not a 2020-12 keyword */
531 317188 ▶ if (N->v30 && kl == 8 && memEQ(k, "nullable", 8)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 8 && memEQ(k, "nullable", 8)) continue; 197603 55408 253011
if (N->v30 && kl == 8 && memEQ(k, "nullable", 8)) continue; 15411 182192 197603
if (N->v30 && kl == 8 && memEQ(k, "nullable", 8)) continue; 1829 13582 15411
532  
533 /* beside a $ref, assertions are ignored under 3.0 */
534 251182 ▶ 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; 195774 55408 251182
if (N->v30 && has_ref && !oa_ref_sibling_ok(k, kl)) continue; 26048 169726 195774
if (N->v30 && has_ref && !oa_ref_sibling_ok(k, kl)) continue; 3 26045 26048
535  
536 /* $ref itself: the nullable form rebuilds it as an anyOf below */
537 251179 ▶ if (kl == 4 && memEQ(k, "$ref", 4)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "$ref", 4)) { 135130 116049 251179
if (kl == 4 && memEQ(k, "$ref", 4)) { 32649 102481 135130
538 32649 ▶ if (!(has_ref && is_nullable))
Branch: 2 branches
BranchTrueFalseTotal
if (!(has_ref && is_nullable)) 32649 0 32649
if (!(has_ref && is_nullable)) 32044 605 32649
539 32044 (void)hv_store(dst, k, i32kl, newSVsv(v), 0);
540 32649 continue;
541 }
542  
543 /* the union the discriminator replaces */
544 218530 ▶ if (expand && from_union
Branch: 2 branches
BranchTrueFalseTotal
if (expand && from_union 10932 207598 218530
if (expand && from_union 4 10928 10932
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 218528 ▶ if (expand && kl == 13 && memEQ(k, "discriminator", 13)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (expand && kl == 13 && memEQ(k, "discriminator", 13)) continue; 10930 207598 218528
if (expand && kl == 13 && memEQ(k, "discriminator", 13)) continue; 2437 8493 10930
if (expand && kl == 13 && memEQ(k, "discriminator", 13)) continue; 2437 0 2437
550  
551 /* previously generated members: dropped, then rebuilt */
552 216091 ▶ if (expand && kl == 5 && memEQ(k, "allOf", 5)) {
Branch: 3 branches
BranchTrueFalseTotal
if (expand && kl == 5 && memEQ(k, "allOf", 5)) { 8493 207598 216091
if (expand && kl == 5 && memEQ(k, "allOf", 5)) { 19 8474 8493
if (expand && kl == 5 && memEQ(k, "allOf", 5)) { 7 12 19
553 7 AV *keep = newAV();
554 7 AV *av = oa_av_of(v);
555 7 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 7 0 7
556 26 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 19 7 26
557 19 SV **e = av_fetch(av, i, 0);
558 19 ▶ HV *mh = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 19 0 19
HV *mh = (e && *e) ? oa_hv_of(*e) : NULL; 19 0 19
559 19 ▶ 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; 19 0 19
if (mh && hv_exists(mh, OA_DISC_MARK, OA_DISC_MARK_LEN)) continue; 19 0 19
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 7 (void)hv_stores(dst, "allOf", newRV_noinc((SV *)keep));
564 7 continue;
565 }
566  
567 /* numeric bounds: the boolean exclusive* form folds into the number */
568 216084 ▶ if (N->v30 && kl == 7 && memEQ(k, "minimum", 7)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 7 && memEQ(k, "minimum", 7)) { 170033 46051 216084
if (N->v30 && kl == 7 && memEQ(k, "minimum", 7)) { 15149 154884 170033
if (N->v30 && kl == 7 && memEQ(k, "minimum", 7)) { 4248 10901 15149
569 4248 ▶ 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); 1826 2422 4248
570 4248 continue;
571 }
572 211836 ▶ if (N->v30 && kl == 7 && memEQ(k, "maximum", 7)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 7 && memEQ(k, "maximum", 7)) { 165785 46051 211836
if (N->v30 && kl == 7 && memEQ(k, "maximum", 7)) { 10901 154884 165785
if (N->v30 && kl == 7 && memEQ(k, "maximum", 7)) { 2707 8194 10901
573 2707 ▶ 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); 2704 3 2707
574 2707 continue;
575 }
576 209129 ▶ if (kl == 16 && memEQ(k, "exclusiveMinimum", 16)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 16 && memEQ(k, "exclusiveMinimum", 16)) { 3417 205712 209129
if (kl == 16 && memEQ(k, "exclusiveMinimum", 16)) { 2457 960 3417
577 2457 ▶ 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); 32 2425 2457
578 2425 ▶ 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); 2422 3 2425
579 2457 continue; /* `true` with no minimum, or `false`: meaningless */
580 }
581 206672 ▶ if (kl == 16 && memEQ(k, "exclusiveMaximum", 16)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 16 && memEQ(k, "exclusiveMaximum", 16)) { 960 205712 206672
if (kl == 16 && memEQ(k, "exclusiveMaximum", 16)) { 927 33 960
582 927 ▶ 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 887 927
583 887 ▶ 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); 3 884 887
584 927 continue;
585 }
586  
587 /* tuple items -> prefixItems, and additionalItems takes over `items` */
588 205745 ▶ if (kl == 5 && memEQ(k, "items", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 5 && memEQ(k, "items", 5)) { 25399 180346 205745
if (kl == 5 && memEQ(k, "items", 5)) { 15116 10283 25399
589 15116 ▶ if (tuple)
Branch: 1 branch
BranchTrueFalseTotal
if (tuple) 1816 13300 15116
590 1816 (void)hv_stores(dst, "prefixItems",
591 oa_conv_schema_list(aTHX_ N, v, depth));
592 else
593 13300 (void)hv_store(dst, k, i32kl,
594 oa_conv_schema(aTHX_ N, v, depth + 1, NULL, 0), 0);
595 15116 continue;
596 }
597 190629 ▶ if (N->v30 && kl == 15 && memEQ(k, "additionalItems", 15)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 15 && memEQ(k, "additionalItems", 15)) { 146244 44385 190629
if (N->v30 && kl == 15 && memEQ(k, "additionalItems", 15)) { 610 145634 146244
if (N->v30 && kl == 15 && memEQ(k, "additionalItems", 15)) { 605 5 610
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 190024 ▶ if (N->v30 && kl == 7 && memEQ(k, "example", 7)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && kl == 7 && memEQ(k, "example", 7)) { 145639 44385 190024
if (N->v30 && kl == 7 && memEQ(k, "example", 7)) { 8194 137445 145639
if (N->v30 && kl == 7 && memEQ(k, "example", 7)) { 3019 5175 8194
607 3019 ▶ if (!oa_get(aTHX_ src, "examples")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ src, "examples")) { 3019 0 3019
608 3019 AV *ex = newAV();
609 3019 av_push(ex, newSVsv(v));
610 3019 (void)hv_stores(dst, "examples", newRV_noinc((SV *)ex));
611 }
612 3019 continue;
613 }
614  
615 187005 ▶ if (oa_kw_schema(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_kw_schema(k, kl)) { 1406 185599 187005
616 1406 (void)hv_store(dst, k, i32kl,
617 oa_conv_schema(aTHX_ N, v, depth + 1, NULL, 0), 0);
618 185599 ▶ } else if (oa_kw_schema_map(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (oa_kw_schema_map(k, kl)) { 20026 165573 185599
619 20026 (void)hv_store(dst, k, i32kl, oa_conv_schema_map(aTHX_ N, v, depth), 0);
620 165573 ▶ } else if (oa_kw_schema_list(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (oa_kw_schema_list(k, kl)) { 10263 155310 165573
621 10263 (void)hv_store(dst, k, i32kl, oa_conv_schema_list(aTHX_ N, v, depth), 0);
622 } else {
623 155310 (void)hv_store(dst, k, i32kl, newSVsv(v), 0); /* value or annotation */
624 }
625 }
626  
627 /* ---- rules that add keys ---- */
628  
629 149415 ▶ if (is_nullable) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_nullable) { 1828 147587 149415
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 149415 ▶ if (N->v30 && fmt && !SvROK(fmt)) {
Branch: 3 branches
BranchTrueFalseTotal
if (N->v30 && fmt && !SvROK(fmt)) { 112008 37407 149415
if (N->v30 && fmt && !SvROK(fmt)) { 10500 101508 112008
if (N->v30 && fmt && !SvROK(fmt)) { 10500 0 10500
684 10500 STRLEN l; const char *p = SvPV_const(fmt, l);
685 10500 ▶ 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")) 1827 8673 10500
if (l == 4 && memEQ(p, "byte", 4) && !oa_get(aTHX_ src, "contentEncoding")) 1820 7 1827
if (l == 4 && memEQ(p, "byte", 4) && !oa_get(aTHX_ src, "contentEncoding")) 1815 5 1820
686 1815 (void)hv_stores(dst, "contentEncoding", newSVpvs("base64"));
687 8685 ▶ else if (l == 6 && memEQ(p, "binary", 6)
Branch: 2 branches
BranchTrueFalseTotal
else if (l == 6 && memEQ(p, "binary", 6) 2464 6221 8685
else if (l == 6 && memEQ(p, "binary", 6) 2416 48 2464
688 2416 ▶ && !oa_get(aTHX_ src, "contentMediaType"))
Branch: 1 branch
BranchTrueFalseTotal
&& !oa_get(aTHX_ src, "contentMediaType")) 2413 3 2416
689 2413 (void)hv_stores(dst, "contentMediaType",
690 newSVpvs("application/octet-stream"));
691 }
692  
693 /* the discriminator chain */
694 149415 ▶ if (expand) {
Branch: 1 branch
BranchTrueFalseTotal
if (expand) { 2437 146978 149415
695 2437 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 2437 HV *d2 = oa_conv_copy_hv(aTHX_ dh);
702 2437 HV *m2 = newHV();
703 7304 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4867 2437 7304
704 4867 SV *k = *av_fetch(dkeys, i, 0);
705 4867 (void)hv_store(m2, SvPVX_const(k), (I32)SvCUR(k),
706 newSVsv(*av_fetch(drefs, i, 0)), 0);
707 }
708 2437 (void)hv_stores(d2, "mapping", newRV_noinc((SV *)m2));
709 2437 (void)hv_stores(dst, "discriminator", newRV_noinc((SV *)d2));
710 }
711  
712 2437 slot = hv_fetchs(dst, "allOf", 0);
713 2437 ▶ 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; 7 2430 2437
chain = (slot && *slot && oa_av_of(*slot)) ? oa_av_of(*slot) : NULL; 7 0 7
chain = (slot && *slot && oa_av_of(*slot)) ? oa_av_of(*slot) : NULL; 7 0 7
714 2437 ▶ if (!chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (!chain) { 2430 7 2437
715 2430 chain = newAV();
716 2430 (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 2437 AV *en = newAV();
723 2437 HV *inner = newHV();
724 SV *probe;
725 7304 ▶ 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))); 4867 2437 7304
726 2437 (void)hv_stores(inner, "enum", newRV_noinc((SV *)en));
727 2437 probe = oa_disc_probe(aTHX_ pn, newRV_noinc((SV *)inner));
728 2437 (void)hv_stores((HV *)SvRV(probe), OA_DISC_MARK, newSVsv(pn));
729 2437 av_push(chain, probe);
730 }
731  
732 7304 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4867 2437 7304
733 4867 HV *branch = newHV(), *inner = newHV();
734 4867 SV *ref_i = *av_fetch(drefs, i, 0);
735 4867 (void)hv_stores(inner, "const", newSVsv(*av_fetch(dkeys, i, 0)));
736 4867 (void)hv_stores(branch, OA_DISC_MARK, newSVsv(pn));
737 4867 (void)hv_stores(branch, "if",
738 oa_disc_probe(aTHX_ pn, newRV_noinc((SV *)inner)));
739 4867 (void)hv_stores(branch, "then",
740 oa_disc_then(aTHX_ N, ref_i, nm, nml, depth));
741 4867 av_push(chain, newRV_noinc((SV *)branch));
742 }
743 }
744  
745 149415 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 27947 static SV *oa_conv_media(pTHX_ oa_norm *N, SV *media) {
760 27947 HV *mh = oa_hv_of(media), *mc;
761 SV *schema, *examples;
762 27947 ▶ if (!mh) return newSVsv(media);
Branch: 1 branch
BranchTrueFalseTotal
if (!mh) return newSVsv(media); 0 27947 27947
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 27947 ▶ 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")) 640 27307 27947
if (oa_get(aTHX_ mh, "example") && oa_get(aTHX_ mh, "examples")) 1 639 640
768 1 croak("Open::API: a media type declares both 'example' and "
769 "'examples', which are mutually exclusive");
770 27946 schema = oa_get(aTHX_ mh, "schema");
771 27946 examples = oa_get(aTHX_ mh, "examples");
772 27946 ▶ if (!schema && !examples) return newSVsv(media);
Branch: 2 branches
BranchTrueFalseTotal
if (!schema && !examples) return newSVsv(media); 1330 26616 27946
if (!schema && !examples) return newSVsv(media); 1330 0 1330
773 26616 mc = oa_conv_copy_hv(aTHX_ mh);
774 26616 sv_2mortal((SV *)mc);
775 26616 ▶ if (schema)
Branch: 1 branch
BranchTrueFalseTotal
if (schema) 26616 0 26616
776 26616 (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 26610 ▶ if (examples)
Branch: 1 branch
BranchTrueFalseTotal
if (examples) 9 26601 26610
779 9 (void)hv_stores(mc, "examples", oa_conv_map(aTHX_ N, examples, oa_conv_example));
780 26608 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 22284 static SV *oa_conv_content(pTHX_ oa_norm *N, SV *content) {
789 22284 HV *ch = oa_hv_of(content);
790 22284 ▶ if (ch) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch) { 22284 0 22284
791 HE *he;
792 22284 hv_iterinit(ch);
793 50231 ▶ while ((he = hv_iternext(ch))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(ch))) { 27951 22280 50231
794 27951 I32 kl; const char *k = hv_iterkey(he, &kl);
795 27951 HV *mh = oa_hv_of(hv_iterval(ch, he));
796 27951 ▶ 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; 27951 0 27951
797 HV *props;
798 HE *ehe;
799 27951 ▶ if (!enc) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) continue; 26718 1233 27951
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 1233 ▶ if (!oa_ctype_is_form(k, (STRLEN)kl)
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_ctype_is_form(k, (STRLEN)kl) 609 624 1233
805 609 ▶ && !oa_ctype_is_multipart(k, (STRLEN)kl))
Branch: 1 branch
BranchTrueFalseTotal
&& !oa_ctype_is_multipart(k, (STRLEN)kl)) 2 607 609
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 1231 props = oa_hv_of(oa_get(aTHX_ mh, "schema"));
812 1231 ▶ 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; 1231 0 1231
813 1231 ▶ if (!props) continue; /* no properties to check against */
Branch: 1 branch
BranchTrueFalseTotal
if (!props) continue; /* no properties to check against */ 0 1231 1231
814 1231 hv_iterinit(enc);
815 2460 ▶ while ((ehe = hv_iternext(enc))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ehe = hv_iternext(enc))) { 1231 1229 2460
816 1231 I32 el; const char *e = hv_iterkey(ehe, &el);
817 1231 ▶ if (!hv_exists(props, e, el))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(props, e, el)) 2 1229 1231
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 22280 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 4291 static SV *oa_conv_deref_only(pTHX_ oa_norm *N, SV *sv,
877 const char *const *sections) {
878 4291 HV *h = oa_hv_of(sv), *c;
879 4291 ▶ if (!h) return newSVsv(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(sv); 0 4291 4291
880 4291 c = oa_deref_object(aTHX_ N, h, sections);
881 4291 ▶ return c ? newRV_noinc((SV *)c) : newSVsv(sv);
Branch: 1 branch
BranchTrueFalseTotal
return c ? newRV_noinc((SV *)c) : newSVsv(sv); 4 4287 4291
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 1236 static SV *oa_conv_header(pTHX_ oa_norm *N, SV *hv) {
901 1236 HV *hh = oa_hv_of(hv);
902 SV *st;
903 1236 ▶ 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"))) 1236 0 1236
if (hh && (oa_get(aTHX_ hh, "name") || oa_get(aTHX_ hh, "in"))) 1235 1 1236
if (hh && (oa_get(aTHX_ hh, "name") || oa_get(aTHX_ hh, "in"))) 0 1235 1235
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 1235 ▶ st = hh ? oa_get(aTHX_ hh, "style") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
st = hh ? oa_get(aTHX_ hh, "style") : NULL; 1235 0 1235
910 1235 ▶ if (st && !SvROK(st)) {
Branch: 2 branches
BranchTrueFalseTotal
if (st && !SvROK(st)) { 3 1232 1235
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 1233 return oa_conv_param(aTHX_ N, hv);
917 }
918 4271 static SV *oa_conv_scheme(pTHX_ oa_norm *N, SV *s) {
919 4271 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 3074 static SV *oa_component_target(pTHX_ oa_norm *N, SV *ref,
928 const char *const *sections) {
929 static const char pfx[] = "#/components/";
930 3074 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 3074 int i, hit = -1;
936  
937 3074 ▶ if (!ref || SvROK(ref) || !SvOK(ref)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 3074 0 3074
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 3074 0 3074
if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; 0 3074 3074
938 3074 p = SvPV_const(ref, l);
939 3074 ▶ if (l <= pfxl || !memEQ(p, pfx, pfxl)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (l <= pfxl || !memEQ(p, pfx, pfxl)) return NULL; 3073 1 3074
if (l <= pfxl || !memEQ(p, pfx, pfxl)) return NULL; 3 3070 3073
940  
941 3070 sec = p + pfxl;
942 3070 slash = (const char *)memchr(sec, '/', l - pfxl);
943 3070 ▶ if (!slash) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!slash) return NULL; 0 3070 3070
944 3070 sl = (STRLEN)(slash - sec);
945 3070 nm = slash + 1;
946 3070 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 3070 ▶ if (!nl || memchr(nm, '/', nl)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!nl || memchr(nm, '/', nl)) return NULL; 3070 0 3070
if (!nl || memchr(nm, '/', nl)) return NULL; 3 3067 3070
950  
951 3071 ▶ for (i = 0; sections[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; sections[i]; i++) { 3071 0 3071
952 3071 STRLEN wl = (STRLEN)strlen(sections[i]);
953 3071 ▶ 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; } 3067 4 3071
if (wl == sl && memEQ(sec, sections[i], sl)) { hit = i; break; } 3067 0 3067
954 }
955 3067 ▶ if (hit < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (hit < 0) return NULL; 0 3067 3067
956  
957 3067 ▶ 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; 3067 0 3067
958 3067 ▶ 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; 3067 0 3067
959 3067 ▶ e = bucket ? hv_fetch(bucket, nm, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
e = bucket ? hv_fetch(bucket, nm, (I32)nl, 0) : NULL; 3064 3 3067
960 3067 ▶ return (e && *e && oa_hv_of(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 3061 6 3067
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 3061 0 3061
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 3061 0 3061
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 66625 static HV *oa_deref_object(pTHX_ oa_norm *N, HV *src,
968 const char *const *sections) {
969 66625 HV *cur = src, *out;
970 HE *he;
971 int hops;
972  
973 69686 ▶ for (hops = 0; hops < OA_COMP_MAX_HOPS; hops++) {
Branch: 1 branch
BranchTrueFalseTotal
for (hops = 0; hops < OA_COMP_MAX_HOPS; hops++) { 69684 2 69686
974 69684 SV *ref = oa_get(aTHX_ cur, "$ref");
975 SV *t;
976 HV *target;
977 69684 ▶ if (!ref) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!ref) break; 66610 3074 69684
978 3074 t = oa_component_target(aTHX_ N, ref, sections);
979 3074 ▶ if (!t) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) break; 13 3061 3074
980 3061 target = oa_hv_of(t);
981 3061 ▶ if (!target || target == cur) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!target || target == cur) break; 3061 0 3061
if (!target || target == cur) break; 3061 0 3061
982 3061 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 66625 SV *left = oa_get(aTHX_ cur, "$ref");
997 66625 ▶ if (left) {
Branch: 1 branch
BranchTrueFalseTotal
if (left) { 15 66610 66625
998 15 STRLEN rl; const char *rp = SvPV_const(left, rl);
999 15 croak("Open::API: cannot resolve $ref '%.*s'", (int)rl, rp);
1000 }
1001 }
1002 66610 ▶ if (cur == src) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (cur == src) return NULL; 63565 3045 66610
1003  
1004 3045 out = oa_conv_copy_hv(aTHX_ cur);
1005 3045 hv_iterinit(src);
1006 6092 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 3047 3045 6092
1007 3047 I32 kl; const char *k = hv_iterkey(he, &kl);
1008 3047 ▶ if (kl == 4 && memEQ(k, "$ref", 4)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "$ref", 4)) continue; 3045 2 3047
if (kl == 4 && memEQ(k, "$ref", 4)) continue; 3045 0 3045
1009 2 (void)hv_store(out, k, kl, newSVsv(hv_iterval(src, he)), 0);
1010 }
1011 3045 return out;
1012 }
1013  
1014 /* A Parameter or Header Object: `schema`, or `content` holding one. */
1015 21514 static SV *oa_conv_param(pTHX_ oa_norm *N, SV *p) {
1016 21514 HV *ph = oa_hv_of(p), *pc;
1017 SV *schema, *content;
1018 21514 ▶ if (!ph) return newSVsv(p);
Branch: 1 branch
BranchTrueFalseTotal
if (!ph) return newSVsv(p); 0 21514 21514
1019 21514 pc = oa_deref_object(aTHX_ N, ph, oa_sec_param);
1020 21509 ▶ 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 */ 618 20891 21509
1021 /* `example` and `examples` are mutually exclusive here too, for the same
1022 * reason they are on a Media Type Object */
1023 21509 ▶ 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 21508 21509
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 21508 schema = oa_get(aTHX_ ph, "schema");
1027 21508 content = oa_get(aTHX_ ph, "content");
1028 21508 ▶ if (!schema && !content)
Branch: 2 branches
BranchTrueFalseTotal
if (!schema && !content) 612 20896 21508
if (!schema && !content) 2 610 612
1029 2 ▶ return pc ? newRV_noinc((SV *)pc) : newSVsv(p);
Branch: 1 branch
BranchTrueFalseTotal
return pc ? newRV_noinc((SV *)pc) : newSVsv(p); 0 2 2
1030 21506 ▶ if (!pc) pc = oa_conv_copy_hv(aTHX_ ph);
Branch: 1 branch
BranchTrueFalseTotal
if (!pc) pc = oa_conv_copy_hv(aTHX_ ph); 20888 618 21506
1031 21506 ▶ if (schema) (void)hv_stores(pc, "schema",
Branch: 1 branch
BranchTrueFalseTotal
if (schema) (void)hv_stores(pc, "schema", 20896 610 21506
1032 oa_conv_schema(aTHX_ N, schema, 0, NULL, 0));
1033 21506 ▶ 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 20896 21506
1034 21506 return newRV_noinc((SV *)pc);
1035 }
1036  
1037 13881 static SV *oa_conv_param_list(pTHX_ oa_norm *N, SV *params) {
1038 13881 AV *src = oa_av_of(params), *dst;
1039 SSize_t i, n;
1040 13881 ▶ if (!src) return newSVsv(params);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(params); 0 13881 13881
1041 13881 dst = newAV();
1042 13881 sv_2mortal((SV *)dst); /* see oa_conv_map: oa_conv_param croaks */
1043 13881 n = av_len(src) + 1;
1044 33532 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 19657 13875 33532
1045 19657 SV **e = av_fetch(src, i, 0);
1046 19657 ▶ 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)); 19657 0 19657
av_push(dst, (e && *e) ? oa_conv_param(aTHX_ N, *e) : newSV(0)); 19657 0 19657
1047 }
1048 13875 return newRV_inc((SV *)dst);
1049 }
1050  
1051 7152 static SV *oa_conv_body(pTHX_ oa_norm *N, SV *rb) {
1052 7152 HV *h = oa_hv_of(rb), *c;
1053 SV *content;
1054 7152 ▶ if (!h) return newSVsv(rb);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(rb); 0 7152 7152
1055 7152 c = oa_deref_object(aTHX_ N, h, oa_sec_body);
1056 7147 ▶ if (c) h = c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) h = c; 4 7143 7147
1057 7147 content = oa_get(aTHX_ h, "content");
1058 7147 ▶ if (!content)
Branch: 1 branch
BranchTrueFalseTotal
if (!content) 0 7147 7147
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 7147 ▶ if (!c) c = oa_conv_copy_hv(aTHX_ h);
Branch: 1 branch
BranchTrueFalseTotal
if (!c) c = oa_conv_copy_hv(aTHX_ h); 7143 4 7147
1061 7147 (void)hv_stores(c, "content", oa_conv_content(aTHX_ N, content));
1062 7134 return newRV_noinc((SV *)c);
1063 }
1064  
1065 23405 static SV *oa_conv_response(pTHX_ oa_norm *N, SV *r) {
1066 23405 HV *h = oa_hv_of(r), *c;
1067 SV *content, *hdrs, *links;
1068 23405 ▶ if (!h) return newSVsv(r);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(r); 604 22801 23405
1069 22801 c = oa_deref_object(aTHX_ N, h, oa_sec_resp);
1070 22799 ▶ if (c) h = c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) h = c; 2416 20383 22799
1071 22799 content = oa_get(aTHX_ h, "content");
1072 22799 hdrs = oa_get(aTHX_ h, "headers");
1073 22799 links = oa_get(aTHX_ h, "links");
1074 22799 ▶ if (!content && !hdrs && !links)
Branch: 3 branches
BranchTrueFalseTotal
if (!content && !hdrs && !links) 8272 14527 22799
if (!content && !hdrs && !links) 8254 18 8272
if (!content && !hdrs && !links) 8249 5 8254
1075 8249 ▶ return c ? newRV_noinc((SV *)c) : newSVsv(r);
Branch: 1 branch
BranchTrueFalseTotal
return c ? newRV_noinc((SV *)c) : newSVsv(r); 608 7641 8249
1076 14550 ▶ if (!c) c = oa_conv_copy_hv(aTHX_ h);
Branch: 1 branch
BranchTrueFalseTotal
if (!c) c = oa_conv_copy_hv(aTHX_ h); 12742 1808 14550
1077 14550 ▶ 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)); 14527 23 14550
1078 14550 ▶ if (hdrs) (void)hv_stores(c, "headers",
Branch: 1 branch
BranchTrueFalseTotal
if (hdrs) (void)hv_stores(c, "headers", 632 13918 14550
1079 oa_conv_map(aTHX_ N, hdrs, oa_conv_header));
1080 14547 ▶ if (links) (void)hv_stores(c, "links",
Branch: 1 branch
BranchTrueFalseTotal
if (links) (void)hv_stores(c, "links", 5 14542 14547
1081 oa_conv_map(aTHX_ N, links, oa_conv_link));
1082 14547 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 15327 static SV *oa_conv_operation(pTHX_ oa_norm *N, SV *op) {
1101 15327 HV *h = oa_hv_of(op), *c;
1102 SV *params, *rb, *resps, *cbs;
1103 15327 ▶ if (!h) return newSVsv(op);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(op); 0 15327 15327
1104 15327 params = oa_get(aTHX_ h, "parameters");
1105 15327 rb = oa_get(aTHX_ h, "requestBody");
1106 15327 resps = oa_get(aTHX_ h, "responses");
1107 15327 cbs = oa_get(aTHX_ h, "callbacks");
1108 15327 ▶ if (!params && !rb && !resps && !cbs) return newSVsv(op);
Branch: 4 branches
BranchTrueFalseTotal
if (!params && !rb && !resps && !cbs) return newSVsv(op); 3355 11972 15327
if (!params && !rb && !resps && !cbs) return newSVsv(op); 257 3098 3355
if (!params && !rb && !resps && !cbs) return newSVsv(op); 0 257 257
if (!params && !rb && !resps && !cbs) return newSVsv(op); 0 0 0
1109 15327 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 15327 sv_2mortal((SV *)c);
1114 15327 ▶ 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)); 11972 3355 15327
1115 15321 ▶ 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)); 6541 8780 15321
1116 15303 ▶ if (resps) (void)hv_stores(c, "responses",
Branch: 1 branch
BranchTrueFalseTotal
if (resps) (void)hv_stores(c, "responses", 15303 0 15303
1117 oa_conv_map(aTHX_ N, resps, oa_conv_response));
1118 15298 ▶ if (cbs) (void)hv_stores(c, "callbacks",
Branch: 1 branch
BranchTrueFalseTotal
if (cbs) (void)hv_stores(c, "callbacks", 6 15292 15298
1119 oa_conv_map(aTHX_ N, cbs, oa_conv_callback));
1120 15298 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 10859 static SV *oa_conv_path_item(pTHX_ oa_norm *N, SV *item) {
1128 10859 HV *h = oa_hv_of(item), *c, *d;
1129 SV *params;
1130 int m;
1131 10859 ▶ if (!h) return newSVsv(item);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(item); 0 10859 10859
1132 10859 d = oa_deref_object(aTHX_ N, h, oa_sec_pathitem);
1133 10856 ▶ if (d) h = d;
Branch: 1 branch
BranchTrueFalseTotal
if (d) h = d; 2 10854 10856
1134 10856 ▶ c = d ? d : oa_conv_copy_hv(aTHX_ h);
Branch: 1 branch
BranchTrueFalseTotal
c = d ? d : oa_conv_copy_hv(aTHX_ h); 10854 2 10856
1135 10856 sv_2mortal((SV *)c); /* see oa_conv_operation */
1136 10856 params = oa_get(aTHX_ h, "parameters");
1137 10856 ▶ 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)); 1909 8947 10856
1138 97508 ▶ for (m = 0; oa_methods[m]; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; oa_methods[m]; m++) { 86681 10827 97508
1139 86681 SV *op = oa_get(aTHX_ h, oa_methods[m]);
1140 86681 ▶ 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]), 15327 71354 86681
1141 oa_conv_operation(aTHX_ N, op), 0);
1142 }
1143 10827 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 3728 static SV *oa_conv_schemas(pTHX_ oa_norm *N, SV *schemas) {
1149 3728 HV *src = oa_hv_of(schemas), *dst;
1150 HE *he;
1151 3728 ▶ if (!src) return newSVsv(schemas);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(schemas); 0 3728 3728
1152 3728 dst = newHV();
1153 3728 hv_iterinit(src);
1154 18234 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 14506 3728 18234
1155 14506 I32 kl; const char *k = hv_iterkey(he, &kl);
1156 14506 (void)hv_store(dst, k, kl,
1157 oa_conv_schema(aTHX_ N, hv_iterval(src, he), 0,
1158 k, (STRLEN)kl), 0);
1159 }
1160 3728 return newRV_noinc((SV *)dst);
1161 }
1162  
1163 3795 static SV *oa_conv_components(pTHX_ oa_norm *N, SV *comp) {
1164 3795 HV *h = oa_hv_of(comp), *c;
1165 SV *v;
1166 3795 ▶ if (!h) return newSVsv(comp);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(comp); 0 3795 3795
1167 3795 c = oa_conv_copy_hv(aTHX_ h);
1168 3795 ▶ if ((v = oa_get(aTHX_ h, "schemas")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "schemas"))) 3728 67 3795
1169 3728 (void)hv_stores(c, "schemas", oa_conv_schemas(aTHX_ N, v));
1170 3795 ▶ if ((v = oa_get(aTHX_ h, "parameters")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "parameters"))) 613 3182 3795
1171 613 (void)hv_stores(c, "parameters", oa_conv_map(aTHX_ N, v, oa_conv_param));
1172 3795 ▶ if ((v = oa_get(aTHX_ h, "headers")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "headers"))) 11 3784 3795
1173 11 (void)hv_stores(c, "headers", oa_conv_map(aTHX_ N, v, oa_conv_param));
1174 3795 ▶ if ((v = oa_get(aTHX_ h, "requestBodies")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "requestBodies"))) 611 3184 3795
1175 611 (void)hv_stores(c, "requestBodies", oa_conv_map(aTHX_ N, v, oa_conv_body));
1176 3795 ▶ if ((v = oa_get(aTHX_ h, "responses")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "responses"))) 611 3184 3795
1177 611 (void)hv_stores(c, "responses", oa_conv_map(aTHX_ N, v, oa_conv_response));
1178 3795 ▶ if ((v = oa_get(aTHX_ h, "pathItems")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "pathItems"))) 4 3791 3795
1179 4 (void)hv_stores(c, "pathItems", oa_conv_map(aTHX_ N, v, oa_conv_path_item));
1180 3795 ▶ if ((v = oa_get(aTHX_ h, "examples")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "examples"))) 3 3792 3795
1181 3 (void)hv_stores(c, "examples", oa_conv_map(aTHX_ N, v, oa_conv_example));
1182 3795 ▶ if ((v = oa_get(aTHX_ h, "securitySchemes")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "securitySchemes"))) 645 3150 3795
1183 645 (void)hv_stores(c, "securitySchemes", oa_conv_map(aTHX_ N, v, oa_conv_scheme));
1184 3795 ▶ if ((v = oa_get(aTHX_ h, "callbacks")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "callbacks"))) 2 3793 3795
1185 2 (void)hv_stores(c, "callbacks", oa_conv_map(aTHX_ N, v, oa_conv_callback));
1186 3795 ▶ if ((v = oa_get(aTHX_ h, "links")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "links"))) 2 3793 3795
1187 2 (void)hv_stores(c, "links", oa_conv_map(aTHX_ N, v, oa_conv_link));
1188 3795 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 5786 static SV *oa_normalize(pTHX_ SV *doc, int v30) {
1194 5786 HV *h = oa_hv_of(doc), *c;
1195 SV *v;
1196 oa_norm N;
1197 5786 ▶ if (!h) return newSVsv(doc);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSVsv(doc); 0 5786 5786
1198 5786 N.v30 = v30;
1199 5786 N.doc = h;
1200 5786 N.kids = NULL;
1201 5786 c = oa_conv_copy_hv(aTHX_ h);
1202 5786 ▶ if ((v = oa_get(aTHX_ h, "paths")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "paths"))) 5786 0 5786
1203 5786 (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 5754 ▶ if ((v = oa_get(aTHX_ h, "webhooks")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "webhooks"))) 4 5750 5754
1210 4 (void)hv_stores(c, "webhooks", oa_conv_map(aTHX_ &N, v, oa_conv_path_item));
1211 5754 ▶ if ((v = oa_get(aTHX_ h, "components")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "components"))) 3795 1959 5754
1212 3795 (void)hv_stores(c, "components", oa_conv_components(aTHX_ &N, v));
1213 5754 ▶ if (N.kids) SvREFCNT_dec((SV *)N.kids);
Branch: 1 branch
BranchTrueFalseTotal
if (N.kids) SvREFCNT_dec((SV *)N.kids); 1212 4542 5754
1214 5754 return newRV_noinc((SV *)c);
1215 }
1216  
1217 #endif /* OA_NORMALIZE_H */
« include/oa_swagger2.h ↑ Summary include/oa_route.h »