Skip to content

Commit

Permalink
[PHP-Client] Allow Content-Type merge-match+json for encoding (#19479)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
frickelbruder authored Oct 3, 2024
1 parent d521cdd commit 2437d7f
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 74 deletions.
1 change: 1 addition & 0 deletions .ddev/web-build/Dockerfile.maven
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUN apt update && apt install -y maven
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}};
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function selectAcceptHeader(array $accept): ?string
}

# 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);
}
Expand All @@ -95,6 +95,34 @@ private function selectAcceptHeader(array $accept): ?string
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
*
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/php/psr-18/lib/Api/AnotherFakeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public function call123TestSpecialTagsRequest($client)

// 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;
Expand All @@ -396,7 +396,7 @@ public function call123TestSpecialTagsRequest($client)
// 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 {
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/php/psr-18/lib/Api/DefaultApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function fooGetRequest()
// 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 {
Expand Down
Loading

0 comments on commit 2437d7f

Please sign in to comment.