Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[php-symfony] Fix problem with clients, that put charset in content type header. #6078

Merged
merged 5 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ class {{controllerName}} extends Controller
{{#bodyParams}}
// Make sure that the client is providing something that we can consume
$consumes = [{{#consumes}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}];
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
if (sizeof($consumes) > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, PR is ready to be merged now. But I would suggest to put your changes into base Controller/Controller.php class method and cover it with unit tests to avoid this bug in future. Public method(maybe static) like:

public static function isContentTypeAllowed(Request $request, array $consumes = []): bool;

Of course you can stay with your changes, it's just a suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a shame, but I'm not sure that I can correctly cover the code with a unit test :( There is no experience with this yet.

if ($request->headers->has('Content-Type')) {
$inputFormat = explode(";", $request->headers->get('Content-Type'))[0];
} else {
$inputFormat = $consumes[0];
}
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
}
}

{{/bodyParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ public function addPetAction(Request $request)
{
// Make sure that the client is providing something that we can consume
$consumes = ['application/json', 'application/xml'];
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
if (sizeof($consumes) > 0) {
if ($request->headers->has('Content-Type')) {
$inputFormat = explode(";", $request->headers->get('Content-Type'))[0];
} else {
$inputFormat = $consumes[0];
}
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
}
}

// Handle authentication
Expand Down Expand Up @@ -491,10 +497,16 @@ public function updatePetAction(Request $request)
{
// Make sure that the client is providing something that we can consume
$consumes = ['application/json', 'application/xml'];
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
if (sizeof($consumes) > 0) {
if ($request->headers->has('Content-Type')) {
$inputFormat = explode(";", $request->headers->get('Content-Type'))[0];
} else {
$inputFormat = $consumes[0];
}
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
}
}

// Handle authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,16 @@ public function placeOrderAction(Request $request)
{
// Make sure that the client is providing something that we can consume
$consumes = [];
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
if (sizeof($consumes) > 0) {
if ($request->headers->has('Content-Type')) {
$inputFormat = explode(";", $request->headers->get('Content-Type'))[0];
} else {
$inputFormat = $consumes[0];
}
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
}
}

// Figure out what data format to return to the client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ public function createUserAction(Request $request)
{
// Make sure that the client is providing something that we can consume
$consumes = [];
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
if (sizeof($consumes) > 0) {
if ($request->headers->has('Content-Type')) {
$inputFormat = explode(";", $request->headers->get('Content-Type'))[0];
} else {
$inputFormat = $consumes[0];
}
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
}
}

// Handle authentication
Expand Down Expand Up @@ -138,10 +144,16 @@ public function createUsersWithArrayInputAction(Request $request)
{
// Make sure that the client is providing something that we can consume
$consumes = [];
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
if (sizeof($consumes) > 0) {
if ($request->headers->has('Content-Type')) {
$inputFormat = explode(";", $request->headers->get('Content-Type'))[0];
} else {
$inputFormat = $consumes[0];
}
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
}
}

// Handle authentication
Expand Down Expand Up @@ -217,10 +229,16 @@ public function createUsersWithListInputAction(Request $request)
{
// Make sure that the client is providing something that we can consume
$consumes = [];
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
if (sizeof($consumes) > 0) {
if ($request->headers->has('Content-Type')) {
$inputFormat = explode(";", $request->headers->get('Content-Type'))[0];
} else {
$inputFormat = $consumes[0];
}
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
}
}

// Handle authentication
Expand Down Expand Up @@ -592,10 +610,16 @@ public function updateUserAction(Request $request, $username)
{
// Make sure that the client is providing something that we can consume
$consumes = [];
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
if (sizeof($consumes) > 0) {
if ($request->headers->has('Content-Type')) {
$inputFormat = explode(";", $request->headers->get('Content-Type'))[0];
} else {
$inputFormat = $consumes[0];
}
if (!in_array($inputFormat, $consumes)) {
// We can't consume the content that the client is sending us
return new Response('', 415);
}
}

// Handle authentication
Expand Down