include/oa_mock.h

filter: Statementstmt 86.5% 418 / 483 Branchbran 52.5% 414 / 788 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.4% 832 / 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 681 static SV *oa_doc_at(pTHX_ HV *doc, const char *ref, STRLEN rl) {
64 681 SV *node = NULL;
65 681 STRLEN i = 0;
66 681 ▶ if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL; 681 0 681
if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL; 681 0 681
if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL; 681 0 681
if (!doc || rl < 2 || ref[0] != '#' || ref[1] != '/') return NULL; 0 681 681
67 681 i = 2;
68 2724 ▶ while (i <= rl) {
Branch: 1 branch
BranchTrueFalseTotal
while (i <= rl) { 2043 681 2724
69 char seg[256];
70 2043 STRLEN sl = 0, e = i;
71 17896 ▶ while (e < rl && ref[e] != '/') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < rl && ref[e] != '/') e++; 17215 681 17896
while (e < rl && ref[e] != '/') e++; 15853 1362 17215
72 /* unescape ~1 -> '/', ~0 -> '~' */
73 17896 ▶ while (i < e && sl + 1 < sizeof seg) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < e && sl + 1 < sizeof seg) { 15853 2043 17896
while (i < e && sl + 1 < sizeof seg) { 15853 0 15853
74 15853 ▶ 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 15853 15853
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 15853 } else seg[sl++] = ref[i++];
78 }
79 2043 ▶ if (!node) {
Branch: 1 branch
BranchTrueFalseTotal
if (!node) { 681 1362 2043
80 681 SV **v = hv_fetch(doc, seg, (I32)sl, 0);
81 681 ▶ node = (v && *v) ? *v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
node = (v && *v) ? *v : NULL; 681 0 681
node = (v && *v) ? *v : NULL; 681 0 681
82 1362 ▶ } else if (oa_hv_of(node)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (oa_hv_of(node)) { 1362 0 1362
83 1362 SV **v = hv_fetch(oa_hv_of(node), seg, (I32)sl, 0);
84 1362 ▶ node = (v && *v) ? *v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
node = (v && *v) ? *v : NULL; 1362 0 1362
node = (v && *v) ? *v : NULL; 1362 0 1362
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 2043 ▶ if (!node) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!node) return NULL; 0 2043 2043
94 2043 i = e + 1;
95 }
96 681 return node;
97 }
98  
99 /* follow $ref until a concrete schema (or the depth guard). Borrowed. */
100 3821 static SV *oa_mock_deref(pTHX_ HV *doc, SV *schema, int depth) {
101 3821 int hops = 0;
102 4498 ▶ while (schema && hops++ < OA_MOCK_MAX_DEPTH) {
Branch: 2 branches
BranchTrueFalseTotal
while (schema && hops++ < OA_MOCK_MAX_DEPTH) { 4498 0 4498
while (schema && hops++ < OA_MOCK_MAX_DEPTH) { 4498 0 4498
103 4498 HV *h = oa_hv_of(schema);
104 4498 ▶ SV *r = h ? oa_get(aTHX_ h, "$ref") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *r = h ? oa_get(aTHX_ h, "$ref") : NULL; 4498 0 4498
105 STRLEN rl; const char *rp;
106 4498 ▶ if (!r || SvROK(r)) return schema;
Branch: 2 branches
BranchTrueFalseTotal
if (!r || SvROK(r)) return schema; 677 3821 4498
if (!r || SvROK(r)) return schema; 0 677 677
107 677 rp = SvPV_const(r, rl);
108 677 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 661 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 661 ▶ 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; 529 132 661
if (!n || !elem || !SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVHV) return; 529 0 529
if (!n || !elem || !SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVHV) return; 108 421 529
if (!n || !elem || !SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVHV) return; 0 108 108
235 108 eh = (HV *)SvRV(elem);
236  
237 108 ▶ deref = items ? oa_mock_deref(aTHX_ doc, items, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
deref = items ? oa_mock_deref(aTHX_ doc, items, 0) : NULL; 108 0 108
238 108 ▶ sh = deref ? oa_hv_of(deref) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
sh = deref ? oa_hv_of(deref) : NULL; 108 0 108
239 108 ▶ 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; 108 0 108
240 108 ▶ if (!props) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!props) return; 36 72 108
241  
242 72 hv_iterinit(eh);
243 168 ▶ while ((he = hv_iternext(eh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(eh))) { 96 72 168
244 I32 kl;
245 96 const char *kp = hv_iterkey(he, &kl);
246 96 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 96 ▶ if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i'
Branch: 4 branches
BranchTrueFalseTotal
if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 96 0 96
if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 0 96 96
if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 0 0 0
if (!(kl >= 2 && toLOWER((U8)kp[kl - 2]) == 'i' 48 48 96
255 96 ▶ && toLOWER((U8)kp[kl - 1]) == 'd')) continue;
Branch: 3 branches
BranchTrueFalseTotal
&& toLOWER((U8)kp[kl - 1]) == 'd')) continue; 0 48 48
&& toLOWER((U8)kp[kl - 1]) == 'd')) continue; 0 0 0
&& toLOWER((U8)kp[kl - 1]) == 'd')) continue; 0 48 48
256 48 ▶ if (!val || !SvOK(val) || SvROK(val)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!val || !SvOK(val) || SvROK(val)) continue; 48 0 48
if (!val || !SvOK(val) || SvROK(val)) continue; 48 0 48
if (!val || !SvOK(val) || SvROK(val)) continue; 0 48 48
257  
258 48 ps = hv_fetch(props, kp, kl, 0);
259 48 ▶ if (!ps || !*ps) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!ps || !*ps) continue; 48 0 48
if (!ps || !*ps) continue; 0 48 48
260 48 ph = oa_hv_of(oa_mock_deref(aTHX_ doc, *ps, 0));
261 48 ▶ if (!ph) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ph) continue; 0 48 48
262  
263 48 t = oa_gen_type_of(aTHX_ ph);
264 48 ▶ if (!t) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) continue; 0 48 48
265 48 tp = SvPV_const(t, tl);
266 48 ▶ if (!(tl == 7 && memEQ(tp, "integer", 7))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(tl == 7 && memEQ(tp, "integer", 7))) continue; 48 0 48
if (!(tl == 7 && memEQ(tp, "integer", 7))) continue; 0 48 48
267  
268 48 mx = oa_get(aTHX_ ph, "maximum");
269 48 ▶ if (mx && (NV)(SvIV(val) + n) > SvNV(mx)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (mx && (NV)(SvIV(val) + n) > SvNV(mx)) continue; 0 48 48
if (mx && (NV)(SvIV(val) + n) > SvNV(mx)) continue; 0 0 0
270 48 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 2614 static SV *oa_gen_type_of(pTHX_ HV *h) {
323 2614 SV *t = oa_get(aTHX_ h, "type");
324 2614 ▶ if (!t) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return NULL; 54 2560 2614
325 2560 ▶ if (!SvROK(t)) return t;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(t)) return t; 2457 103 2560
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 1177 static SV *oa_gen_string(pTHX_ HV *h) {
358 1177 SV *f = oa_get(aTHX_ h, "format");
359 1177 SV *minl = oa_get(aTHX_ h, "minLength");
360 1177 SV *maxl = oa_get(aTHX_ h, "maxLength");
361 SV *out;
362 1177 const char *fixed = NULL;
363 1177 ▶ if (f && !SvROK(f)) {
Branch: 2 branches
BranchTrueFalseTotal
if (f && !SvROK(f)) { 206 971 1177
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 1177 ▶ out = newSVpv(fixed ? fixed : "string", 0);
Branch: 1 branch
BranchTrueFalseTotal
out = newSVpv(fixed ? fixed : "string", 0); 206 971 1177
368 1177 ▶ if (minl && SvIV(minl) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (minl && SvIV(minl) > 0) { 721 456 1177
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 1177 ▶ 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 1177 1177
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 1177 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 661 static SV *oa_gen_number(pTHX_ HV *h, int is_int) {
384 661 NV v = 0.0;
385 661 SV *mi = oa_get(aTHX_ h, "minimum");
386 661 SV *xm = oa_get(aTHX_ h, "exclusiveMinimum");
387 661 SV *ma = oa_get(aTHX_ h, "maximum");
388 661 SV *mo = oa_get(aTHX_ h, "multipleOf");
389 661 ▶ if (mi) v = SvNV(mi);
Branch: 1 branch
BranchTrueFalseTotal
if (mi) v = SvNV(mi); 206 455 661
390 779 ▶ if (xm && !SvROK(xm) && looks_like_number(xm)) {
Branch: 3 branches
BranchTrueFalseTotal
if (xm && !SvROK(xm) && looks_like_number(xm)) { 118 543 661
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 543 ▶ } else if (xm && SvTRUE(xm) && mi) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (xm && SvTRUE(xm) && mi) { 0 543 543
} 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 661 ▶ if (mo && SvNV(mo) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mo && SvNV(mo) > 0) { 103 558 661
if (mo && SvNV(mo) > 0) { 103 0 103
398 103 NV m = SvNV(mo);
399 103 v = ceil(v / m) * m;
400 }
401 661 ▶ 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 660 661
if (ma && SvNV(ma) < v) v = SvNV(ma); /* contradictory spec: best effort */ 0 1 1
402 661 ▶ 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 558 661
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 168 static SV *oa_gen_allof(pTHX_ HV *doc, AV *all, HV *caps, int depth) {
410 168 HV *m = (HV *)sv_2mortal((SV *)newHV());
411 168 HV *mp = NULL;
412 168 AV *mr = NULL;
413 168 SSize_t i, n = av_len(all) + 1;
414 504 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 336 168 504
415 336 SV **e = av_fetch(all, i, 0);
416 336 ▶ 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; 336 0 336
SV *sub = e && *e ? oa_mock_deref(aTHX_ doc, *e, depth) : NULL; 336 0 336
417 336 ▶ HV *sh = sub ? oa_hv_of(sub) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *sh = sub ? oa_hv_of(sub) : NULL; 336 0 336
418 HE *he;
419 336 ▶ if (!sh) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sh) continue; 0 336 336
420 336 hv_iterinit(sh);
421 1285 ▶ while ((he = hv_iternext(sh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(sh))) { 949 336 1285
422 949 I32 kl; const char *k = hv_iterkey(he, &kl);
423 949 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 951 ▶ if (kl == 13 && memEQ(k, "discriminator", 13)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 13 && memEQ(k, "discriminator", 13)) continue; 2 947 949
if (kl == 13 && memEQ(k, "discriminator", 13)) continue; 2 0 2
430 947 ▶ if (kl == 5 && memEQ(k, "allOf", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 5 && memEQ(k, "allOf", 5)) { 2 945 947
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 1281 ▶ if (kl == 10 && memEQ(k, "properties", 10)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 10 && memEQ(k, "properties", 10)) { 336 609 945
if (kl == 10 && memEQ(k, "properties", 10)) { 336 0 336
448 336 HV *ph = oa_hv_of(v);
449 HE *pe;
450 336 ▶ if (!ph) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ph) continue; 0 336 336
451 336 ▶ if (!mp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mp) { 168 168 336
452 168 mp = newHV();
453 168 (void)hv_stores(m, "properties", newRV_noinc((SV *)mp));
454 }
455 336 hv_iterinit(ph);
456 737 ▶ while ((pe = hv_iternext(ph))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((pe = hv_iternext(ph))) { 401 336 737
457 401 I32 pkl; const char *pk = hv_iterkey(pe, &pkl);
458 401 (void)hv_store(mp, pk, pkl,
459 SvREFCNT_inc(hv_iterval(ph, pe)), 0);
460 }
461 945 ▶ } else if (kl == 8 && memEQ(k, "required", 8)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (kl == 8 && memEQ(k, "required", 8)) { 336 273 609
} else if (kl == 8 && memEQ(k, "required", 8)) { 336 0 336
462 336 AV *ra = oa_av_of(v);
463 336 ▶ SSize_t j, rn = ra ? av_len(ra) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, rn = ra ? av_len(ra) + 1 : 0; 336 0 336
464 336 ▶ if (!mr) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mr) { 168 168 336
465 168 mr = newAV();
466 168 (void)hv_stores(m, "required", newRV_noinc((SV *)mr));
467 }
468 674 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 338 336 674
469 338 SV **r = av_fetch(ra, j, 0);
470 338 ▶ if (r && *r) av_push(mr, newSVsv(*r));
Branch: 2 branches
BranchTrueFalseTotal
if (r && *r) av_push(mr, newSVsv(*r)); 338 0 338
if (r && *r) av_push(mr, newSVsv(*r)); 338 0 338
471 }
472 } else {
473 273 (void)hv_store(m, k, kl, SvREFCNT_inc(v), 0);
474 }
475 }
476 }
477 168 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 3041 static SV *oa_gen(pTHX_ HV *doc, SV *schema, HV *caps, int depth) {
484 HV *h;
485 SV *v, *t;
486 3041 ▶ if (depth > OA_MOCK_MAX_DEPTH) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (depth > OA_MOCK_MAX_DEPTH) return newSV(0); 0 3041 3041
487 3041 schema = oa_mock_deref(aTHX_ doc, schema, depth);
488 3041 ▶ h = schema ? oa_hv_of(schema) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
h = schema ? oa_hv_of(schema) : NULL; 3041 0 3041
489 3041 ▶ 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 3041 3041
490  
491 3041 ▶ 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 2938 3041
492 2938 ▶ if ((v = oa_get(aTHX_ h, "enum"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "enum"))) { 103 2835 2938
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 2835 ▶ 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 2835 2835
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 2835 ▶ if ((v = oa_get(aTHX_ h, "discriminator"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = oa_get(aTHX_ h, "discriminator"))) { 4 2831 2835
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 2831 ▶ 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)) 168 2663 2831
if ((v = oa_get(aTHX_ h, "allOf")) && oa_av_of(v)) 168 0 168
540 168 return oa_gen_allof(aTHX_ doc, oa_av_of(v), caps, depth);
541 2663 ▶ 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 2560 2663
if (((v = oa_get(aTHX_ h, "oneOf")) && oa_av_of(v)) 0 103 103
542 2560 ▶ || ((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 2560 2560
|| ((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 2560 t = oa_gen_type_of(aTHX_ h);
548 2560 ▶ if (!t) {
Branch: 1 branch
BranchTrueFalseTotal
if (!t) { 54 2506 2560
549 /* no type declared: shape hints, then null (valid anywhere) */
550 54 ▶ 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 54 54
if (oa_get(aTHX_ h, "properties") || oa_get(aTHX_ h, "required")) 0 0 0
551 54 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 2560 STRLEN tl; const char *tp = SvPV_const(t, tl);
559 2560 ▶ if (tl == 6 && memEQ(tp, "string", 6))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(tp, "string", 6)) 1766 794 2560
if (tl == 6 && memEQ(tp, "string", 6)) 1177 589 1766
560 2560 return oa_gen_string(aTHX_ h);
561 1383 ▶ if (tl == 7 && memEQ(tp, "integer", 7))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 7 && memEQ(tp, "integer", 7)) 661 722 1383
if (tl == 7 && memEQ(tp, "integer", 7)) 558 103 661
562 558 return oa_gen_number(aTHX_ h, 1);
563 825 ▶ if (tl == 6 && memEQ(tp, "number", 6))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(tp, "number", 6)) 589 236 825
if (tl == 6 && memEQ(tp, "number", 6)) 103 486 589
564 103 return oa_gen_number(aTHX_ h, 0);
565 722 ▶ if (tl == 7 && memEQ(tp, "boolean", 7))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 7 && memEQ(tp, "boolean", 7)) 103 619 722
if (tl == 7 && memEQ(tp, "boolean", 7)) 103 0 103
566 103 return newRV_noinc(newSViv(1)); /* \1 encodes as true */
567 619 ▶ if (tl == 4 && memEQ(tp, "null", 4))
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 4 && memEQ(tp, "null", 4)) 0 619 619
if (tl == 4 && memEQ(tp, "null", 4)) 0 0 0
568 0 return newSV(0);
569 619 ▶ if (tl == 5 && memEQ(tp, "array", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 5 && memEQ(tp, "array", 5)) { 133 486 619
if (tl == 5 && memEQ(tp, "array", 5)) { 133 0 133
570 133 AV *out = newAV();
571 133 SV *items = oa_get(aTHX_ h, "items");
572 133 SV *mn = oa_get(aTHX_ h, "minItems");
573 133 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 133 IV want = OA_MOCK_ARRAY_ITEMS, i;
587 133 ▶ if (mn && SvIV(mn) > want) want = SvIV(mn);
Branch: 2 branches
BranchTrueFalseTotal
if (mn && SvIV(mn) > want) want = SvIV(mn); 104 29 133
if (mn && SvIV(mn) > want) want = SvIV(mn); 1 103 104
588 133 ▶ if (mx && SvIV(mx) < want) want = SvIV(mx);
Branch: 2 branches
BranchTrueFalseTotal
if (mx && SvIV(mx) < want) want = SvIV(mx); 2 131 133
if (mx && SvIV(mx) < want) want = SvIV(mx); 2 0 2
589 133 ▶ if (want > 64) want = 64; /* bounded, like the pad */
Branch: 1 branch
BranchTrueFalseTotal
if (want > 64) want = 64; /* bounded, like the pad */ 0 133 133
590 133 ▶ if (want < 0) want = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (want < 0) want = 0; 0 133 133
591 794 ▶ for (i = 0; i < want; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < want; i++) { 661 133 794
592 661 SV *el = items ? oa_gen(aTHX_ doc, items, caps, depth + 1)
593 661 ▶ : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
: newSV(0); 661 0 661
594 661 oa_seq_id(aTHX_ doc, items, el, i);
595 661 av_push(out, el);
596 }
597 133 return newRV_noinc((SV *)out);
598 }
599 486 ▶ if (tl == 6 && memEQ(tp, "object", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(tp, "object", 6)) { 486 0 486
if (tl == 6 && memEQ(tp, "object", 6)) { 486 0 486
600 486 HV *out = newHV();
601 486 HV *props = oa_hv_of(oa_get(aTHX_ h, "properties"));
602 486 AV *req = oa_av_of(oa_get(aTHX_ h, "required"));
603 486 ▶ SSize_t i, n = req ? av_len(req) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = req ? av_len(req) + 1 : 0; 452 34 486
604 2420 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1934 486 2420
605 1934 SV **e = av_fetch(req, i, 0);
606 STRLEN kl; const char *kp;
607 SV **ps;
608 1934 ▶ if (!e || !*e || !SvOK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvOK(*e)) continue; 1934 0 1934
if (!e || !*e || !SvOK(*e)) continue; 1934 0 1934
if (!e || !*e || !SvOK(*e)) continue; 0 1934 1934
609 1934 SV *echo = NULL;
610 1934 kp = SvPV_const(*e, kl);
611 1934 ▶ ps = props ? hv_fetch(props, kp, (I32)kl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ps = props ? hv_fetch(props, kp, (I32)kl, 0) : NULL; 1934 0 1934
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 1934 ▶ if (caps && ps && *ps) {
Branch: 3 branches
BranchTrueFalseTotal
if (caps && ps && *ps) { 10 1924 1934
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 1934 ▶ (void)hv_store(out, kp, (I32)kl,
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_store(out, kp, (I32)kl, 1930 4 1934
(void)hv_store(out, kp, (I32)kl, 1930 0 1930
(void)hv_store(out, kp, (I32)kl, 1930 0 1930
623 echo ? echo
624 : (ps && *ps) ? oa_gen(aTHX_ doc, *ps, caps, depth + 1)
625 : newSV(0), 0);
626 }
627 486 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 1005 static HV *oa_raw_op(pTHX_ oa_api *a, oa_op *o) {
637 1005 HV *doc = (HV *)SvRV(a->spec);
638 1005 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 1005 ▶ if (!paths) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!paths) return NULL; 0 1005 1005
644 1005 pp = SvPV_const(o->path, pl);
645 1005 e = hv_fetch(paths, pp, (I32)pl, 0);
646 1005 ▶ item = (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
item = (e && *e) ? *e : NULL; 1005 0 1005
item = (e && *e) ? *e : NULL; 1005 0 1005
647 1005 ▶ ih = item ? oa_hv_of(item) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ih = item ? oa_hv_of(item) : NULL; 1005 0 1005
648 1005 ▶ if (!ih) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!ih) return NULL; 0 1005 1005
649 2010 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 1005 static int oa_mock_status(pTHX_ HV *responses, int want, HV **robj) {
656 HE *he;
657 char kb[8];
658 1005 *robj = NULL;
659 1005 ▶ if (!responses) return want ? want : 200;
Branch: 2 branches
BranchTrueFalseTotal
if (!responses) return want ? want : 200; 0 1005 1005
if (!responses) return want ? want : 200; 0 0 0
660  
661 1005 ▶ if (want) {
Branch: 1 branch
BranchTrueFalseTotal
if (want) { 35 970 1005
662 SV **e;
663 35 STRLEN kl = (STRLEN)my_snprintf(kb, sizeof kb, "%d", want);
664 35 e = hv_fetch(responses, kb, (I32)kl, 0);
665 35 ▶ if (e && *e) { *robj = oa_hv_of(*e); return want; }
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) { *robj = oa_hv_of(*e); return want; } 33 2 35
if (e && *e) { *robj = oa_hv_of(*e); return want; } 33 0 33
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 970 int best2 = 0, bestany = 0;
673 970 SV *best2sv = NULL, *bestanysv = NULL;
674 970 hv_iterinit(responses);
675 2179 ▶ while ((he = hv_iternext(responses))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(responses))) { 1209 970 2179
676 1209 I32 kl; const char *k = hv_iterkey(he, &kl);
677 1209 char *end = NULL;
678 1209 long s = strtol(k, &end, 10);
679 1209 ▶ if (!end || end != k + kl || s < 100 || s > 599) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!end || end != k + kl || s < 100 || s > 599) continue; 1209 0 1209
if (!end || end != k + kl || s < 100 || s > 599) continue; 1080 129 1209
if (!end || end != k + kl || s < 100 || s > 599) continue; 1080 0 1080
if (!end || end != k + kl || s < 100 || s > 599) continue; 0 1080 1080
680 1080 ▶ if (s >= 200 && s < 300 && (!best2 || s < best2)) {
Branch: 4 branches
BranchTrueFalseTotal
if (s >= 200 && s < 300 && (!best2 || s < best2)) { 1080 0 1080
if (s >= 200 && s < 300 && (!best2 || s < best2)) { 971 109 1080
if (s >= 200 && s < 300 && (!best2 || s < best2)) { 1 970 971
if (s >= 200 && s < 300 && (!best2 || s < best2)) { 0 1 1
681 970 best2 = (int)s; best2sv = hv_iterval(responses, he);
682 }
683 1080 ▶ if (!bestany || s < bestany) {
Branch: 2 branches
BranchTrueFalseTotal
if (!bestany || s < bestany) { 110 970 1080
if (!bestany || s < bestany) { 1 109 110
684 971 bestany = (int)s; bestanysv = hv_iterval(responses, he);
685 }
686 }
687 970 ▶ if (best2) { *robj = oa_hv_of(best2sv); return best2; }
Branch: 1 branch
BranchTrueFalseTotal
if (best2) { *robj = oa_hv_of(best2sv); return best2; } 970 0 970
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 1005 static HV *oa_mock_media(pTHX_ HV *content, SV **ctype_out) {
701 HE *he;
702 1005 SV *bestk = NULL, *bestv = NULL;
703 SV **exact;
704 1005 *ctype_out = NULL;
705 1005 ▶ if (!content) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!content) return NULL; 510 495 1005
706 495 exact = hv_fetch(content, "application/json", 16, 0);
707 495 ▶ if (exact && *exact && oa_hv_of(*exact)) {
Branch: 3 branches
BranchTrueFalseTotal
if (exact && *exact && oa_hv_of(*exact)) { 495 0 495
if (exact && *exact && oa_hv_of(*exact)) { 495 0 495
if (exact && *exact && oa_hv_of(*exact)) { 495 0 495
708 495 SV *ct = sv_newmortal();
709 495 sv_setpvn(ct, "application/json", 16);
710 495 *ctype_out = ct;
711 495 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 495 static SV *oa_mock_encode(pTHX_ SV *val) {
774 495 const frj_abi *J = oa_frj(aTHX);
775 frj_opts o;
776 495 J->opts_init(&o);
777 495 o.canonical = 1;
778 495 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 1005 static SV *oa_synthesize(pTHX_ oa_api *a, oa_op *o, int want_code, SV *want_name,
790 HV *caps) {
791 1005 HV *doc = (HV *)SvRV(a->spec);
792 1005 HV *rawop = oa_raw_op(aTHX_ a, o);
793 1005 ▶ 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; 1005 0 1005
794 1005 HV *robj = NULL;
795 1005 int status = oa_mock_status(aTHX_ responses, want_code, &robj);
796 1005 ▶ 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; 1004 1 1005
797 1005 SV *ctype = NULL;
798 1005 HV *media = oa_mock_media(aTHX_ content, &ctype);
799 1005 SV *value = NULL, *json = NULL;
800 AV *ha, *ba, *tv;
801  
802 1005 ▶ if (media) {
Branch: 1 branch
BranchTrueFalseTotal
if (media) { 495 510 1005
803 SV *ex;
804 /* 1/2. example, or a named / the first examples entry */
805 495 ▶ if (want_name && SvOK(want_name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (want_name && SvOK(want_name)) { 3 492 495
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 495 ▶ if (!value && (ex = oa_get(aTHX_ media, "example")))
Branch: 2 branches
BranchTrueFalseTotal
if (!value && (ex = oa_get(aTHX_ media, "example"))) 493 2 495
if (!value && (ex = oa_get(aTHX_ media, "example"))) 111 382 493
813 111 value = ex;
814 495 ▶ if (!value) {
Branch: 1 branch
BranchTrueFalseTotal
if (!value) { 382 113 495
815 382 HV *exs = oa_hv_of(oa_get(aTHX_ media, "examples"));
816 382 ▶ if (exs) {
Branch: 1 branch
BranchTrueFalseTotal
if (exs) { 104 278 382
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) { 2 102 104
823 106 ▶ if (!bestk) bestk = sv_newmortal();
Branch: 1 branch
BranchTrueFalseTotal
if (!bestk) bestk = sv_newmortal(); 104 2 106
824 106 sv_setpvn(bestk, k, (STRLEN)kl);
825 106 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 495 ▶ if (!value) {
Branch: 1 branch
BranchTrueFalseTotal
if (!value) { 278 217 495
834 278 SV *schema = oa_get(aTHX_ media, "schema");
835 278 ▶ 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; 278 0 278
836 278 ▶ HV *sh = res ? oa_hv_of(res) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *sh = res ? oa_hv_of(res) : NULL; 278 0 278
837 278 ▶ SV *def = sh ? oa_get(aTHX_ sh, "default") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *def = sh ? oa_get(aTHX_ sh, "default") : NULL; 278 0 278
838 278 ▶ if (def) value = def;
Branch: 1 branch
BranchTrueFalseTotal
if (def) value = def; 103 175 278
839 175 ▶ 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)); 175 0 175
840 }
841 495 ▶ if (value) json = sv_2mortal(oa_mock_encode(aTHX_ value));
Branch: 1 branch
BranchTrueFalseTotal
if (value) json = sv_2mortal(oa_mock_encode(aTHX_ value)); 495 0 495
842 }
843  
844 /* nothing owned is allocated before this point, so the encode above may
845 * croak without leaking */
846 1005 ha = newAV(); ba = newAV(); tv = newAV();
847 1005 ▶ if (json) {
Branch: 1 branch
BranchTrueFalseTotal
if (json) { 495 510 1005
848 495 av_push(ha, newSVpvs("Content-Type"));
849 495 av_push(ha, newSVsv(ctype));
850 495 av_push(ha, newSVpvs("Content-Length"));
851 495 av_push(ha, newSVuv((UV)SvCUR(json)));
852 495 av_push(ba, newSVsv(json));
853 } else {
854 510 av_push(ha, newSVpvs("Content-Length"));
855 510 av_push(ha, newSVpvs("0"));
856 510 av_push(ba, newSVpvs(""));
857 }
858 1005 av_push(tv, newSViv(status));
859 1005 av_push(tv, newRV_noinc((SV *)ha));
860 1005 av_push(tv, newRV_noinc((SV *)ba));
861 1005 return newRV_noinc((SV *)tv);
862 }
863  
864 #endif /* OA_MOCK_H */
« include/oa_client.h ↑ Summary include/oa_abi_impl.h »