| 1 |
|
| #ifndef OA_SWAGGER2_H |
| 2 |
|
| #define OA_SWAGGER2_H |
| 3 |
|
| |
| 4 |
|
| /* Swagger 2.0 -> OpenAPI 3.0 shape, at document level. |
| 5 |
|
| * |
| 6 |
|
| * A 2.0 Schema Object is a draft-04 subset of 3.0's, so the 3.0 pass in |
| 7 |
|
| * oa_normalize.h already does the schema work (boolean exclusive bounds, |
| 8 |
|
| * tuple items, example, byte/binary, $ref siblings, discriminator). This |
| 9 |
|
| * pass does only what 2.0 spells DIFFERENTLY: the root (host, basePath, |
| 10 |
|
| * schemes, the definition sections, consumes, produces, securityDefinitions), |
| 11 |
|
| * the operation (body and formData parameters, response schema and |
| 12 |
|
| * examples), the parameter (inline type keywords, Items, collectionFormat) |
| 13 |
|
| * and three schema spellings ($ref prefixes, the string discriminator, the |
| 14 |
|
| * type MUST). See plan_openapi_v2/00-overview.md for the field map. |
| 15 |
|
| * |
| 16 |
|
| * Contract, the same as oa_normalize's: the caller's document is never |
| 17 |
|
| * mutated; a NEW root is returned and becomes the document the 3.0 pass |
| 18 |
|
| * resolves against, so a `$ref` rewritten here to `#/components/...` is found |
| 19 |
|
| * there. The output keeps `swagger: "2.0"`; every rule fires only on a 2.0 |
| 20 |
|
| * spelling, which the rule itself removes or converts, so running the pass |
| 21 |
|
| * again over its own output changes nothing (D2 in the plan). |
| 22 |
|
| * |
| 23 |
|
| * Position-aware, like the 3.0 pass: the schema walk descends only where a |
| 24 |
|
| * Schema Object can appear, because `default`, `example` and `enum` hold |
| 25 |
|
| * user JSON that may itself carry a key spelled `$ref`. |
| 26 |
|
| * |
| 27 |
|
| * Built in phases: 03 the root (this file's first half: servers, the |
| 28 |
|
| * sections, security schemes, the $ref table, a light walk of paths that |
| 29 |
|
| * rewrites reference strings), 04 the operation, 05 the parameter and the |
| 30 |
|
| * Items, 06 the schema. Where a later phase owns a conversion the earlier |
| 31 |
|
| * one copies the object unchanged and says so. */ |
| 32 |
|
| |
| 33 |
|
| typedef struct oa_sw2 { |
| 34 |
|
| HV *src; /* the 2.0 document as loaded, never written */ |
| 35 |
|
| HV *out; /* the 3.0-shaped root being built */ |
| 36 |
|
| AV *consumes; /* root consumes, borrowed from src, or NULL */ |
| 37 |
|
| AV *produces; /* root produces, borrowed from src, or NULL */ |
| 38 |
|
| int has_host; /* a server URL can be rendered for operation schemes */ |
| 39 |
|
| SV *host; /* borrowed from src when has_host */ |
| 40 |
|
| SV *basepath; /* borrowed from src, or NULL */ |
| 41 |
|
| } oa_sw2; |
| 42 |
|
| |
| 43 |
|
| #define OA_SW2_MAX_HOPS 8 |
| 44 |
|
| #define OA_SW2_SCHEMA_DEPTH 64 |
| 45 |
|
| |
| 46 |
|
| /* ---- small helpers ----------------------------------------------------------- */ |
| 47 |
|
| |
| 48 |
|
| /* one level deep: keys duplicated, values shared through newSVsv (the same |
| 49 |
|
| * shape as oa_conv_copy_hv, which is defined after this header is included) */ |
| 50 |
30915 |
| static HV *oa_sw2_copy_hv(pTHX_ HV *src) { |
| 51 |
30915 |
| HV *dst = newHV(); |
| 52 |
|
| HE *he; |
| 53 |
30915 |
| hv_iterinit(src); |
| 54 |
122440 |
▶ | while ((he = hv_iternext(src))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(src))) { |
91525 |
30915 |
122440 |
|
| 55 |
91525 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 56 |
91525 |
| (void)hv_store(dst, k, kl, newSVsv(hv_iterval(src, he)), 0); |
| 57 |
|
| } |
| 58 |
30915 |
| return dst; |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
52045 |
| static int oa_sw2_is_ext(const char *k, STRLEN kl) { |
| 62 |
52045 |
▶ | return kl >= 2 && k[0] == 'x' && k[1] == '-'; |
Branch: 3 branches
| Branch | True | False | Total |
| return kl >= 2 && k[0] == 'x' && k[1] == '-'; |
52045 |
0 |
52045 |
| return kl >= 2 && k[0] == 'x' && k[1] == '-'; |
1206 |
50839 |
52045 |
| return kl >= 2 && k[0] == 'x' && k[1] == '-'; |
1206 |
0 |
1206 |
|
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
|
| /* `description` and every ^x- key of src onto dst: what a rebuilt object |
| 66 |
|
| * keeps of the original beside the fields the rebuild knows about */ |
| 67 |
8266 |
| static void oa_sw2_copy_extras(pTHX_ HV *dst, HV *src) { |
| 68 |
|
| HE *he; |
| 69 |
8266 |
| hv_iterinit(src); |
| 70 |
38294 |
▶ | while ((he = hv_iternext(src))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(src))) { |
30028 |
8266 |
38294 |
|
| 71 |
30028 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 72 |
30028 |
▶ | if (oa_sw2_is_ext(k, (STRLEN)kl) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_is_ext(k, (STRLEN)kl) |
29426 |
602 |
30028 |
|
| 73 |
29426 |
▶ | || (kl == 11 && memEQ(k, "description", 11))) |
Branch: 2 branches
| Branch | True | False | Total |
| || (kl == 11 && memEQ(k, "description", 11))) |
2434 |
26992 |
29426 |
| || (kl == 11 && memEQ(k, "description", 11))) |
2434 |
0 |
2434 |
|
| 74 |
3036 |
| (void)hv_store(dst, k, kl, newSVsv(hv_iterval(src, he)), 0); |
| 75 |
|
| } |
| 76 |
8266 |
| } |
| 77 |
|
| |
| 78 |
3349 |
| static int oa_sw2_has(const char *p, STRLEN l, const char *needle) { |
| 79 |
3349 |
| STRLEN nl = strlen(needle), i; |
| 80 |
3349 |
▶ | if (l < nl) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (l < nl) return 0; |
0 |
3349 |
3349 |
|
| 81 |
35515 |
▶ | for (i = 0; i + nl <= l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + nl <= l; i++) |
34267 |
1248 |
35515 |
|
| 82 |
34267 |
▶ | if (memEQ(p + i, needle, nl)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memEQ(p + i, needle, nl)) return 1; |
2101 |
32166 |
34267 |
|
| 83 |
1248 |
| return 0; |
| 84 |
|
| } |
| 85 |
|
| |
| 86 |
136715 |
| static int oa_sw2_str_is(pTHX_ SV *sv, const char *want) { |
| 87 |
|
| STRLEN l; const char *p; |
| 88 |
136715 |
▶ | if (!sv || SvROK(sv) || !SvOK(sv)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!sv || SvROK(sv) || !SvOK(sv)) return 0; |
130520 |
6195 |
136715 |
| if (!sv || SvROK(sv) || !SvOK(sv)) return 0; |
130520 |
0 |
130520 |
| if (!sv || SvROK(sv) || !SvOK(sv)) return 0; |
0 |
130520 |
130520 |
|
| 89 |
130520 |
| p = SvPV_const(sv, l); |
| 90 |
130520 |
▶ | return l == strlen(want) && memEQ(p, want, l); |
Branch: 2 branches
| Branch | True | False | Total |
| return l == strlen(want) && memEQ(p, want, l); |
22293 |
108227 |
130520 |
| return l == strlen(want) && memEQ(p, want, l); |
16794 |
5499 |
22293 |
|
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| /* RFC 6901: `~1` is `/`, `~0` is `~`. The caller owns the result. */ |
| 94 |
4220 |
| static SV *oa_sw2_unescape(pTHX_ const char *p, STRLEN l) { |
| 95 |
4220 |
| SV *out = newSVpvs(""); |
| 96 |
|
| STRLEN i; |
| 97 |
42152 |
▶ | for (i = 0; i < l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) { |
37932 |
4220 |
42152 |
|
| 98 |
37932 |
▶ | if (p[i] == '~' && i + 1 < l && (p[i + 1] == '1' || p[i + 1] == '0')) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (p[i] == '~' && i + 1 < l && (p[i + 1] == '1' || p[i + 1] == '0')) { |
0 |
37932 |
37932 |
| if (p[i] == '~' && i + 1 < l && (p[i + 1] == '1' || p[i + 1] == '0')) { |
0 |
0 |
0 |
| if (p[i] == '~' && i + 1 < l && (p[i + 1] == '1' || p[i + 1] == '0')) { |
0 |
0 |
0 |
| if (p[i] == '~' && i + 1 < l && (p[i + 1] == '1' || p[i + 1] == '0')) { |
0 |
0 |
0 |
|
| 99 |
0 |
▶ | sv_catpvn(out, p[i + 1] == '1' ? "/" : "~", 1); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_catpvn(out, p[i + 1] == '1' ? "/" : "~", 1); |
0 |
0 |
0 |
|
| 100 |
0 |
| i++; |
| 101 |
0 |
| continue; |
| 102 |
|
| } |
| 103 |
37932 |
| sv_catpvn(out, p + i, 1); |
| 104 |
|
| } |
| 105 |
4220 |
| return out; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
|
| /* ---- rule 6: the pass's own dereference ------------------------------------- * |
| 109 |
|
| * |
| 110 |
|
| * The 3.0 normaliser resolves `#/components/...` against the document it is |
| 111 |
|
| * handed, which is this pass's OUTPUT. This pass needs to read the SOURCE: a |
| 112 |
|
| * use site of `#/parameters/X` becomes a request body, a form member or a |
| 113 |
|
| * component parameter depending on the target's `in`, and only the raw 2.0 |
| 114 |
|
| * object says which. Draft-04 semantics: a Reference Object has no siblings, |
| 115 |
|
| * so nothing beside the `$ref` is read. */ |
| 116 |
|
| |
| 117 |
|
| /* the raw object `#/<section>/<name>` names in the source, borrowed, or NULL |
| 118 |
|
| * when the string is not such a reference or it names nothing */ |
| 119 |
4221 |
| static HV *oa_sw2_target(pTHX_ oa_sw2 *S, SV *ref, const char *section) { |
| 120 |
4221 |
| STRLEN l, secl = strlen(section), nl; |
| 121 |
|
| const char *p, *nm; |
| 122 |
|
| HV *bucket; |
| 123 |
|
| SV **e, *name; |
| 124 |
4221 |
▶ | if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; |
4221 |
0 |
4221 |
| if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; |
4221 |
0 |
4221 |
| if (!ref || SvROK(ref) || !SvOK(ref)) return NULL; |
0 |
4221 |
4221 |
|
| 125 |
4221 |
| p = SvPV_const(ref, l); |
| 126 |
4221 |
▶ | if (l < secl + 4 || p[0] != '#' || p[1] != '/' |
Branch: 3 branches
| Branch | True | False | Total |
| if (l < secl + 4 || p[0] != '#' || p[1] != '/' |
4221 |
0 |
4221 |
| if (l < secl + 4 || p[0] != '#' || p[1] != '/' |
4221 |
0 |
4221 |
| if (l < secl + 4 || p[0] != '#' || p[1] != '/' |
4221 |
0 |
4221 |
|
| 127 |
4221 |
▶ | || !memEQ(p + 2, section, secl) || p[2 + secl] != '/') return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| || !memEQ(p + 2, section, secl) || p[2 + secl] != '/') return NULL; |
4221 |
0 |
4221 |
| || !memEQ(p + 2, section, secl) || p[2 + secl] != '/') return NULL; |
0 |
4221 |
4221 |
|
| 128 |
4221 |
| nm = p + 3 + secl; |
| 129 |
4221 |
| nl = l - 3 - secl; |
| 130 |
|
| /* a deeper pointer addresses something inside the object, not the object */ |
| 131 |
4221 |
▶ | if (!nl || memchr(nm, '/', nl)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!nl || memchr(nm, '/', nl)) return NULL; |
4221 |
0 |
4221 |
| if (!nl || memchr(nm, '/', nl)) return NULL; |
0 |
4221 |
4221 |
|
| 132 |
4221 |
| bucket = oa_hv_of(oa_get(aTHX_ S->src, section)); |
| 133 |
4221 |
▶ | if (!bucket) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bucket) return NULL; |
1 |
4220 |
4221 |
|
| 134 |
4220 |
| name = sv_2mortal(oa_sw2_unescape(aTHX_ nm, nl)); |
| 135 |
4220 |
| e = hv_fetch(bucket, SvPVX(name), (I32)SvCUR(name), 0); |
| 136 |
4220 |
▶ | return (e && *e) ? oa_hv_of(*e) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (e && *e) ? oa_hv_of(*e) : NULL; |
4220 |
0 |
4220 |
| return (e && *e) ? oa_hv_of(*e) : NULL; |
4220 |
0 |
4220 |
|
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
|
| /* the object a Reference Object (or a chain of them within one section) |
| 140 |
|
| * finally names, borrowed; NULL when it cannot be followed, including a cycle |
| 141 |
|
| * or a chain past OA_SW2_MAX_HOPS - the caller then leaves the reference as |
| 142 |
|
| * it was, and the 3.0 pass refuses it by name rather than this pass hanging */ |
| 143 |
4221 |
| static HV *oa_sw2_deref(pTHX_ oa_sw2 *S, HV *obj, const char *section) { |
| 144 |
4221 |
| HV *cur = obj; |
| 145 |
|
| int hops; |
| 146 |
7838 |
▶ | for (hops = 0; hops < OA_SW2_MAX_HOPS; hops++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (hops = 0; hops < OA_SW2_MAX_HOPS; hops++) { |
7838 |
0 |
7838 |
|
| 147 |
7838 |
| SV *ref = oa_get(aTHX_ cur, "$ref"); |
| 148 |
|
| HV *t; |
| 149 |
7838 |
▶ | if (!ref) return cur; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ref) return cur; |
4220 |
3618 |
7838 |
|
| 150 |
3618 |
| t = oa_sw2_target(aTHX_ S, ref, section); |
| 151 |
3618 |
▶ | if (!t || t == cur) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!t || t == cur) return NULL; |
3617 |
1 |
3618 |
| if (!t || t == cur) return NULL; |
0 |
3617 |
3617 |
|
| 152 |
3617 |
| cur = t; |
| 153 |
|
| } |
| 154 |
0 |
| return NULL; |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
|
| /* ---- rule 5: the $ref prefix table ------------------------------------------ * |
| 158 |
|
| * |
| 159 |
|
| * A reference string rewritten to its 3.0 home, as a new SV the caller owns. |
| 160 |
|
| * `#/definitions/` and `#/responses/` move wholesale. `#/parameters/` depends |
| 161 |
|
| * on the target: a component parameter moves to components.parameters; a BODY |
| 162 |
|
| * definition is a request body and a FORMDATA one is inlined at its use site, |
| 163 |
|
| * and both are the operation walk's business (phase 04), so their references |
| 164 |
|
| * are returned unchanged here. A string this pass cannot place is also |
| 165 |
|
| * returned unchanged, so the 3.0 pass refuses it loudly by name. JSON-pointer |
| 166 |
|
| * escapes survive a prefix swap untouched. */ |
| 167 |
14904 |
| static SV *oa_sw2_ref(pTHX_ oa_sw2 *S, SV *ref) { |
| 168 |
|
| static const struct { const char *from; const char *to; } tbl[] = { |
| 169 |
|
| { "#/definitions/", "#/components/schemas/" }, |
| 170 |
|
| { "#/responses/", "#/components/responses/" }, |
| 171 |
|
| { NULL, NULL } |
| 172 |
|
| }; |
| 173 |
|
| static const char pf[] = "#/parameters/"; |
| 174 |
14904 |
| const STRLEN pfl = sizeof(pf) - 1; |
| 175 |
|
| STRLEN l; |
| 176 |
|
| const char *p; |
| 177 |
|
| int i; |
| 178 |
14904 |
▶ | if (!ref || SvROK(ref) || !SvOK(ref)) return newSVsv(ref); |
Branch: 3 branches
| Branch | True | False | Total |
| if (!ref || SvROK(ref) || !SvOK(ref)) return newSVsv(ref); |
14904 |
0 |
14904 |
| if (!ref || SvROK(ref) || !SvOK(ref)) return newSVsv(ref); |
14904 |
0 |
14904 |
| if (!ref || SvROK(ref) || !SvOK(ref)) return newSVsv(ref); |
0 |
14904 |
14904 |
|
| 179 |
14904 |
| p = SvPV_const(ref, l); |
| 180 |
18525 |
▶ | for (i = 0; tbl[i].from; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; tbl[i].from; i++) { |
17919 |
606 |
18525 |
|
| 181 |
17919 |
| STRLEN fl = strlen(tbl[i].from); |
| 182 |
17919 |
▶ | if (l > fl && memEQ(p, tbl[i].from, fl)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (l > fl && memEQ(p, tbl[i].from, fl)) { |
17918 |
1 |
17919 |
| if (l > fl && memEQ(p, tbl[i].from, fl)) { |
14298 |
3620 |
17918 |
|
| 183 |
14298 |
| SV *out = newSVpv(tbl[i].to, 0); |
| 184 |
14298 |
| sv_catpvn(out, p + fl, l - fl); |
| 185 |
14298 |
| return out; |
| 186 |
|
| } |
| 187 |
|
| } |
| 188 |
606 |
▶ | if (l > pfl && memEQ(p, pf, pfl)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (l > pfl && memEQ(p, pf, pfl)) { |
606 |
0 |
606 |
| if (l > pfl && memEQ(p, pf, pfl)) { |
603 |
3 |
606 |
|
| 189 |
603 |
| HV *t = oa_sw2_target(aTHX_ S, ref, "parameters"); |
| 190 |
603 |
▶ | HV *d = t ? oa_sw2_deref(aTHX_ S, t, "parameters") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| HV *d = t ? oa_sw2_deref(aTHX_ S, t, "parameters") : NULL; |
603 |
0 |
603 |
|
| 191 |
603 |
▶ | if (d && !oa_sw2_str_is(aTHX_ oa_get(aTHX_ d, "in"), "body") |
Branch: 2 branches
| Branch | True | False | Total |
| if (d && !oa_sw2_str_is(aTHX_ oa_get(aTHX_ d, "in"), "body") |
603 |
0 |
603 |
| if (d && !oa_sw2_str_is(aTHX_ oa_get(aTHX_ d, "in"), "body") |
603 |
0 |
603 |
|
| 192 |
603 |
▶ | && !oa_sw2_str_is(aTHX_ oa_get(aTHX_ d, "in"), "formData")) { |
Branch: 1 branch
| Branch | True | False | Total |
| && !oa_sw2_str_is(aTHX_ oa_get(aTHX_ d, "in"), "formData")) { |
603 |
0 |
603 |
|
| 193 |
603 |
| SV *out = newSVpvs("#/components/parameters/"); |
| 194 |
603 |
| sv_catpvn(out, p + pfl, l - pfl); |
| 195 |
603 |
| return out; |
| 196 |
|
| } |
| 197 |
|
| } |
| 198 |
3 |
| return newSVsv(ref); |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
|
| /* ---- the schema walk --------------------------------------------------------- * |
| 202 |
|
| * |
| 203 |
|
| * Position-aware: descends only where a Schema Object can appear, through the |
| 204 |
|
| * keyword tables oa_compile.h shares with the 3.0 pass. Phase 03: the `$ref` |
| 205 |
|
| * rewrite only. Phase 06 adds the string discriminator, the type MUST and the |
| 206 |
|
| * nullable drop; everything else about a schema is the 3.0 pass's. */ |
| 207 |
|
| |
| 208 |
|
| static SV *oa_sw2_schema(pTHX_ oa_sw2 *S, SV *schema, int depth); |
| 209 |
|
| |
| 210 |
2477 |
| static SV *oa_sw2_schema_list(pTHX_ oa_sw2 *S, SV *list, int depth) { |
| 211 |
2477 |
| AV *src = oa_av_of(list), *dst; |
| 212 |
|
| I32 i, n; |
| 213 |
2477 |
▶ | if (!src) return newSVsv(list); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return newSVsv(list); |
0 |
2477 |
2477 |
|
| 214 |
2477 |
| dst = newAV(); |
| 215 |
2477 |
| n = av_len(src); |
| 216 |
7420 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
4943 |
2477 |
7420 |
|
| 217 |
4943 |
| SV **e = av_fetch(src, i, 0); |
| 218 |
4943 |
▶ | av_push(dst, (e && *e) ? oa_sw2_schema(aTHX_ S, *e, depth) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(dst, (e && *e) ? oa_sw2_schema(aTHX_ S, *e, depth) : newSV(0)); |
4943 |
0 |
4943 |
| av_push(dst, (e && *e) ? oa_sw2_schema(aTHX_ S, *e, depth) : newSV(0)); |
4943 |
0 |
4943 |
|
| 219 |
|
| } |
| 220 |
2477 |
| return newRV_noinc((SV *)dst); |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
5073 |
| static SV *oa_sw2_schema_map(pTHX_ oa_sw2 *S, SV *map, int depth) { |
| 224 |
5073 |
| HV *src = oa_hv_of(map), *dst; |
| 225 |
|
| HE *he; |
| 226 |
5073 |
▶ | if (!src) return newSVsv(map); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return newSVsv(map); |
0 |
5073 |
5073 |
|
| 227 |
5073 |
| dst = newHV(); |
| 228 |
5073 |
| hv_iterinit(src); |
| 229 |
18830 |
▶ | while ((he = hv_iternext(src))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(src))) { |
13757 |
5073 |
18830 |
|
| 230 |
13757 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 231 |
13757 |
| (void)hv_store(dst, k, kl, oa_sw2_schema(aTHX_ S, hv_iterval(src, he), depth), 0); |
| 232 |
|
| } |
| 233 |
5073 |
| return newRV_noinc((SV *)dst); |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
|
| /* The string discriminator (phase 06). 2.0: "The property name used MUST be |
| 237 |
|
| * defined at this schema and it MUST be in the required property list." Both |
| 238 |
|
| * are checked on THIS schema, then the string becomes the 3.0 object form the |
| 239 |
|
| * 3.0 pass expands (inheritance through the children's allOf $ref back to the |
| 240 |
|
| * base, exactly as for a 3.x document). The object form is left alone, which |
| 241 |
|
| * is what a converted document fed back through new() carries. */ |
| 242 |
609 |
| static SV *oa_sw2_discriminator(pTHX_ HV *schema, SV *disc) { |
| 243 |
609 |
| HV *props = oa_hv_of(oa_get(aTHX_ schema, "properties")), *out; |
| 244 |
609 |
| AV *req = oa_av_of(oa_get(aTHX_ schema, "required")); |
| 245 |
|
| STRLEN dl; const char *dp; |
| 246 |
609 |
| int found = 0; |
| 247 |
|
| I32 i, n; |
| 248 |
609 |
▶ | if (SvROK(disc)) return newSVsv(disc); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(disc)) return newSVsv(disc); |
0 |
609 |
609 |
|
| 249 |
609 |
| dp = SvPV_const(disc, dl); |
| 250 |
609 |
▶ | if (!props || !hv_exists(props, dp, (I32)dl)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!props || !hv_exists(props, dp, (I32)dl)) |
607 |
2 |
609 |
| if (!props || !hv_exists(props, dp, (I32)dl)) |
0 |
607 |
607 |
|
| 251 |
2 |
| croak("Open::API: discriminator property '%.*s' is not defined at this " |
| 252 |
|
| "schema (it MUST be)", (int)dl, dp); |
| 253 |
607 |
▶ | if (req) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (req) { |
605 |
2 |
607 |
|
| 254 |
605 |
| n = av_len(req); |
| 255 |
2414 |
▶ | for (i = 0; i <= n && !found; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i <= n && !found; i++) { |
1809 |
605 |
2414 |
| for (i = 0; i <= n && !found; i++) { |
1809 |
0 |
1809 |
|
| 256 |
1809 |
| SV **e = av_fetch(req, i, 0); |
| 257 |
|
| STRLEN el; const char *ep; |
| 258 |
1809 |
▶ | if (!e || !*e || SvROK(*e)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!e || !*e || SvROK(*e)) continue; |
1809 |
0 |
1809 |
| if (!e || !*e || SvROK(*e)) continue; |
1809 |
0 |
1809 |
| if (!e || !*e || SvROK(*e)) continue; |
0 |
1809 |
1809 |
|
| 259 |
1809 |
| ep = SvPV_const(*e, el); |
| 260 |
1809 |
▶ | if (el == dl && memEQ(ep, dp, dl)) found = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (el == dl && memEQ(ep, dp, dl)) found = 1; |
605 |
1204 |
1809 |
| if (el == dl && memEQ(ep, dp, dl)) found = 1; |
605 |
0 |
605 |
|
| 261 |
|
| } |
| 262 |
|
| } |
| 263 |
607 |
▶ | if (!found) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found) |
2 |
605 |
607 |
|
| 264 |
2 |
| croak("Open::API: discriminator property '%.*s' is not in the required " |
| 265 |
|
| "property list (it MUST be)", (int)dl, dp); |
| 266 |
605 |
| out = newHV(); |
| 267 |
605 |
| (void)hv_stores(out, "propertyName", newSVpvn(dp, dl)); |
| 268 |
605 |
| return newRV_noinc((SV *)out); |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
37232 |
| static SV *oa_sw2_schema(pTHX_ oa_sw2 *S, SV *schema, int depth) { |
| 272 |
37232 |
| HV *src = oa_hv_of(schema), *dst; |
| 273 |
|
| HE *he; |
| 274 |
37232 |
▶ | if (!src) return newSVsv(schema); /* a boolean, or not a schema */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return newSVsv(schema); /* a boolean, or not a schema */ |
0 |
37232 |
37232 |
|
| 275 |
37232 |
▶ | if (depth > OA_SW2_SCHEMA_DEPTH) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth > OA_SW2_SCHEMA_DEPTH) |
0 |
37232 |
37232 |
|
| 276 |
0 |
| croak("Open::API: schema nesting deeper than %d", OA_SW2_SCHEMA_DEPTH); |
| 277 |
37232 |
| dst = newHV(); |
| 278 |
37232 |
| hv_iterinit(src); |
| 279 |
102457 |
▶ | while ((he = hv_iternext(src))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(src))) { |
65231 |
37226 |
102457 |
|
| 280 |
65231 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 281 |
65231 |
| SV *v = hv_iterval(src, he), *nv; |
| 282 |
|
| /* "type: Value MUST be a string. Multiple types via an array are not |
| 283 |
|
| * supported." Nothing in this pipeline makes a type array for a 2.0 |
| 284 |
|
| * document once `nullable` is dropped below, so the refusal survives a |
| 285 |
|
| * second pass. */ |
| 286 |
65231 |
▶ | if (kl == 4 && memEQ(k, "type", 4) && oa_av_of(v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (kl == 4 && memEQ(k, "type", 4) && oa_av_of(v)) |
35734 |
29497 |
65231 |
| if (kl == 4 && memEQ(k, "type", 4) && oa_av_of(v)) |
23122 |
12612 |
35734 |
| if (kl == 4 && memEQ(k, "type", 4) && oa_av_of(v)) |
2 |
23120 |
23122 |
|
| 287 |
2 |
| croak("Open::API: Swagger 2.0 schema: type must be a string, not an " |
| 288 |
|
| "array of types"); |
| 289 |
|
| /* not a 2.0 keyword; draft-04 ignores what it does not know. Left in, |
| 290 |
|
| * the 3.0 pass would consume it into a type union and the second pass |
| 291 |
|
| * would refuse this build's own output under the rule above. */ |
| 292 |
65229 |
▶ | if (kl == 8 && memEQ(k, "nullable", 8)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (kl == 8 && memEQ(k, "nullable", 8)) continue; |
5606 |
59623 |
65229 |
| if (kl == 8 && memEQ(k, "nullable", 8)) continue; |
3 |
5603 |
5606 |
|
| 293 |
65226 |
▶ | if (kl == 4 && memEQ(k, "$ref", 4)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (kl == 4 && memEQ(k, "$ref", 4)) |
35732 |
29494 |
65226 |
| if (kl == 4 && memEQ(k, "$ref", 4)) |
11892 |
23840 |
35732 |
|
| 294 |
11892 |
| nv = oa_sw2_ref(aTHX_ S, v); |
| 295 |
53334 |
▶ | else if (kl == 13 && memEQ(k, "discriminator", 13)) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (kl == 13 && memEQ(k, "discriminator", 13)) |
609 |
52725 |
53334 |
| else if (kl == 13 && memEQ(k, "discriminator", 13)) |
609 |
0 |
609 |
|
| 296 |
609 |
| nv = oa_sw2_discriminator(aTHX_ src, v); |
| 297 |
52725 |
▶ | else if (kl == 5 && memEQ(k, "items", 5)) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (kl == 5 && memEQ(k, "items", 5)) |
4378 |
48347 |
52725 |
| else if (kl == 5 && memEQ(k, "items", 5)) |
2506 |
1872 |
4378 |
|
| 298 |
5012 |
| nv = oa_av_of(v) ? oa_sw2_schema_list(aTHX_ S, v, depth + 1) |
| 299 |
2506 |
▶ | : oa_sw2_schema(aTHX_ S, v, depth + 1); |
Branch: 1 branch
| Branch | True | False | Total |
| : oa_sw2_schema(aTHX_ S, v, depth + 1); |
605 |
1901 |
2506 |
|
| 300 |
50219 |
▶ | else if (oa_kw_schema(k, (STRLEN)kl)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (oa_kw_schema(k, (STRLEN)kl)) |
602 |
49617 |
50219 |
|
| 301 |
602 |
| nv = oa_sw2_schema(aTHX_ S, v, depth + 1); |
| 302 |
49617 |
▶ | else if (oa_kw_schema_map(k, (STRLEN)kl)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (oa_kw_schema_map(k, (STRLEN)kl)) |
5073 |
44544 |
49617 |
|
| 303 |
5073 |
| nv = oa_sw2_schema_map(aTHX_ S, v, depth + 1); |
| 304 |
44544 |
▶ | else if (oa_kw_schema_list(k, (STRLEN)kl)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (oa_kw_schema_list(k, (STRLEN)kl)) |
1872 |
42672 |
44544 |
|
| 305 |
1872 |
| nv = oa_sw2_schema_list(aTHX_ S, v, depth + 1); |
| 306 |
|
| else |
| 307 |
42672 |
| nv = newSVsv(v); /* default, example, enum, x-: user data */ |
| 308 |
65222 |
| (void)hv_store(dst, k, kl, nv, 0); |
| 309 |
|
| } |
| 310 |
37226 |
| return newRV_noinc((SV *)dst); |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
|
| /* ---- rule 2: host, basePath, schemes -> servers ------------------------------ */ |
| 314 |
|
| |
| 315 |
2453 |
| static int oa_sw2_scheme_ok(const char *s, STRLEN l) { |
| 316 |
2453 |
▶ | return (l == 4 && memEQ(s, "http", 4)) || (l == 5 && memEQ(s, "https", 5)) |
Branch: 3 branches
| Branch | True | False | Total |
| return (l == 4 && memEQ(s, "http", 4)) || (l == 5 && memEQ(s, "https", 5)) |
0 |
1234 |
1234 |
| return (l == 4 && memEQ(s, "http", 4)) || (l == 5 && memEQ(s, "https", 5)) |
1216 |
3 |
1219 |
| return (l == 4 && memEQ(s, "http", 4)) || (l == 5 && memEQ(s, "https", 5)) |
0 |
1216 |
1216 |
|
| 317 |
4906 |
▶ | || (l == 2 && memEQ(s, "ws", 2)) || (l == 3 && memEQ(s, "wss", 3)); |
Branch: 5 branches
| Branch | True | False | Total |
| || (l == 2 && memEQ(s, "ws", 2)) || (l == 3 && memEQ(s, "wss", 3)); |
1234 |
1219 |
2453 |
| || (l == 2 && memEQ(s, "ws", 2)) || (l == 3 && memEQ(s, "wss", 3)); |
0 |
3 |
3 |
| || (l == 2 && memEQ(s, "ws", 2)) || (l == 3 && memEQ(s, "wss", 3)); |
0 |
0 |
0 |
| || (l == 2 && memEQ(s, "ws", 2)) || (l == 3 && memEQ(s, "wss", 3)); |
3 |
0 |
3 |
| || (l == 2 && memEQ(s, "ws", 2)) || (l == 3 && memEQ(s, "wss", 3)); |
2 |
1 |
3 |
|
| 318 |
|
| } |
| 319 |
|
| |
| 320 |
2453 |
| static void oa_sw2_push_server(pTHX_ AV *servers, SV *url) { |
| 321 |
2453 |
| HV *s = newHV(); |
| 322 |
2453 |
| (void)hv_stores(s, "url", url); |
| 323 |
2453 |
| av_push(servers, newRV_noinc((SV *)s)); |
| 324 |
2453 |
| } |
| 325 |
|
| |
| 326 |
|
| /* One server per scheme, `scheme://host/basePath`. A host with no schemes is |
| 327 |
|
| * scheme-relative, `//host/basePath` ("the one used to access the Swagger |
| 328 |
|
| * definition itself"). A basePath alone is a relative URL. Nothing at all |
| 329 |
|
| * invents no servers. The MUSTs are checked by presence, so a converted |
| 330 |
|
| * document fed back through new() (which has none of the three) is untouched. */ |
| 331 |
3921 |
| static void oa_sw2_servers(pTHX_ oa_sw2 *S) { |
| 332 |
3921 |
| SV *host = oa_get(aTHX_ S->src, "host"); |
| 333 |
3921 |
| SV *bp = oa_get(aTHX_ S->src, "basePath"); |
| 334 |
3921 |
| SV *sch = oa_get(aTHX_ S->src, "schemes"); |
| 335 |
3921 |
▶ | AV *schemes = sch ? oa_av_of(sch) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| AV *schemes = sch ? oa_av_of(sch) : NULL; |
1246 |
2675 |
3921 |
|
| 336 |
3921 |
| STRLEN hl = 0, bl = 0; |
| 337 |
3921 |
| const char *hp = NULL, *bpp = NULL; |
| 338 |
|
| AV *servers; |
| 339 |
|
| I32 i, n; |
| 340 |
|
| |
| 341 |
3921 |
▶ | if (host) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (host) { |
3349 |
572 |
3921 |
|
| 342 |
3349 |
▶ | if (SvROK(host)) croak("Open::API: host must be a string"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(host)) croak("Open::API: host must be a string"); |
0 |
3349 |
3349 |
|
| 343 |
3349 |
| hp = SvPV_const(host, hl); |
| 344 |
3349 |
▶ | if (oa_sw2_has(hp, hl, "://")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_has(hp, hl, "://")) |
2101 |
1248 |
3349 |
|
| 345 |
2101 |
| croak("Open::API: host '%.*s' must be the host only, without a scheme", |
| 346 |
|
| (int)hl, hp); |
| 347 |
1248 |
▶ | if (!hl || memchr(hp, '/', hl)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!hl || memchr(hp, '/', hl)) |
1248 |
0 |
1248 |
| if (!hl || memchr(hp, '/', hl)) |
1 |
1247 |
1248 |
|
| 348 |
1 |
| croak("Open::API: host '%.*s' must be the host only, without a path", |
| 349 |
|
| (int)hl, hp); |
| 350 |
1247 |
| S->has_host = 1; |
| 351 |
1247 |
| S->host = host; |
| 352 |
|
| } |
| 353 |
1819 |
▶ | if (bp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bp) { |
1246 |
573 |
1819 |
|
| 354 |
1246 |
▶ | if (SvROK(bp)) croak("Open::API: basePath must be a string"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(bp)) croak("Open::API: basePath must be a string"); |
0 |
1246 |
1246 |
|
| 355 |
1246 |
| S->basepath = bp; |
| 356 |
1246 |
| bpp = SvPV_const(bp, bl); |
| 357 |
1246 |
▶ | if (!bl || bpp[0] != '/') |
Branch: 2 branches
| Branch | True | False | Total |
| if (!bl || bpp[0] != '/') |
1246 |
0 |
1246 |
| if (!bl || bpp[0] != '/') |
1 |
1245 |
1246 |
|
| 358 |
1 |
| croak("Open::API: basePath '%.*s' must start with a leading slash", |
| 359 |
|
| (int)bl, bpp); |
| 360 |
|
| } |
| 361 |
1818 |
▶ | if (sch) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sch) { |
1246 |
572 |
1818 |
|
| 362 |
1246 |
▶ | if (!schemes) croak("Open::API: schemes must be a list"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!schemes) croak("Open::API: schemes must be a list"); |
0 |
1246 |
1246 |
|
| 363 |
1246 |
| n = av_len(schemes); |
| 364 |
3094 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
1849 |
1245 |
3094 |
|
| 365 |
1849 |
| SV **e = av_fetch(schemes, i, 0); |
| 366 |
|
| STRLEN sl; const char *sp; |
| 367 |
1849 |
▶ | if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
1849 |
0 |
1849 |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
1849 |
0 |
1849 |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
1849 |
0 |
1849 |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
0 |
1849 |
1849 |
|
| 368 |
0 |
| croak("Open::API: schemes: a scheme must be a string"); |
| 369 |
1849 |
| sp = SvPV_const(*e, sl); |
| 370 |
1849 |
▶ | if (!oa_sw2_scheme_ok(sp, sl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_sw2_scheme_ok(sp, sl)) |
1 |
1848 |
1849 |
|
| 371 |
1 |
| croak("Open::API: schemes value '%.*s' is not one of http, " |
| 372 |
|
| "https, ws, wss", (int)sl, sp); |
| 373 |
|
| } |
| 374 |
|
| } |
| 375 |
1817 |
▶ | if (!host && !bp) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!host && !bp) return; |
571 |
1246 |
1817 |
| if (!host && !bp) return; |
570 |
1 |
571 |
|
| 376 |
|
| |
| 377 |
1247 |
| servers = newAV(); |
| 378 |
1247 |
| (void)hv_stores(S->out, "servers", newRV_noinc((SV *)servers)); |
| 379 |
1247 |
▶ | if (host) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (host) { |
1246 |
1 |
1247 |
|
| 380 |
1246 |
▶ | if (schemes && av_len(schemes) >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (schemes && av_len(schemes) >= 0) { |
1245 |
1 |
1246 |
| if (schemes && av_len(schemes) >= 0) { |
1245 |
0 |
1245 |
|
| 381 |
1245 |
| n = av_len(schemes); |
| 382 |
3093 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
1848 |
1245 |
3093 |
|
| 383 |
1848 |
| SV **e = av_fetch(schemes, i, 0); |
| 384 |
1848 |
| SV *url = newSVsv(*e); |
| 385 |
1848 |
| sv_catpvs(url, "://"); |
| 386 |
1848 |
| sv_catpvn(url, hp, hl); |
| 387 |
1848 |
▶ | if (bp) sv_catpvn(url, bpp, bl); |
Branch: 1 branch
| Branch | True | False | Total |
| if (bp) sv_catpvn(url, bpp, bl); |
1846 |
2 |
1848 |
|
| 388 |
1848 |
| oa_sw2_push_server(aTHX_ servers, url); |
| 389 |
|
| } |
| 390 |
|
| } else { |
| 391 |
1 |
| SV *url = newSVpvs("//"); |
| 392 |
1 |
| sv_catpvn(url, hp, hl); |
| 393 |
1 |
▶ | if (bp) sv_catpvn(url, bpp, bl); |
Branch: 1 branch
| Branch | True | False | Total |
| if (bp) sv_catpvn(url, bpp, bl); |
1 |
0 |
1 |
|
| 394 |
1 |
| oa_sw2_push_server(aTHX_ servers, url); |
| 395 |
|
| } |
| 396 |
|
| } else { |
| 397 |
1 |
| oa_sw2_push_server(aTHX_ servers, newSVpvn(bpp, bl)); |
| 398 |
|
| } |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
|
| /* ---- rule 3: consumes and produces ------------------------------------------- */ |
| 402 |
|
| |
| 403 |
|
| /* the list, borrowed, with every entry checked to be a media type ("Value |
| 404 |
|
| * MUST be as described under Mime Types": type/subtype); NULL when absent */ |
| 405 |
11505 |
| static AV *oa_sw2_mimes(pTHX_ SV *list, const char *what) { |
| 406 |
|
| AV *av; |
| 407 |
|
| I32 i, n; |
| 408 |
11505 |
▶ | if (!list) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!list) return NULL; |
4934 |
6571 |
11505 |
|
| 409 |
6571 |
| av = oa_av_of(list); |
| 410 |
6571 |
▶ | if (!av) croak("Open::API: %s must be a list of media types", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!av) croak("Open::API: %s must be a list of media types", what); |
0 |
6571 |
6571 |
|
| 411 |
6571 |
| n = av_len(av); |
| 412 |
13200 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
6631 |
6569 |
13200 |
|
| 413 |
6631 |
| SV **e = av_fetch(av, i, 0); |
| 414 |
|
| STRLEN l; const char *p, *sl; |
| 415 |
6631 |
▶ | if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
6631 |
0 |
6631 |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
6631 |
0 |
6631 |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
6631 |
0 |
6631 |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
0 |
6631 |
6631 |
|
| 416 |
0 |
| croak("Open::API: %s: a media type must be a string", what); |
| 417 |
6631 |
| p = SvPV_const(*e, l); |
| 418 |
6631 |
▶ | sl = l ? (const char *)memchr(p, '/', l) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| sl = l ? (const char *)memchr(p, '/', l) : NULL; |
6631 |
0 |
6631 |
|
| 419 |
6631 |
▶ | if (!sl || sl == p || sl == p + l - 1) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!sl || sl == p || sl == p + l - 1) |
6629 |
2 |
6631 |
| if (!sl || sl == p || sl == p + l - 1) |
6629 |
0 |
6629 |
| if (!sl || sl == p || sl == p + l - 1) |
0 |
6629 |
6629 |
|
| 420 |
2 |
| croak("Open::API: %s: '%.*s' is not a media type (type/subtype)", |
| 421 |
|
| what, (int)l, p); |
| 422 |
|
| } |
| 423 |
6569 |
| return av; |
| 424 |
|
| } |
| 425 |
|
| |
| 426 |
|
| /* ---- rule 9: securityDefinitions -> components.securitySchemes --------------- * |
| 427 |
|
| * |
| 428 |
|
| * Every 2.0 MUST about a scheme is checked HERE, because the compiler reads |
| 429 |
|
| * the converted shape: a `basic` it would refuse as an unknown type, a cookie |
| 430 |
|
| * apiKey it would accept, a missing `flow` it could not see. The croak names |
| 431 |
|
| * the field it is about, which the catalogue requires of it. */ |
| 432 |
4245 |
| static SV *oa_sw2_scheme(pTHX_ oa_sw2 *S, const char *name, I32 nl, SV *sv) { |
| 433 |
4245 |
| HV *h = oa_hv_of(sv), *out; |
| 434 |
|
| SV *tv; |
| 435 |
|
| STRLEN tl; const char *tp; |
| 436 |
|
| PERL_UNUSED_ARG(S); |
| 437 |
4245 |
▶ | if (!h) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) |
0 |
4245 |
4245 |
|
| 438 |
0 |
| croak("Open::API: securityDefinitions '%.*s' is not an object", (int)nl, name); |
| 439 |
4245 |
| tv = oa_get(aTHX_ h, "type"); |
| 440 |
4245 |
▶ | if (!tv || SvROK(tv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!tv || SvROK(tv)) |
4245 |
0 |
4245 |
| if (!tv || SvROK(tv)) |
0 |
4245 |
4245 |
|
| 441 |
0 |
| croak("Open::API: securityDefinitions '%.*s' has no 'type' (it is required)", |
| 442 |
|
| (int)nl, name); |
| 443 |
4245 |
| tp = SvPV_const(tv, tl); |
| 444 |
4245 |
| out = newHV(); |
| 445 |
4245 |
| sv_2mortal((SV *)out); /* the checks below croak */ |
| 446 |
4245 |
▶ | if (tl == 5 && memEQ(tp, "basic", 5)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tl == 5 && memEQ(tp, "basic", 5)) { |
605 |
3640 |
4245 |
| if (tl == 5 && memEQ(tp, "basic", 5)) { |
605 |
0 |
605 |
|
| 447 |
605 |
| (void)hv_stores(out, "type", newSVpvs("http")); |
| 448 |
605 |
| (void)hv_stores(out, "scheme", newSVpvs("basic")); |
| 449 |
|
| } |
| 450 |
4856 |
▶ | else if (tl == 6 && memEQ(tp, "apiKey", 6)) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (tl == 6 && memEQ(tp, "apiKey", 6)) { |
3640 |
0 |
3640 |
| else if (tl == 6 && memEQ(tp, "apiKey", 6)) { |
1218 |
2422 |
3640 |
|
| 451 |
1218 |
| SV *nm = oa_get(aTHX_ h, "name"), *in = oa_get(aTHX_ h, "in"); |
| 452 |
1218 |
▶ | if (!nm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nm) |
1 |
1217 |
1218 |
|
| 453 |
1 |
| croak("Open::API: securityDefinitions '%.*s': apiKey has no 'name' " |
| 454 |
|
| "(it is required)", (int)nl, name); |
| 455 |
1217 |
▶ | if (!in || SvROK(in)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!in || SvROK(in)) |
1217 |
0 |
1217 |
| if (!in || SvROK(in)) |
0 |
1217 |
1217 |
|
| 456 |
0 |
| croak("Open::API: securityDefinitions '%.*s': apiKey has no 'in' " |
| 457 |
|
| "(it is required)", (int)nl, name); |
| 458 |
1217 |
▶ | if (!oa_sw2_str_is(aTHX_ in, "query") && !oa_sw2_str_is(aTHX_ in, "header")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!oa_sw2_str_is(aTHX_ in, "query") && !oa_sw2_str_is(aTHX_ in, "header")) { |
614 |
603 |
1217 |
| if (!oa_sw2_str_is(aTHX_ in, "query") && !oa_sw2_str_is(aTHX_ in, "header")) { |
1 |
613 |
614 |
|
| 459 |
1 |
| STRLEN il; const char *ip = SvPV_const(in, il); |
| 460 |
1 |
| croak("Open::API: securityDefinitions '%.*s': apiKey 'in' must be " |
| 461 |
|
| "query or header (got %.*s)", (int)nl, name, (int)il, ip); |
| 462 |
|
| } |
| 463 |
1216 |
| (void)hv_stores(out, "type", newSVpvs("apiKey")); |
| 464 |
1216 |
| (void)hv_stores(out, "name", newSVsv(nm)); |
| 465 |
1216 |
| (void)hv_stores(out, "in", newSVsv(in)); |
| 466 |
|
| } |
| 467 |
4835 |
▶ | else if (tl == 6 && memEQ(tp, "oauth2", 6)) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (tl == 6 && memEQ(tp, "oauth2", 6)) { |
2422 |
0 |
2422 |
| else if (tl == 6 && memEQ(tp, "oauth2", 6)) { |
2421 |
1 |
2422 |
|
| 468 |
2421 |
| SV *flow = oa_get(aTHX_ h, "flow"), *scopes = oa_get(aTHX_ h, "scopes"); |
| 469 |
2421 |
| SV *au = oa_get(aTHX_ h, "authorizationUrl"), *tu = oa_get(aTHX_ h, "tokenUrl"); |
| 470 |
|
| STRLEN fl; const char *fp; |
| 471 |
|
| const char *dest; |
| 472 |
|
| int wants_auth, wants_token; |
| 473 |
|
| HV *flows, *f; |
| 474 |
2421 |
▶ | if (!flow || SvROK(flow)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!flow || SvROK(flow)) |
2420 |
1 |
2421 |
| if (!flow || SvROK(flow)) |
0 |
2420 |
2420 |
|
| 475 |
1 |
| croak("Open::API: securityDefinitions '%.*s': oauth2 has no 'flow' " |
| 476 |
|
| "(it is required)", (int)nl, name); |
| 477 |
2420 |
| fp = SvPV_const(flow, fl); |
| 478 |
2420 |
▶ | if (fl == 8 && memEQ(fp, "implicit", 8)) { dest = "implicit"; wants_auth = 1; wants_token = 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (fl == 8 && memEQ(fp, "implicit", 8)) { dest = "implicit"; wants_auth = 1; wants_token = 0; } |
1209 |
1211 |
2420 |
| if (fl == 8 && memEQ(fp, "implicit", 8)) { dest = "implicit"; wants_auth = 1; wants_token = 0; } |
605 |
604 |
1209 |
|
| 479 |
1815 |
▶ | else if (fl == 8 && memEQ(fp, "password", 8)) { dest = "password"; wants_auth = 0; wants_token = 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (fl == 8 && memEQ(fp, "password", 8)) { dest = "password"; wants_auth = 0; wants_token = 1; } |
604 |
1211 |
1815 |
| else if (fl == 8 && memEQ(fp, "password", 8)) { dest = "password"; wants_auth = 0; wants_token = 1; } |
604 |
0 |
604 |
|
| 480 |
1211 |
▶ | else if (fl == 11 && memEQ(fp, "application", 11)) { dest = "clientCredentials"; wants_auth = 0; wants_token = 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (fl == 11 && memEQ(fp, "application", 11)) { dest = "clientCredentials"; wants_auth = 0; wants_token = 1; } |
605 |
606 |
1211 |
| else if (fl == 11 && memEQ(fp, "application", 11)) { dest = "clientCredentials"; wants_auth = 0; wants_token = 1; } |
605 |
0 |
605 |
|
| 481 |
606 |
▶ | else if (fl == 10 && memEQ(fp, "accessCode", 10)) { dest = "authorizationCode"; wants_auth = 1; wants_token = 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (fl == 10 && memEQ(fp, "accessCode", 10)) { dest = "authorizationCode"; wants_auth = 1; wants_token = 1; } |
605 |
1 |
606 |
| else if (fl == 10 && memEQ(fp, "accessCode", 10)) { dest = "authorizationCode"; wants_auth = 1; wants_token = 1; } |
605 |
0 |
605 |
|
| 482 |
|
| else |
| 483 |
1 |
| croak("Open::API: securityDefinitions '%.*s': oauth2 flow '%.*s' is not " |
| 484 |
|
| "one of implicit, password, application or accessCode", |
| 485 |
|
| (int)nl, name, (int)fl, fp); |
| 486 |
2419 |
▶ | if (!scopes || !oa_hv_of(scopes)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!scopes || !oa_hv_of(scopes)) |
2418 |
1 |
2419 |
| if (!scopes || !oa_hv_of(scopes)) |
0 |
2418 |
2418 |
|
| 487 |
1 |
| croak("Open::API: securityDefinitions '%.*s': oauth2 has no 'scopes' " |
| 488 |
|
| "(it is required)", (int)nl, name); |
| 489 |
2418 |
▶ | if (wants_auth && !au) |
Branch: 2 branches
| Branch | True | False | Total |
| if (wants_auth && !au) |
1209 |
1209 |
2418 |
| if (wants_auth && !au) |
2 |
1207 |
1209 |
|
| 490 |
2 |
| croak("Open::API: securityDefinitions '%.*s': oauth2 flow '%.*s' has no " |
| 491 |
|
| "'authorizationUrl' (it is required)", (int)nl, name, (int)fl, fp); |
| 492 |
2416 |
▶ | if (wants_token && !tu) |
Branch: 2 branches
| Branch | True | False | Total |
| if (wants_token && !tu) |
1813 |
603 |
2416 |
| if (wants_token && !tu) |
3 |
1810 |
1813 |
|
| 493 |
3 |
| croak("Open::API: securityDefinitions '%.*s': oauth2 flow '%.*s' has no " |
| 494 |
|
| "'tokenUrl' (it is required)", (int)nl, name, (int)fl, fp); |
| 495 |
2413 |
| f = newHV(); |
| 496 |
2413 |
▶ | if (au) (void)hv_stores(f, "authorizationUrl", newSVsv(au)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (au) (void)hv_stores(f, "authorizationUrl", newSVsv(au)); |
1206 |
1207 |
2413 |
|
| 497 |
2413 |
▶ | if (tu) (void)hv_stores(f, "tokenUrl", newSVsv(tu)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (tu) (void)hv_stores(f, "tokenUrl", newSVsv(tu)); |
1810 |
603 |
2413 |
|
| 498 |
2413 |
| (void)hv_stores(f, "scopes", newSVsv(scopes)); |
| 499 |
2413 |
| flows = newHV(); |
| 500 |
2413 |
| (void)hv_store(flows, dest, (I32)strlen(dest), newRV_noinc((SV *)f), 0); |
| 501 |
2413 |
| (void)hv_stores(out, "type", newSVpvs("oauth2")); |
| 502 |
2413 |
| (void)hv_stores(out, "flows", newRV_noinc((SV *)flows)); |
| 503 |
|
| } |
| 504 |
|
| else |
| 505 |
1 |
| croak("Open::API: securityDefinitions '%.*s': type '%.*s' is not basic, " |
| 506 |
|
| "apiKey or oauth2", (int)nl, name, (int)tl, tp); |
| 507 |
4234 |
| oa_sw2_copy_extras(aTHX_ out, h); |
| 508 |
4234 |
| return newRV_inc((SV *)out); |
| 509 |
|
| } |
| 510 |
|
| |
| 511 |
|
| /* ---- shared builders for rules 4, 5, 8 and 10 --------------------------------- */ |
| 512 |
|
| |
| 513 |
|
| /* `{<each media type>: {schema}}`: the schema under every type in `types`, or |
| 514 |
|
| * under `dflt` when the list is NULL or empty (D4: a body with no consumes |
| 515 |
|
| * anywhere is JSON, a response with no produces likewise). `conv` is owned by |
| 516 |
|
| * the caller and handed over here. */ |
| 517 |
10865 |
| static SV *oa_sw2_content(pTHX_ SV *conv, AV *types, const char *dflt) { |
| 518 |
10865 |
| HV *content = newHV(); |
| 519 |
|
| I32 i, n; |
| 520 |
10865 |
▶ | if (types && (n = av_len(types)) >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (types && (n = av_len(types)) >= 0) { |
10261 |
604 |
10865 |
| if (types && (n = av_len(types)) >= 0) { |
10261 |
0 |
10261 |
|
| 521 |
23051 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
12790 |
10261 |
23051 |
|
| 522 |
12790 |
| SV **e = av_fetch(types, i, 0); |
| 523 |
12790 |
| HV *mt = newHV(); |
| 524 |
12790 |
| STRLEN cl; const char *cp = SvPV_const(*e, cl); |
| 525 |
12790 |
▶ | (void)hv_stores(mt, "schema", i == n ? conv : newSVsv(conv)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(mt, "schema", i == n ? conv : newSVsv(conv)); |
2529 |
10261 |
12790 |
|
| 526 |
12790 |
| (void)hv_store(content, cp, (I32)cl, newRV_noinc((SV *)mt), 0); |
| 527 |
|
| } |
| 528 |
|
| } else { |
| 529 |
604 |
| HV *mt = newHV(); |
| 530 |
604 |
| (void)hv_stores(mt, "schema", conv); |
| 531 |
604 |
| (void)hv_store(content, dflt, (I32)strlen(dflt), newRV_noinc((SV *)mt), 0); |
| 532 |
|
| } |
| 533 |
10865 |
| return newRV_noinc((SV *)content); |
| 534 |
|
| } |
| 535 |
|
| |
| 536 |
|
| /* the operation's own list when the key is PRESENT (an empty list "MAY be used |
| 537 |
|
| * to clear the global definition" and then falls to the default, not to the |
| 538 |
|
| * root), else the root's; NULL means the default */ |
| 539 |
14694 |
| static AV *oa_sw2_effective(pTHX_ oa_sw2 *S, HV *op, const char *key, AV *root) { |
| 540 |
14694 |
▶ | SV **e = op ? hv_fetch(op, key, (I32)strlen(key), 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **e = op ? hv_fetch(op, key, (I32)strlen(key), 0) : NULL; |
14694 |
0 |
14694 |
|
| 541 |
|
| AV *av; |
| 542 |
|
| PERL_UNUSED_ARG(S); |
| 543 |
14694 |
▶ | if (!e || !*e || !SvOK(*e)) return root; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!e || !*e || !SvOK(*e)) return root; |
3660 |
11034 |
14694 |
| if (!e || !*e || !SvOK(*e)) return root; |
3660 |
0 |
3660 |
| if (!e || !*e || !SvOK(*e)) return root; |
0 |
3660 |
3660 |
|
| 544 |
3660 |
| av = oa_sw2_mimes(aTHX_ *e, key); |
| 545 |
3660 |
▶ | return (av && av_len(av) >= 0) ? av : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (av && av_len(av) >= 0) ? av : NULL; |
3660 |
0 |
3660 |
| return (av && av_len(av) >= 0) ? av : NULL; |
2455 |
1205 |
3660 |
|
| 546 |
|
| } |
| 547 |
|
| |
| 548 |
|
| /* The inline type of a Parameter, Items or Header Object as a Schema Object: |
| 549 |
|
| * the draft-04 keywords 2.0 lists travel as they are, `type: file` becomes a |
| 550 |
|
| * binary string (the 3.0 pass then adds contentMediaType), and a nested Items |
| 551 |
|
| * Object recurses with its own collectionFormat carried as the |
| 552 |
|
| * x-oa-collectionFormat extension for the compiler to read (D5). `depth` is |
| 553 |
|
| * the Items nesting: 0 is the parameter itself, 4 the deepest accepted. */ |
| 554 |
|
| static const char *const oa_sw2_type_kws[] = { |
| 555 |
|
| "type", "format", "default", "enum", "maximum", "exclusiveMaximum", |
| 556 |
|
| "minimum", "exclusiveMinimum", "maxLength", "minLength", "pattern", |
| 557 |
|
| "maxItems", "minItems", "uniqueItems", "multipleOf", NULL |
| 558 |
|
| }; |
| 559 |
|
| |
| 560 |
25027 |
| static SV *oa_sw2_type_schema(pTHX_ oa_sw2 *S, HV *p, int depth, const char *where) { |
| 561 |
|
| HV *out; |
| 562 |
|
| SV *v, *type; |
| 563 |
|
| int i; |
| 564 |
25027 |
▶ | if (depth > 4) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth > 4) |
1 |
25026 |
25027 |
|
| 565 |
1 |
| croak("Open::API: %s: array items nested deeper than 4 levels", where); |
| 566 |
25026 |
| type = oa_get(aTHX_ p, "type"); |
| 567 |
25026 |
▶ | if (depth > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth > 0) { |
7914 |
17112 |
25026 |
|
| 568 |
7914 |
▶ | if (!type) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!type) |
1 |
7913 |
7914 |
|
| 569 |
1 |
| croak("Open::API: %s: items has no 'type' (it is required)", where); |
| 570 |
7913 |
▶ | if (oa_sw2_str_is(aTHX_ type, "file")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ type, "file")) |
0 |
7913 |
7913 |
|
| 571 |
0 |
| croak("Open::API: %s: items of type file: \"Files and models are not " |
| 572 |
|
| "allowed\"", where); |
| 573 |
|
| } |
| 574 |
25025 |
| out = newHV(); |
| 575 |
25025 |
| sv_2mortal((SV *)out); |
| 576 |
400400 |
▶ | for (i = 0; oa_sw2_type_kws[i]; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; oa_sw2_type_kws[i]; i++) |
375375 |
25025 |
400400 |
|
| 577 |
375375 |
▶ | if ((v = oa_get(aTHX_ p, oa_sw2_type_kws[i]))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = oa_get(aTHX_ p, oa_sw2_type_kws[i]))) |
31179 |
344196 |
375375 |
|
| 578 |
31179 |
| (void)hv_store(out, oa_sw2_type_kws[i], (I32)strlen(oa_sw2_type_kws[i]), |
| 579 |
|
| newSVsv(v), 0); |
| 580 |
25025 |
▶ | if (oa_sw2_str_is(aTHX_ type, "file")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ type, "file")) { |
603 |
24422 |
25025 |
|
| 581 |
603 |
| (void)hv_stores(out, "type", newSVpvs("string")); |
| 582 |
603 |
| (void)hv_stores(out, "format", newSVpvs("binary")); |
| 583 |
|
| } |
| 584 |
25025 |
▶ | if ((v = oa_get(aTHX_ p, "items"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = oa_get(aTHX_ p, "items"))) { |
7915 |
17110 |
25025 |
|
| 585 |
7915 |
| HV *ih = oa_hv_of(v); |
| 586 |
7915 |
▶ | if (!ih) croak("Open::API: %s: items must be an object", where); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ih) croak("Open::API: %s: items must be an object", where); |
0 |
7915 |
7915 |
|
| 587 |
7915 |
| (void)hv_stores(out, "items", oa_sw2_type_schema(aTHX_ S, ih, depth + 1, where)); |
| 588 |
17110 |
▶ | } else if (oa_sw2_str_is(aTHX_ type, "array")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (oa_sw2_str_is(aTHX_ type, "array")) { |
1 |
17109 |
17110 |
|
| 589 |
1 |
| croak("Open::API: %s: type array has no 'items' (it is required)", where); |
| 590 |
|
| } |
| 591 |
25017 |
▶ | if (depth > 0 && (v = oa_get(aTHX_ p, "collectionFormat"))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth > 0 && (v = oa_get(aTHX_ p, "collectionFormat"))) { |
7909 |
17108 |
25017 |
| if (depth > 0 && (v = oa_get(aTHX_ p, "collectionFormat"))) { |
606 |
7303 |
7909 |
|
| 592 |
606 |
▶ | if (oa_sw2_str_is(aTHX_ v, "multi")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ v, "multi")) |
1 |
605 |
606 |
|
| 593 |
1 |
| croak("Open::API: %s: collectionFormat multi is not a value of an " |
| 594 |
|
| "Items Object", where); |
| 595 |
605 |
| (void)hv_stores(out, "x-oa-collectionFormat", newSVsv(v)); |
| 596 |
|
| } |
| 597 |
|
| { /* an Items Object's extensions */ |
| 598 |
|
| HE *he; |
| 599 |
25016 |
| hv_iterinit(p); |
| 600 |
114319 |
▶ | while ((he = hv_iternext(p))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(p))) { |
89303 |
25016 |
114319 |
|
| 601 |
89303 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 602 |
89303 |
▶ | if (depth > 0 && oa_sw2_is_ext(k, (STRLEN)kl)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth > 0 && oa_sw2_is_ext(k, (STRLEN)kl)) |
9122 |
80181 |
89303 |
| if (depth > 0 && oa_sw2_is_ext(k, (STRLEN)kl)) |
0 |
9122 |
9122 |
|
| 603 |
0 |
| (void)hv_store(out, k, kl, newSVsv(hv_iterval(p, he)), 0); |
| 604 |
|
| } |
| 605 |
|
| } |
| 606 |
25016 |
| return newRV_inc((SV *)out); |
| 607 |
|
| } |
| 608 |
|
| |
| 609 |
|
| /* ---- rule 10: a Header Object -> a 3.x header carrying a schema -------------- */ |
| 610 |
1218 |
| static SV *oa_sw2_header(pTHX_ oa_sw2 *S, SV *hsv, const char *name, const char *where) { |
| 611 |
1218 |
| HV *h = oa_hv_of(hsv), *out; |
| 612 |
|
| SV *type, *cf; |
| 613 |
1218 |
▶ | if (!h) return newSVsv(hsv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) return newSVsv(hsv); |
0 |
1218 |
1218 |
|
| 614 |
|
| /* already 3.x-shaped: a converted document fed back through new() */ |
| 615 |
1218 |
▶ | if (oa_get(aTHX_ h, "schema") || oa_get(aTHX_ h, "$ref")) return newSVsv(hsv); |
Branch: 2 branches
| Branch | True | False | Total |
| if (oa_get(aTHX_ h, "schema") || oa_get(aTHX_ h, "$ref")) return newSVsv(hsv); |
1215 |
3 |
1218 |
| if (oa_get(aTHX_ h, "schema") || oa_get(aTHX_ h, "$ref")) return newSVsv(hsv); |
0 |
1215 |
1215 |
|
| 616 |
1215 |
| type = oa_get(aTHX_ h, "type"); |
| 617 |
1215 |
▶ | if (!type) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!type) |
0 |
1215 |
1215 |
|
| 618 |
0 |
| croak("Open::API: %s: header '%s' has no 'type' (it is required)", where, name); |
| 619 |
1215 |
▶ | if (oa_sw2_str_is(aTHX_ type, "file")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ type, "file")) |
0 |
1215 |
1215 |
|
| 620 |
0 |
| croak("Open::API: %s: header '%s': type file is not a header type", where, name); |
| 621 |
1215 |
| out = newHV(); |
| 622 |
1215 |
| sv_2mortal((SV *)out); |
| 623 |
1215 |
| (void)hv_stores(out, "schema", oa_sw2_type_schema(aTHX_ S, h, 0, where)); |
| 624 |
1215 |
▶ | if ((cf = oa_get(aTHX_ h, "collectionFormat"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((cf = oa_get(aTHX_ h, "collectionFormat"))) { |
605 |
610 |
1215 |
|
| 625 |
605 |
▶ | if (oa_sw2_str_is(aTHX_ cf, "multi")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ cf, "multi")) |
1 |
604 |
605 |
|
| 626 |
1 |
| croak("Open::API: %s: header '%s': collectionFormat multi is valid " |
| 627 |
|
| "only for parameters in query or formData", where, name); |
| 628 |
|
| /* a 3.x header is simple (comma) and nothing else; the other three |
| 629 |
|
| * delimiters travel as the extension the compiler reads (05) */ |
| 630 |
604 |
▶ | if (!oa_sw2_str_is(aTHX_ cf, "csv")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_sw2_str_is(aTHX_ cf, "csv")) |
604 |
0 |
604 |
|
| 631 |
604 |
| (void)hv_stores(out, "x-oa-collectionFormat", newSVsv(cf)); |
| 632 |
|
| } |
| 633 |
1214 |
| oa_sw2_copy_extras(aTHX_ out, h); |
| 634 |
1214 |
| return newRV_inc((SV *)out); |
| 635 |
|
| } |
| 636 |
|
| |
| 637 |
|
| /* ---- rules 8, 9, 10: a Response Object -------------------------------------- * |
| 638 |
|
| * |
| 639 |
|
| * `schema` becomes `content[<each produces type>].schema` (a root `type: file` |
| 640 |
|
| * is a binary string), `examples` {mime: value} becomes `content[mime].example`, |
| 641 |
|
| * each Header Object gets a schema. A `$ref` response is rewritten and left for |
| 642 |
|
| * the 3.0 pass to inline. By presence: a converted response has none of the |
| 643 |
|
| * three 2.0 spellings. */ |
| 644 |
13496 |
| static SV *oa_sw2_response(pTHX_ oa_sw2 *S, SV *r, AV *produces, const char *where) { |
| 645 |
13496 |
| HV *h = oa_hv_of(r), *out; |
| 646 |
|
| SV *v, *ref; |
| 647 |
13496 |
▶ | if (!h) return newSVsv(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) return newSVsv(r); |
0 |
13496 |
13496 |
|
| 648 |
13496 |
| out = oa_sw2_copy_hv(aTHX_ h); |
| 649 |
13496 |
| sv_2mortal((SV *)out); |
| 650 |
13496 |
▶ | if ((ref = oa_get(aTHX_ h, "$ref"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ref = oa_get(aTHX_ h, "$ref"))) { |
2409 |
11087 |
13496 |
|
| 651 |
2409 |
| (void)hv_stores(out, "$ref", oa_sw2_ref(aTHX_ S, ref)); |
| 652 |
2409 |
| return newRV_inc((SV *)out); |
| 653 |
|
| } |
| 654 |
11087 |
▶ | if ((v = oa_get(aTHX_ h, "schema"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = oa_get(aTHX_ h, "schema"))) { |
8650 |
2437 |
11087 |
|
| 655 |
8650 |
| HV *sh = oa_hv_of(v); |
| 656 |
|
| SV *conv; |
| 657 |
9253 |
▶ | if (sh && oa_sw2_str_is(aTHX_ oa_get(aTHX_ sh, "type"), "file")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sh && oa_sw2_str_is(aTHX_ oa_get(aTHX_ sh, "type"), "file")) { |
8650 |
0 |
8650 |
| if (sh && oa_sw2_str_is(aTHX_ oa_get(aTHX_ sh, "type"), "file")) { |
603 |
8047 |
8650 |
|
| 658 |
603 |
| HV *bin = newHV(); |
| 659 |
603 |
| (void)hv_stores(bin, "type", newSVpvs("string")); |
| 660 |
603 |
| (void)hv_stores(bin, "format", newSVpvs("binary")); |
| 661 |
603 |
| oa_sw2_copy_extras(aTHX_ bin, sh); |
| 662 |
603 |
| conv = newRV_noinc((SV *)bin); |
| 663 |
|
| } else { |
| 664 |
8047 |
| conv = oa_sw2_schema(aTHX_ S, v, 0); |
| 665 |
|
| } |
| 666 |
8650 |
| (void)hv_stores(out, "content", oa_sw2_content(aTHX_ conv, produces, "application/json")); |
| 667 |
8650 |
| (void)hv_delete(out, "schema", 6, G_DISCARD); |
| 668 |
|
| } |
| 669 |
11087 |
▶ | if ((v = oa_get(aTHX_ h, "examples"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = oa_get(aTHX_ h, "examples"))) { |
624 |
10463 |
11087 |
|
| 670 |
624 |
| HV *ex = oa_hv_of(v), *content; |
| 671 |
|
| HE *he; |
| 672 |
624 |
▶ | if (!ex) croak("Open::API: %s: examples must be an object keyed by media type", where); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ex) croak("Open::API: %s: examples must be an object keyed by media type", where); |
0 |
624 |
624 |
|
| 673 |
624 |
| content = oa_hv_of(oa_get(aTHX_ out, "content")); |
| 674 |
624 |
▶ | if (!content) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!content) { |
21 |
603 |
624 |
|
| 675 |
21 |
| content = newHV(); |
| 676 |
21 |
| (void)hv_stores(out, "content", newRV_noinc((SV *)content)); |
| 677 |
|
| } |
| 678 |
624 |
| hv_iterinit(ex); |
| 679 |
1248 |
▶ | while ((he = hv_iternext(ex))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(ex))) { |
624 |
624 |
1248 |
|
| 680 |
624 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 681 |
624 |
| SV **mte = hv_fetch(content, k, kl, 0); |
| 682 |
624 |
▶ | HV *mt = (mte && *mte) ? oa_hv_of(*mte) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| HV *mt = (mte && *mte) ? oa_hv_of(*mte) : NULL; |
603 |
21 |
624 |
| HV *mt = (mte && *mte) ? oa_hv_of(*mte) : NULL; |
603 |
0 |
603 |
|
| 683 |
624 |
▶ | if (!mt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mt) { |
21 |
603 |
624 |
|
| 684 |
21 |
| mt = newHV(); |
| 685 |
21 |
| (void)hv_store(content, k, kl, newRV_noinc((SV *)mt), 0); |
| 686 |
|
| } |
| 687 |
624 |
| (void)hv_stores(mt, "example", newSVsv(hv_iterval(ex, he))); |
| 688 |
|
| } |
| 689 |
624 |
| (void)hv_delete(out, "examples", 8, G_DISCARD); |
| 690 |
|
| } |
| 691 |
11087 |
▶ | if ((v = oa_get(aTHX_ h, "headers"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = oa_get(aTHX_ h, "headers"))) { |
615 |
10472 |
11087 |
|
| 692 |
615 |
| HV *hs = oa_hv_of(v); |
| 693 |
615 |
▶ | if (hs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hs) { |
615 |
0 |
615 |
|
| 694 |
615 |
| HV *nh = newHV(); |
| 695 |
|
| HE *he; |
| 696 |
615 |
| (void)hv_stores(out, "headers", newRV_noinc((SV *)nh)); |
| 697 |
615 |
| hv_iterinit(hs); |
| 698 |
1832 |
▶ | while ((he = hv_iternext(hs))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(hs))) { |
1218 |
614 |
1832 |
|
| 699 |
1218 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 700 |
1218 |
| SV *name = sv_2mortal(newSVpvn(k, (STRLEN)kl)); |
| 701 |
1218 |
| (void)hv_store(nh, k, kl, |
| 702 |
|
| oa_sw2_header(aTHX_ S, hv_iterval(hs, he), SvPVX(name), where), 0); |
| 703 |
|
| } |
| 704 |
|
| } |
| 705 |
|
| } |
| 706 |
11086 |
| return newRV_inc((SV *)out); |
| 707 |
|
| } |
| 708 |
|
| |
| 709 |
|
| /* ---- rules 7 and 8: the definition sections ---------------------------------- */ |
| 710 |
|
| |
| 711 |
|
| /* a body parameter definition as a Request Body Object: `required`, |
| 712 |
|
| * `description`, the schema under every root consumes type (D4's default when |
| 713 |
|
| * there are none), and the parameter's extensions; `name` is informational in |
| 714 |
|
| * 2.0 and dropped */ |
| 715 |
604 |
| static SV *oa_sw2_body_def(pTHX_ oa_sw2 *S, HV *p, const char *name, I32 nl) { |
| 716 |
|
| HV *out; |
| 717 |
604 |
| SV *schema = oa_get(aTHX_ p, "schema"), *req = oa_get(aTHX_ p, "required"); |
| 718 |
604 |
▶ | if (!schema) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!schema) |
0 |
604 |
604 |
|
| 719 |
0 |
| croak("Open::API: parameters '%.*s': a body parameter has no 'schema' " |
| 720 |
|
| "(it is required)", (int)nl, name); |
| 721 |
604 |
| out = newHV(); |
| 722 |
604 |
▶ | if (req) (void)hv_stores(out, "required", newSVsv(req)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (req) (void)hv_stores(out, "required", newSVsv(req)); |
603 |
1 |
604 |
|
| 723 |
604 |
| (void)hv_stores(out, "content", |
| 724 |
|
| oa_sw2_content(aTHX_ oa_sw2_schema(aTHX_ S, schema, 0), |
| 725 |
|
| S->consumes, "application/json")); |
| 726 |
604 |
| oa_sw2_copy_extras(aTHX_ out, p); |
| 727 |
604 |
| return newRV_noinc((SV *)out); |
| 728 |
|
| } |
| 729 |
|
| |
| 730 |
|
| /* ---- phase 05: a non-body Parameter Object ----------------------------------- * |
| 731 |
|
| * |
| 732 |
|
| * The inline type keywords become `schema` (oa_sw2_type_schema), and |
| 733 |
|
| * collectionFormat becomes what 3.x can spell for the location, or the |
| 734 |
|
| * x-oa-collectionFormat extension where it cannot (D5). THE CATCH: 2.0's |
| 735 |
|
| * default for an array is csv, which is NOT exploded, while 3.x's default for |
| 736 |
|
| * a query parameter is form WITH explode, the reverse - so csv on a query |
| 737 |
|
| * array is written out as `style: form, explode: false`, and only multi gets |
| 738 |
|
| * explode true. In a path or header, csv IS the default (simple), and ssv, |
| 739 |
|
| * tsv and pipes have no 3.x style at all, so they travel as the extension. */ |
| 740 |
12258 |
| static void oa_sw2_collection(pTHX_ HV *out, SV *cf, const char *in, int is_array, |
| 741 |
|
| const char *where) { |
| 742 |
12258 |
| const char *fmt = "csv"; |
| 743 |
12258 |
| STRLEN fl = 3; |
| 744 |
12258 |
| int query = strEQ(in, "query"); |
| 745 |
12258 |
▶ | if (cf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cf) { |
4871 |
7387 |
12258 |
|
| 746 |
4871 |
▶ | if (SvROK(cf)) croak("Open::API: parameter '%s': collectionFormat must be a string", where); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(cf)) croak("Open::API: parameter '%s': collectionFormat must be a string", where); |
0 |
4871 |
4871 |
|
| 747 |
4871 |
| fmt = SvPV_const(cf, fl); |
| 748 |
|
| } |
| 749 |
|
| /* "Determines the format of the array if type array is used": on a |
| 750 |
|
| * scalar it says nothing, and nothing is written */ |
| 751 |
12258 |
▶ | if (!is_array) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_array) return; |
6779 |
5479 |
12258 |
|
| 752 |
|
| #define OA_SW2_FMT(s) (fl == sizeof(s) - 1 && memEQ(fmt, s, fl)) |
| 753 |
5479 |
▶ | if (OA_SW2_FMT("multi")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (OA_SW2_FMT("multi")) { |
1818 |
3661 |
5479 |
| if (OA_SW2_FMT("multi")) { |
607 |
1211 |
1818 |
|
| 754 |
607 |
▶ | if (!query) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!query) |
2 |
605 |
607 |
|
| 755 |
2 |
| croak("Open::API: parameter '%s': collectionFormat multi is valid only for " |
| 756 |
|
| "parameters in query or formData", where); |
| 757 |
605 |
| (void)hv_stores(out, "style", newSVpvs("form")); |
| 758 |
605 |
| (void)hv_stores(out, "explode", newRV_noinc(newSViv(1))); |
| 759 |
4872 |
▶ | } else if (OA_SW2_FMT("csv")) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (OA_SW2_FMT("csv")) { |
3661 |
1211 |
4872 |
| } else if (OA_SW2_FMT("csv")) { |
2443 |
1218 |
3661 |
|
| 760 |
2443 |
▶ | if (query) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (query) { |
2438 |
5 |
2443 |
|
| 761 |
2438 |
| (void)hv_stores(out, "style", newSVpvs("form")); |
| 762 |
2438 |
| (void)hv_stores(out, "explode", newRV_noinc(newSViv(0))); |
| 763 |
|
| } |
| 764 |
2429 |
▶ | } else if (OA_SW2_FMT("ssv")) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (OA_SW2_FMT("ssv")) { |
1218 |
1211 |
2429 |
| } else if (OA_SW2_FMT("ssv")) { |
609 |
609 |
1218 |
|
| 765 |
609 |
▶ | if (query) (void)hv_stores(out, "style", newSVpvs("spaceDelimited")); |
Branch: 1 branch
| Branch | True | False | Total |
| if (query) (void)hv_stores(out, "style", newSVpvs("spaceDelimited")); |
605 |
4 |
609 |
|
| 766 |
4 |
| else (void)hv_stores(out, "x-oa-collectionFormat", newSVpvs("ssv")); |
| 767 |
1820 |
▶ | } else if (OA_SW2_FMT("pipes")) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (OA_SW2_FMT("pipes")) { |
1211 |
609 |
1820 |
| } else if (OA_SW2_FMT("pipes")) { |
1211 |
0 |
1211 |
|
| 768 |
1211 |
▶ | if (query) (void)hv_stores(out, "style", newSVpvs("pipeDelimited")); |
Branch: 1 branch
| Branch | True | False | Total |
| if (query) (void)hv_stores(out, "style", newSVpvs("pipeDelimited")); |
605 |
606 |
1211 |
|
| 769 |
606 |
| else (void)hv_stores(out, "x-oa-collectionFormat", newSVpvs("pipes")); |
| 770 |
609 |
▶ | } else if (OA_SW2_FMT("tsv")) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (OA_SW2_FMT("tsv")) { |
609 |
0 |
609 |
| } else if (OA_SW2_FMT("tsv")) { |
609 |
0 |
609 |
|
| 771 |
609 |
▶ | if (query) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (query) { |
605 |
4 |
609 |
|
| 772 |
605 |
| (void)hv_stores(out, "style", newSVpvs("form")); |
| 773 |
605 |
| (void)hv_stores(out, "explode", newRV_noinc(newSViv(0))); |
| 774 |
|
| } |
| 775 |
609 |
| (void)hv_stores(out, "x-oa-collectionFormat", newSVpvs("tsv")); |
| 776 |
|
| } else { |
| 777 |
0 |
| croak("Open::API: parameter '%s': collectionFormat '%.*s' is not csv, ssv, tsv, " |
| 778 |
|
| "pipes or multi", where, (int)fl, fmt); |
| 779 |
|
| } |
| 780 |
|
| #undef OA_SW2_FMT |
| 781 |
|
| } |
| 782 |
|
| |
| 783 |
|
| /* the keys oa_sw2_type_schema moves into the schema, plus the two the |
| 784 |
|
| * conversion consumes; everything else on the parameter stays */ |
| 785 |
61413 |
| static int oa_sw2_param_moved(const char *k, STRLEN kl) { |
| 786 |
|
| int i; |
| 787 |
734909 |
▶ | for (i = 0; oa_sw2_type_kws[i]; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; oa_sw2_type_kws[i]; i++) |
690707 |
44202 |
734909 |
|
| 788 |
690707 |
▶ | if (strlen(oa_sw2_type_kws[i]) == kl && memEQ(k, oa_sw2_type_kws[i], kl)) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (strlen(oa_sw2_type_kws[i]) == kl && memEQ(k, oa_sw2_type_kws[i], kl)) return 1; |
67584 |
623123 |
690707 |
| if (strlen(oa_sw2_type_kws[i]) == kl && memEQ(k, oa_sw2_type_kws[i], kl)) return 1; |
17211 |
50373 |
67584 |
|
| 789 |
44202 |
▶ | return (kl == 5 && memEQ(k, "items", 5)) || (kl == 16 && memEQ(k, "collectionFormat", 16)); |
Branch: 4 branches
| Branch | True | False | Total |
| return (kl == 5 && memEQ(k, "items", 5)) || (kl == 16 && memEQ(k, "collectionFormat", 16)); |
5482 |
38720 |
44202 |
| return (kl == 5 && memEQ(k, "items", 5)) || (kl == 16 && memEQ(k, "collectionFormat", 16)); |
0 |
5482 |
5482 |
| return (kl == 5 && memEQ(k, "items", 5)) || (kl == 16 && memEQ(k, "collectionFormat", 16)); |
4871 |
33849 |
38720 |
| return (kl == 5 && memEQ(k, "items", 5)) || (kl == 16 && memEQ(k, "collectionFormat", 16)); |
4871 |
0 |
4871 |
|
| 790 |
|
| } |
| 791 |
|
| |
| 792 |
12310 |
| static SV *oa_sw2_param_def(pTHX_ oa_sw2 *S, HV *p) { |
| 793 |
12310 |
| SV *type = oa_get(aTHX_ p, "type"), *schema = oa_get(aTHX_ p, "schema"); |
| 794 |
12310 |
| SV *in = oa_get(aTHX_ p, "in"), *name = oa_get(aTHX_ p, "name"); |
| 795 |
24620 |
▶ | const char *where = name && !SvROK(name) ? SvPV_nolen(name) : "(unnamed)"; |
Branch: 2 branches
| Branch | True | False | Total |
| const char *where = name && !SvROK(name) ? SvPV_nolen(name) : "(unnamed)"; |
12310 |
0 |
12310 |
| const char *where = name && !SvROK(name) ? SvPV_nolen(name) : "(unnamed)"; |
12310 |
0 |
12310 |
|
| 796 |
|
| const char *inp; |
| 797 |
|
| HV *out; |
| 798 |
|
| HE *he; |
| 799 |
|
| int is_path, is_query, is_header; |
| 800 |
|
| |
| 801 |
|
| /* a definition that is itself a Reference Object (not legal 2.0, which |
| 802 |
|
| * maps a name to a Parameter Object, but harmless): the string is |
| 803 |
|
| * rewritten and the 3.0 pass follows it, bounded, and refuses a cycle */ |
| 804 |
12310 |
▶ | if (oa_get(aTHX_ p, "$ref")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_get(aTHX_ p, "$ref")) { |
0 |
12310 |
12310 |
|
| 805 |
0 |
| out = oa_sw2_copy_hv(aTHX_ p); |
| 806 |
0 |
| (void)hv_stores(out, "$ref", oa_sw2_ref(aTHX_ S, oa_get(aTHX_ p, "$ref"))); |
| 807 |
0 |
| return newRV_noinc((SV *)out); |
| 808 |
|
| } |
| 809 |
|
| /* already 3.x-shaped (a converted document fed back, or an author who |
| 810 |
|
| * wrote `schema`): copied, with the schema walked for its $refs. "type: |
| 811 |
|
| * Required" is phrased so that shape survives: a parameter with neither |
| 812 |
|
| * type nor schema nor content is the one refused. */ |
| 813 |
12310 |
▶ | if (!type || schema) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!type || schema) { |
12265 |
45 |
12310 |
| if (!type || schema) { |
0 |
12265 |
12265 |
|
| 814 |
45 |
▶ | if (!type && !schema && !oa_get(aTHX_ p, "content")) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!type && !schema && !oa_get(aTHX_ p, "content")) |
45 |
0 |
45 |
| if (!type && !schema && !oa_get(aTHX_ p, "content")) |
1 |
44 |
45 |
| if (!type && !schema && !oa_get(aTHX_ p, "content")) |
1 |
0 |
1 |
|
| 815 |
1 |
| croak("Open::API: parameter '%s' has no 'type' (it is required)", where); |
| 816 |
44 |
| out = oa_sw2_copy_hv(aTHX_ p); |
| 817 |
44 |
▶ | if (schema) (void)hv_stores(out, "schema", oa_sw2_schema(aTHX_ S, schema, 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (schema) (void)hv_stores(out, "schema", oa_sw2_schema(aTHX_ S, schema, 0)); |
44 |
0 |
44 |
|
| 818 |
44 |
| return newRV_noinc((SV *)out); |
| 819 |
|
| } |
| 820 |
12265 |
▶ | if (!in || SvROK(in)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!in || SvROK(in)) |
12265 |
0 |
12265 |
| if (!in || SvROK(in)) |
0 |
12265 |
12265 |
|
| 821 |
0 |
| croak("Open::API: parameter '%s' has no 'in' (it is required)", where); |
| 822 |
12265 |
| inp = SvPV_nolen(in); |
| 823 |
12265 |
| is_path = strEQ(inp, "path"); is_query = strEQ(inp, "query"); is_header = strEQ(inp, "header"); |
| 824 |
12265 |
▶ | if (!is_path && !is_query && !is_header) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!is_path && !is_query && !is_header) |
7989 |
4276 |
12265 |
| if (!is_path && !is_query && !is_header) |
1212 |
6777 |
7989 |
| if (!is_path && !is_query && !is_header) |
1 |
1211 |
1212 |
|
| 825 |
1 |
| croak("Open::API: parameter '%s': in '%s' is not one of query, header, path, " |
| 826 |
|
| "formData or body", where, inp); |
| 827 |
12264 |
▶ | if (oa_sw2_str_is(aTHX_ type, "file")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ type, "file")) |
1 |
12263 |
12264 |
|
| 828 |
1 |
| croak("Open::API: parameter '%s': type file: \"the parameter MUST be in formData\"", |
| 829 |
|
| where); |
| 830 |
12263 |
▶ | if (is_path && !oa_sv_truthy(aTHX_ oa_get(aTHX_ p, "required"))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (is_path && !oa_sv_truthy(aTHX_ oa_get(aTHX_ p, "required"))) |
4276 |
7987 |
12263 |
| if (is_path && !oa_sv_truthy(aTHX_ oa_get(aTHX_ p, "required"))) |
1 |
4275 |
4276 |
|
| 831 |
1 |
| croak("Open::API: path parameter '%s' must have required: true", where); |
| 832 |
|
| |
| 833 |
12262 |
| out = newHV(); |
| 834 |
12262 |
| sv_2mortal((SV *)out); |
| 835 |
12262 |
| hv_iterinit(p); |
| 836 |
73675 |
▶ | while ((he = hv_iternext(p))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(p))) { |
61413 |
12262 |
73675 |
|
| 837 |
61413 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 838 |
61413 |
▶ | if (oa_sw2_param_moved(k, (STRLEN)kl)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_param_moved(k, (STRLEN)kl)) continue; |
27564 |
33849 |
61413 |
|
| 839 |
33849 |
| (void)hv_store(out, k, kl, newSVsv(hv_iterval(p, he)), 0); |
| 840 |
|
| } |
| 841 |
12262 |
| (void)hv_stores(out, "schema", oa_sw2_type_schema(aTHX_ S, p, 0, where)); |
| 842 |
12258 |
| oa_sw2_collection(aTHX_ out, oa_get(aTHX_ p, "collectionFormat"), inp, |
| 843 |
|
| oa_sw2_str_is(aTHX_ type, "array"), where); |
| 844 |
12256 |
| return newRV_inc((SV *)out); |
| 845 |
|
| } |
| 846 |
|
| |
| 847 |
1817 |
| static void oa_sw2_components(pTHX_ oa_sw2 *S) { |
| 848 |
1817 |
| SV *existing = oa_get(aTHX_ S->out, "components"); |
| 849 |
1817 |
▶ | HV *comp = oa_hv_of(existing) ? oa_sw2_copy_hv(aTHX_ oa_hv_of(existing)) : newHV(); |
Branch: 1 branch
| Branch | True | False | Total |
| HV *comp = oa_hv_of(existing) ? oa_sw2_copy_hv(aTHX_ oa_hv_of(existing)) : newHV(); |
10 |
1807 |
1817 |
|
| 850 |
|
| HV *defs, *ps, *rs, *sd; |
| 851 |
|
| HE *he; |
| 852 |
1817 |
| (void)hv_stores(S->out, "components", newRV_noinc((SV *)comp)); |
| 853 |
|
| |
| 854 |
1817 |
▶ | if ((defs = oa_hv_of(oa_get(aTHX_ S->src, "definitions")))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((defs = oa_hv_of(oa_get(aTHX_ S->src, "definitions")))) { |
1425 |
392 |
1817 |
|
| 855 |
1425 |
| HV *schemas = newHV(); |
| 856 |
1425 |
| (void)hv_stores(comp, "schemas", newRV_noinc((SV *)schemas)); |
| 857 |
1425 |
| hv_iterinit(defs); |
| 858 |
7142 |
▶ | while ((he = hv_iternext(defs))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(defs))) { |
5717 |
1425 |
7142 |
|
| 859 |
5717 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 860 |
5717 |
| (void)hv_store(schemas, k, kl, oa_sw2_schema(aTHX_ S, hv_iterval(defs, he), 0), 0); |
| 861 |
|
| } |
| 862 |
|
| } |
| 863 |
1817 |
▶ | if ((ps = oa_hv_of(oa_get(aTHX_ S->src, "parameters")))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ps = oa_hv_of(oa_get(aTHX_ S->src, "parameters")))) { |
607 |
1210 |
1817 |
|
| 864 |
607 |
| HV *params = newHV(), *bodies = newHV(); |
| 865 |
607 |
| (void)hv_stores(comp, "parameters", newRV_noinc((SV *)params)); |
| 866 |
607 |
| (void)hv_stores(comp, "requestBodies", newRV_noinc((SV *)bodies)); |
| 867 |
607 |
| hv_iterinit(ps); |
| 868 |
3022 |
▶ | while ((he = hv_iternext(ps))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(ps))) { |
2415 |
607 |
3022 |
|
| 869 |
2415 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 870 |
2415 |
| HV *p = oa_hv_of(hv_iterval(ps, he)); |
| 871 |
|
| SV *in; |
| 872 |
2415 |
▶ | if (!p) croak("Open::API: parameters '%.*s' is not an object", (int)kl, k); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) croak("Open::API: parameters '%.*s' is not an object", (int)kl, k); |
0 |
2415 |
2415 |
|
| 873 |
2415 |
| in = oa_get(aTHX_ p, "in"); |
| 874 |
2415 |
▶ | if (oa_sw2_str_is(aTHX_ in, "body")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ in, "body")) |
604 |
1811 |
2415 |
|
| 875 |
604 |
| (void)hv_store(bodies, k, kl, oa_sw2_body_def(aTHX_ S, p, k, kl), 0); |
| 876 |
1811 |
▶ | else if (oa_sw2_str_is(aTHX_ in, "formData")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (oa_sw2_str_is(aTHX_ in, "formData")) |
1206 |
605 |
1811 |
|
| 877 |
|
| /* several form members make ONE body, so no component can |
| 878 |
|
| * stand for one of them: it is inlined at every use (04) |
| 879 |
|
| * and a definition nothing references is dead */ |
| 880 |
1206 |
| continue; |
| 881 |
|
| else |
| 882 |
605 |
| (void)hv_store(params, k, kl, oa_sw2_param_def(aTHX_ S, p), 0); |
| 883 |
|
| } |
| 884 |
607 |
▶ | if (!HvUSEDKEYS(params)) (void)hv_delete(comp, "parameters", 10, G_DISCARD); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!HvUSEDKEYS(params)) (void)hv_delete(comp, "parameters", 10, G_DISCARD); |
0 |
607 |
607 |
| if (!HvUSEDKEYS(params)) (void)hv_delete(comp, "parameters", 10, G_DISCARD); |
2 |
605 |
607 |
|
| 885 |
607 |
▶ | if (!HvUSEDKEYS(bodies)) (void)hv_delete(comp, "requestBodies", 13, G_DISCARD); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!HvUSEDKEYS(bodies)) (void)hv_delete(comp, "requestBodies", 13, G_DISCARD); |
0 |
607 |
607 |
| if (!HvUSEDKEYS(bodies)) (void)hv_delete(comp, "requestBodies", 13, G_DISCARD); |
3 |
604 |
607 |
|
| 886 |
|
| } |
| 887 |
1817 |
▶ | if ((rs = oa_hv_of(oa_get(aTHX_ S->src, "responses")))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((rs = oa_hv_of(oa_get(aTHX_ S->src, "responses")))) { |
604 |
1213 |
1817 |
|
| 888 |
604 |
| HV *responses = newHV(); |
| 889 |
604 |
| (void)hv_stores(comp, "responses", newRV_noinc((SV *)responses)); |
| 890 |
604 |
| hv_iterinit(rs); |
| 891 |
1810 |
▶ | while ((he = hv_iternext(rs))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(rs))) { |
1206 |
604 |
1810 |
|
| 892 |
1206 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 893 |
1206 |
| (void)hv_store(responses, k, kl, |
| 894 |
|
| oa_sw2_response(aTHX_ S, hv_iterval(rs, he), S->produces, "responses"), 0); |
| 895 |
|
| } |
| 896 |
|
| } |
| 897 |
1817 |
▶ | if ((sd = oa_hv_of(oa_get(aTHX_ S->src, "securityDefinitions")))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((sd = oa_hv_of(oa_get(aTHX_ S->src, "securityDefinitions")))) { |
630 |
1187 |
1817 |
|
| 898 |
630 |
| HV *schemes = newHV(); |
| 899 |
630 |
| (void)hv_stores(comp, "securitySchemes", newRV_noinc((SV *)schemes)); |
| 900 |
630 |
| hv_iterinit(sd); |
| 901 |
4864 |
▶ | while ((he = hv_iternext(sd))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(sd))) { |
4245 |
619 |
4864 |
|
| 902 |
4245 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 903 |
4245 |
| (void)hv_store(schemes, k, kl, oa_sw2_scheme(aTHX_ S, k, kl, hv_iterval(sd, he)), 0); |
| 904 |
|
| } |
| 905 |
|
| } |
| 906 |
1806 |
▶ | if (!HvUSEDKEYS(comp)) (void)hv_delete(S->out, "components", 10, G_DISCARD); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!HvUSEDKEYS(comp)) (void)hv_delete(S->out, "components", 10, G_DISCARD); |
0 |
1806 |
1806 |
| if (!HvUSEDKEYS(comp)) (void)hv_delete(S->out, "components", 10, G_DISCARD); |
350 |
1456 |
1806 |
|
| 907 |
1806 |
| } |
| 908 |
|
| |
| 909 |
|
| /* ---- rules 1 to 7 and 11: the operation ------------------------------------- * |
| 910 |
|
| * |
| 911 |
|
| * The effective parameter list is the path item's with the operation's layered |
| 912 |
|
| * over by (name, in). Entries with `in` path, query or header STAY where they |
| 913 |
|
| * were (the compiler merges those levels itself, and moving them would change |
| 914 |
|
| * operation_doc). Entries with `in` body or formData, from either level, come |
| 915 |
|
| * out of their lists and become the operation's requestBody: one body at most, |
| 916 |
|
| * never beside formData, keyed by the effective consumes (D4). A `$ref` entry |
| 917 |
|
| * is followed into the SOURCE to learn its `in`; a body or formData definition |
| 918 |
|
| * reached that way is inlined, since the request body depends on THIS |
| 919 |
|
| * operation's consumes and several form members make one body. */ |
| 920 |
|
| |
| 921 |
|
| /* a non-body list entry: a reference rewritten, or the object with its schema |
| 922 |
|
| * walked (phase 05 turns the inline type into a schema here) */ |
| 923 |
12308 |
| static SV *oa_sw2_param_entry(pTHX_ oa_sw2 *S, HV *p) { |
| 924 |
12308 |
| SV *ref = oa_get(aTHX_ p, "$ref"); |
| 925 |
12308 |
▶ | if (ref) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ref) { |
603 |
11705 |
12308 |
|
| 926 |
603 |
| HV *c = oa_sw2_copy_hv(aTHX_ p); |
| 927 |
603 |
| (void)hv_stores(c, "$ref", oa_sw2_ref(aTHX_ S, ref)); |
| 928 |
603 |
| return newRV_noinc((SV *)c); |
| 929 |
|
| } |
| 930 |
11705 |
| return oa_sw2_param_def(aTHX_ S, p); |
| 931 |
|
| } |
| 932 |
|
| |
| 933 |
|
| /* the raw object a list entry stands for: itself, or the definition its $ref |
| 934 |
|
| * names; NULL when the reference cannot be followed (left for the 3.0 pass) */ |
| 935 |
18780 |
| static HV *oa_sw2_entry_raw(pTHX_ oa_sw2 *S, HV *p) { |
| 936 |
18780 |
▶ | return oa_get(aTHX_ p, "$ref") ? oa_sw2_deref(aTHX_ S, p, "parameters") : p; |
Branch: 1 branch
| Branch | True | False | Total |
| return oa_get(aTHX_ p, "$ref") ? oa_sw2_deref(aTHX_ S, p, "parameters") : p; |
3618 |
15162 |
18780 |
|
| 937 |
|
| } |
| 938 |
|
| |
| 939 |
36280 |
| static int oa_sw2_same_param(pTHX_ HV *x, HV *y) { |
| 940 |
36280 |
| SV *xn = oa_get(aTHX_ x, "name"), *yn = oa_get(aTHX_ y, "name"); |
| 941 |
36280 |
| SV *xi = oa_get(aTHX_ x, "in"), *yi = oa_get(aTHX_ y, "in"); |
| 942 |
36280 |
▶ | if (!xn || !yn || !xi || !yi) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!xn || !yn || !xi || !yi) return 0; |
36280 |
0 |
36280 |
| if (!xn || !yn || !xi || !yi) return 0; |
36280 |
0 |
36280 |
| if (!xn || !yn || !xi || !yi) return 0; |
36280 |
0 |
36280 |
| if (!xn || !yn || !xi || !yi) return 0; |
0 |
36280 |
36280 |
|
| 943 |
36280 |
▶ | return sv_eq(xn, yn) && sv_eq(xi, yi); |
Branch: 2 branches
| Branch | True | False | Total |
| return sv_eq(xn, yn) && sv_eq(xi, yi); |
3 |
36277 |
36280 |
| return sv_eq(xn, yn) && sv_eq(xi, yi); |
3 |
0 |
3 |
|
| 944 |
|
| } |
| 945 |
|
| |
| 946 |
624 |
| static int oa_sw2_is_form_type(const char *p, STRLEN l) { |
| 947 |
17 |
▶ | return (l == 33 && memEQ(p, "application/x-www-form-urlencoded", 33)) |
Branch: 1 branch
| Branch | True | False | Total |
| return (l == 33 && memEQ(p, "application/x-www-form-urlencoded", 33)) |
0 |
17 |
17 |
|
| 948 |
641 |
▶ | || (l == 19 && memEQ(p, "multipart/form-data", 19)); |
Branch: 3 branches
| Branch | True | False | Total |
| || (l == 19 && memEQ(p, "multipart/form-data", 19)); |
17 |
607 |
624 |
| || (l == 19 && memEQ(p, "multipart/form-data", 19)); |
605 |
2 |
607 |
| || (l == 19 && memEQ(p, "multipart/form-data", 19)); |
605 |
0 |
605 |
|
| 949 |
|
| } |
| 950 |
|
| |
| 951 |
|
| /* the Encoding Object entry a formData array member needs: csv is form |
| 952 |
|
| * UNEXPLODED (3.x's default for a form member is exploded, the reverse), multi |
| 953 |
|
| * exploded, ssv and pipes their named styles, tsv the extension (D5) */ |
| 954 |
1214 |
| static SV *oa_sw2_form_encoding(pTHX_ SV *cf, const char *where, const char *name) { |
| 955 |
1214 |
| HV *e = newHV(); |
| 956 |
1214 |
| const char *fmt = "csv"; |
| 957 |
1214 |
| STRLEN fl = 3; |
| 958 |
1214 |
▶ | if (cf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cf) { |
1214 |
0 |
1214 |
|
| 959 |
1214 |
▶ | if (SvROK(cf)) croak("Open::API: %s: formData '%s': collectionFormat must be a string", where, name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(cf)) croak("Open::API: %s: formData '%s': collectionFormat must be a string", where, name); |
0 |
1214 |
1214 |
|
| 960 |
1214 |
| fmt = SvPV_const(cf, fl); |
| 961 |
|
| } |
| 962 |
1214 |
▶ | if (fl == 3 && memEQ(fmt, "csv", 3)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (fl == 3 && memEQ(fmt, "csv", 3)) { |
608 |
606 |
1214 |
| if (fl == 3 && memEQ(fmt, "csv", 3)) { |
604 |
4 |
608 |
|
| 963 |
604 |
| (void)hv_stores(e, "style", newSVpvs("form")); |
| 964 |
604 |
| (void)hv_stores(e, "explode", newRV_noinc(newSViv(0))); |
| 965 |
610 |
▶ | } else if (fl == 5 && memEQ(fmt, "multi", 5)) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (fl == 5 && memEQ(fmt, "multi", 5)) { |
606 |
4 |
610 |
| } else if (fl == 5 && memEQ(fmt, "multi", 5)) { |
604 |
2 |
606 |
|
| 966 |
604 |
| (void)hv_stores(e, "style", newSVpvs("form")); |
| 967 |
604 |
| (void)hv_stores(e, "explode", newRV_noinc(newSViv(1))); |
| 968 |
6 |
▶ | } else if (fl == 3 && memEQ(fmt, "ssv", 3)) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (fl == 3 && memEQ(fmt, "ssv", 3)) { |
4 |
2 |
6 |
| } else if (fl == 3 && memEQ(fmt, "ssv", 3)) { |
2 |
2 |
4 |
|
| 969 |
2 |
| (void)hv_stores(e, "style", newSVpvs("spaceDelimited")); |
| 970 |
4 |
▶ | } else if (fl == 5 && memEQ(fmt, "pipes", 5)) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (fl == 5 && memEQ(fmt, "pipes", 5)) { |
2 |
2 |
4 |
| } else if (fl == 5 && memEQ(fmt, "pipes", 5)) { |
2 |
0 |
2 |
|
| 971 |
2 |
| (void)hv_stores(e, "style", newSVpvs("pipeDelimited")); |
| 972 |
2 |
▶ | } else if (fl == 3 && memEQ(fmt, "tsv", 3)) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (fl == 3 && memEQ(fmt, "tsv", 3)) { |
2 |
0 |
2 |
| } else if (fl == 3 && memEQ(fmt, "tsv", 3)) { |
2 |
0 |
2 |
|
| 973 |
2 |
| (void)hv_stores(e, "style", newSVpvs("form")); |
| 974 |
2 |
| (void)hv_stores(e, "explode", newRV_noinc(newSViv(0))); |
| 975 |
2 |
| (void)hv_stores(e, "x-oa-collectionFormat", newSVpvs("tsv")); |
| 976 |
|
| } else { |
| 977 |
0 |
| croak("Open::API: %s: formData '%s': collectionFormat '%.*s' is not csv, " |
| 978 |
|
| "ssv, tsv, pipes or multi", where, name, (int)fl, fmt); |
| 979 |
|
| } |
| 980 |
1214 |
| return newRV_noinc((SV *)e); |
| 981 |
|
| } |
| 982 |
|
| |
| 983 |
|
| #define OA_SW2_MAX_PARAMS 64 |
| 984 |
|
| |
| 985 |
7347 |
| static SV *oa_sw2_operation(pTHX_ oa_sw2 *S, HV *item, HV *op, const char *where) { |
| 986 |
7347 |
| HV *out = oa_sw2_copy_hv(aTHX_ op); |
| 987 |
7347 |
| AV *consumes = oa_sw2_effective(aTHX_ S, op, "consumes", S->consumes); |
| 988 |
7347 |
| AV *produces = oa_sw2_effective(aTHX_ S, op, "produces", S->produces); |
| 989 |
7347 |
| HV *body = NULL, *forms[OA_SW2_MAX_PARAMS], *seen[OA_SW2_MAX_PARAMS]; |
| 990 |
7347 |
| int nforms = 0, nseen = 0; |
| 991 |
7347 |
| AV *op_params = oa_av_of(oa_get(aTHX_ op, "parameters")); |
| 992 |
7347 |
| AV *item_params = oa_av_of(oa_get(aTHX_ item, "parameters")); |
| 993 |
|
| SV *v; |
| 994 |
|
| I32 i, n; |
| 995 |
7347 |
| sv_2mortal((SV *)out); |
| 996 |
|
| |
| 997 |
|
| /* the operation's own list */ |
| 998 |
7347 |
▶ | if (op_params) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (op_params) { |
7253 |
94 |
7347 |
|
| 999 |
7253 |
| AV *keep = newAV(); |
| 1000 |
7253 |
| (void)hv_stores(out, "parameters", newRV_noinc((SV *)keep)); |
| 1001 |
7253 |
| n = av_len(op_params); |
| 1002 |
24205 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
16965 |
7240 |
24205 |
|
| 1003 |
16965 |
| SV **e = av_fetch(op_params, i, 0); |
| 1004 |
16965 |
▶ | HV *p = (e && *e) ? oa_hv_of(*e) : NULL, *raw; |
Branch: 2 branches
| Branch | True | False | Total |
| HV *p = (e && *e) ? oa_hv_of(*e) : NULL, *raw; |
16965 |
0 |
16965 |
| HV *p = (e && *e) ? oa_hv_of(*e) : NULL, *raw; |
16965 |
0 |
16965 |
|
| 1005 |
|
| int k; |
| 1006 |
16965 |
▶ | if (!p) croak("Open::API: %s: a parameter is not an object", where); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) croak("Open::API: %s: a parameter is not an object", where); |
0 |
16965 |
16965 |
|
| 1007 |
16965 |
| raw = oa_sw2_entry_raw(aTHX_ S, p); |
| 1008 |
16965 |
▶ | if (!raw) { av_push(keep, newSVsv(*e)); continue; } /* refused by name later */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!raw) { av_push(keep, newSVsv(*e)); continue; } /* refused by name later */ |
1 |
16964 |
16965 |
|
| 1009 |
47212 |
▶ | for (k = 0; k < nseen; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nseen; k++) |
30250 |
16962 |
47212 |
|
| 1010 |
30250 |
▶ | if (oa_sw2_same_param(aTHX_ seen[k], raw)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_same_param(aTHX_ seen[k], raw)) |
2 |
30248 |
30250 |
|
| 1011 |
6 |
| croak("Open::API: %s: parameter '%s' (in %s) is declared twice in " |
| 1012 |
|
| "one list", where, SvPV_nolen(oa_get(aTHX_ raw, "name")), |
| 1013 |
|
| SvPV_nolen(oa_get(aTHX_ raw, "in"))); |
| 1014 |
16962 |
▶ | if (nseen < OA_SW2_MAX_PARAMS) seen[nseen++] = raw; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nseen < OA_SW2_MAX_PARAMS) seen[nseen++] = raw; |
16962 |
0 |
16962 |
|
| 1015 |
16962 |
▶ | if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "body")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "body")) { |
1620 |
15342 |
16962 |
|
| 1016 |
1620 |
▶ | if (body) |
Branch: 1 branch
| Branch | True | False | Total |
| if (body) |
1 |
1619 |
1620 |
|
| 1017 |
1 |
| croak("Open::API: %s: there can be one body parameter at most", where); |
| 1018 |
1619 |
| body = raw; |
| 1019 |
15342 |
▶ | } else if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "formData")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "formData")) { |
3639 |
11703 |
15342 |
|
| 1020 |
3639 |
▶ | if (nforms >= OA_SW2_MAX_PARAMS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nforms >= OA_SW2_MAX_PARAMS) |
0 |
3639 |
3639 |
|
| 1021 |
0 |
| croak("Open::API: %s: more than %d formData parameters", where, OA_SW2_MAX_PARAMS); |
| 1022 |
3639 |
| forms[nforms++] = raw; |
| 1023 |
|
| } else { |
| 1024 |
11703 |
| av_push(keep, oa_sw2_param_entry(aTHX_ S, p)); |
| 1025 |
|
| } |
| 1026 |
|
| } |
| 1027 |
|
| } |
| 1028 |
|
| /* the path item's list: body and formData members apply to this operation |
| 1029 |
|
| * unless it declares the same (name, in) itself; the rest stay on the item */ |
| 1030 |
7334 |
▶ | if (item_params) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (item_params) { |
1209 |
6125 |
7334 |
|
| 1031 |
1209 |
| n = av_len(item_params); |
| 1032 |
2418 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
1209 |
1209 |
2418 |
|
| 1033 |
1209 |
| SV **e = av_fetch(item_params, i, 0); |
| 1034 |
1209 |
▶ | HV *p = (e && *e) ? oa_hv_of(*e) : NULL, *raw; |
Branch: 2 branches
| Branch | True | False | Total |
| HV *p = (e && *e) ? oa_hv_of(*e) : NULL, *raw; |
1209 |
0 |
1209 |
| HV *p = (e && *e) ? oa_hv_of(*e) : NULL, *raw; |
1209 |
0 |
1209 |
|
| 1035 |
1209 |
| int k, overridden = 0; |
| 1036 |
1209 |
▶ | if (!p) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) continue; |
0 |
1209 |
1209 |
|
| 1037 |
1209 |
| raw = oa_sw2_entry_raw(aTHX_ S, p); |
| 1038 |
1209 |
▶ | if (!raw) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!raw) continue; |
0 |
1209 |
1209 |
|
| 1039 |
7238 |
▶ | for (k = 0; k < nseen; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nseen; k++) |
6030 |
1208 |
7238 |
|
| 1040 |
6030 |
▶ | if (oa_sw2_same_param(aTHX_ seen[k], raw)) { overridden = 1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_same_param(aTHX_ seen[k], raw)) { overridden = 1; break; } |
1 |
6029 |
6030 |
|
| 1041 |
1209 |
▶ | if (overridden) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (overridden) continue; |
1 |
1208 |
1209 |
|
| 1042 |
1208 |
▶ | if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "body")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "body")) { |
1 |
1207 |
1208 |
|
| 1043 |
1 |
▶ | if (body) |
Branch: 1 branch
| Branch | True | False | Total |
| if (body) |
0 |
1 |
1 |
|
| 1044 |
0 |
| croak("Open::API: %s: there can be one body parameter at most " |
| 1045 |
|
| "(one is declared on the path item too)", where); |
| 1046 |
1 |
| body = raw; |
| 1047 |
1207 |
▶ | } else if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "formData")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "formData")) { |
0 |
1207 |
1207 |
|
| 1048 |
0 |
▶ | if (nforms >= OA_SW2_MAX_PARAMS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nforms >= OA_SW2_MAX_PARAMS) |
0 |
0 |
0 |
|
| 1049 |
0 |
| croak("Open::API: %s: more than %d formData parameters", where, OA_SW2_MAX_PARAMS); |
| 1050 |
0 |
| forms[nforms++] = raw; |
| 1051 |
|
| } |
| 1052 |
|
| } |
| 1053 |
|
| } |
| 1054 |
7334 |
▶ | if (body && nforms) |
Branch: 2 branches
| Branch | True | False | Total |
| if (body && nforms) |
1619 |
5715 |
7334 |
| if (body && nforms) |
1 |
1618 |
1619 |
|
| 1055 |
1 |
| croak("Open::API: %s: a body parameter and formData parameters cannot be " |
| 1056 |
|
| "declared together", where); |
| 1057 |
|
| |
| 1058 |
|
| /* rule 4: the body */ |
| 1059 |
7333 |
▶ | if (body) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (body) { |
1618 |
5715 |
7333 |
|
| 1060 |
1618 |
| HV *rb = newHV(); |
| 1061 |
1618 |
| SV *schema = oa_get(aTHX_ body, "schema"), *req = oa_get(aTHX_ body, "required"); |
| 1062 |
1618 |
| (void)hv_stores(out, "requestBody", newRV_noinc((SV *)rb)); |
| 1063 |
1618 |
▶ | if (!schema) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!schema) |
1 |
1617 |
1618 |
|
| 1064 |
1 |
| croak("Open::API: %s: the body parameter has no 'schema' (it is required)", where); |
| 1065 |
1617 |
▶ | if (req) (void)hv_stores(rb, "required", newSVsv(req)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (req) (void)hv_stores(rb, "required", newSVsv(req)); |
1614 |
3 |
1617 |
|
| 1066 |
1617 |
| (void)hv_stores(rb, "content", |
| 1067 |
|
| oa_sw2_content(aTHX_ oa_sw2_schema(aTHX_ S, schema, 0), |
| 1068 |
|
| consumes, "application/json")); |
| 1069 |
1611 |
| oa_sw2_copy_extras(aTHX_ rb, body); |
| 1070 |
|
| } |
| 1071 |
|
| /* rule 5: the form */ |
| 1072 |
5715 |
▶ | else if (nforms) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (nforms) { |
1226 |
4489 |
5715 |
|
| 1073 |
1226 |
| HV *rb = newHV(), *schema = newHV(), *props = newHV(), *enc = newHV(); |
| 1074 |
1226 |
| AV *required = newAV(), *types; |
| 1075 |
1226 |
| int f, has_file = 0, any_required = 0; |
| 1076 |
1226 |
| (void)hv_stores(out, "requestBody", newRV_noinc((SV *)rb)); |
| 1077 |
1226 |
| sv_2mortal((SV *)schema); |
| 1078 |
1226 |
| sv_2mortal((SV *)enc); |
| 1079 |
1226 |
| (void)hv_stores(schema, "type", newSVpvs("object")); |
| 1080 |
1226 |
| (void)hv_stores(schema, "properties", newRV_noinc((SV *)props)); |
| 1081 |
1226 |
| (void)hv_stores(schema, "required", newRV_noinc((SV *)required)); |
| 1082 |
4863 |
▶ | for (f = 0; f < nforms; f++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (f = 0; f < nforms; f++) |
3637 |
1226 |
4863 |
|
| 1083 |
3637 |
▶ | if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ forms[f], "type"), "file")) has_file = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ forms[f], "type"), "file")) has_file = 1; |
604 |
3033 |
3637 |
|
| 1084 |
|
| /* the form media types: those of the effective consumes, or D4's default */ |
| 1085 |
1226 |
| types = (AV *)sv_2mortal((SV *)newAV()); |
| 1086 |
1226 |
▶ | if (consumes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (consumes) { |
624 |
602 |
1226 |
|
| 1087 |
624 |
| I32 cn = av_len(consumes); |
| 1088 |
1248 |
▶ | for (i = 0; i <= cn; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= cn; i++) { |
624 |
624 |
1248 |
|
| 1089 |
624 |
| SV **e = av_fetch(consumes, i, 0); |
| 1090 |
624 |
| STRLEN cl; const char *cp = SvPV_const(*e, cl); |
| 1091 |
624 |
▶ | if (oa_sw2_is_form_type(cp, cl)) av_push(types, newSVsv(*e)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_is_form_type(cp, cl)) av_push(types, newSVsv(*e)); |
622 |
2 |
624 |
|
| 1092 |
|
| } |
| 1093 |
624 |
▶ | if (av_len(types) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(types) < 0) { |
2 |
622 |
624 |
|
| 1094 |
2 |
▶ | if (has_file) |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_file) |
1 |
1 |
2 |
|
| 1095 |
1 |
| croak("Open::API: %s: a formData parameter of type file requires " |
| 1096 |
|
| "consumes multipart/form-data, application/x-www-form-urlencoded " |
| 1097 |
|
| "or both", where); |
| 1098 |
1 |
| croak("Open::API: %s: formData parameters need a form media type in " |
| 1099 |
|
| "consumes (multipart/form-data or application/x-www-form-urlencoded)", |
| 1100 |
|
| where); |
| 1101 |
|
| } |
| 1102 |
|
| } else { |
| 1103 |
602 |
▶ | av_push(types, newSVpv(has_file ? "multipart/form-data" |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(types, newSVpv(has_file ? "multipart/form-data" |
0 |
602 |
602 |
|
| 1104 |
|
| : "application/x-www-form-urlencoded", 0)); |
| 1105 |
|
| } |
| 1106 |
4859 |
▶ | for (f = 0; f < nforms; f++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (f = 0; f < nforms; f++) { |
3635 |
1224 |
4859 |
|
| 1107 |
3635 |
| HV *p = forms[f]; |
| 1108 |
3635 |
| SV *nm = oa_get(aTHX_ p, "name"), *prop; |
| 1109 |
|
| STRLEN nl; const char *np; |
| 1110 |
|
| /* two form members with one name were refused above as a |
| 1111 |
|
| * duplicated (name, in): the mutation table showed a check here |
| 1112 |
|
| * could never fire, so there is none */ |
| 1113 |
3635 |
▶ | if (!nm) croak("Open::API: %s: a formData parameter has no 'name'", where); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nm) croak("Open::API: %s: a formData parameter has no 'name'", where); |
0 |
3635 |
3635 |
|
| 1114 |
3635 |
| np = SvPV_const(nm, nl); |
| 1115 |
3635 |
| prop = oa_sw2_type_schema(aTHX_ S, p, 0, where); |
| 1116 |
3635 |
▶ | if (oa_sv_truthy(aTHX_ oa_get(aTHX_ p, "allowEmptyValue"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sv_truthy(aTHX_ oa_get(aTHX_ p, "allowEmptyValue"))) { |
603 |
3032 |
3635 |
|
| 1117 |
|
| /* "valid only for either query or formData": as a property the |
| 1118 |
|
| * keyword is gone, so the widening is written out (no const |
| 1119 |
|
| * in 3.0) */ |
| 1120 |
603 |
| HV *w = newHV(), *empty = newHV(); |
| 1121 |
603 |
| AV *any = newAV(), *en = newAV(); |
| 1122 |
603 |
| av_push(en, newSVpvs("")); |
| 1123 |
603 |
| (void)hv_stores(empty, "type", newSVpvs("string")); |
| 1124 |
603 |
| (void)hv_stores(empty, "enum", newRV_noinc((SV *)en)); |
| 1125 |
603 |
| av_push(any, prop); |
| 1126 |
603 |
| av_push(any, newRV_noinc((SV *)empty)); |
| 1127 |
603 |
| (void)hv_stores(w, "anyOf", newRV_noinc((SV *)any)); |
| 1128 |
603 |
| prop = newRV_noinc((SV *)w); |
| 1129 |
|
| } |
| 1130 |
3635 |
| (void)hv_store(props, np, (I32)nl, prop, 0); |
| 1131 |
3635 |
▶ | if (oa_sv_truthy(aTHX_ oa_get(aTHX_ p, "required"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sv_truthy(aTHX_ oa_get(aTHX_ p, "required"))) { |
1211 |
2424 |
3635 |
|
| 1132 |
1211 |
| av_push(required, newSVsv(nm)); |
| 1133 |
1211 |
| any_required = 1; |
| 1134 |
|
| } |
| 1135 |
3635 |
▶ | if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ p, "type"), "array")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ p, "type"), "array")) |
1214 |
2421 |
3635 |
|
| 1136 |
1214 |
| (void)hv_store(enc, np, (I32)nl, |
| 1137 |
|
| oa_sw2_form_encoding(aTHX_ oa_get(aTHX_ p, "collectionFormat"), where, np), 0); |
| 1138 |
|
| } |
| 1139 |
1224 |
▶ | if (av_len(required) < 0) (void)hv_delete(schema, "required", 8, G_DISCARD); |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(required) < 0) (void)hv_delete(schema, "required", 8, G_DISCARD); |
14 |
1210 |
1224 |
|
| 1140 |
1224 |
▶ | if (any_required) (void)hv_stores(rb, "required", newRV_noinc(newSViv(1))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (any_required) (void)hv_stores(rb, "required", newRV_noinc(newSViv(1))); |
1210 |
14 |
1224 |
|
| 1141 |
|
| { |
| 1142 |
1224 |
| HV *content = newHV(); |
| 1143 |
1224 |
| I32 tn = av_len(types); |
| 1144 |
1224 |
| (void)hv_stores(rb, "content", newRV_noinc((SV *)content)); |
| 1145 |
2448 |
▶ | for (i = 0; i <= tn; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= tn; i++) { |
1224 |
1224 |
2448 |
|
| 1146 |
1224 |
| SV **e = av_fetch(types, i, 0); |
| 1147 |
1224 |
| HV *mt = newHV(); |
| 1148 |
1224 |
| STRLEN cl; const char *cp = SvPV_const(*e, cl); |
| 1149 |
1224 |
| (void)hv_stores(mt, "schema", newRV_inc((SV *)schema)); |
| 1150 |
1224 |
▶ | if (HvUSEDKEYS(enc)) (void)hv_stores(mt, "encoding", newRV_inc((SV *)enc)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (HvUSEDKEYS(enc)) (void)hv_stores(mt, "encoding", newRV_inc((SV *)enc)); |
0 |
1224 |
1224 |
| if (HvUSEDKEYS(enc)) (void)hv_stores(mt, "encoding", newRV_inc((SV *)enc)); |
1214 |
10 |
1224 |
|
| 1151 |
1224 |
| (void)hv_store(content, cp, (I32)cl, newRV_noinc((SV *)mt), 0); |
| 1152 |
|
| } |
| 1153 |
|
| } |
| 1154 |
|
| } |
| 1155 |
7324 |
| (void)hv_delete(out, "consumes", 8, G_DISCARD); |
| 1156 |
7324 |
| (void)hv_delete(out, "produces", 8, G_DISCARD); |
| 1157 |
|
| |
| 1158 |
|
| /* rules 6 to 10: the responses */ |
| 1159 |
|
| { |
| 1160 |
7324 |
| HV *rh = oa_hv_of(oa_get(aTHX_ op, "responses")), *nr; |
| 1161 |
|
| HE *he; |
| 1162 |
7324 |
▶ | if (!rh) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rh) |
1 |
7323 |
7324 |
|
| 1163 |
1 |
| croak("Open::API: %s: operation has no 'responses' (it is required)", where); |
| 1164 |
7323 |
▶ | if (!HvUSEDKEYS(rh)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!HvUSEDKEYS(rh)) |
0 |
7323 |
7323 |
| if (!HvUSEDKEYS(rh)) |
1 |
7322 |
7323 |
|
| 1165 |
1 |
| croak("Open::API: %s: responses \"MUST contain at least one response code\"", where); |
| 1166 |
7322 |
| nr = newHV(); |
| 1167 |
7322 |
| (void)hv_stores(out, "responses", newRV_noinc((SV *)nr)); |
| 1168 |
7322 |
| hv_iterinit(rh); |
| 1169 |
20215 |
▶ | while ((he = hv_iternext(rh))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(rh))) { |
12895 |
7320 |
20215 |
|
| 1170 |
12895 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 1171 |
12895 |
▶ | if (oa_sw2_is_ext(k, (STRLEN)kl)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_is_ext(k, (STRLEN)kl)) { |
604 |
12291 |
12895 |
|
| 1172 |
604 |
| (void)hv_store(nr, k, kl, newSVsv(hv_iterval(rh, he)), 0); |
| 1173 |
604 |
| continue; |
| 1174 |
|
| } |
| 1175 |
|
| /* 2.0's keys are HTTP status codes and `default`; a range such as |
| 1176 |
|
| * 2XX is 3.0's, and the compiler would honour it silently */ |
| 1177 |
12291 |
▶ | if (kl == 3 && k[0] >= '1' && k[0] <= '5' |
Branch: 3 branches
| Branch | True | False | Total |
| if (kl == 3 && k[0] >= '1' && k[0] <= '5' |
8540 |
3751 |
12291 |
| if (kl == 3 && k[0] >= '1' && k[0] <= '5' |
8540 |
0 |
8540 |
| if (kl == 3 && k[0] >= '1' && k[0] <= '5' |
8540 |
0 |
8540 |
|
| 1178 |
8540 |
▶ | && (k[1] == 'X' || k[1] == 'x') && (k[2] == 'X' || k[2] == 'x')) |
Branch: 4 branches
| Branch | True | False | Total |
| && (k[1] == 'X' || k[1] == 'x') && (k[2] == 'X' || k[2] == 'x')) |
8539 |
1 |
8540 |
| && (k[1] == 'X' || k[1] == 'x') && (k[2] == 'X' || k[2] == 'x')) |
0 |
8539 |
8539 |
| && (k[1] == 'X' || k[1] == 'x') && (k[2] == 'X' || k[2] == 'x')) |
0 |
1 |
1 |
| && (k[1] == 'X' || k[1] == 'x') && (k[2] == 'X' || k[2] == 'x')) |
0 |
0 |
0 |
|
| 1179 |
1 |
| croak("Open::API: %s: response key '%.*s' is a status range, which " |
| 1180 |
|
| "Swagger 2.0 does not define (keys are status codes or default)", |
| 1181 |
|
| where, (int)kl, k); |
| 1182 |
12290 |
| (void)hv_store(nr, k, kl, |
| 1183 |
|
| oa_sw2_response(aTHX_ S, hv_iterval(rh, he), produces, where), 0); |
| 1184 |
|
| } |
| 1185 |
|
| } |
| 1186 |
|
| |
| 1187 |
|
| /* rule 11: operation schemes -> operation servers, when a host exists */ |
| 1188 |
7320 |
▶ | if ((v = oa_get(aTHX_ op, "schemes"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = oa_get(aTHX_ op, "schemes"))) { |
604 |
6716 |
7320 |
|
| 1189 |
604 |
| AV *schemes = oa_av_of(v); |
| 1190 |
604 |
▶ | if (!schemes) croak("Open::API: %s: schemes must be a list", where); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!schemes) croak("Open::API: %s: schemes must be a list", where); |
0 |
604 |
604 |
|
| 1191 |
604 |
| n = av_len(schemes); |
| 1192 |
1208 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
604 |
604 |
1208 |
|
| 1193 |
604 |
| SV **e = av_fetch(schemes, i, 0); |
| 1194 |
|
| STRLEN sl; const char *sp; |
| 1195 |
604 |
▶ | if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
604 |
0 |
604 |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
604 |
0 |
604 |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
604 |
0 |
604 |
| if (!e || !*e || SvROK(*e) || !SvOK(*e)) |
0 |
604 |
604 |
|
| 1196 |
0 |
| croak("Open::API: %s: schemes: a scheme must be a string", where); |
| 1197 |
604 |
| sp = SvPV_const(*e, sl); |
| 1198 |
604 |
▶ | if (!oa_sw2_scheme_ok(sp, sl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_sw2_scheme_ok(sp, sl)) |
0 |
604 |
604 |
|
| 1199 |
0 |
| croak("Open::API: %s: schemes value '%.*s' is not one of http, https, " |
| 1200 |
|
| "ws, wss", where, (int)sl, sp); |
| 1201 |
|
| } |
| 1202 |
604 |
▶ | if (S->has_host && n >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (S->has_host && n >= 0) { |
603 |
1 |
604 |
| if (S->has_host && n >= 0) { |
603 |
0 |
603 |
|
| 1203 |
603 |
| AV *servers = newAV(); |
| 1204 |
603 |
| STRLEN hl, bl = 0; const char *hp = SvPV_const(S->host, hl), *bpp = NULL; |
| 1205 |
1205 |
▶ | if (S->basepath) bpp = SvPV_const(S->basepath, bl); |
Branch: 1 branch
| Branch | True | False | Total |
| if (S->basepath) bpp = SvPV_const(S->basepath, bl); |
602 |
1 |
603 |
|
| 1206 |
603 |
| (void)hv_stores(out, "servers", newRV_noinc((SV *)servers)); |
| 1207 |
1206 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
603 |
603 |
1206 |
|
| 1208 |
603 |
| SV **e = av_fetch(schemes, i, 0); |
| 1209 |
603 |
| SV *url = newSVsv(*e); |
| 1210 |
603 |
| sv_catpvs(url, "://"); |
| 1211 |
603 |
| sv_catpvn(url, hp, hl); |
| 1212 |
603 |
▶ | if (bpp) sv_catpvn(url, bpp, bl); |
Branch: 1 branch
| Branch | True | False | Total |
| if (bpp) sv_catpvn(url, bpp, bl); |
602 |
1 |
603 |
|
| 1213 |
603 |
| oa_sw2_push_server(aTHX_ servers, url); |
| 1214 |
|
| } |
| 1215 |
|
| } |
| 1216 |
604 |
| (void)hv_delete(out, "schemes", 7, G_DISCARD); |
| 1217 |
|
| } |
| 1218 |
7320 |
| return newRV_inc((SV *)out); |
| 1219 |
|
| } |
| 1220 |
|
| |
| 1221 |
5491 |
| static SV *oa_sw2_path_item(pTHX_ oa_sw2 *S, SV *item, const char *path) { |
| 1222 |
5491 |
| HV *h = oa_hv_of(item), *out; |
| 1223 |
|
| SV *v; |
| 1224 |
|
| int m; |
| 1225 |
5491 |
▶ | if (!h) return newSVsv(item); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) return newSVsv(item); |
0 |
5491 |
5491 |
|
| 1226 |
5491 |
| out = oa_sw2_copy_hv(aTHX_ h); |
| 1227 |
5491 |
| sv_2mortal((SV *)out); |
| 1228 |
|
| /* path-level parameters: body and formData members were folded into every |
| 1229 |
|
| * operation; the rest stay here for the compiler's merge */ |
| 1230 |
5491 |
▶ | if ((v = oa_get(aTHX_ h, "parameters"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = oa_get(aTHX_ h, "parameters"))) { |
606 |
4885 |
5491 |
|
| 1231 |
606 |
| AV *src = oa_av_of(v), *keep; |
| 1232 |
|
| HV *seen[OA_SW2_MAX_PARAMS]; |
| 1233 |
606 |
| int nseen = 0; |
| 1234 |
|
| I32 i, n; |
| 1235 |
606 |
▶ | if (!src) croak("Open::API: %s: parameters must be a list", path); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) croak("Open::API: %s: parameters must be a list", path); |
0 |
606 |
606 |
|
| 1236 |
606 |
| keep = newAV(); |
| 1237 |
606 |
| (void)hv_stores(out, "parameters", newRV_noinc((SV *)keep)); |
| 1238 |
606 |
| n = av_len(src); |
| 1239 |
1212 |
▶ | for (i = 0; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; i++) { |
606 |
606 |
1212 |
|
| 1240 |
606 |
| SV **e = av_fetch(src, i, 0); |
| 1241 |
606 |
▶ | HV *p = (e && *e) ? oa_hv_of(*e) : NULL, *raw; |
Branch: 2 branches
| Branch | True | False | Total |
| HV *p = (e && *e) ? oa_hv_of(*e) : NULL, *raw; |
606 |
0 |
606 |
| HV *p = (e && *e) ? oa_hv_of(*e) : NULL, *raw; |
606 |
0 |
606 |
|
| 1242 |
|
| int k; |
| 1243 |
606 |
▶ | if (!p) croak("Open::API: %s: a parameter is not an object", path); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) croak("Open::API: %s: a parameter is not an object", path); |
0 |
606 |
606 |
|
| 1244 |
606 |
| raw = oa_sw2_entry_raw(aTHX_ S, p); |
| 1245 |
606 |
▶ | if (!raw) { av_push(keep, newSVsv(*e)); continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!raw) { av_push(keep, newSVsv(*e)); continue; } |
0 |
606 |
606 |
|
| 1246 |
606 |
▶ | for (k = 0; k < nseen; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nseen; k++) |
0 |
606 |
606 |
|
| 1247 |
0 |
▶ | if (oa_sw2_same_param(aTHX_ seen[k], raw)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_same_param(aTHX_ seen[k], raw)) |
0 |
0 |
0 |
|
| 1248 |
0 |
| croak("Open::API: %s: path item parameter '%s' (in %s) is declared " |
| 1249 |
|
| "twice in one list", path, SvPV_nolen(oa_get(aTHX_ raw, "name")), |
| 1250 |
|
| SvPV_nolen(oa_get(aTHX_ raw, "in"))); |
| 1251 |
606 |
▶ | if (nseen < OA_SW2_MAX_PARAMS) seen[nseen++] = raw; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nseen < OA_SW2_MAX_PARAMS) seen[nseen++] = raw; |
606 |
0 |
606 |
|
| 1252 |
606 |
▶ | if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "body") |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "body") |
605 |
1 |
606 |
|
| 1253 |
606 |
▶ | || oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "formData")) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| || oa_sw2_str_is(aTHX_ oa_get(aTHX_ raw, "in"), "formData")) continue; |
0 |
605 |
605 |
|
| 1254 |
605 |
| av_push(keep, oa_sw2_param_entry(aTHX_ S, p)); |
| 1255 |
|
| } |
| 1256 |
|
| } |
| 1257 |
49227 |
▶ | for (m = 0; oa_methods[m]; m++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (m = 0; oa_methods[m]; m++) { |
43763 |
5464 |
49227 |
|
| 1258 |
|
| HV *op; |
| 1259 |
43763 |
▶ | if (!(v = oa_get(aTHX_ h, oa_methods[m]))) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(v = oa_get(aTHX_ h, oa_methods[m]))) continue; |
36416 |
7347 |
43763 |
|
| 1260 |
7347 |
| op = oa_hv_of(v); |
| 1261 |
7347 |
▶ | if (!op) croak("Open::API: %s: %s is not an object", path, oa_methods[m]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!op) croak("Open::API: %s: %s is not an object", path, oa_methods[m]); |
0 |
7347 |
7347 |
|
| 1262 |
|
| { |
| 1263 |
7347 |
| SV *where = sv_2mortal(newSVpvf("%s %s", oa_methods[m], path)); |
| 1264 |
7347 |
| (void)hv_store(out, oa_methods[m], (I32)strlen(oa_methods[m]), |
| 1265 |
|
| oa_sw2_operation(aTHX_ S, h, op, SvPVX(where)), 0); |
| 1266 |
|
| } |
| 1267 |
|
| } |
| 1268 |
5464 |
| return newRV_inc((SV *)out); |
| 1269 |
|
| } |
| 1270 |
|
| |
| 1271 |
1806 |
| static void oa_sw2_paths(pTHX_ oa_sw2 *S) { |
| 1272 |
1806 |
| HV *paths = oa_hv_of(oa_get(aTHX_ S->src, "paths")), *dst; |
| 1273 |
|
| HE *he; |
| 1274 |
1806 |
▶ | if (!paths) return; /* rule 1 was checked by the caller */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!paths) return; /* rule 1 was checked by the caller */ |
0 |
1806 |
1806 |
|
| 1275 |
1806 |
| dst = newHV(); |
| 1276 |
1806 |
| (void)hv_stores(S->out, "paths", newRV_noinc((SV *)dst)); |
| 1277 |
1806 |
| hv_iterinit(paths); |
| 1278 |
7270 |
▶ | while ((he = hv_iternext(paths))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(paths))) { |
5491 |
1779 |
7270 |
|
| 1279 |
5491 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 1280 |
5491 |
| SV *path = sv_2mortal(newSVpvn(k, (STRLEN)kl)); |
| 1281 |
5491 |
| (void)hv_store(dst, k, kl, |
| 1282 |
|
| oa_sw2_path_item(aTHX_ S, hv_iterval(paths, he), SvPVX(path)), 0); |
| 1283 |
|
| } |
| 1284 |
|
| } |
| 1285 |
|
| |
| 1286 |
|
| /* ---- the entry point --------------------------------------------------------- */ |
| 1287 |
|
| |
| 1288 |
3924 |
| static SV *oa_swagger2_to_30(pTHX_ SV *doc) { |
| 1289 |
|
| oa_sw2 S; |
| 1290 |
|
| static const char *const gone[] = { |
| 1291 |
|
| "host", "basePath", "schemes", "consumes", "produces", |
| 1292 |
|
| "definitions", "parameters", "responses", "securityDefinitions", NULL |
| 1293 |
|
| }; |
| 1294 |
|
| int i; |
| 1295 |
3924 |
| S.src = (HV *)SvRV(doc); |
| 1296 |
3924 |
| S.out = oa_sw2_copy_hv(aTHX_ S.src); |
| 1297 |
3924 |
| S.consumes = NULL; |
| 1298 |
3924 |
| S.produces = NULL; |
| 1299 |
3924 |
| S.has_host = 0; |
| 1300 |
3924 |
| S.host = NULL; |
| 1301 |
3924 |
| S.basepath = NULL; |
| 1302 |
3924 |
| sv_2mortal((SV *)S.out); /* the rules below croak */ |
| 1303 |
|
| |
| 1304 |
|
| /* rule 1: "paths: Required." 3.1 allows a document without one; 2.0 does |
| 1305 |
|
| * not, and one that lacks it would load as an API with no operations. */ |
| 1306 |
3924 |
▶ | if (!oa_get(aTHX_ S.src, "paths")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_get(aTHX_ S.src, "paths")) |
1 |
3923 |
3924 |
|
| 1307 |
1 |
| croak("Open::API: a Swagger 2.0 document has no 'paths' (it is required)"); |
| 1308 |
|
| |
| 1309 |
3923 |
| S.consumes = oa_sw2_mimes(aTHX_ oa_get(aTHX_ S.src, "consumes"), "consumes"); |
| 1310 |
3922 |
| S.produces = oa_sw2_mimes(aTHX_ oa_get(aTHX_ S.src, "produces"), "produces"); |
| 1311 |
3921 |
| oa_sw2_servers(aTHX_ &S); |
| 1312 |
1817 |
| oa_sw2_components(aTHX_ &S); |
| 1313 |
1806 |
| oa_sw2_paths(aTHX_ &S); |
| 1314 |
|
| |
| 1315 |
|
| /* rule 10: the 2.0 keys are consumed, not copied beside their |
| 1316 |
|
| * replacements, or the second pass would convert them again */ |
| 1317 |
17790 |
▶ | for (i = 0; gone[i]; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; gone[i]; i++) |
16011 |
1779 |
17790 |
|
| 1318 |
16011 |
| (void)hv_delete(S.out, gone[i], (I32)strlen(gone[i]), G_DISCARD); |
| 1319 |
|
| |
| 1320 |
1779 |
| return newRV_inc((SV *)S.out); |
| 1321 |
|
| } |
| 1322 |
|
| |
| 1323 |
|
| #endif /* OA_SWAGGER2_H */ |