Skip to content

Commit

Permalink
Add "servers" support to the operation, path in PHP API client (#2072)
Browse files Browse the repository at this point in the history
* add servers support to path, operation in php client

* remove servers from spec

* update based on feedback
  • Loading branch information
wing328 authored and ackintosh committed Feb 9, 2019
1 parent ef7b28d commit e0ed671
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 24 deletions.
91 changes: 86 additions & 5 deletions modules/openapi-generator/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,47 @@ use {{invokerPackage}}\ObjectSerializer;
*/
protected $headerSelector;
/**
* @var int Host index
*/
protected $hostIndex;
/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand All @@ -93,6 +121,15 @@ use {{invokerPackage}}\ObjectSerializer;
* Note: the input parameter is an associative array with the keys listed as the parameter name below
*
{{/vendorExtensions.x-group-parameters}}
{{#servers}}
{{#-first}}
* This oepration contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
{{/-first}}
* URL: {{{url}}}
{{#-last}}
*
{{/-last}}
{{/servers}}
{{#allParams}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}}{{^description}} {{paramName}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
Expand All @@ -119,9 +156,18 @@ use {{invokerPackage}}\ObjectSerializer;
*
{{/description}}
{{#vendorExtensions.x-group-parameters}}
* Note: the inpput parameter is an associative array with the keys listed as the parameter name below
* Note: the input parameter is an associative array with the keys listed as the parameter name below
*
{{/vendorExtensions.x-group-parameters}}
{{#servers}}
{{#-first}}
* This oepration contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
{{/-first}}
* URL: {{{url}}}
{{#-last}}
*
{{/-last}}
{{/servers}}
{{#allParams}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
Expand Down Expand Up @@ -235,9 +281,18 @@ use {{invokerPackage}}\ObjectSerializer;
*
{{/description}}
{{#vendorExtensions.x-group-parameters}}
* Note: the inpput parameter is an associative array with the keys listed as the parameter name below
* Note: the input parameter is an associative array with the keys listed as the parameter name below
*
{{/vendorExtensions.x-group-parameters}}
{{#servers}}
{{#-first}}
* This oepration contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
{{/-first}}
* URL: {{{url}}}
{{#-last}}
*
{{/-last}}
{{/servers}}
{{#allParams}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
Expand Down Expand Up @@ -265,9 +320,18 @@ use {{invokerPackage}}\ObjectSerializer;
*
{{/description}}
{{#vendorExtensions.x-group-parameters}}
* Note: the inpput parameter is an associative array with the keys listed as the parameter name below
* Note: the input parameter is an associative array with the keys listed as the parameter name below
*
{{/vendorExtensions.x-group-parameters}}
{{#servers}}
{{#-first}}
* This oepration contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
{{/-first}}
* URL: {{{url}}}
{{#-last}}
*
{{/-last}}
{{/servers}}
{{#allParams}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
Expand Down Expand Up @@ -326,6 +390,15 @@ use {{invokerPackage}}\ObjectSerializer;
* Note: the input parameter is an associative array with the keys listed as the parameter name below
*
{{/vendorExtensions.x-group-parameters}}
{{#servers}}
{{#-first}}
* This oepration contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
{{/-first}}
* URL: {{{url}}}
{{#-last}}
*
{{/-last}}
{{/servers}}
{{#allParams}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
Expand Down Expand Up @@ -538,10 +611,18 @@ use {{invokerPackage}}\ObjectSerializer;
$headers
);

{{#servers.0}}
$operationHosts = [{{#servers}}"{{{url}}}"{{^-last}}, {{/-last}}{{/servers}}];
if ($this->hostIndex < 0 || $this->hostIndex >= sizeof($operationHosts)) {
throw new \InvalidArgumentException("Invalid index {$this->hostIndex} when selecting the host. Must be less than ".sizeof($operationHosts));
}
$operationHost = $operationHosts[$this->hostIndex];

{{/servers.0}}
$query = \GuzzleHttp\Psr7\build_query($queryParams);
return new Request(
'{{httpMethod}}',
$this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''),
{{^servers.0}}$this->config->getHost(){{/servers.0}}{{#servers.0}}$operationHost{{/servers.0}} . $resourcePath . ($query ? "?{$query}" : ''),
$headers,
$httpBody
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class AnotherFakeApi
*/
protected $headerSelector;

/**
* @var int Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down
36 changes: 32 additions & 4 deletions samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class FakeApi
*/
protected $headerSelector;

/**
* @var int Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down Expand Up @@ -2764,7 +2792,7 @@ public function testGroupParameters($associative_array)
*
* Fake endpoint to test group parameters (optional)
*
* Note: the inpput parameter is an associative array with the keys listed as the parameter name below
* Note: the input parameter is an associative array with the keys listed as the parameter name below
*
* @param int $required_string_group Required String in group parameters (required)
* @param bool $required_boolean_group Required Boolean in group parameters (required)
Expand Down Expand Up @@ -2823,7 +2851,7 @@ public function testGroupParametersWithHttpInfo($associative_array)
*
* Fake endpoint to test group parameters (optional)
*
* Note: the inpput parameter is an associative array with the keys listed as the parameter name below
* Note: the input parameter is an associative array with the keys listed as the parameter name below
*
* @param int $required_string_group Required String in group parameters (required)
* @param bool $required_boolean_group Required Boolean in group parameters (required)
Expand All @@ -2850,7 +2878,7 @@ function ($response) {
*
* Fake endpoint to test group parameters (optional)
*
* Note: the inpput parameter is an associative array with the keys listed as the parameter name below
* Note: the input parameter is an associative array with the keys listed as the parameter name below
*
* @param int $required_string_group Required String in group parameters (required)
* @param bool $required_boolean_group Required Boolean in group parameters (required)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class FakeClassnameTags123Api
*/
protected $headerSelector;

/**
* @var int Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down
30 changes: 29 additions & 1 deletion samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class PetApi
*/
protected $headerSelector;

/**
* @var int Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down
Loading

0 comments on commit e0ed671

Please sign in to comment.