From 20d1743a363bf03e52b433db6c4f91e2acadaf9e Mon Sep 17 00:00:00 2001 From: Guillaume Turri Date: Mon, 24 Jul 2023 05:34:12 +0200 Subject: [PATCH] Fix parsing of Accept header like '*/*;q=0.8' (#16169) This fixes #15043 The issue is that browsers like "text/html,...,*/*;q=0.8" (see for instance https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values ) Without this commit we end up with an array of accepted type like `("text/html", "*/*;q=0.8)` so when we then check if the array contains `*/*` the check fails, and we return a 406 even though the client is able to get the response. This commit fixes it by removing the `;q=0.8` part. (Ideally we should not just discard that part, we should extract that value, and order by it. See https://developer.mozilla.org/en-US/docs/Glossary/Quality_values for more info about that. However this could be done in a subsequent PR: this already fixes the 406 error, which is pretty blocking) --- .../src/main/resources/php-symfony/Controller.mustache | 3 +++ .../php-symfony/SymfonyBundle-php/Controller/Controller.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/php-symfony/Controller.mustache b/modules/openapi-generator/src/main/resources/php-symfony/Controller.mustache index 9366004e84b..b74fa68699c 100644 --- a/modules/openapi-generator/src/main/resources/php-symfony/Controller.mustache +++ b/modules/openapi-generator/src/main/resources/php-symfony/Controller.mustache @@ -183,6 +183,9 @@ class Controller extends AbstractController // Figure out what the client accepts $accept = preg_split("/[\s,]+/", $accept); + // Remove q-factor weighting. E.g. "application/json;q=0.8" becomes "application/json" + $accept = array_map(function ($type) {return explode(';', $type)[0];}, $accept); + if (in_array('*/*', $accept) || in_array('application/*', $accept)) { // Prefer JSON if the client has no preference if (in_array('application/json', $produced)) { diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/Controller.php b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/Controller.php index 559f3bd5a4b..f1d6d929d53 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/Controller.php +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/Controller.php @@ -193,6 +193,9 @@ class Controller extends AbstractController // Figure out what the client accepts $accept = preg_split("/[\s,]+/", $accept); + // Remove q-factor weighting. E.g. "application/json;q=0.8" becomes "application/json" + $accept = array_map(function ($type) {return explode(';', $type)[0];}, $accept); + if (in_array('*/*', $accept) || in_array('application/*', $accept)) { // Prefer JSON if the client has no preference if (in_array('application/json', $produced)) {