OpenAPITools / OpenAPITools/openapi-generator
[REQ] [PHP] Allow user to return binary data of file directly instead of temp file
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
Not related to a problem, merely an enhancement
Describe the solution you'd like
Currently when an endpoint return type is defined in the OAS as the following:
"responses": {
"200": {
"description": "successful operation",
"headers": {},
"content": {
"application/octet-stream": {
"schema": {
"type": "string",
"description": "successful operation",
"format": "binary"
}
}
}
},
the client generates a method that returns a reference to an SplFileObject
Ex:
$config = \OpenAPI\Client\Configuration::getDefaultConfiguration()
->setUsername(getenv('API_USERNAME'))
->setPassword(getenv('API_PASSWORD'));
$apiInstance = new \OpenAPI\Client\Api\MediaApi(
new \GuzzleHttp\Client(),
$config
);
$account_id = getenv('ACCOUNT_ID'); // string
try {
$result = $apiInstance->getMedia($account_id, 'test.png');
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling MediaApi->getMedia: ', $e->getMessage(), PHP_EOL;
Output:
SplFileObject Object
(
[pathName:SplFileInfo:private] => /private/var/folders/89/vf520s8j5xx5m6qp344kcb_r0000gp/T/THzOgp
[fileName:SplFileInfo:private] => THzOgp
[openMode:SplFileObject:private] => r
[delimiter:SplFileObject:private] => ,
[enclosure:SplFileObject:private] => "
)
And then to write that to a file the user must use the fread() method of the SplFileObject Object
$myfile = fopen("testfile.png", "w");
fwrite($myfile, $result->fread($result->getSize()));
fclose($myfile);
It would be cleaner to be able to pass the binary string directly to the user with a binary=True flag in the getMedia method that the user could then write directly to a file and not have to worry about a temp file.
Describe alternatives you've considered
No alternatives available - the client SDK basically forces this method for anything that is a binary file
This if in the {}Api file ensures the stream goes to the serializer and a temp file is generated
switch($statusCode) {
case 200:
if ('\SplFileObject' === '\SplFileObject') {
$content = $response->getBody(); //stream goes to serializer
} else {
$content = (string) $response->getBody();
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the generated PHP {}Api response switch for status 200, especially the SplFileObject branch shown in the issue, and identify the generator source that produces it. Define how a binary option should select a direct string while preserving the existing temporary-file behavior, then verify the generated client returns the requested representation for an application/octet-stream binary response.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, php
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100