Skip to content

Commit c278dea

Browse files
committed
Did you know input was set to json content if there is json contant
1 parent 74247e0 commit c278dea

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

sample/src/Routes/Post.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class PostRequest extends AbstractRequest
2222
public function __construct(
2323
#[HeaderRequestParam(name: 'X-My-Authorization')]
2424
public readonly string $authorization,
25-
#[CookieRequestParam(name: 'X-My-Cookie')]
2625
public readonly string $someVar,
2726
public readonly string $someMessage,
2827
public readonly PostRequestSubObject $subObject,

src/Model/Request/RequestParser.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ public static function generateRequest(
2121
}
2222

2323
$propertyTypes = self::getParamTypes($requestClass, $method);
24-
$cacheJsonContent = null;
2524
$constructorArgs = array_map(
26-
fn (RequestProperty $propertyType) => self::getConstructorArgument($request, $propertyType, $cacheJsonContent),
25+
fn (RequestProperty $propertyType) => self::getConstructorArgument($request, $propertyType),
2726
$propertyTypes,
2827
);
2928

@@ -37,16 +36,10 @@ public static function generateRequest(
3736
private static function getConstructorArgument(
3837
Request $request,
3938
RequestProperty $propertyType,
40-
?array &$cacheJsonContent,
4139
): mixed {
42-
if ($propertyType->type === InputParamType::Json && !isset($cacheJsonContent)) {
43-
$cacheJsonContent = $request->content->getParsedBody() ?? [];
44-
}
45-
4640
$content = (match ($propertyType->type) {
4741
InputParamType::Query => $request->query[$propertyType->name] ?? null,
48-
InputParamType::Json => $cacheJsonContent[$propertyType->name] ?? null,
49-
InputParamType::Input => $request->input[$propertyType->name] ?? null,
42+
InputParamType::Input, InputParamType::Json => $request->input[$propertyType->name] ?? null,
5043
InputParamType::Header => $request->headers[strtolower($propertyType->name)] ?? null,
5144
InputParamType::Cookie => $request->cookies[$propertyType->name] ?? null,
5245
default => null,
@@ -111,6 +104,7 @@ public static function getParamTypes(string $requestClass, ?string $httpMethod,
111104

112105
$constructorParams = $constructor->getParameters();
113106
$result = [];
107+
$contentType = null;
114108

115109
foreach ($constructorParams as $param) {
116110
$property = $reflectionClass->getProperty($param->getName());
@@ -150,6 +144,17 @@ public static function getParamTypes(string $requestClass, ?string $httpMethod,
150144
$subProperties = RequestParser::getParamTypes($propertyType->getName(), $httpMethod, false);
151145
}
152146

147+
if (!isset($contentType) && in_array($inputParamType, [InputParamType::Input, InputParamType::Json], true)) {
148+
$contentType = $inputParamType;
149+
} elseif (in_array($inputParamType, [InputParamType::Input, InputParamType::Json], true)
150+
&& $inputParamType != $contentType
151+
) {
152+
throw new InvalidArgumentException(
153+
"Request class $requestClass has conflicting input types for property $name. "
154+
. "Cannot be both $inputParamType and $contentType",
155+
);
156+
}
157+
153158
$result[] = new RequestProperty(
154159
name: $name,
155160
propertyName: $property->getName(),

0 commit comments

Comments
 (0)