[PHP-Client] Allow Content-Type merge-match+json for encoding (#19479)

* Allow Content-Type merge-match+json for encoding

* Changes JSON recognition to more flexible regex

* Removes now useless JSON format list

* Removes empty spaces

* Adds explanatory PHPDoc comment

* Moves Json-detection to class HeaderSelector
This commit is contained in:
Markus Holstein 2024-10-03 14:53:15 +02:00 committed by GitHub
parent d521cddc3b
commit 2437d7fa97
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 159 additions and 74 deletions

View File

@ -0,0 +1 @@
RUN apt update && apt install -y maven

View File

@ -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
*

View File

@ -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 {

View File

@ -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
*

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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
*