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-Client] Allow Content-Type merge-match+json for encoding #19479

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading