include/oa_mock.h

filter: Statementstmt 86.1% 416 / 483 Branchbran 51.6% 407 / 788 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 64.7% 823 / 1271 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_client.h ↑ Summary include/oa_abi_impl.h »
1 #ifndef OA_MOCK_H
2 #define OA_MOCK_H
3  
4 #include <math.h>
5  
6 /* Response synthesis: a body for an operation + status, from the document
7 * alone. This is what serves a mock - paste a spec, get a working API - so
8 * two properties outrank everything else here:
9 *
10 * DETERMINISTIC. The same request produces byte-identical bodies, every
11 * time, because a mock that changes under you is useless for tests.
12 * There is no randomness anywhere below - "first" always means the same
13 * member, formats map to fixed strings, numbers sit on their bounds.
14 *
15 * SELF-CONSISTENT. What the generator produces must validate against the
16 * schema it was produced from - the mock may not generate something its
17 * own validator rejects. That is a gate (a property test over whole
18 * specs), not an aspiration, and it is why the generator honours the
19 * constraining keywords (enum, const, minimum, minLength, minItems,
20 * required) rather than emitting the friendliest-looking value.
21 *
22 * The body for a chosen response is, in order:
23 *
24 * 1. the media type's `example`
25 * 2. the media type's named `examples` - the requested name when
26 * `Prefer: example=name` asked for one, the lexicographically first
27 * entry otherwise (hash order would be per-process noise)
28 * 3. the (resolved) schema's `default`
29 * 4. a value generated from the schema
30 *
31 * Status selection: `Prefer: code=404` picks a declared status; without a
32 * preference the lowest declared 2xx wins, then `default` (as 200), then
33 * the lowest declared status. A preferred status the document does not
34 * declare is still answered - with the `default` response's body when one
35 * is declared, empty otherwise - because the point of Prefer is exercising
36 * error handling, and refusing undeclared codes would make the feature
37 * useless against the specs that need it most. */
38  
39 /* generated strings are padded to minLength at most this far - a pasted
40 * spec must not be able to ask a mock for a gigabyte of 'a' */
41 #define OA_MOCK_MAX_PAD 4096
42  
43 /* recursion guard: $ref cycles and pathological nesting synthesize null
44 * past this depth rather than recursing forever */
45 #define OA_MOCK_MAX_DEPTH 32
46  
47 /* How many elements an array gets when the document does not say.
48 *
49 * Not zero, which is what `minItems` absent literally means and which is
50 * a correct answer to the wrong question: a list endpoint that mocks as
51 * [] validates perfectly and tells the caller nothing about the shape of
52 * what they will get. Not one either - a single-element list is a shape
53 * you cannot build a table, a pagination control or an empty-state
54 * against. Five is enough to look like data and small enough to read.
55 *
56 * `minItems` still raises it and `maxItems` still caps it, including
57 * `maxItems: 0` where the document has said it means empty. */
58 #define OA_MOCK_ARRAY_ITEMS 5
59  
60 /* ---- the document, by JSON pointer ---------------------------------------- */
61  
62 /* "#/components/schemas/Pet" -> the node, or NULL. Borrowed. */
63 377 static SV *oa_doc_at(pTHX_ HV *doc, const char *ref, STRLEN rl) {
64 377 SV *node = NULL;
65 377 STRLEN i = 0;
66 377 ▶ if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL; 377 0 377
if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL; 377 0 377
if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL; 377 0 377
if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL; 0 377 377
67 377 i = 2;
68 1508 ▶ while (i <= rl) {
Branch: 1 branch
BranchTrueFalseTotal
while (i <= rl) { 1131 377 1508
69 char seg[256];
70 1131 STRLEN sl = 0, e = i;
71 10339 ▶ while (e < rl && ref[e] != '/') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < rl && ref[e] != '/') e++; 9962 377 10339
while (e < rl && ref[e] != '/') e++; 9208 754 9962
72 /* unescape ~1 -> '/', ~0 -> '~' */
73 10339 ▶ while (i < e && sl + 1 < sizeof seg) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < e && sl + 1 < sizeof seg) { 9208 1131 10339
while (i < e && sl + 1 < sizeof seg) { 9208 0 9208
74 9208 ▶ if (ref[i] == '~' && i + 1 < e && (ref[i+1] == '0' || ref[i+1] == '1')) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref[i] == '~' && i + 1 < e && (ref[i+1] == '0' || ref[i+1] == '1')) { 0 9208 9208
if (ref[i] == '~' && i + 1 < e && (ref[i+1] == '0' || ref[i+1] == '1')) { 0 0 0
if (ref[i] == '~' && i + 1 < e && (ref[i+1] == '0' || ref[i+1] == '1')) { 0 0 0
if (ref[i] == '~' && i + 1 < e && (ref[i+1] == '0' || ref[i+1] == '1')) { 0 0 0
75 0 ▶ seg[sl++] = ref[i+1] == '1' ? '/' : '~';
Branch: 1 branch
BranchTrueFalseTotal
seg[sl++] = ref[i+1] == '1' ? '/' : '~'; 0 0 0
76 0 i += 2;
77 9208 } else seg[sl++] = ref[i++];
78 }
79 1131 ▶ if (!node) {
Branch: 1 branch
BranchTrueFalseTotal
if (!node) { 377 754 1131
80 377 SV **v = hv_fetch(doc, seg, (I32)sl, 0);
81 377 ▶ node = (v && *v) ? *v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
node = (v && *v) ? *v : NULL; 377 0 377
node = (v && *v) ? *v : NULL; 377 0 377
82 754 ▶ } else if (oa_hv_of(node)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (oa_hv_of(node)) { 754 0 754
83 754 SV **v = hv_fetch(oa_hv_of(node), seg, (I32)sl, 0);
84 754 ▶ node = (v && *v) ? *v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
node = (v && *v) ? *v : NULL; 754 0 754
node = (v && *v) ? *v : NULL; 754 0 754
85 0 ▶ } else if (oa_av_of(node)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (oa_av_of(node)) { 0 0 0
86 0 char *end = NULL;
87 0 long ix = strtol(seg, &end, 10);
88 SV **v;
89 0 ▶ if (!end || *end || ix < 0) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!end || *end || ix < 0) return NULL; 0 0 0
if (!end || *end || ix < 0) return NULL; 0 0 0
if (!end || *end || ix < 0) return NULL; 0 0 0
90 0 v = av_fetch(oa_av_of(node), (SSize_t)ix, 0);
91 0 ▶ node = (v && *v) ? *v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
node = (v && *v) ? *v : NULL; 0 0 0
node = (v && *v) ? *v : NULL; 0 0 0
92 0 } else return NULL;
93 1131 ▶ if (!node) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!node) return NULL; 0 1131 1131
94 1131 i = e + 1;
95 }
96 377 return node;
97 }
98  
99 /* follow $ref until a concrete schema (or the depth guard). Borrowed. */
100 3147 static SV *oa_mock_deref(pTHX_ HV *doc, SV *schema, int depth) {
101 3147 int hops = 0;
102 3520 ▶ while (schema && hops++ < OA_MOCK_MAX_DEPTH) {
Branch: 2 branches
BranchTrueFalseTotal
while (schema && hops++ < OA_MOCK_MAX_DEPTH) { 3520 0 3520
while (schema && hops++ < OA_MOCK_MAX_DEPTH) { 3520 0 3520
103 3520 HV *h = oa_hv_of(schema);
104 3520 ▶ SV *r = h ? oa_get(aTHX_ h, "$ref") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *r = h ? oa_get(aTHX_ h, "$ref") : NULL; 3520 0 3520
105 STRLEN rl; const char *rp;
106 3520 ▶ if (!r || SvROK(r)) return schema;
Branch: 2 branches
BranchTrueFalseTotal
if (!r || SvROK(r)) return schema; 373 3147 3520
if (!r || SvROK(r)) return schema; 0 373 373
107 373 rp = SvPV_const(r, rl);
108 373 schema = oa_doc_at(aTHX_ doc, rp, rl);
109 }
110 PERL_UNUSED_VAR(depth);
111 0 return schema;
112 }
113  
114 /* ---- generation ------------------------------------------------------------ */
115  
116 /* `caps` is the request's path captures, or NULL. A generated object
117 * prefers a captured value for a property it names - see oa_gen_echo -
118 * so `GET /pets/42` answers with id 42 rather than with the schema's
119 * placeholder. It is threaded through rather than consulted globally
120 * because a mock that echoed at every depth would put the pet's id on
121 * the owner and the vet as well. */
122 static SV *oa_gen(pTHX_ HV *doc, SV *schema, HV *caps, int depth); /* forward */
123 static SV *oa_gen_type_of(pTHX_ HV *h); /* forward */
124  
125 /* Does `val` (always a string, off the wire) satisfy this schema well
126 * enough to answer with? Returns the value to use - coerced to the
127 * declared type - or NULL to generate instead.
128 *
129 * Conservative on purpose. The mock's contract is that it cannot
130 * produce something its own validator would reject, and this is the one
131 * place a value arrives that the document did not write. So anything
132 * this cannot check, it refuses: a `pattern` is not evaluated here, and
133 * its presence alone sends the value back to the generator. */
134 6 static SV *oa_gen_fits(pTHX_ HV *h, SV *val) {
135 SV *t;
136 STRLEN tl, vl;
137 const char *tp, *vp;
138 SV *e;
139  
140 6 ▶ if (!h || !val || !SvOK(val)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!h || !val || !SvOK(val)) return NULL; 6 0 6
if (!h || !val || !SvOK(val)) return NULL; 6 0 6
if (!h || !val || !SvOK(val)) return NULL; 0 6 6
141 6 vp = SvPV_const(val, vl);
142 6 ▶ if (!vl) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!vl) return NULL; 0 6 6
143  
144 /* a regex we are not going to run is a rule we cannot honour */
145 6 ▶ if (oa_get(aTHX_ h, "pattern")) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_get(aTHX_ h, "pattern")) return NULL; 0 6 6
146  
147 /* an enum or a const is the document naming the only answers */
148 6 ▶ if ((e = oa_get(aTHX_ h, "enum"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((e = oa_get(aTHX_ h, "enum"))) { 0 6 6
149 0 AV *av = oa_av_of(e);
150 0 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 0 0 0
151 0 int found = 0;
152 0 ▶ for (i = 0; i < n && !found; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && !found; i++) { 0 0 0
for (i = 0; i < n && !found; i++) { 0 0 0
153 0 SV **m = av_fetch(av, i, 0);
154 STRLEN ml; const char *mp;
155 0 ▶ if (!m || !*m || !SvOK(*m) || SvROK(*m)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!m || !*m || !SvOK(*m) || SvROK(*m)) continue; 0 0 0
if (!m || !*m || !SvOK(*m) || SvROK(*m)) continue; 0 0 0
if (!m || !*m || !SvOK(*m) || SvROK(*m)) continue; 0 0 0
if (!m || !*m || !SvOK(*m) || SvROK(*m)) continue; 0 0 0
156 0 mp = SvPV_const(*m, ml);
157 0 ▶ if (ml == vl && memEQ(mp, vp, vl)) found = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (ml == vl && memEQ(mp, vp, vl)) found = 1; 0 0 0
if (ml == vl && memEQ(mp, vp, vl)) found = 1; 0 0 0
158 }
159 0 ▶ if (!found) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!found) return NULL; 0 0 0
160 }
161 6 ▶ if ((e = oa_get(aTHX_ h, "const"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((e = oa_get(aTHX_ h, "const"))) { 0 6 6
162 STRLEN cl; const char *cp;
163 0 ▶ if (SvROK(e) || !SvOK(e)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(e) || !SvOK(e)) return NULL; 0 0 0
if (SvROK(e) || !SvOK(e)) return NULL; 0 0 0
164 0 cp = SvPV_const(e, cl);
165 0 ▶ if (!(cl == vl && memEQ(cp, vp, vl))) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!(cl == vl && memEQ(cp, vp, vl))) return NULL; 0 0 0
if (!(cl == vl && memEQ(cp, vp, vl))) return NULL; 0 0 0
166 }
167  
168 6 t = oa_gen_type_of(aTHX_ h);
169 6 ▶ if (!t) return newSVpvn(vp, vl); /* untyped: a string will do */
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return newSVpvn(vp, vl); /* untyped: a string will do */ 0 6 6
170 6 tp = SvPV_const(t, tl);
171  
172 6 ▶ if (tl == 6 && memEQ(tp, "string", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(tp, "string", 6)) { 1 5 6
if (tl == 6 && memEQ(tp, "string", 6)) { 1 0 1
173 1 SV *mn = oa_get(aTHX_ h, "minLength");
174 1 SV *mx = oa_get(aTHX_ h, "maxLength");
175 1 ▶ if (mn && (IV)vl < SvIV(mn)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (mn && (IV)vl < SvIV(mn)) return NULL; 0 1 1
if (mn && (IV)vl < SvIV(mn)) return NULL; 0 0 0
176 1 ▶ if (mx && (IV)vl > SvIV(mx)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (mx && (IV)vl > SvIV(mx)) return NULL; 0 1 1
if (mx && (IV)vl > SvIV(mx)) return NULL; 0 0 0
177 1 return newSVpvn(vp, vl);
178 }
179  
180 5 ▶ if ((tl == 7 && memEQ(tp, "integer", 7))
Branch: 2 branches
BranchTrueFalseTotal
if ((tl == 7 && memEQ(tp, "integer", 7)) 5 0 5
if ((tl == 7 && memEQ(tp, "integer", 7)) 0 5 5
181 0 ▶ || (tl == 6 && memEQ(tp, "number", 6))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (tl == 6 && memEQ(tp, "number", 6))) { 0 0 0
|| (tl == 6 && memEQ(tp, "number", 6))) { 0 0 0
182 5 int is_int = (tl == 7);
183 5 STRLEN i = 0;
184 5 int dot = 0;
185 SV *out;
186 5 ▶ if (vp[0] == '-' || vp[0] == '+') i = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (vp[0] == '-' || vp[0] == '+') i = 1; 5 0 5
if (vp[0] == '-' || vp[0] == '+') i = 1; 0 5 5
187 5 ▶ if (i >= vl) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= vl) return NULL; 0 5 5
188 13 ▶ for (; i < vl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < vl; i++) { 9 4 13
189 9 ▶ if (vp[i] == '.' && !is_int && !dot) { dot = 1; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (vp[i] == '.' && !is_int && !dot) { dot = 1; continue; } 0 9 9
if (vp[i] == '.' && !is_int && !dot) { dot = 1; continue; } 0 0 0
if (vp[i] == '.' && !is_int && !dot) { dot = 1; continue; } 0 0 0
190 9 ▶ if (vp[i] < '0' || vp[i] > '9') return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (vp[i] < '0' || vp[i] > '9') return NULL; 9 0 9
if (vp[i] < '0' || vp[i] > '9') return NULL; 1 8 9
191 }
192 /* the numeric bounds, which a string that merely looks like a
193 * number can still be outside of */
194 {
195 4 NV n = SvNV(val);
196 SV *b;
197 4 ▶ if ((b = oa_get(aTHX_ h, "minimum")) && n < SvNV(b)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if ((b = oa_get(aTHX_ h, "minimum")) && n < SvNV(b)) return NULL; 0 4 4
if ((b = oa_get(aTHX_ h, "minimum")) && n < SvNV(b)) return NULL; 0 0 0
198 4 ▶ if ((b = oa_get(aTHX_ h, "maximum")) && n > SvNV(b)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if ((b = oa_get(aTHX_ h, "maximum")) && n > SvNV(b)) return NULL; 1 3 4
if ((b = oa_get(aTHX_ h, "maximum")) && n > SvNV(b)) return NULL; 1 0 1
199 3 ▶ if ((b = oa_get(aTHX_ h, "exclusiveMinimum")) && n <= SvNV(b)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if ((b = oa_get(aTHX_ h, "exclusiveMinimum")) && n <= SvNV(b)) return NULL; 0 3 3
if ((b = oa_get(aTHX_ h, "exclusiveMinimum")) && n <= SvNV(b)) return NULL; 0 0 0
200 3 ▶ if ((b = oa_get(aTHX_ h, "exclusiveMaximum")) && n >= SvNV(b)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if ((b = oa_get(aTHX_ h, "exclusiveMaximum")) && n >= SvNV(b)) return NULL; 0 3 3
if ((b = oa_get(aTHX_ h, "exclusiveMaximum")) && n >= SvNV(b)) return NULL; 0 0 0
201 3 ▶ out = is_int ? newSViv((IV)n) : newSVnv(n);
Branch: 1 branch
BranchTrueFalseTotal
out = is_int ? newSViv((IV)n) : newSVnv(n); 3 0 3
202 }
203 3 return out;
204 }
205  
206 0 ▶ if (tl == 7 && memEQ(tp, "boolean", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 7 && memEQ(tp, "boolean", 7)) { 0 0 0
if (tl == 7 && memEQ(tp, "boolean", 7)) { 0 0 0
207 0 ▶ if (vl == 4 && memEQ(vp, "true", 4)) return newRV_noinc(newSViv(1));
Branch: 2 branches
BranchTrueFalseTotal
if (vl == 4 && memEQ(vp, "true", 4)) return newRV_noinc(newSViv(1)); 0 0 0
if (vl == 4 && memEQ(vp, "true", 4)) return newRV_noinc(newSViv(1)); 0 0 0
208 0 ▶ if (vl == 5 && memEQ(vp, "false", 5)) return newRV_noinc(newSViv(0));
Branch: 2 branches
BranchTrueFalseTotal
if (vl == 5 && memEQ(vp, "false", 5)) return newRV_noinc(newSViv(0)); 0 0 0
if (vl == 5 && memEQ(vp, "false", 5)) return newRV_noinc(newSViv(0)); 0 0 0
209 0 return NULL;
210 }
211  
212 /* an object or an array cannot come from one path segment */
213 0 return NULL;
214 }
215  
216 /* Give the nth element of an array a distinct identifier.
217 *
218 * Five rows that are byte-identical are not a list, they are the same
219 * row five times - you cannot tell a rendering bug from a keying bug
220 * against them, and nothing that consumes ids (a table, a router, a
221 * cache) behaves the way it will in production. So the integer fields
222 * that name a thing get the element's index added to whatever the
223 * schema generated.
224 *
225 * Deterministic: the index is the position, not a counter, so the same
226 * request still gives byte-identical bodies. Still valid: the base came
227 * from the generator inside the schema's own bounds, and anything that
228 * would leave `maximum` behind is left alone. */
229 556 static void oa_seq_id(pTHX_ HV *doc, SV *items, SV *elem, IV n) {
230 HV *eh, *sh, *props;
231 SV *deref;
232 HE *he;
233  
234 556 ▶ if (!n || !elem || !SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVHV) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!n || !elem || !SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVHV) return; 445 111 556
if (!n || !elem || !SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVHV) return; 445 0 445
if (!n || !elem || !SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVHV) return; 24 421 445
if (!n || !elem || !SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVHV) return; 0 24 24
235 24 eh = (HV *)SvRV(elem);
236  
237 24 ▶ deref = items ? oa_mock_deref(aTHX_ doc, items, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
deref = items ? oa_mock_deref(aTHX_ doc, items, 0) : NULL; 24 0 24
238 24 ▶ sh = deref ? oa_hv_of(deref) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
sh = deref ? oa_hv_of(deref) : NULL; 24 0 24
239 24 ▶ props = sh ? oa_hv_of(oa_get(aTHX_ sh, "properties")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
props = sh ? oa_hv_of(oa_get(aTHX_ sh, "properties")) : NULL; 24 0 24
240 24 ▶ if (!props) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!props) return; 0 24 24
241  
242 24 hv_iterinit(eh);
243 72 ▶ while ((he = hv_iternext(eh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(eh))) { 48 24 72
244 I32 kl;
245 48 const char *kp = hv_iterkey(he, &kl);
246 48 SV *val = hv_iterval(eh, he);
247 SV **ps;
248 HV *ph;
249 SV *t, *mx;
250 STRLEN tl;
251 const char *tp;
252  
253 /* the fields that name a thing: `id`, `petId`, `order_id` */
254 48 ▶ if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i'
Branch: 4 branches
BranchTrueFalseTotal
if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 48 0 48
if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 0 48 48
if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 0 0 0
if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 24 24 48
255 48 ▶ && toLOWER((U8)kp[kl - 1]) == 'd')) continue;
Branch: 3 branches
BranchTrueFalseTotal
&& toLOWER((U8)kp[kl - 1]) == 'd')) continue; 0 24 24
&& toLOWER((U8)kp[kl - 1]) == 'd')) continue; 0 0 0
&& toLOWER((U8)kp[kl - 1]) == 'd')) continue; 0 24 24
256 24 ▶ if (!val || !SvOK(val) || SvROK(val)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!val || !SvOK(val) || SvROK(val)) continue; 24 0 24
if (!val || !SvOK(val) || SvROK(val)) continue; 24 0 24
if (!val || !SvOK(val) || SvROK(val)) continue; 0 24 24
257  
258 24 ps = hv_fetch(props, kp, kl, 0);
259 24 ▶ if (!ps || !*ps) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!ps || !*ps) continue; 24 0 24
if (!ps || !*ps) continue; 0 24 24
260 24 ph = oa_hv_of(oa_mock_deref(aTHX_ doc, *ps, 0));
261 24 ▶ if (!ph) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ph) continue; 0 24 24
262  
263 24 t = oa_gen_type_of(aTHX_ ph);
264 24 ▶ if (!t) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) continue; 0 24 24
265 24 tp = SvPV_const(t, tl);
266 24 ▶ if (!(tl == 7 && memEQ(tp, "integer", 7))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(tl == 7 && memEQ(tp, "integer", 7))) continue; 24 0 24
if (!(tl == 7 && memEQ(tp, "integer", 7))) continue; 0 24 24
267  
268 24 mx = oa_get(aTHX_ ph, "maximum");
269 24 ▶ if (mx && (NV)(SvIV(val) + n) > SvNV(mx)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (mx && (NV)(SvIV(val) + n) > SvNV(mx)) continue; 0 24 24
if (mx && (NV)(SvIV(val) + n) > SvNV(mx)) continue; 0 0 0
270 24 sv_setiv(val, SvIV(val) + n);
271 }
272 }
273  
274 /* The captured value to answer a property with, or NULL.
275 *
276 * Matching is by name, in the order people actually write documents:
277 * the same name, the same name in any case, and then the convention
278 * every REST document uses - a path templated `{petId}` naming the
279 * resource whose schema calls the field `id`.
280 *
281 * That last rule applies only when exactly one capture ends in `id`.
282 * `/pets/{petId}/toys/{toyId}` has two, and there is no honest way to
283 * say which of them the `id` on a nested object means, so it generates
284 * rather than guesses. Guessing here would put a toy's id on a pet. */
285 10 static SV *oa_gen_echo(pTHX_ HV *caps, const char *name, STRLEN nl, HV *h) {
286 SV **hit;
287 HE *he;
288 10 SV *only = NULL;
289 10 int nid = 0;
290  
291 10 ▶ if (!caps || !name) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!caps || !name) return NULL; 10 0 10
if (!caps || !name) return NULL; 0 10 10
292  
293 10 ▶ if ((hit = hv_fetch(caps, name, (I32)nl, 0)) && *hit && SvOK(*hit))
Branch: 3 branches
BranchTrueFalseTotal
if ((hit = hv_fetch(caps, name, (I32)nl, 0)) && *hit && SvOK(*hit)) 1 9 10
if ((hit = hv_fetch(caps, name, (I32)nl, 0)) && *hit && SvOK(*hit)) 1 0 1
if ((hit = hv_fetch(caps, name, (I32)nl, 0)) && *hit && SvOK(*hit)) 1 0 1
294 1 return oa_gen_fits(aTHX_ h, *hit);
295  
296 9 hv_iterinit(caps);
297 18 ▶ while ((he = hv_iternext(caps))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(caps))) { 9 9 18
298 I32 kl;
299 9 const char *kp = hv_iterkey(he, &kl);
300 9 SV *v = hv_iterval(caps, he);
301 STRLEN i;
302 9 int same = ((STRLEN)kl == nl);
303 9 ▶ for (i = 0; same && i < nl; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; same && i < nl; i++) 0 9 9
for (i = 0; same && i < nl; i++) 0 0 0
304 0 ▶ if (toLOWER((U8)kp[i]) != toLOWER((U8)name[i])) same = 0;
Branch: 3 branches
BranchTrueFalseTotal
if (toLOWER((U8)kp[i]) != toLOWER((U8)name[i])) same = 0; 0 0 0
if (toLOWER((U8)kp[i]) != toLOWER((U8)name[i])) same = 0; 0 0 0
if (toLOWER((U8)kp[i]) != toLOWER((U8)name[i])) same = 0; 0 0 0
305 9 ▶ if (same) return oa_gen_fits(aTHX_ h, v);
Branch: 1 branch
BranchTrueFalseTotal
if (same) return oa_gen_fits(aTHX_ h, v); 0 9 9
306  
307 9 ▶ if (kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i'
Branch: 3 branches
BranchTrueFalseTotal
if (kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 9 0 9
if (kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 9 0 9
if (kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 9 0 9
308 9 ▶ && toLOWER((U8)kp[kl - 1]) == 'd') {
Branch: 2 branches
BranchTrueFalseTotal
&& toLOWER((U8)kp[kl - 1]) == 'd') { 0 9 9
&& toLOWER((U8)kp[kl - 1]) == 'd') { 9 0 9
309 9 nid++;
310 9 only = v;
311 }
312 }
313  
314 9 ▶ if (nid == 1 && nl == 2 && toLOWER((U8)name[0]) == 'i'
Branch: 4 branches
BranchTrueFalseTotal
if (nid == 1 && nl == 2 && toLOWER((U8)name[0]) == 'i' 9 0 9
if (nid == 1 && nl == 2 && toLOWER((U8)name[0]) == 'i' 5 4 9
if (nid == 1 && nl == 2 && toLOWER((U8)name[0]) == 'i' 0 5 5
if (nid == 1 && nl == 2 && toLOWER((U8)name[0]) == 'i' 5 0 5
315 5 ▶ && toLOWER((U8)name[1]) == 'd')
Branch: 2 branches
BranchTrueFalseTotal
&& toLOWER((U8)name[1]) == 'd') 0 5 5
&& toLOWER((U8)name[1]) == 'd') 5 0 5
316 5 return oa_gen_fits(aTHX_ h, only);
317  
318 4 return NULL;
319 }
320  
321 /* the first non-"null" entry of a type union, or NULL for "just null" */
322 2256 static SV *oa_gen_type_of(pTHX_ HV *h) {
323 2256 SV *t = oa_get(aTHX_ h, "type");
324 2256 ▶ if (!t) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return NULL; 0 2256 2256
325 2256 ▶ if (!SvROK(t)) return t;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(t)) return t; 2153 103 2256
326 {
327 103 AV *av = oa_av_of(t);
328 103 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 103 0 103
329 206 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 206 0 206
330 206 SV **e = av_fetch(av, i, 0);
331 206 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 206 0 206
if (e && *e && SvOK(*e)) { 206 0 206
if (e && *e && SvOK(*e)) { 206 0 206
332 206 STRLEN l; const char *p = SvPV_const(*e, l);
333 206 ▶ if (!(l == 4 && memEQ(p, "null", 4))) return *e;
Branch: 2 branches
BranchTrueFalseTotal
if (!(l == 4 && memEQ(p, "null", 4))) return *e; 103 103 206
if (!(l == 4 && memEQ(p, "null", 4))) return *e; 0 103 103
334 }
335 }
336 }
337 0 return NULL;
338 }
339  
340 /* fixed, valid representative for a `format` (NULL: no special value) */
341 206 static const char *oa_gen_format(const char *p, STRLEN l) {
342 206 ▶ if (l == 9 && memEQ(p, "date-time", 9)) return "1970-01-01T00:00:00Z";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 9 && memEQ(p, "date-time", 9)) return "1970-01-01T00:00:00Z"; 103 103 206
if (l == 9 && memEQ(p, "date-time", 9)) return "1970-01-01T00:00:00Z"; 103 0 103
343 103 ▶ if (l == 4 && memEQ(p, "date", 4)) return "1970-01-01";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "date", 4)) return "1970-01-01"; 103 0 103
if (l == 4 && memEQ(p, "date", 4)) return "1970-01-01"; 0 103 103
344 103 ▶ if (l == 4 && memEQ(p, "time", 4)) return "00:00:00Z";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "time", 4)) return "00:00:00Z"; 103 0 103
if (l == 4 && memEQ(p, "time", 4)) return "00:00:00Z"; 0 103 103
345 103 ▶ if (l == 8 && memEQ(p, "duration", 8)) return "PT0S";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 8 && memEQ(p, "duration", 8)) return "PT0S"; 0 103 103
if (l == 8 && memEQ(p, "duration", 8)) return "PT0S"; 0 0 0
346 103 ▶ if (l == 4 && memEQ(p, "uuid", 4)) return "00000000-0000-0000-0000-000000000000";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "uuid", 4)) return "00000000-0000-0000-0000-000000000000"; 103 0 103
if (l == 4 && memEQ(p, "uuid", 4)) return "00000000-0000-0000-0000-000000000000"; 103 0 103
347 0 ▶ if (l == 5 && memEQ(p, "email", 5)) return "user@example.com";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && memEQ(p, "email", 5)) return "user@example.com"; 0 0 0
if (l == 5 && memEQ(p, "email", 5)) return "user@example.com"; 0 0 0
348 0 ▶ if (l == 3 && memEQ(p, "uri", 3)) return "https://example.com/";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 3 && memEQ(p, "uri", 3)) return "https://example.com/"; 0 0 0
if (l == 3 && memEQ(p, "uri", 3)) return "https://example.com/"; 0 0 0
349 0 ▶ if (l == 13 && memEQ(p, "uri-reference", 13)) return "/";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 13 && memEQ(p, "uri-reference", 13)) return "/"; 0 0 0
if (l == 13 && memEQ(p, "uri-reference", 13)) return "/"; 0 0 0
350 0 ▶ if (l == 8 && memEQ(p, "hostname", 8)) return "example.com";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 8 && memEQ(p, "hostname", 8)) return "example.com"; 0 0 0
if (l == 8 && memEQ(p, "hostname", 8)) return "example.com"; 0 0 0
351 0 ▶ if (l == 4 && memEQ(p, "ipv4", 4)) return "127.0.0.1";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "ipv4", 4)) return "127.0.0.1"; 0 0 0
if (l == 4 && memEQ(p, "ipv4", 4)) return "127.0.0.1"; 0 0 0
352 0 ▶ if (l == 4 && memEQ(p, "ipv6", 4)) return "::1";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "ipv6", 4)) return "::1"; 0 0 0
if (l == 4 && memEQ(p, "ipv6", 4)) return "::1"; 0 0 0
353 0 ▶ if (l == 4 && memEQ(p, "byte", 4)) return "AA==";
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "byte", 4)) return "AA=="; 0 0 0
if (l == 4 && memEQ(p, "byte", 4)) return "AA=="; 0 0 0
354 0 return NULL;
355 }
356  
357 1084 static SV *oa_gen_string(pTHX_ HV *h) {
358 1084 SV *f = oa_get(aTHX_ h, "format");
359 1084 SV *minl = oa_get(aTHX_ h, "minLength");
360 1084 SV *maxl = oa_get(aTHX_ h, "maxLength");
361 SV *out;
362 1084 const char *fixed = NULL;
363 1084 ▶ if (f && !SvROK(f)) {
Branch: 2 branches
BranchTrueFalseTotal
if (f && !SvROK(f)) { 206 878 1084
if (f && !SvROK(f)) { 206 0 206
364 206 STRLEN fl; const char *fp = SvPV_const(f, fl);
365 206 fixed = oa_gen_format(fp, fl);
366 }
367 1084 ▶ out = newSVpv(fixed ? fixed : "string", 0);
Branch: 1 branch
BranchTrueFalseTotal
out = newSVpv(fixed ? fixed : "string", 0); 206 878 1084
368 1084 ▶ if (minl && SvIV(minl) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (minl && SvIV(minl) > 0) { 721 363 1084
if (minl && SvIV(minl) > 0) { 721 0 721
369 721 IV want = SvIV(minl);
370 721 ▶ if (want > OA_MOCK_MAX_PAD) want = OA_MOCK_MAX_PAD;
Branch: 1 branch
BranchTrueFalseTotal
if (want > OA_MOCK_MAX_PAD) want = OA_MOCK_MAX_PAD; 0 721 721
371 721 ▶ while ((IV)SvCUR(out) < want) sv_catpvs(out, "a");
Branch: 1 branch
BranchTrueFalseTotal
while ((IV)SvCUR(out) < want) sv_catpvs(out, "a"); 0 721 721
372 }
373 1084 ▶ if (maxl && SvIV(maxl) >= 0 && (IV)SvCUR(out) > SvIV(maxl)) {
Branch: 3 branches
BranchTrueFalseTotal
if (maxl && SvIV(maxl) >= 0 && (IV)SvCUR(out) > SvIV(maxl)) { 0 1084 1084
if (maxl && SvIV(maxl) >= 0 && (IV)SvCUR(out) > SvIV(maxl)) { 0 0 0
if (maxl && SvIV(maxl) >= 0 && (IV)SvCUR(out) > SvIV(maxl)) { 0 0 0
374 0 SvCUR_set(out, (STRLEN)SvIV(maxl));
375 0 *SvEND(out) = '\0';
376 }
377 1084 return out;
378 }
379  
380 /* a number sitting on its lower bound (0 without one), nudged onto
381 * multipleOf. Integer types return an IV; number types return an IV when
382 * the value is integral, so 0 encodes as 0 and not 0.0. */
383 568 static SV *oa_gen_number(pTHX_ HV *h, int is_int) {
384 568 NV v = 0.0;
385 568 SV *mi = oa_get(aTHX_ h, "minimum");
386 568 SV *xm = oa_get(aTHX_ h, "exclusiveMinimum");
387 568 SV *ma = oa_get(aTHX_ h, "maximum");
388 568 SV *mo = oa_get(aTHX_ h, "multipleOf");
389 568 ▶ if (mi) v = SvNV(mi);
Branch: 1 branch
BranchTrueFalseTotal
if (mi) v = SvNV(mi); 206 362 568
390 686 ▶ if (xm && !SvROK(xm) && looks_like_number(xm)) {
Branch: 3 branches
BranchTrueFalseTotal
if (xm && !SvROK(xm) && looks_like_number(xm)) { 118 450 568
if (xm && !SvROK(xm) && looks_like_number(xm)) { 118 0 118
if (xm && !SvROK(xm) && looks_like_number(xm)) { 118 0 118
391 /* draft 2020-12: a number the value must sit strictly above */
392 118 NV x = SvNV(xm);
393 118 ▶ if (v <= x) v = is_int ? floor(x) + 1 : x + 1;
Branch: 2 branches
BranchTrueFalseTotal
if (v <= x) v = is_int ? floor(x) + 1 : x + 1; 118 0 118
if (v <= x) v = is_int ? floor(x) + 1 : x + 1; 15 103 118
394 450 ▶ } else if (xm && SvTRUE(xm) && mi) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (xm && SvTRUE(xm) && mi) { 0 450 450
} else if (xm && SvTRUE(xm) && mi) { 0 0 0
} else if (xm && SvTRUE(xm) && mi) { 0 0 0
395 0 v += 1; /* OAS 3.0 boolean form */
396 }
397 568 ▶ if (mo && SvNV(mo) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mo && SvNV(mo) > 0) { 103 465 568
if (mo && SvNV(mo) > 0) { 103 0 103
398 103 NV m = SvNV(mo);
399 103 v = ceil(v / m) * m;
400 }
401 568 ▶ if (ma && SvNV(ma) < v) v = SvNV(ma); /* contradictory spec: best effort */
Branch: 2 branches
BranchTrueFalseTotal
if (ma && SvNV(ma) < v) v = SvNV(ma); /* contradictory spec: best effort */ 1 567 568
if (ma && SvNV(ma) < v) v = SvNV(ma); /* contradictory spec: best effort */ 0 1 1
402 568 ▶ if (is_int || v == floor(v)) return newSViv((IV)v);
Branch: 2 branches
BranchTrueFalseTotal
if (is_int || v == floor(v)) return newSViv((IV)v); 103 465 568
if (is_int || v == floor(v)) return newSViv((IV)v); 0 103 103
403 103 return newSVnv(v);
404 }
405  
406 /* allOf: fold the members into one working schema - properties merged
407 * key-wise, required concatenated, scalar keywords last-wins - then
408 * generate from the fold. Mortal intermediate, +1 result. */
409 105 static SV *oa_gen_allof(pTHX_ HV *doc, AV *all, HV *caps, int depth) {
410 105 HV *m = (HV *)sv_2mortal((SV *)newHV());
411 105 HV *mp = NULL;
412 105 AV *mr = NULL;
413 105 SSize_t i, n = av_len(all) + 1;
414 315 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 210 105 315
415 210 SV **e = av_fetch(all, i, 0);
416 210 ▶ SV *sub = e && *e ? oa_mock_deref(aTHX_ doc, *e, depth) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *sub = e && *e ? oa_mock_deref(aTHX_ doc, *e, depth) : NULL; 210 0 210
SV *sub = e && *e ? oa_mock_deref(aTHX_ doc, *e, depth) : NULL; 210 0 210
417 210 ▶ HV *sh = sub ? oa_hv_of(sub) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *sh = sub ? oa_hv_of(sub) : NULL; 210 0 210
418 HE *he;
419 210 ▶ if (!sh) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sh) continue; 0 210 210
420 210 hv_iterinit(sh);
421 844 ▶ while ((he = hv_iternext(sh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(sh))) { 634 210 844
422 634 I32 kl; const char *k = hv_iterkey(he, &kl);
423 634 SV *v = hv_iterval(sh, he);
424 /* A folded base carries its discriminator dispatch with it. None of
425 * that describes the shape of a value, and folding it back in would
426 * send oa_gen round the same branch for ever - base to child to
427 * base - so the annotation and the generated members are dropped
428 * and only the constraints are merged. */
429 636 ▶ if (kl == 13 && memEQ(k, "discriminator", 13)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 13 && memEQ(k, "discriminator", 13)) continue; 2 632 634
if (kl == 13 && memEQ(k, "discriminator", 13)) continue; 2 0 2
430 632 ▶ if (kl == 5 && memEQ(k, "allOf", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 5 && memEQ(k, "allOf", 5)) { 2 630 632
if (kl == 5 && memEQ(k, "allOf", 5)) { 2 0 2
431 2 AV *src = oa_av_of(v), *keep;
432 2 ▶ SSize_t x, xn = src ? av_len(src) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t x, xn = src ? av_len(src) + 1 : 0; 2 0 2
433 2 ▶ if (!src) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!src) continue; 0 2 2
434 2 keep = newAV();
435 8 ▶ for (x = 0; x < xn; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < xn; x++) { 6 2 8
436 6 SV **me = av_fetch(src, x, 0);
437 6 ▶ HV *mhv = (me && *me) ? oa_hv_of(*me) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *mhv = (me && *me) ? oa_hv_of(*me) : NULL; 6 0 6
HV *mhv = (me && *me) ? oa_hv_of(*me) : NULL; 6 0 6
438 6 ▶ if (mhv && hv_exists(mhv, OA_DISC_MARK, OA_DISC_MARK_LEN))
Branch: 2 branches
BranchTrueFalseTotal
if (mhv && hv_exists(mhv, OA_DISC_MARK, OA_DISC_MARK_LEN)) 6 0 6
if (mhv && hv_exists(mhv, OA_DISC_MARK, OA_DISC_MARK_LEN)) 6 0 6
439 6 continue;
440 0 ▶ if (me && *me) av_push(keep, SvREFCNT_inc(*me));
Branch: 2 branches
BranchTrueFalseTotal
if (me && *me) av_push(keep, SvREFCNT_inc(*me)); 0 0 0
if (me && *me) av_push(keep, SvREFCNT_inc(*me)); 0 0 0
441 }
442 2 ▶ if (av_len(keep) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(keep) >= 0) 0 2 2
443 0 (void)hv_store(m, k, kl, newRV_noinc((SV *)keep), 0);
444 2 else SvREFCNT_dec((SV *)keep);
445 2 continue;
446 }
447 840 ▶ if (kl == 10 && memEQ(k, "properties", 10)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 10 && memEQ(k, "properties", 10)) { 210 420 630
if (kl == 10 && memEQ(k, "properties", 10)) { 210 0 210
448 210 HV *ph = oa_hv_of(v);
449 HE *pe;
450 210 ▶ if (!ph) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ph) continue; 0 210 210
451 210 ▶ if (!mp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mp) { 105 105 210
452 105 mp = newHV();
453 105 (void)hv_stores(m, "properties", newRV_noinc((SV *)mp));
454 }
455 210 hv_iterinit(ph);
456 422 ▶ while ((pe = hv_iternext(ph))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((pe = hv_iternext(ph))) { 212 210 422
457 212 I32 pkl; const char *pk = hv_iterkey(pe, &pkl);
458 212 (void)hv_store(mp, pk, pkl,
459 SvREFCNT_inc(hv_iterval(ph, pe)), 0);
460 }
461 630 ▶ } else if (kl == 8 && memEQ(k, "required", 8)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (kl == 8 && memEQ(k, "required", 8)) { 210 210 420
} else if (kl == 8 && memEQ(k, "required", 8)) { 210 0 210
462 210 AV *ra = oa_av_of(v);
463 210 ▶ SSize_t j, rn = ra ? av_len(ra) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, rn = ra ? av_len(ra) + 1 : 0; 210 0 210
464 210 ▶ if (!mr) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mr) { 105 105 210
465 105 mr = newAV();
466 105 (void)hv_stores(m, "required", newRV_noinc((SV *)mr));
467 }
468 422 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 212 210 422
469 212 SV **r = av_fetch(ra, j, 0);
470 212 ▶ if (r && *r) av_push(mr, newSVsv(*r));
Branch: 2 branches
BranchTrueFalseTotal
if (r && *r) av_push(mr, newSVsv(*r)); 212 0 212
if (r && *r) av_push(mr, newSVsv(*r)); 212 0 212
471 }
472 } else {
473 210 (void)hv_store(m, k, kl, SvREFCNT_inc(v), 0);
474 }
475 }
476 }
477 105 return oa_gen(aTHX_ doc, sv_2mortal(newRV_inc((SV *)m)), caps, depth + 1);
478 }
479  
480 /* Generate a value that validates against `schema`. Always answers -
481 * anything unresolvable becomes JSON null, which is at least honest and
482 * still deterministic. (+1) */
483 2644 static SV *oa_gen(pTHX_ HV *doc, SV *schema, HV *caps, int depth) {
484 HV *h;
485 SV *v, *t;
486 2644 ▶ if (depth > OA_MOCK_MAX_DEPTH) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (depth > OA_MOCK_MAX_DEPTH) return newSV(0); 0 2644 2644
487 2644 schema = oa_mock_deref(aTHX_ doc, schema, depth);
488 2644 ▶ h = schema ? oa_hv_of(schema) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
h = schema ? oa_hv_of(schema) : NULL; 2644 0 2644
489 2644 ▶ if (!h) return newSV(0); /* true/false schema, or junk */
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return newSV(0); /* true/false schema, or junk */ 0 2644 2644
490  
491 2644 ▶ if ((v = oa_get(aTHX_ h, "const"))) return newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "const"))) return newSVsv(v); 103 2541 2644
492 2541 ▶ if ((v = oa_get(aTHX_ h, "enum"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "enum"))) { 103 2438 2541
493 103 AV *av = oa_av_of(v);
494 103 ▶ SV **e = av ? av_fetch(av, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = av ? av_fetch(av, 0, 0) : NULL; 103 0 103
495 103 ▶ if (e && *e) return newSVsv(*e);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) return newSVsv(*e); 103 0 103
if (e && *e) return newSVsv(*e); 103 0 103
496 }
497 2438 ▶ if ((v = oa_get(aTHX_ h, "default"))) return newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "default"))) return newSVsv(v); 0 2438 2438
498  
499 /* A discriminated schema: generate the branch the document names first and
500 * stamp the discriminator property with its key, so the mock is a coherent
501 * member of the union rather than a fold of all of them. The mapping is
502 * always explicit and its keys are taken in order, so the same document
503 * always mocks the same branch. */
504 2438 ▶ if ((v = oa_get(aTHX_ h, "discriminator"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "discriminator"))) { 4 2434 2438
505 4 HV *dh = oa_hv_of(v);
506 4 ▶ SV *pn = dh ? oa_get(aTHX_ dh, "propertyName") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *pn = dh ? oa_get(aTHX_ dh, "propertyName") : NULL; 4 0 4
507 4 ▶ HV *map = dh ? oa_hv_of(oa_get(aTHX_ dh, "mapping")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *map = dh ? oa_hv_of(oa_get(aTHX_ dh, "mapping")) : NULL; 4 0 4
508 4 ▶ if (pn && !SvROK(pn) && map && HvUSEDKEYS(map)) {
Branch: 5 branches
BranchTrueFalseTotal
if (pn && !SvROK(pn) && map && HvUSEDKEYS(map)) { 4 0 4
if (pn && !SvROK(pn) && map && HvUSEDKEYS(map)) { 4 0 4
if (pn && !SvROK(pn) && map && HvUSEDKEYS(map)) { 4 0 4
if (pn && !SvROK(pn) && map && HvUSEDKEYS(map)) { 0 4 4
if (pn && !SvROK(pn) && map && HvUSEDKEYS(map)) { 4 0 4
509 4 AV *ks = (AV *)sv_2mortal((SV *)newAV());
510 HE *he;
511 STRLEN pl;
512 (void)SvPV_const(pn, pl);
513 4 hv_iterinit(map);
514 12 ▶ while ((he = hv_iternext(map))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(map))) { 8 4 12
515 8 I32 kl; const char *k = hv_iterkey(he, &kl);
516 8 av_push(ks, newSVpvn(k, (STRLEN)kl));
517 }
518 4 oa_sort_av(aTHX_ ks);
519 4 ▶ if (pl && av_len(ks) >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pl && av_len(ks) >= 0) { 4 0 4
if (pl && av_len(ks) >= 0) { 4 0 4
520 4 SV *key = *av_fetch(ks, 0, 0);
521 4 STRLEN kl; const char *kp = SvPV_const(key, kl);
522 4 SV **tv = hv_fetch(map, kp, (I32)kl, 0);
523 4 SV *out = NULL;
524 4 ▶ if (tv && *tv && !SvROK(*tv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (tv && *tv && !SvROK(*tv)) { 4 0 4
if (tv && *tv && !SvROK(*tv)) { 4 0 4
if (tv && *tv && !SvROK(*tv)) { 4 0 4
525 4 STRLEN rl; const char *rp = SvPV_const(*tv, rl);
526 4 SV *target = oa_doc_at(aTHX_ doc, rp, rl);
527 4 ▶ if (target) out = oa_gen(aTHX_ doc, target, caps, depth + 1);
Branch: 1 branch
BranchTrueFalseTotal
if (target) out = oa_gen(aTHX_ doc, target, caps, depth + 1); 4 0 4
528 }
529 4 ▶ if (out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV) { 4 0 4
if (out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV) { 4 0 4
if (out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV) { 4 0 4
530 4 (void)hv_store((HV *)SvRV(out), SvPVX_const(pn), (I32)pl,
531 newSVsv(key), 0);
532 4 return out;
533 }
534 0 ▶ if (out) SvREFCNT_dec(out);
Branch: 1 branch
BranchTrueFalseTotal
if (out) SvREFCNT_dec(out); 0 0 0
535 }
536 }
537 }
538  
539 2434 ▶ if ((v = oa_get(aTHX_ h, "allOf")) && oa_av_of(v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "allOf")) && oa_av_of(v)) 105 2329 2434
if ((v = oa_get(aTHX_ h, "allOf")) && oa_av_of(v)) 105 0 105
540 105 return oa_gen_allof(aTHX_ doc, oa_av_of(v), caps, depth);
541 2329 ▶ if (((v = oa_get(aTHX_ h, "oneOf")) && oa_av_of(v))
Branch: 2 branches
BranchTrueFalseTotal
if (((v = oa_get(aTHX_ h, "oneOf")) && oa_av_of(v)) 103 2226 2329
if (((v = oa_get(aTHX_ h, "oneOf")) && oa_av_of(v)) 0 103 103
542 2226 ▶ || ((v = oa_get(aTHX_ h, "anyOf")) && oa_av_of(v))) {
Branch: 2 branches
BranchTrueFalseTotal
|| ((v = oa_get(aTHX_ h, "anyOf")) && oa_av_of(v))) { 0 2226 2226
|| ((v = oa_get(aTHX_ h, "anyOf")) && oa_av_of(v))) { 0 0 0
543 103 SV **e = av_fetch(oa_av_of(v), 0, 0);
544 103 ▶ return (e && *e) ? oa_gen(aTHX_ doc, *e, caps, depth + 1) : newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? oa_gen(aTHX_ doc, *e, caps, depth + 1) : newSV(0); 103 0 103
return (e && *e) ? oa_gen(aTHX_ doc, *e, caps, depth + 1) : newSV(0); 103 0 103
545 }
546  
547 2226 t = oa_gen_type_of(aTHX_ h);
548 2226 ▶ if (!t) {
Branch: 1 branch
BranchTrueFalseTotal
if (!t) { 0 2226 2226
549 /* no type declared: shape hints, then null (valid anywhere) */
550 0 ▶ if (oa_get(aTHX_ h, "properties") || oa_get(aTHX_ h, "required"))
Branch: 2 branches
BranchTrueFalseTotal
if (oa_get(aTHX_ h, "properties") || oa_get(aTHX_ h, "required")) 0 0 0
if (oa_get(aTHX_ h, "properties") || oa_get(aTHX_ h, "required")) 0 0 0
551 0 t = sv_2mortal(newSVpvs("object"));
552 0 ▶ else if (oa_get(aTHX_ h, "items"))
Branch: 1 branch
BranchTrueFalseTotal
else if (oa_get(aTHX_ h, "items")) 0 0 0
553 0 t = sv_2mortal(newSVpvs("array"));
554 else
555 0 return newSV(0);
556 }
557 {
558 2226 STRLEN tl; const char *tp = SvPV_const(t, tl);
559 2226 ▶ if (tl == 6 && memEQ(tp, "string", 6))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(tp, "string", 6)) 1546 680 2226
if (tl == 6 && memEQ(tp, "string", 6)) 1084 462 1546
560 2226 return oa_gen_string(aTHX_ h);
561 1142 ▶ if (tl == 7 && memEQ(tp, "integer", 7))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 7 && memEQ(tp, "integer", 7)) 568 574 1142
if (tl == 7 && memEQ(tp, "integer", 7)) 465 103 568
562 465 return oa_gen_number(aTHX_ h, 1);
563 677 ▶ if (tl == 6 && memEQ(tp, "number", 6))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(tp, "number", 6)) 462 215 677
if (tl == 6 && memEQ(tp, "number", 6)) 103 359 462
564 103 return oa_gen_number(aTHX_ h, 0);
565 574 ▶ if (tl == 7 && memEQ(tp, "boolean", 7))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 7 && memEQ(tp, "boolean", 7)) 103 471 574
if (tl == 7 && memEQ(tp, "boolean", 7)) 103 0 103
566 103 return newRV_noinc(newSViv(1)); /* \1 encodes as true */
567 471 ▶ if (tl == 4 && memEQ(tp, "null", 4))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 4 && memEQ(tp, "null", 4)) 0 471 471
if (tl == 4 && memEQ(tp, "null", 4)) 0 0 0
568 0 return newSV(0);
569 471 ▶ if (tl == 5 && memEQ(tp, "array", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 5 && memEQ(tp, "array", 5)) { 112 359 471
if (tl == 5 && memEQ(tp, "array", 5)) { 112 0 112
570 112 AV *out = newAV();
571 112 SV *items = oa_get(aTHX_ h, "items");
572 112 SV *mn = oa_get(aTHX_ h, "minItems");
573 112 SV *mx = oa_get(aTHX_ h, "maxItems");
574 /* One element, not none, when the document does not say.
575 *
576 * An empty array satisfies `type: array` and is therefore a
577 * correct answer, which is precisely why this was wrong: a
578 * list endpoint that mocks as [] tells the caller nothing
579 * about the shape of what they will get, and no client can
580 * be developed against it. The useful answer is the
581 * smallest one that shows the shape.
582 *
583 * An explicit minItems still wins, and maxItems still caps -
584 * including `maxItems: 0`, where the document has said it
585 * means empty and we believe it. */
586 112 IV want = OA_MOCK_ARRAY_ITEMS, i;
587 112 ▶ if (mn && SvIV(mn) > want) want = SvIV(mn);
Branch: 2 branches
BranchTrueFalseTotal
if (mn && SvIV(mn) > want) want = SvIV(mn); 104 8 112
if (mn && SvIV(mn) > want) want = SvIV(mn); 1 103 104
588 112 ▶ if (mx && SvIV(mx) < want) want = SvIV(mx);
Branch: 2 branches
BranchTrueFalseTotal
if (mx && SvIV(mx) < want) want = SvIV(mx); 2 110 112
if (mx && SvIV(mx) < want) want = SvIV(mx); 2 0 2
589 112 ▶ if (want > 64) want = 64; /* bounded, like the pad */
Branch: 1 branch
BranchTrueFalseTotal
if (want > 64) want = 64; /* bounded, like the pad */ 0 112 112
590 112 ▶ if (want < 0) want = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (want < 0) want = 0; 0 112 112
591 668 ▶ for (i = 0; i < want; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < want; i++) { 556 112 668
592 556 SV *el = items ? oa_gen(aTHX_ doc, items, caps, depth + 1)
593 556 ▶ : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
: newSV(0); 556 0 556
594 556 oa_seq_id(aTHX_ doc, items, el, i);
595 556 av_push(out, el);
596 }
597 112 return newRV_noinc((SV *)out);
598 }
599 359 ▶ if (tl == 6 && memEQ(tp, "object", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(tp, "object", 6)) { 359 0 359
if (tl == 6 && memEQ(tp, "object", 6)) { 359 0 359
600 359 HV *out = newHV();
601 359 HV *props = oa_hv_of(oa_get(aTHX_ h, "properties"));
602 359 AV *req = oa_av_of(oa_get(aTHX_ h, "required"));
603 359 ▶ SSize_t i, n = req ? av_len(req) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = req ? av_len(req) + 1 : 0; 359 0 359
604 2107 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1748 359 2107
605 1748 SV **e = av_fetch(req, i, 0);
606 STRLEN kl; const char *kp;
607 SV **ps;
608 1748 ▶ if (!e || !*e || !SvOK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvOK(*e)) continue; 1748 0 1748
if (!e || !*e || !SvOK(*e)) continue; 1748 0 1748
if (!e || !*e || !SvOK(*e)) continue; 0 1748 1748
609 1748 SV *echo = NULL;
610 1748 kp = SvPV_const(*e, kl);
611 1748 ▶ ps = props ? hv_fetch(props, kp, (I32)kl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ps = props ? hv_fetch(props, kp, (I32)kl, 0) : NULL; 1748 0 1748
612  
613 /* The request, where it answers the question better than
614 * the schema does. `GET /pets/42` replying with id 0 is
615 * a mock nobody can develop a client against. */
616 1748 ▶ if (caps && ps && *ps) {
Branch: 3 branches
BranchTrueFalseTotal
if (caps && ps && *ps) { 10 1738 1748
if (caps && ps && *ps) { 10 0 10
if (caps && ps && *ps) { 10 0 10
617 10 SV *rs = oa_mock_deref(aTHX_ doc, *ps, depth);
618 10 ▶ HV *rh = rs ? oa_hv_of(rs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *rh = rs ? oa_hv_of(rs) : NULL; 10 0 10
619 10 ▶ if (rh) echo = oa_gen_echo(aTHX_ caps, kp, (STRLEN)kl, rh);
Branch: 1 branch
BranchTrueFalseTotal
if (rh) echo = oa_gen_echo(aTHX_ caps, kp, (STRLEN)kl, rh); 10 0 10
620 }
621  
622 1748 ▶ (void)hv_store(out, kp, (I32)kl,
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_store(out, kp, (I32)kl, 1744 4 1748
(void)hv_store(out, kp, (I32)kl, 1744 0 1744
(void)hv_store(out, kp, (I32)kl, 1744 0 1744
623 echo ? echo
624 : (ps && *ps) ? oa_gen(aTHX_ doc, *ps, caps, depth + 1)
625 : newSV(0), 0);
626 }
627 359 return newRV_noinc((SV *)out);
628 }
629 }
630 0 return newSV(0);
631 }
632  
633 /* ---- the declared responses, raw -------------------------------------------- */
634  
635 /* the raw operation object (paths -> template -> method), or NULL. Borrowed. */
636 954 static HV *oa_raw_op(pTHX_ oa_api *a, oa_op *o) {
637 954 HV *doc = (HV *)SvRV(a->spec);
638 954 HV *paths = oa_hv_of(oa_get(aTHX_ doc, "paths"));
639 SV *item;
640 HV *ih;
641 STRLEN pl; const char *pp;
642 SV **e;
643 954 ▶ if (!paths) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!paths) return NULL; 0 954 954
644 954 pp = SvPV_const(o->path, pl);
645 954 e = hv_fetch(paths, pp, (I32)pl, 0);
646 954 ▶ item = (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
item = (e && *e) ? *e : NULL; 954 0 954
item = (e && *e) ? *e : NULL; 954 0 954
647 954 ▶ ih = item ? oa_hv_of(item) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ih = item ? oa_hv_of(item) : NULL; 954 0 954
648 954 ▶ if (!ih) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!ih) return NULL; 0 954 954
649 1908 return oa_hv_of(oa_get(aTHX_ ih, SvPV_nolen(o->method)));
650 }
651  
652 /* Choose the status and its raw response object. `want` is the preferred
653 * code (0 = none). Fills *robj (may end NULL: answer with no body) and
654 * returns the status. */
655 954 static int oa_mock_status(pTHX_ HV *responses, int want, HV **robj) {
656 HE *he;
657 char kb[8];
658 954 *robj = NULL;
659 954 ▶ if (!responses) return want ? want : 200;
Branch: 2 branches
BranchTrueFalseTotal
if (!responses) return want ? want : 200; 0 954 954
if (!responses) return want ? want : 200; 0 0 0
660  
661 954 ▶ if (want) {
Branch: 1 branch
BranchTrueFalseTotal
if (want) { 15 939 954
662 SV **e;
663 15 STRLEN kl = (STRLEN)my_snprintf(kb, sizeof kb, "%d", want);
664 15 e = hv_fetch(responses, kb, (I32)kl, 0);
665 15 ▶ if (e && *e) { *robj = oa_hv_of(*e); return want; }
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) { *robj = oa_hv_of(*e); return want; } 13 2 15
if (e && *e) { *robj = oa_hv_of(*e); return want; } 13 0 13
666 2 e = hv_fetch(responses, "default", 7, 0);
667 2 ▶ if (e && *e) { *robj = oa_hv_of(*e); return want; }
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) { *robj = oa_hv_of(*e); return want; } 1 1 2
if (e && *e) { *robj = oa_hv_of(*e); return want; } 1 0 1
668 1 return want; /* undeclared: empty body */
669 }
670  
671 {
672 939 int best2 = 0, bestany = 0;
673 939 SV *best2sv = NULL, *bestanysv = NULL;
674 939 hv_iterinit(responses);
675 2088 ▶ while ((he = hv_iternext(responses))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(responses))) { 1149 939 2088
676 1149 I32 kl; const char *k = hv_iterkey(he, &kl);
677 1149 char *end = NULL;
678 1149 long s = strtol(k, &end, 10);
679 1149 ▶ if (!end || end != k + kl || s < 100 || s > 599) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!end || end != k + kl || s < 100 || s > 599) continue; 1149 0 1149
if (!end || end != k + kl || s < 100 || s > 599) continue; 1047 102 1149
if (!end || end != k + kl || s < 100 || s > 599) continue; 1047 0 1047
if (!end || end != k + kl || s < 100 || s > 599) continue; 0 1047 1047
680 1047 ▶ if (s >= 200 && s < 300 && (!best2 || s < best2)) {
Branch: 4 branches
BranchTrueFalseTotal
if (s >= 200 && s < 300 && (!best2 || s < best2)) { 1047 0 1047
if (s >= 200 && s < 300 && (!best2 || s < best2)) { 939 108 1047
if (s >= 200 && s < 300 && (!best2 || s < best2)) { 0 939 939
if (s >= 200 && s < 300 && (!best2 || s < best2)) { 0 0 0
681 939 best2 = (int)s; best2sv = hv_iterval(responses, he);
682 }
683 1047 ▶ if (!bestany || s < bestany) {
Branch: 2 branches
BranchTrueFalseTotal
if (!bestany || s < bestany) { 108 939 1047
if (!bestany || s < bestany) { 106 2 108
684 1045 bestany = (int)s; bestanysv = hv_iterval(responses, he);
685 }
686 }
687 939 ▶ if (best2) { *robj = oa_hv_of(best2sv); return best2; }
Branch: 1 branch
BranchTrueFalseTotal
if (best2) { *robj = oa_hv_of(best2sv); return best2; } 939 0 939
688 {
689 0 SV **d = hv_fetch(responses, "default", 7, 0);
690 0 ▶ if (d && *d) { *robj = oa_hv_of(*d); return 200; }
Branch: 2 branches
BranchTrueFalseTotal
if (d && *d) { *robj = oa_hv_of(*d); return 200; } 0 0 0
if (d && *d) { *robj = oa_hv_of(*d); return 200; } 0 0 0
691 }
692 0 ▶ if (bestany) { *robj = oa_hv_of(bestanysv); return bestany; }
Branch: 1 branch
BranchTrueFalseTotal
if (bestany) { *robj = oa_hv_of(bestanysv); return bestany; } 0 0 0
693 }
694 0 return 200;
695 }
696  
697 /* the JSON media entry of a content object: exact application/json first,
698 * then the lexicographically first declared JSON type (deterministic where
699 * hash order is not). *ctype_out borrowed. NULL when none. */
700 954 static HV *oa_mock_media(pTHX_ HV *content, SV **ctype_out) {
701 HE *he;
702 954 SV *bestk = NULL, *bestv = NULL;
703 SV **exact;
704 954 *ctype_out = NULL;
705 954 ▶ if (!content) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!content) return NULL; 505 449 954
706 449 exact = hv_fetch(content, "application/json", 16, 0);
707 449 ▶ if (exact && *exact && oa_hv_of(*exact)) {
Branch: 3 branches
BranchTrueFalseTotal
if (exact && *exact && oa_hv_of(*exact)) { 449 0 449
if (exact && *exact && oa_hv_of(*exact)) { 449 0 449
if (exact && *exact && oa_hv_of(*exact)) { 449 0 449
708 449 SV *ct = sv_newmortal();
709 449 sv_setpvn(ct, "application/json", 16);
710 449 *ctype_out = ct;
711 449 return oa_hv_of(*exact);
712 }
713 0 hv_iterinit(content);
714 0 ▶ while ((he = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(content))) { 0 0 0
715 0 I32 kl; const char *k = hv_iterkey(he, &kl);
716 0 ▶ if (!oa_ctype_is_json(k, (STRLEN)kl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_ctype_is_json(k, (STRLEN)kl)) continue; 0 0 0
717 0 ▶ if (!oa_hv_of(hv_iterval(content, he))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_hv_of(hv_iterval(content, he))) continue; 0 0 0
718 0 ▶ if (!bestk || strcmp(k, SvPV_nolen(bestk)) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!bestk || strcmp(k, SvPV_nolen(bestk)) < 0) { 0 0 0
if (!bestk || strcmp(k, SvPV_nolen(bestk)) < 0) { 0 0 0
719 0 ▶ if (!bestk) bestk = sv_newmortal();
Branch: 1 branch
BranchTrueFalseTotal
if (!bestk) bestk = sv_newmortal(); 0 0 0
720 0 sv_setpvn(bestk, k, (STRLEN)kl);
721 0 bestv = hv_iterval(content, he);
722 }
723 }
724 0 ▶ if (!bestv) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!bestv) return NULL; 0 0 0
725 0 *ctype_out = bestk;
726 0 return oa_hv_of(bestv);
727 }
728  
729 /* "code=404, example=named" - the value of one token of a Prefer header.
730 * Mortal SV, or NULL. Values may be double-quoted. */
731 10 static SV *oa_prefer_token(pTHX_ SV *prefer, const char *name, STRLEN nl) {
732 STRLEN pl; const char *pp;
733 10 STRLEN i = 0;
734 10 ▶ if (!prefer || !SvOK(prefer)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!prefer || !SvOK(prefer)) return NULL; 10 0 10
if (!prefer || !SvOK(prefer)) return NULL; 0 10 10
735 10 pp = SvPV_const(prefer, pl);
736 16 ▶ while (i < pl) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < pl) { 11 5 16
737 11 STRLEN e = i, eq, vs, ve;
738 131 ▶ while (e < pl && pp[e] != ',' && pp[e] != ';') e++;
Branch: 3 branches
BranchTrueFalseTotal
while (e < pl && pp[e] != ',' && pp[e] != ';') e++; 122 9 131
while (e < pl && pp[e] != ',' && pp[e] != ';') e++; 120 2 122
while (e < pl && pp[e] != ',' && pp[e] != ';') e++; 120 0 120
739 14 ▶ while (i < e && isSPACE((U8)pp[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < e && isSPACE((U8)pp[i])) i++; 14 0 14
while (i < e && isSPACE((U8)pp[i])) i++; 3 11 14
740 11 eq = i;
741 68 ▶ while (eq < e && pp[eq] != '=') eq++;
Branch: 2 branches
BranchTrueFalseTotal
while (eq < e && pp[eq] != '=') eq++; 67 1 68
while (eq < e && pp[eq] != '=') eq++; 57 10 67
742 {
743 11 STRLEN ne = eq;
744 13 ▶ while (ne > i && isSPACE((U8)pp[ne-1])) ne--;
Branch: 2 branches
BranchTrueFalseTotal
while (ne > i && isSPACE((U8)pp[ne-1])) ne--; 13 0 13
while (ne > i && isSPACE((U8)pp[ne-1])) ne--; 2 11 13
745 11 ▶ if (ne - i == nl) {
Branch: 1 branch
BranchTrueFalseTotal
if (ne - i == nl) { 5 6 11
746 STRLEN k;
747 5 int same = 1;
748 31 ▶ for (k = 0; k < nl; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nl; k++) 26 5 31
749 26 ▶ if (toLOWER((U8)pp[i+k]) != (U8)name[k]) { same = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (toLOWER((U8)pp[i+k]) != (U8)name[k]) { same = 0; break; } 0 26 26
if (toLOWER((U8)pp[i+k]) != (U8)name[k]) { same = 0; break; } 0 26 26
750 5 ▶ if (same && eq < e) {
Branch: 2 branches
BranchTrueFalseTotal
if (same && eq < e) { 5 0 5
if (same && eq < e) { 5 0 5
751 5 vs = eq + 1;
752 5 ve = e;
753 6 ▶ while (vs < ve && isSPACE((U8)pp[vs])) vs++;
Branch: 2 branches
BranchTrueFalseTotal
while (vs < ve && isSPACE((U8)pp[vs])) vs++; 6 0 6
while (vs < ve && isSPACE((U8)pp[vs])) vs++; 1 5 6
754 6 ▶ while (ve > vs && isSPACE((U8)pp[ve-1])) ve--;
Branch: 2 branches
BranchTrueFalseTotal
while (ve > vs && isSPACE((U8)pp[ve-1])) ve--; 6 0 6
while (ve > vs && isSPACE((U8)pp[ve-1])) ve--; 1 5 6
755 5 ▶ if (ve > vs + 1 && pp[vs] == '"' && pp[ve-1] == '"') { vs++; ve--; }
Branch: 3 branches
BranchTrueFalseTotal
if (ve > vs + 1 && pp[vs] == '"' && pp[ve-1] == '"') { vs++; ve--; } 5 0 5
if (ve > vs + 1 && pp[vs] == '"' && pp[ve-1] == '"') { vs++; ve--; } 1 4 5
if (ve > vs + 1 && pp[vs] == '"' && pp[ve-1] == '"') { vs++; ve--; } 1 0 1
756 5 return sv_2mortal(newSVpvn(pp + vs, ve - vs));
757 }
758 }
759 }
760 6 i = e + 1;
761 }
762 5 return NULL;
763 }
764  
765 /* ---- the synthesized response ---------------------------------------------- */
766  
767 /* Canonical JSON (sorted keys, minimal whitespace) through the frj ABI.
768 * Perl randomises hash iteration order per hash, so an ordinary encode of a
769 * freshly built object would shuffle its keys on every request - canonical
770 * form is what makes "byte-identical bodies" true, and it holds across
771 * processes, not merely within one. (+1; croaks on an unencodable shape,
772 * which a value built from a decoded document cannot be.) */
773 449 static SV *oa_mock_encode(pTHX_ SV *val) {
774 449 const frj_abi *J = oa_frj(aTHX);
775 frj_opts o;
776 449 J->opts_init(&o);
777 449 o.canonical = 1;
778 449 return J->encode(aTHX_ val, &o);
779 }
780  
781 /* Build the PSGI triplet for op + options (+1).
782 *
783 * want_code 0, or the status Prefer asked for
784 * want_name NULL, or the named example Prefer asked for
785 *
786 * The body precedence is the header comment's; a response with no JSON
787 * content answers with an empty body and no Content-Type, which is what a
788 * 204 or a bare "404: no such pet" declares. */
789 954 static SV *oa_synthesize(pTHX_ oa_api *a, oa_op *o, int want_code, SV *want_name,
790 HV *caps) {
791 954 HV *doc = (HV *)SvRV(a->spec);
792 954 HV *rawop = oa_raw_op(aTHX_ a, o);
793 954 ▶ HV *responses = rawop ? oa_hv_of(oa_get(aTHX_ rawop, "responses")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *responses = rawop ? oa_hv_of(oa_get(aTHX_ rawop, "responses")) : NULL; 954 0 954
794 954 HV *robj = NULL;
795 954 int status = oa_mock_status(aTHX_ responses, want_code, &robj);
796 954 ▶ HV *content = robj ? oa_hv_of(oa_get(aTHX_ robj, "content")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *content = robj ? oa_hv_of(oa_get(aTHX_ robj, "content")) : NULL; 953 1 954
797 954 SV *ctype = NULL;
798 954 HV *media = oa_mock_media(aTHX_ content, &ctype);
799 954 SV *value = NULL, *json = NULL;
800 AV *ha, *ba, *tv;
801  
802 954 ▶ if (media) {
Branch: 1 branch
BranchTrueFalseTotal
if (media) { 449 505 954
803 SV *ex;
804 /* 1/2. example, or a named / the first examples entry */
805 449 ▶ if (want_name && SvOK(want_name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (want_name && SvOK(want_name)) { 3 446 449
if (want_name && SvOK(want_name)) { 3 0 3
806 3 HV *exs = oa_hv_of(oa_get(aTHX_ media, "examples"));
807 3 STRLEN wl; const char *wp = SvPV_const(want_name, wl);
808 3 ▶ SV **e = exs ? hv_fetch(exs, wp, (I32)wl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = exs ? hv_fetch(exs, wp, (I32)wl, 0) : NULL; 3 0 3
809 3 ▶ HV *eh = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *eh = (e && *e) ? oa_hv_of(*e) : NULL; 2 1 3
HV *eh = (e && *e) ? oa_hv_of(*e) : NULL; 2 0 2
810 3 ▶ if (eh) value = oa_get(aTHX_ eh, "value");
Branch: 1 branch
BranchTrueFalseTotal
if (eh) value = oa_get(aTHX_ eh, "value"); 2 1 3
811 }
812 449 ▶ if (!value && (ex = oa_get(aTHX_ media, "example")))
Branch: 2 branches
BranchTrueFalseTotal
if (!value && (ex = oa_get(aTHX_ media, "example"))) 447 2 449
if (!value && (ex = oa_get(aTHX_ media, "example"))) 108 339 447
813 108 value = ex;
814 449 ▶ if (!value) {
Branch: 1 branch
BranchTrueFalseTotal
if (!value) { 339 110 449
815 339 HV *exs = oa_hv_of(oa_get(aTHX_ media, "examples"));
816 339 ▶ if (exs) {
Branch: 1 branch
BranchTrueFalseTotal
if (exs) { 104 235 339
817 HE *he;
818 104 SV *bestk = NULL, *bestv = NULL;
819 104 hv_iterinit(exs);
820 312 ▶ while ((he = hv_iternext(exs))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(exs))) { 208 104 312
821 208 I32 kl; const char *k = hv_iterkey(he, &kl);
822 312 ▶ if (!bestk || strcmp(k, SvPV_nolen(bestk)) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!bestk || strcmp(k, SvPV_nolen(bestk)) < 0) { 104 104 208
if (!bestk || strcmp(k, SvPV_nolen(bestk)) < 0) { 102 2 104
823 206 ▶ if (!bestk) bestk = sv_newmortal();
Branch: 1 branch
BranchTrueFalseTotal
if (!bestk) bestk = sv_newmortal(); 104 102 206
824 206 sv_setpvn(bestk, k, (STRLEN)kl);
825 206 bestv = hv_iterval(exs, he);
826 }
827 }
828 104 ▶ if (bestv && oa_hv_of(bestv))
Branch: 2 branches
BranchTrueFalseTotal
if (bestv && oa_hv_of(bestv)) 104 0 104
if (bestv && oa_hv_of(bestv)) 104 0 104
829 104 value = oa_get(aTHX_ oa_hv_of(bestv), "value");
830 }
831 }
832 /* 3/4. the schema's default, else generated from the schema */
833 449 ▶ if (!value) {
Branch: 1 branch
BranchTrueFalseTotal
if (!value) { 235 214 449
834 235 SV *schema = oa_get(aTHX_ media, "schema");
835 235 ▶ SV *res = schema ? oa_mock_deref(aTHX_ doc, schema, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *res = schema ? oa_mock_deref(aTHX_ doc, schema, 0) : NULL; 235 0 235
836 235 ▶ HV *sh = res ? oa_hv_of(res) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *sh = res ? oa_hv_of(res) : NULL; 235 0 235
837 235 ▶ SV *def = sh ? oa_get(aTHX_ sh, "default") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *def = sh ? oa_get(aTHX_ sh, "default") : NULL; 235 0 235
838 235 ▶ if (def) value = def;
Branch: 1 branch
BranchTrueFalseTotal
if (def) value = def; 103 132 235
839 132 ▶ else if (schema) value = sv_2mortal(oa_gen(aTHX_ doc, schema, caps, 0));
Branch: 1 branch
BranchTrueFalseTotal
else if (schema) value = sv_2mortal(oa_gen(aTHX_ doc, schema, caps, 0)); 132 0 132
840 }
841 449 ▶ if (value) json = sv_2mortal(oa_mock_encode(aTHX_ value));
Branch: 1 branch
BranchTrueFalseTotal
if (value) json = sv_2mortal(oa_mock_encode(aTHX_ value)); 449 0 449
842 }
843  
844 /* nothing owned is allocated before this point, so the encode above may
845 * croak without leaking */
846 954 ha = newAV(); ba = newAV(); tv = newAV();
847 954 ▶ if (json) {
Branch: 1 branch
BranchTrueFalseTotal
if (json) { 449 505 954
848 449 av_push(ha, newSVpvs("Content-Type"));
849 449 av_push(ha, newSVsv(ctype));
850 449 av_push(ha, newSVpvs("Content-Length"));
851 449 av_push(ha, newSVuv((UV)SvCUR(json)));
852 449 av_push(ba, newSVsv(json));
853 } else {
854 505 av_push(ha, newSVpvs("Content-Length"));
855 505 av_push(ha, newSVpvs("0"));
856 505 av_push(ba, newSVpvs(""));
857 }
858 954 av_push(tv, newSViv(status));
859 954 av_push(tv, newRV_noinc((SV *)ha));
860 954 av_push(tv, newRV_noinc((SV *)ba));
861 954 return newRV_noinc((SV *)tv);
862 }
863  
864 #endif /* OA_MOCK_H */
« include/oa_client.h ↑ Summary include/oa_abi_impl.h »