Skip to content

Fix #352 - [PHP] [Improvement] Make Api return object instead of array #353

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

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void processOpts() {

supportingFiles.add(new SupportingFile("ApiException.mustache", toSrcPath(invokerPackage, srcBasePath), "ApiException.php"));
supportingFiles.add(new SupportingFile("Configuration.mustache", toSrcPath(invokerPackage, srcBasePath), "Configuration.php"));
supportingFiles.add(new SupportingFile("ApiResponse.mustache", toSrcPath(invokerPackage, srcBasePath), "ApiResponse.php"));
supportingFiles.add(new SupportingFile("ObjectSerializer.mustache", toSrcPath(invokerPackage, srcBasePath), "ObjectSerializer.php"));
supportingFiles.add(new SupportingFile("ModelInterface.mustache", toSrcPath(modelPackage, srcBasePath), "ModelInterface.php"));
supportingFiles.add(new SupportingFile("HeaderSelector.mustache", toSrcPath(invokerPackage, srcBasePath), "HeaderSelector.php"));
Expand Down
133 changes: 133 additions & 0 deletions modules/openapi-generator/src/main/resources/php/ApiResponse.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* ApiResponse
* PHP version 5
*
* @category Class
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/

{{>partial_header}}
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

namespace {{invokerPackage}};

/**
* ApiResponse Class Doc Comment
* PHP version 5
*
* @category Class
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class ApiResponse
{
/**
* Message (response) from API call
*
* @var mixed
*/
protected $message;
/**
* Response status code
*
* @var int
*/
protected $statusCode;
/**
* Message headers
*
* @var string[]
*/
protected $headers;

/**
* Constructor
*
* @param mixed $message Message (response) from API call
* @param int $statusCode Response status code
* @param string[] $headers Message headers
*/
public function __construct($message, $statusCode, $headers)
{
$this->message = $message;
$this->statusCode = $statusCode;
$this->headers = $headers;
}

/**
* Sets message (response) from API call
*
* @param mixed $message Message (response) from API call
*
* @return $this
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}

/**
* Gets message (response) from API call
*
* @return mixed Message (response) from API call
*/
public function getMessage()
{
return $this->message;
}

/**
* Sets response status code
*
* @param int $statusCode Response status code
*
* @return $this
*/
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}

/**
* Gets response status code
*
* @return int Response status code
*/
public function getStatusCode()
{
return $this->statusCode;
}

/**
* Sets message headers
*
* @param string[] $headers Message headers
*
* @return $this
*/
public function setHeaders($headers)
{
$this->headers = $headers;
return $this;
}

