Skip to content

Commit 964387a

Browse files
committed
Merge branch 'meta-testing' of github.com:ReCodEx/api into meta-testing
2 parents d109873 + b639c9e commit 964387a

3 files changed

Lines changed: 424 additions & 26 deletions

File tree

app/V1Module/presenters/base/BasePresenter.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use App\Responses\StorageFileResponse;
2828
use App\Responses\ZipFilesResponse;
2929
use Nette\Application\Application;
30+
use Nette\Application\Request;
3031
use Nette\Http\IResponse;
3132
use Tracy\ILogger;
3233
use ReflectionClass;
@@ -129,7 +130,7 @@ public function startup()
129130
$this->tryCall($this->formatPermissionCheckMethod($this->getAction()), $this->params);
130131

131132
Validators::init();
132-
$this->processParams($actionReflection);
133+
$this->processParams($this->getRequest(), $actionReflection);
133134
}
134135

135136
protected function isRequestJson(): bool
@@ -205,29 +206,30 @@ public function getFormatInstance(): MetaFormat
205206
return $this->requestFormatInstance;
206207
}
207208

208-
private function processParams(ReflectionMethod $reflection)
209+
private function processParams(Request $request, ReflectionMethod $reflection)
209210
{
210211
// use a method specialized for formats if there is a format available
211212
$format = MetaFormatHelper::extractFormatFromAttribute($reflection);
212213
if ($format !== null) {
213-
$this->requestFormatInstance = $this->processParamsFormat($format, null);
214+
$this->requestFormatInstance = $this->processParamsFormat($request, $format, null);
214215
}
215216

216217
// handle loose parameters
217218
$paramData = MetaFormatHelper::extractRequestParamData($reflection);
218-
$this->processParamsLoose($paramData);
219+
$this->processParamsLoose($request, $paramData);
219220
}
220221

221222
/**
222223
* Processes loose parameters. Request parameters are validated, no new data is created.
223224
* @throws InvalidApiArgumentException Thrown when the request parameter values do not conform to the definition.
225+
* @param Request $request Request object holding the request data.
224226
* @param array $paramData Parameter data to be validated.
225227
*/
226-
private function processParamsLoose(array $paramData)
228+
private function processParamsLoose(Request $request, array $paramData)
227229
{
228230
// validate each param
229231
foreach ($paramData as $param) {
230-
$paramValue = $this->getValueFromParamData($param);
232+
$paramValue = $this->getValueFromParamData($request, $param);
231233

232234
// this throws when it does not conform
233235
$param->conformsToDefinition($paramValue);
@@ -237,6 +239,7 @@ private function processParamsLoose(array $paramData)
237239
/**
238240
* Processes parameters defined by a format. Request parameters are validated and a format instance with
239241
* parameter values created.
242+
* @param Request $request Request object holding the request data.
240243
* @param string $format The format defining the parameters.
241244
* @param ?array $valueDictionary If not null, a nested format instance will be created. The values will be taken
242245
* from here instead of the request object. Format validation ignores parameter type (path, query or post).
@@ -246,7 +249,7 @@ private function processParamsLoose(array $paramData)
246249
* @throws InvalidApiArgumentException Thrown when the request parameter values do not conform to the definition.
247250
* @return MetaFormat Returns a format instance with values filled from the request object.
248251
*/
249-
private function processParamsFormat(string $format, ?array $valueDictionary): MetaFormat
252+
private function processParamsFormat(Request $request, string $format, ?array $valueDictionary): MetaFormat
250253
{
251254
// get the parsed attribute data from the format fields
252255
$formatToFieldDefinitionsMap = FormatCache::getFormatToFieldDefinitionsMap();
@@ -262,7 +265,7 @@ private function processParamsFormat(string $format, ?array $valueDictionary): M
262265
$value = null;
263266
// top-level format
264267
if ($valueDictionary === null) {
265-
$value = $this->getValueFromParamData($requestParamData);
268+
$value = $this->getValueFromParamData($request, $requestParamData);
266269
// nested format
267270
} else {
268271
// Instead of retrieving the values with the getRequest call, use the provided $valueDictionary.
@@ -278,7 +281,7 @@ private function processParamsFormat(string $format, ?array $valueDictionary): M
278281
// replace the value dictionary stored in $value with a format instance
279282
$nestedFormatName = $requestParamData->getFormatName();
280283
if ($nestedFormatName !== null) {
281-
$value = $this->processParamsFormat($nestedFormatName, $value);
284+
$value = $this->processParamsFormat($request, $nestedFormatName, $value);
282285
}
283286

284287
// this throws if the value is invalid
@@ -295,37 +298,37 @@ private function processParamsFormat(string $format, ?array $valueDictionary): M
295298

296299
/**
297300
* Calls either getPostField, getQueryField or getPathField based on the provided metadata.
301+
* @param Request $request Request object holding the request data.
298302
* @param \App\Helpers\MetaFormats\RequestParamData $paramData Metadata of the request parameter.
299303
* @throws \App\Exceptions\InternalServerException Thrown when an unexpected parameter location was set.
300304
* @return mixed Returns the value from the request.
301305
*/
302-
private function getValueFromParamData(RequestParamData $paramData): mixed
306+
private function getValueFromParamData(Request $request, RequestParamData $paramData): mixed
303307
{
304308
switch ($paramData->type) {
305309
case Type::Post:
306-
return $this->getPostField($paramData->name, required: $paramData->required);
310+
return $this->getPostField($request, $paramData->name, required: $paramData->required);
307311
case Type::Query:
308-
return $this->getQueryField($paramData->name, required: $paramData->required);
312+
return $this->getQueryField($request, $paramData->name, required: $paramData->required);
309313
case Type::Path:
310-
return $this->getPathField($paramData->name);
314+
return $this->getPathField($request, $paramData->name);
311315
default:
312316
throw new InternalServerException("Unknown parameter type: {$paramData->type->name}");
313317
}
314318
}
315319

316-
private function getPostField($param, $required = true)
320+
private function getPostField(Request $request, $param, $required = true)
317321
{
318-
$req = $this->getRequest();
319-
$post = $req->getPost();
322+
$post = $request->getPost();
320323

321-
if ($req->isMethod("POST")) {
324+
if ($request->isMethod("POST")) {
322325
// nothing to see here...
323326
} else {
324-
if ($req->isMethod("PUT") || $req->isMethod("DELETE")) {
327+
if ($request->isMethod("PUT") || $request->isMethod("DELETE")) {
325328
parse_str(file_get_contents('php://input'), $post);
326329
} else {
327330
throw new WrongHttpMethodException(
328-
"Cannot get the post parameters in method '" . $req->getMethod() . "'."
331+
"Cannot get the post parameters in method '" . $request->getMethod() . "'."
329332
);
330333
}
331334
}
@@ -341,18 +344,18 @@ private function getPostField($param, $required = true)
341344
}
342345
}
343346

344-
private function getQueryField($param, $required = true)
347+
private function getQueryField(Request $request, $param, $required = true)
345348
{
346-
$value = $this->getRequest()->getParameter($param);
349+
$value = $request->getParameter($param);
347350
if ($value === null && $required) {
348351
throw new BadRequestException("Missing required query field $param");
349352
}
350353
return $value;
351354
}
352355

353-
private function getPathField($param)
356+
private function getPathField(Request $request, $param)
354357
{
355-
$value = $this->getParameter($param);
358+
$value = $request->getParameter($param);
356359
if ($value === null) {
357360
throw new BadRequestException("Missing required path field $param");
358361
}

0 commit comments

Comments
 (0)