diff --git a/.ddev/web-build/Dockerfile.maven b/.ddev/web-build/Dockerfile.maven new file mode 100644 index 00000000000..60a15dbc2b3 --- /dev/null +++ b/.ddev/web-build/Dockerfile.maven @@ -0,0 +1 @@ +RUN apt update && apt install -y maven diff --git a/modules/openapi-generator/src/main/resources/php/HeaderSelector.mustache b/modules/openapi-generator/src/main/resources/php/HeaderSelector.mustache index 1921b061015..ad2a00ea7c2 100644 --- a/modules/openapi-generator/src/main/resources/php/HeaderSelector.mustache +++ b/modules/openapi-generator/src/main/resources/php/HeaderSelector.mustache @@ -76,7 +76,7 @@ class HeaderSelector } # If none of the available Accept headers is of type "json", then just use all them - $headersWithJson = preg_grep('~(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$~', $accept); + $headersWithJson = $this->selectJsonMimeList($accept); if (count($headersWithJson) === 0) { return implode(',', $accept); } @@ -86,6 +86,34 @@ class HeaderSelector return $this->getAcceptHeaderWithAdjustedWeight($accept, $headersWithJson); } + /** + * Detects whether a string contains a valid JSON mime type + * + * @param string $searchString + * @return bool + */ + public function isJsonMime(string $searchString): bool + { + return preg_match('~^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)~', $searchString) === 1; + } + + /** + * Select all items from a list containing a JSON mime type + * + * @param array $mimeList + * @return array + */ + private function selectJsonMimeList(array $mimeList): array { + $jsonMimeList = []; + foreach ($mimeList as $mime) { + if($this->isJsonMime($mime)) { + $jsonMimeList[] = $mime; + } + } + return $jsonMimeList; + } + + /** * Create an Accept header string from the given "Accept" headers array, recalculating all weights * diff --git a/modules/openapi-generator/src/main/resources/php/libraries/psr-18/api.mustache b/modules/openapi-generator/src/main/resources/php/libraries/psr-18/api.mustache index 6138ab17660..8321b1d6072 100644 --- a/modules/openapi-generator/src/main/resources/php/libraries/psr-18/api.mustache +++ b/modules/openapi-generator/src/main/resources/php/libraries/psr-18/api.mustache @@ -613,7 +613,7 @@ use function sprintf; // for model (json/xml) {{#bodyParams}} if (isset(${{paramName}})) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization(${{paramName}})); } else { $httpBody = ${{paramName}}; @@ -637,7 +637,7 @@ use function sprintf; // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/HeaderSelector.php b/samples/client/petstore/php/OpenAPIClient-php/lib/HeaderSelector.php index 027e82a879b..1ddbf2c26f5 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/HeaderSelector.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/HeaderSelector.php @@ -85,7 +85,7 @@ class HeaderSelector } # If none of the available Accept headers is of type "json", then just use all them - $headersWithJson = preg_grep('~(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$~', $accept); + $headersWithJson = $this->selectJsonMimeList($accept); if (count($headersWithJson) === 0) { return implode(',', $accept); } @@ -95,6 +95,34 @@ class HeaderSelector return $this->getAcceptHeaderWithAdjustedWeight($accept, $headersWithJson); } + /** + * Detects whether a string contains a valid JSON mime type + * + * @param string $searchString + * @return bool + */ + public function isJsonMime(string $searchString): bool + { + return preg_match('~^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)~', $searchString) === 1; + } + + /** + * Select all items from a list containing a JSON mime type + * + * @param array $mimeList + * @return array + */ + private function selectJsonMimeList(array $mimeList): array { + $jsonMimeList = []; + foreach ($mimeList as $mime) { + if($this->isJsonMime($mime)) { + $jsonMimeList[] = $mime; + } + } + return $jsonMimeList; + } + + /** * Create an Accept header string from the given "Accept" headers array, recalculating all weights * diff --git a/samples/client/petstore/php/psr-18/lib/Api/AnotherFakeApi.php b/samples/client/petstore/php/psr-18/lib/Api/AnotherFakeApi.php index 54c36208aca..bdd194c1cac 100644 --- a/samples/client/petstore/php/psr-18/lib/Api/AnotherFakeApi.php +++ b/samples/client/petstore/php/psr-18/lib/Api/AnotherFakeApi.php @@ -376,7 +376,7 @@ class AnotherFakeApi // for model (json/xml) if (isset($client)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($client)); } else { $httpBody = $client; @@ -396,7 +396,7 @@ class AnotherFakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { diff --git a/samples/client/petstore/php/psr-18/lib/Api/DefaultApi.php b/samples/client/petstore/php/psr-18/lib/Api/DefaultApi.php index 9cfc3dd22e7..0a6d73bd7f3 100644 --- a/samples/client/petstore/php/psr-18/lib/Api/DefaultApi.php +++ b/samples/client/petstore/php/psr-18/lib/Api/DefaultApi.php @@ -371,7 +371,7 @@ class DefaultApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { diff --git a/samples/client/petstore/php/psr-18/lib/Api/FakeApi.php b/samples/client/petstore/php/psr-18/lib/Api/FakeApi.php index 2fb98109e5b..df928134409 100644 --- a/samples/client/petstore/php/psr-18/lib/Api/FakeApi.php +++ b/samples/client/petstore/php/psr-18/lib/Api/FakeApi.php @@ -371,7 +371,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -907,7 +907,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1119,7 +1119,7 @@ class FakeApi // for model (json/xml) if (isset($pet)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet)); } else { $httpBody = $pet; @@ -1139,7 +1139,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1357,7 +1357,7 @@ class FakeApi // for model (json/xml) if (isset($body)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body)); } else { $httpBody = $body; @@ -1377,7 +1377,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1595,7 +1595,7 @@ class FakeApi // for model (json/xml) if (isset($outer_composite)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($outer_composite)); } else { $httpBody = $outer_composite; @@ -1615,7 +1615,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1833,7 +1833,7 @@ class FakeApi // for model (json/xml) if (isset($body)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body)); } else { $httpBody = $body; @@ -1853,7 +1853,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -2071,7 +2071,7 @@ class FakeApi // for model (json/xml) if (isset($body)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body)); } else { $httpBody = $body; @@ -2091,7 +2091,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -2315,7 +2315,7 @@ class FakeApi // for model (json/xml) if (isset($outer_object_with_enum_property)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($outer_object_with_enum_property)); } else { $httpBody = $outer_object_with_enum_property; @@ -2335,7 +2335,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -2614,7 +2614,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -2801,7 +2801,7 @@ class FakeApi // for model (json/xml) if (isset($request_body)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($request_body)); } else { $httpBody = $request_body; @@ -2821,7 +2821,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -3000,7 +3000,7 @@ class FakeApi // for model (json/xml) if (isset($body)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body)); } else { $httpBody = $body; @@ -3020,7 +3020,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -3199,7 +3199,7 @@ class FakeApi // for model (json/xml) if (isset($file_schema_test_class)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($file_schema_test_class)); } else { $httpBody = $file_schema_test_class; @@ -3219,7 +3219,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -3420,7 +3420,7 @@ class FakeApi // for model (json/xml) if (isset($user)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; @@ -3440,7 +3440,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -3672,7 +3672,7 @@ class FakeApi // for model (json/xml) if (isset($client)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($client)); } else { $httpBody = $client; @@ -3692,7 +3692,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -4087,7 +4087,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -4400,7 +4400,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -4708,7 +4708,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -4899,7 +4899,7 @@ class FakeApi // for model (json/xml) if (isset($request_body)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($request_body)); } else { $httpBody = $request_body; @@ -4919,7 +4919,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -5106,7 +5106,7 @@ class FakeApi // for model (json/xml) if (isset($test_inline_freeform_additional_properties_request)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($test_inline_freeform_additional_properties_request)); } else { $httpBody = $test_inline_freeform_additional_properties_request; @@ -5126,7 +5126,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -5346,7 +5346,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -5660,7 +5660,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -5847,7 +5847,7 @@ class FakeApi // for model (json/xml) if (isset($request_body)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($request_body)); } else { $httpBody = $request_body; @@ -5867,7 +5867,7 @@ class FakeApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { diff --git a/samples/client/petstore/php/psr-18/lib/Api/FakeClassnameTags123Api.php b/samples/client/petstore/php/psr-18/lib/Api/FakeClassnameTags123Api.php index 58953b65e46..9c4d80eb7f9 100644 --- a/samples/client/petstore/php/psr-18/lib/Api/FakeClassnameTags123Api.php +++ b/samples/client/petstore/php/psr-18/lib/Api/FakeClassnameTags123Api.php @@ -376,7 +376,7 @@ class FakeClassnameTags123Api // for model (json/xml) if (isset($client)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($client)); } else { $httpBody = $client; @@ -396,7 +396,7 @@ class FakeClassnameTags123Api // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { diff --git a/samples/client/petstore/php/psr-18/lib/Api/PetApi.php b/samples/client/petstore/php/psr-18/lib/Api/PetApi.php index 4bd148dfdc2..3a6228ecdac 100644 --- a/samples/client/petstore/php/psr-18/lib/Api/PetApi.php +++ b/samples/client/petstore/php/psr-18/lib/Api/PetApi.php @@ -356,7 +356,7 @@ class PetApi // for model (json/xml) if (isset($pet)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet)); } else { $httpBody = $pet; @@ -376,7 +376,7 @@ class PetApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -602,7 +602,7 @@ class PetApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -859,7 +859,7 @@ class PetApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1117,7 +1117,7 @@ class PetApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1375,7 +1375,7 @@ class PetApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1592,7 +1592,7 @@ class PetApi // for model (json/xml) if (isset($pet)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet)); } else { $httpBody = $pet; @@ -1612,7 +1612,7 @@ class PetApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1847,7 +1847,7 @@ class PetApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -2131,7 +2131,7 @@ class PetApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -2421,7 +2421,7 @@ class PetApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { diff --git a/samples/client/petstore/php/psr-18/lib/Api/StoreApi.php b/samples/client/petstore/php/psr-18/lib/Api/StoreApi.php index 49b02997d16..1788c5430ec 100644 --- a/samples/client/petstore/php/psr-18/lib/Api/StoreApi.php +++ b/samples/client/petstore/php/psr-18/lib/Api/StoreApi.php @@ -353,7 +353,7 @@ class StoreApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -588,7 +588,7 @@ class StoreApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -854,7 +854,7 @@ class StoreApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1086,7 +1086,7 @@ class StoreApi // for model (json/xml) if (isset($order)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($order)); } else { $httpBody = $order; @@ -1106,7 +1106,7 @@ class StoreApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { diff --git a/samples/client/petstore/php/psr-18/lib/Api/UserApi.php b/samples/client/petstore/php/psr-18/lib/Api/UserApi.php index 12bff75ef01..7b27e57c518 100644 --- a/samples/client/petstore/php/psr-18/lib/Api/UserApi.php +++ b/samples/client/petstore/php/psr-18/lib/Api/UserApi.php @@ -331,7 +331,7 @@ class UserApi // for model (json/xml) if (isset($user)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; @@ -351,7 +351,7 @@ class UserApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -538,7 +538,7 @@ class UserApi // for model (json/xml) if (isset($user)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; @@ -558,7 +558,7 @@ class UserApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -745,7 +745,7 @@ class UserApi // for model (json/xml) if (isset($user)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; @@ -765,7 +765,7 @@ class UserApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -974,7 +974,7 @@ class UserApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1228,7 +1228,7 @@ class UserApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1507,7 +1507,7 @@ class UserApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1697,7 +1697,7 @@ class UserApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { @@ -1903,7 +1903,7 @@ class UserApi // for model (json/xml) if (isset($user)) { - if ($headers['Content-Type'] === 'application/json') { + if ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; @@ -1923,7 +1923,7 @@ class UserApi // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); - } elseif ($headers['Content-Type'] === 'application/json') { + } elseif ($this->headerSelector->isJsonMime($headers['Content-Type'])) { $httpBody = json_encode($formParams); } else { diff --git a/samples/client/petstore/php/psr-18/lib/HeaderSelector.php b/samples/client/petstore/php/psr-18/lib/HeaderSelector.php index 027e82a879b..1ddbf2c26f5 100644 --- a/samples/client/petstore/php/psr-18/lib/HeaderSelector.php +++ b/samples/client/petstore/php/psr-18/lib/HeaderSelector.php @@ -85,7 +85,7 @@ class HeaderSelector } # If none of the available Accept headers is of type "json", then just use all them - $headersWithJson = preg_grep('~(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$~', $accept); + $headersWithJson = $this->selectJsonMimeList($accept); if (count($headersWithJson) === 0) { return implode(',', $accept); } @@ -95,6 +95,34 @@ class HeaderSelector return $this->getAcceptHeaderWithAdjustedWeight($accept, $headersWithJson); } + /** + * Detects whether a string contains a valid JSON mime type + * + * @param string $searchString + * @return bool + */ + public function isJsonMime(string $searchString): bool + { + return preg_match('~^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)~', $searchString) === 1; + } + + /** + * Select all items from a list containing a JSON mime type + * + * @param array $mimeList + * @return array + */ + private function selectJsonMimeList(array $mimeList): array { + $jsonMimeList = []; + foreach ($mimeList as $mime) { + if($this->isJsonMime($mime)) { + $jsonMimeList[] = $mime; + } + } + return $jsonMimeList; + } + + /** * Create an Accept header string from the given "Accept" headers array, recalculating all weights *