Skip to content

Commit

Permalink
fix content type, special case for multipart/form-data
Browse files Browse the repository at this point in the history
Signed-off-by: Claudiu Pintiuta <[email protected]>
  • Loading branch information
pinclau committed Jul 8, 2024
1 parent d136bad commit 19f746c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
24 changes: 20 additions & 4 deletions config/autoload/content-negotiation.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,35 @@

return [
'content-negotiation' => [
'default' => [ // default to any route if not configured above
'Accept' => [
'default' => [ // default to any route if not configured above
'Accept' => [ // the Accept is what format the server can send back
'application/json',
'application/hal+json',
],
'Content-Type' => [
'Content-Type' => [ // the Content-Type is what format the server can process
'application/json',
'application/hal+json',
],
],
'your.route.name' => [
'your.route.name' => [
'Accept' => [],
'Content-Type' => [],
],
'user.avatar.create' => [
'Accept' => [
'application/json',
'application/hal+json',
],
'Content-Type' => [
'multipart/form-data',
],
],
'user.my-avatar.create' => [
'Accept' => [
'application/json',
'application/hal+json',
],
'Content-Type' => 'multipart/form-data',
],
],
];
7 changes: 5 additions & 2 deletions src/App/src/Middleware/ContentNegotiationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function process(
$response = $handler->handle($request);

$responseContentType = $response->getHeaderLine('Content-Type');

if (! $this->validateResponseContentType($responseContentType, $accept)) {
return $this->notAcceptableResponse('Unable to resolve Accept header to a representation');
}
Expand Down Expand Up @@ -98,15 +99,17 @@ public function checkContentType(string $routeName, string $contentType): bool
return true;
}

$contentType = explode(';', $contentType);

$acceptList = $this->config['default']['Content-Type'] ?? [];
if (! empty($this->config[$routeName]['Content-Type'])) {
$acceptList = $this->config[$routeName]['Content-Type'] ?? [];
}

if (is_array($acceptList)) {
return in_array($contentType, $acceptList, true);
return ! empty(array_intersect($contentType, $acceptList));
} else {
return $contentType === $acceptList;
return in_array($acceptList, $contentType, true);
}
}

Expand Down

0 comments on commit 19f746c

Please sign in to comment.