/**
* Gets message headers
*
* @return string[] Message headers
*/
public function getHeaders()
{
return $this->headers;
}
}
101 changes: 86 additions & 15 deletions modules/openapi-generator/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use {{invokerPackage}}\ApiException;
use {{invokerPackage}}\ApiResponse;
use {{invokerPackage}}\Configuration;
use {{invokerPackage}}\HeaderSelector;
use {{invokerPackage}}\ObjectSerializer;
Expand Down Expand Up @@ -99,12 +100,16 @@ use {{invokerPackage}}\ObjectSerializer;
*/
public function {{operationId}}({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
{{#returnType}}list($response) = {{/returnType}}$this->{{operationId}}WithHttpInfo({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}
return $response;{{/returnType}}
{{#returnType}}$apiResponse = {{/returnType}}$this->{{operationId}}WithApiResponse({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}
return $apiResponse->getMessage();{{/returnType}}
}


/**
* Operation {{{operationId}}}WithHttpInfo
*
* (method preserved to keep backward compatibility)
*
{{#summary}}
*
* {{{summary}}}
Expand All @@ -120,9 +125,41 @@ use {{invokerPackage}}\ObjectSerializer;
*
* @throws \{{invokerPackage}}\ApiException on non-2xx response
* @throws \InvalidArgumentException
* @return array of {{#returnType}}{{#responses}}{{#dataType}}{{^-first}}|{{/-first}}{{/dataType}}{{{dataType}}}{{/responses}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (array of strings)
* @return array of {{#returnType}}{{#responses}}{{#dataType}}{{^-first}}|{{/-first}}{{/dataType}}{{{dataType}}}{{/responses}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (strings[])
*/
public function {{operationId}}WithHttpInfo({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
$apiResponse = $this->{{operationId}}WithApiResponse({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

return [
$apiResponse->getMessage(),
$apiResponse->getStatusCode(),
$apiResponse->getHeaders()
];
}



/**
* Operation {{{operationId}}}WithApiResponse
{{#summary}}
*
* {{{summary}}}
{{/summary}}
*
{{#description}}
* {{.}}
*
{{/description}}
{{#allParams}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
*
* @throws \{{invokerPackage}}\ApiException on non-2xx response
* @throws \InvalidArgumentException
* @return APIResponse with "message" property {{#returnType}}of type {{{returnType}}}{{/returnType}}{{^returnType}}equals to null{{/returnType}}
*/
public function {{operationId}}WithApiResponse({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
$request = $this->{{operationId}}Request({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

Expand Down Expand Up @@ -171,11 +208,11 @@ use {{invokerPackage}}\ObjectSerializer;
}
}

return [
return new ApiResponse(
ObjectSerializer::deserialize($content, '{{dataType}}', []),
$response->getStatusCode(),
$response->getHeaders()
];
);
{{/dataType}}
{{#-last}}
}
Expand All @@ -193,15 +230,15 @@ use {{invokerPackage}}\ObjectSerializer;
}
}

return [
return new ApiResponse(
ObjectSerializer::deserialize($content, $returnType, []),
$response->getStatusCode(),
$response->getHeaders()
];
);
{{/returnType}}
{{^returnType}}

return [null, $statusCode, $response->getHeaders()];
return new ApiResponse(null, $statusCode, $response->getHeaders());
{{/returnType}}

} catch (ApiException $e) {
Expand Down Expand Up @@ -241,17 +278,19 @@ use {{invokerPackage}}\ObjectSerializer;
*/
public function {{operationId}}Async({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
return $this->{{operationId}}AsyncWithHttpInfo({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
return $this->{{operationId}}AsyncWithApiResponse({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
->then(
function ($response) {
return $response[0];
function ($apiResponse) {
return $apiResponse->getMessage();
}
);
}

/**
* Operation {{{operationId}}}AsyncWithHttpInfo
*
* (method preserved to keep backward compatibility)
*
* {{{summary}}}
*
{{#description}}
Expand All @@ -267,13 +306,45 @@ use {{invokerPackage}}\ObjectSerializer;
*/
public function {{operationId}}AsyncWithHttpInfo({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
return $this->{{operationId}}AsyncWithApiResponse({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
->then(
function ($apiResponse) {
return [
$apiResponse->getMessage(),
$apiResponse->getStatusCode(),
$apiResponse->getHeaders()
];
}
);
}

/**
* Operation {{{operationId}}}AsyncWithApiResponse
*
* {{{summary}}}
*
{{#description}}
* {{.}}
*
{{/description}}
{{#allParams}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
*
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function {{operationId}}AsyncWithApiResponse({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
{{#returnType}}
$returnType = '{{returnType}}';
{{/returnType}}
$request = $this->{{operationId}}Request({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

return $this->client
->sendAsync($request, $this->createHttpClientOption())
->then(
function ($response) use ($returnType) {
function ($response){{#returnType}} use ($returnType){{/returnType}} {
{{#returnType}}
$responseBody = $response->getBody();
if ($returnType === '\SplFileObject') {
Expand All @@ -285,14 +356,14 @@ use {{invokerPackage}}\ObjectSerializer;
}
}

return [
return new ApiResponse(
ObjectSerializer::deserialize($content, $returnType, []),
$response->getStatusCode(),
$response->getHeaders()
];
);
{{/returnType}}
{{^returnType}}
return [null, $response->getStatusCode(), $response->getHeaders()];
return new ApiResponse(null, $response->getStatusCode(), $response->getHeaders());
{{/returnType}}
},
function ($exception) {
Expand Down
Loading