From 6ec7a5370061d09bab5c59149125643222138db1 Mon Sep 17 00:00:00 2001 From: Eddie Kohler Date: Thu, 12 Sep 2024 22:31:30 -0400 Subject: [PATCH] API spec --- etc/apifunctions.json | 8 +++--- src/api/api_specvalidator.php | 46 +++++++++++++++++------------------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/etc/apifunctions.json b/etc/apifunctions.json index 75f30fcc2a..fbf9ff20ae 100644 --- a/etc/apifunctions.json +++ b/etc/apifunctions.json @@ -80,7 +80,7 @@ "name": "fieldhtml", "get": true, "function": "Search_API::fieldhtml", "parameters": "f ?aufull ?session q ?t ?sort", - "response": "fields data ?stat" + "response": "fields data ?stat ?classes" }, { "name": "follow", "paper": true, "post": true, "xxx": true, @@ -208,12 +208,14 @@ { "name": "reviewrating", "paper": true, "get": true, "function": "Review_API::reviewrating", - "parameters": "r" + "parameters": "r", + "response": "?ratings ?user_rating" }, { "name": "reviewrating", "paper": true, "post": true, "function": "Review_API::reviewrating", - "parameters": "r =user_rating" + "parameters": "r =user_rating", + "response": "?ratings ?user_rating" }, { "name": "reviewround", "paper": true, "post": true, "xxx": true, diff --git a/src/api/api_specvalidator.php b/src/api/api_specvalidator.php index c73e08ded7..e8fc6cc1f5 100644 --- a/src/api/api_specvalidator.php +++ b/src/api/api_specvalidator.php @@ -29,6 +29,9 @@ static function request($uf, Qrequest $qreq) { } else if ($p[$i] === ":") { $t |= self::F_SUFFIX; $has_suffix = true; + } else if ($p[$i] === "*") { + $t &= ~self::F_REQUIRED; + break; } else { break; } @@ -41,8 +44,7 @@ static function request($uf, Qrequest $qreq) { if (($t = self::lookup_type($n, $known, $has_suffix)) === null) { if (!in_array($n, ["post", "base", "fn", "forceShow", "cap", "actas", "smsg", "_"]) && ($n !== "p" || !($uf->paper ?? false)) - && ($n !== "redirect" || !($uf->redirect ?? false)) - && !isset($known["*"])) { + && ($n !== "redirect" || !($uf->redirect ?? false))) { self::error($qreq, "query param `{$n}` unknown"); } } else if (($t & self::F_BODY) !== 0) { @@ -51,9 +53,7 @@ static function request($uf, Qrequest $qreq) { } foreach (array_keys($_POST) as $n) { if (($t = self::lookup_type($n, $known, $has_suffix)) === null) { - if (!isset($known["*"])) { - self::error($qreq, "post param `{$n}` unknown"); - } + self::error($qreq, "post param `{$n}` unknown"); } else if (!isset($_GET[$n]) && ($t & self::F_BODY) === 0) { self::error($qreq, "post param `{$n}` should be in query"); @@ -62,9 +62,7 @@ static function request($uf, Qrequest $qreq) { foreach (array_keys($_FILES) as $n) { if (($t = self::lookup_type($n, $known, $has_suffix)) === null || ($t & (self::F_FILE | self::F_BODY)) === 0) { - if (!isset($known["*"])) { - self::error($qreq, "file param `{$n}` unknown"); - } + self::error($qreq, "file param `{$n}` unknown"); } } foreach ($known as $n => $t) { @@ -101,6 +99,9 @@ static function response($uf, Qrequest $qreq, JsonResult $jr) { } else if ($p[$i] === ":") { $t |= self::F_SUFFIX; $has_suffix = true; + } else if ($p[$i] === "*") { + $t &= ~self::F_REQUIRED; + break; } else { break; } @@ -138,23 +139,22 @@ static function lookup_type($n, &$known, $has_suffix) { $known[$n] |= self::F_PRESENT; return $known[$n]; } - if (!$has_suffix) { - return null; - } - $colon = strpos($n, ":"); - $slash = strpos($n, "/"); - if ($colon === false || ($slash !== false && $colon > $slash)) { - $colon = $slash; - } - if ($colon !== false) { - $pfx = substr($n, 0, $colon); - $t = $known[$pfx] ?? 0; - if (($t & self::F_SUFFIX) !== 0) { - $known[$pfx] |= self::F_PRESENT; - return $t; + if ($has_suffix) { + $colon = strpos($n, ":"); + $slash = strpos($n, "/"); + if ($colon === false || ($slash !== false && $colon > $slash)) { + $colon = $slash; + } + if ($colon !== false) { + $pfx = substr($n, 0, $colon); + $t = $known[$pfx] ?? 0; + if (($t & self::F_SUFFIX) !== 0) { + $known[$pfx] |= self::F_PRESENT; + return $t; + } } } - return null; + return $known["*"] ?? null; } static function unparse_param_type($n, $t) {