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] Allows setting extra request options via config #11431

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -104,6 +104,13 @@ class Configuration
*/
protected $tempFolderPath;

/**
* Holds any extra request options you want to send
*
* @var array
*/
protected $options = [];

/**
* Constructor
*/
Expand Down Expand Up @@ -500,4 +507,26 @@ class Configuration

return $url;
}

/**
* Set extra request options
*
* @param array $options
* @return $this
*/
public function setOptions(array $options)
{
$this->options = $options;
return $this;
}

/**
* Get extra request options
*
* @return array
*/
public function getOptions(): array
{
return $this->options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ use {{invokerPackage}}\ObjectSerializer;
*/
protected function createHttpClientOption()
{
$options = [];
$options = $this->config->getOptions();
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public function call123TestSpecialTagsRequest($client)
*/
protected function createHttpClientOption()
{
$options = [];
$options = $this->config->getOptions();
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function fooGetRequest()
*/
protected function createHttpClientOption()
{
$options = [];
$options = $this->config->getOptions();
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4691,7 +4691,7 @@ public function testQueryParameterCollectionFormatRequest($pipe, $ioutil, $http,
*/
protected function createHttpClientOption()
{
$options = [];
$options = $this->config->getOptions();
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public function testClassnameRequest($client)
*/
protected function createHttpClientOption()
{
$options = [];
$options = $this->config->getOptions();
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2556,7 +2556,7 @@ public function uploadFileWithRequiredFileRequest($pet_id, $required_file, $addi
*/
protected function createHttpClientOption()
{
$options = [];
$options = $this->config->getOptions();
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ public function placeOrderRequest($order)
*/
protected function createHttpClientOption()
{
$options = [];
$options = $this->config->getOptions();
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,7 @@ public function updateUserRequest($username, $user)
*/
protected function createHttpClientOption()
{
$options = [];
$options = $this->config->getOptions();
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ class Configuration
*/
protected $tempFolderPath;

/**
* Holds any extra request options you want to send
*
* @var array
*/
protected $options = [];

/**
* Constructor
*/
Expand Down Expand Up @@ -520,4 +527,26 @@ public function getHostFromSettings($index, $variables = null)

return $url;
}

/**
* Set extra request options
*
* @param array $options
* @return $this
*/
public function setOptions(array $options)
{
$this->options = $options;
return $this;
}

/**
* Get extra request options
*
* @return array
*/
public function getOptions(): array
{
return $this->options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \OpenAPI\Client\Configuration
*/
class ConfigurationTest extends TestCase
{
/**
Expand Down Expand Up @@ -64,4 +67,33 @@ public function testHostUrlWithInvalidValues()
$url = $config->getHostFromSettings(0, array("server" => "dev-petstore", "port" => "8"));
$this->assertSame("http://dev-petstore.swagger.io:8080/v2", $url);
}

/**
* Tests additional http client options.
*
* @see https://github.com/OpenAPITools/openapi-generator/pull/11431
* @covers ::setOptions
* @covers ::getOptions
*/
public function testHttpClientOptions()
{
$config = Configuration::getDefaultConfiguration();
// default is array and empty
$this->assertIsArray($config->getOptions());
$this->assertEmpty($config->getOptions());

// example from PR itself
$cookie = \GuzzleHttp\Cookie\CookieJar::fromArray([
'XDEBUG_SESSION' => 'xdebug',
], 'example.com');

$self = $config->setOptions(['cookies' => $cookie]);
// assert method is chainable
$this->assertInstanceOf(Configuration::class, $self);

// still array, not empty
$options = $config->getOptions();
$this->assertIsArray($options);
$this->assertArrayHasKey('cookies', $options);
}
}