Skip to content

Add "servers" support to the operation, path in PHP API client #2072

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

Merged
merged 3 commits into from
Feb 9, 2019
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the $host_index generates only if servers exists in pathItems?

I just wondering that the $host_index is defined but unused. 👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, very good suggestion. My intention is to make the constructor consistent across different API classes.

What about updating the doc string to explain it cleary that it's optional and only needed when the endpoint has multiple servers/hosts defined?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with that. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed some enhancements based on your feedback.

) {
$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