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

[Python] Support for per-operation servers #6557

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
81e594d
Dynamic server support
jirikuncar Jun 5, 2020
4985f27
regenerated
jirikuncar Jun 5, 2020
1fd2e65
Apply suggestions from code review
jirikuncar Jun 5, 2020
6caf66f
regenerated
jirikuncar Jun 5, 2020
5eec729
Add ParameterizedServer feature to Python experimental
jirikuncar Jun 5, 2020
df1c324
Fix lookup of server variables
jirikuncar Jun 5, 2020
3920645
Add tests and change default value for servers
jirikuncar Jun 8, 2020
7420edb
Fix server variables
jirikuncar Jun 8, 2020
5422cfd
Return base path when index is None
jirikuncar Jun 8, 2020
280837f
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 8, 2020
294d164
Use HOST
jirikuncar Jun 8, 2020
f38fc72
Apply suggestions from code review
jirikuncar Jun 11, 2020
8ec2cb7
Apply suggestions from code review
jirikuncar Jun 11, 2020
655ecd0
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 11, 2020
6e97360
regenerated
jirikuncar Jun 11, 2020
0a57b4c
Add specific tests for dynamic servers
jirikuncar Jun 12, 2020
5788068
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 12, 2020
1d19097
regenerated
jirikuncar Jun 12, 2020
ff976a4
add docstring
jirikuncar Jun 14, 2020
032533d
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 14, 2020
3706f4d
regenerated
jirikuncar Jun 15, 2020
3a1d0ca
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 23, 2020
66df647
Fix wrong merge resolution
jirikuncar Jun 23, 2020
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
6 changes: 6 additions & 0 deletions bin/configs/python-experimental-features-dynamic-servers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
generatorName: python-experimental
outputDir: samples/openapi3/client/features/dynamic-servers/python-experimental/
inputSpec: modules/openapi-generator/src/test/resources/3_0/features/dynamic-servers.yaml
templateDir: modules/openapi-generator/src/main/resources/python
additionalProperties:
packageName: dynamic_servers
2 changes: 1 addition & 1 deletion docs/generators/python-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ sidebar_label: python-experimental
|Examples|✓|OAS2,OAS3
|XMLStructureDefinitions|✗|OAS2,OAS3
|MultiServer|✗|OAS3
|ParameterizedServer||OAS3
|ParameterizedServer||OAS3
|ParameterStyling|✗|OAS3
|Callbacks|✗|OAS3
|LinkObjects|✗|OAS3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public PythonClientExperimentalCodegen() {
SecurityFeature.ApiKey,
SecurityFeature.OAuth2_Implicit
))
.includeGlobalFeatures(
GlobalFeature.ParameterizedServer
)
.excludeGlobalFeatures(
GlobalFeature.XMLStructureDefinitions,
GlobalFeature.Callbacks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ class Configuration(object):
:param signing_info: Configuration parameters for the HTTP signature security scheme.
Must be an instance of {{{packageName}}}.signing.HttpSigningConfiguration
{{/hasHttpSignatureMethods}}
:param server_index: Index to servers configuration.
:param server_variables: Mapping with string values to replace variables in
templated server configuration. The validation of enums is performed for
variables with defined enum values before.
:param server_operation_index: Mapping from operation ID to an index to server
configuration.
:param server_operation_variables: Mapping from operation ID to a mapping with
string values to replace variables in templated server configuration.
The validation of enums is performed for variables with defined enum values before.

{{#hasAuthMethods}}
:Example:
Expand Down Expand Up @@ -155,20 +164,30 @@ conf = {{{packageName}}}.Configuration(

_default = None

def __init__(self, host="{{{basePath}}}",
def __init__(self, host=None,
api_key=None, api_key_prefix=None,
username=None, password=None,
discard_unknown_keys=False,
disabled_client_side_validations="",
{{#hasHttpSignatureMethods}}
signing_info=None,
{{/hasHttpSignatureMethods}}
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
):
"""Constructor
"""
self.host = host
self._base_path = "{{{basePath}}}" if host is None else host
spacether marked this conversation as resolved.
Show resolved Hide resolved
"""Default Base url
"""
self.server_index = 0 if server_index is None and host is None else server_index
self.server_operation_index = server_operation_index or {}
"""Default server index
"""
self.server_variables = server_variables or {}
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.temp_folder_path = None
"""Temp file folder for downloading files
"""
Expand Down Expand Up @@ -565,14 +584,18 @@ conf = {{{packageName}}}.Configuration(
{{/servers}}
]

def get_host_from_settings(self, index, variables=None):
def get_host_from_settings(self, index, variables=None, servers=None):
"""Gets host URL based on the index and variables
:param index: array index of the host settings
:param variables: hash of variable and the corresponding value

This comment was marked as resolved.

jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
:param servers: an array of host settings or None
:return: URL based on host settings
"""
if index is None:
return self._base_path

variables = {} if variables is None else variables
servers = self.get_host_settings()
servers = self.get_host_settings() if servers is None else servers

try:
server = servers[index]
Expand All @@ -584,7 +607,7 @@ conf = {{{packageName}}}.Configuration(
url = server['url']

# go through variables and replace placeholders
for variable_name, variable in server['variables'].items():
for variable_name, variable in server.get('variables', {}).items():
used_value = variables.get(
variable_name, variable['default_value'])

Expand All @@ -599,3 +622,14 @@ conf = {{{packageName}}}.Configuration(
url = url.replace("{" + variable_name + "}", used_value)

return url

@property
def host(self):
"""Return generated host."""
return self.get_host_from_settings(self.server_index, variables=self.server_variables)

@host.setter
def host(self, value):
"""Fix base path."""
self._base_path = value
self.server_index = None
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ class {{classname}}(object):
_check_return_type (bool): specifies if type checking
should be done one the data received from the server.
Default is True.
_host_index (int): specifies the index of the server
_host_index (int/None): specifies the index of the server
that we want to use.
Default is 0.
Default is read from the configuration.
spacether marked this conversation as resolved.
Show resolved Hide resolved
async_req (bool): execute request asynchronously

Returns:
Expand All @@ -127,7 +127,7 @@ class {{classname}}(object):
kwargs['_check_return_type'] = kwargs.get(
'_check_return_type', True
)
kwargs['_host_index'] = kwargs.get('_host_index', 0)
kwargs['_host_index'] = kwargs.get('_host_index')
{{#requiredParams}}
kwargs['{{paramName}}'] = \
{{paramName}}
Expand Down Expand Up @@ -156,13 +156,37 @@ class {{classname}}(object):
{{#-first}}
'servers': [
{{/-first}}
'{{{url}}}'{{^-last}},{{/-last}}
{
'url': "{{{url}}}",
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}
{{#-first}}
'variables': {
{{/-first}}
'{{{name}}}': {
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
'default_value': "{{{defaultValue}}}",
{{#enumValues}}
{{#-first}}
'enum_values': [
{{/-first}}
"{{{.}}}"{{^-last}},{{/-last}}
{{#-last}}
]
{{/-last}}
{{/enumValues}}
}{{^-last}},{{/-last}}
{{#-last}}
}
{{/-last}}
{{/variables}}
},
{{#-last}}
]
{{/-last}}
{{/servers}}
{{^servers}}
'servers': [],
'servers': None,
{{/servers}}
},
params_map={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ class Endpoint(object):
self.openapi_types = root_map['openapi_types']
extra_types = {
'async_req': (bool,),
'_host_index': (int,),
'_host_index': (none_type, int),
'_preload_content': (bool,),
'_request_timeout': (none_type, int, (int,), [int]),
'_return_http_data_only': (bool,),
Expand Down Expand Up @@ -755,7 +755,15 @@ class Endpoint(object):
def call_with_http_info(self, **kwargs):

try:
_host = self.settings['servers'][kwargs['_host_index']]
index = self.api_client.configuration.server_operation_index.get(
self.settings['operation_id'], self.api_client.configuration.server_index
) if kwargs['_host_index'] is None else kwargs['_host_index']
server_variables = self.api_client.configuration.server_operation_variables.get(
self.settings['operation_id'], self.api_client.configuration.server_variables
)
_host = self.api_client.configuration.get_host_from_settings(
index, variables=server_variables, servers=self.settings['servers']
)
except IndexError:
if self.settings['servers']:
raise ApiValueError(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
openapi: 3.0.0
info:
description: This specification shows how to use dynamic servers.
Copy link
Contributor Author

@jirikuncar jirikuncar Jun 12, 2020

Choose a reason for hiding this comment

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

@jimschubert I have tried to create a minimal specification on which we can showcase the dynamic server configuration to avoid modifications of default "Petstore" example. This should follow the work for extensions from #6469.

version: 1.0.0
title: OpenAPI Extension with dynamic servers
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
tags:
- name: usage
description: Show usage of dynamic servers
servers:
- url: 'http://{server}.swagger.io:{port}/v2'
description: petstore server
variables:
server:
enum:
- 'petstore'
- 'qa-petstore'
- 'dev-petstore'
default: 'petstore'
port:
enum:
- '80'
- '8080'
default: '80'
- url: https://localhost:8080/{version}
description: The local server
variables:
version:
enum:
- 'v1'
- 'v2'
- 'v3'
default: 'v1'
paths:
/default:
get:
tags:
- usage
summary: Use default server
description: Use default server
operationId: defaultServer
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
/custom:
get:
tags:
- usage
servers:
- url: https://{server}.swagger.io:{port}/v2
variables:
server:
enum:
- 'custom-petstore'
- 'custom-qa-petstore'
- 'custom-dev-petstore'
default: 'custom-petstore'
port:
enum:
- '80'
- '8080'
default: '8080'
- url: https://localhost:8081/{version}
description: The local custom server
variables:
version:
enum:
- 'v1'
- 'v2'
- 'v3'
default: 'v2'
- url: https://third.example.com/{prefix}
description: The local custom server
variables:
prefix:
default: 'custom'
summary: Use custom server
description: Use custom server
operationId: customServer
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ class Configuration(object):
disabled. This can be useful to troubleshoot data validation problem, such as
when the OpenAPI document validation rules do not match the actual API data
received by the server.
:param server_index: Index to servers configuration.
:param server_variables: Mapping with string values to replace variables in
templated server configuration. The validation of enums is performed for
variables with defined enum values before.
:param server_operation_index: Mapping from operation ID to an index to server
configuration.
:param server_operation_variables: Mapping from operation ID to a mapping with
string values to replace variables in templated server configuration.
The validation of enums is performed for variables with defined enum values before.

:Example:

Expand Down Expand Up @@ -109,17 +118,27 @@ class Configuration(object):

_default = None

def __init__(self, host="http://petstore.swagger.io:80/v2",
def __init__(self, host=None,
api_key=None, api_key_prefix=None,
username=None, password=None,
discard_unknown_keys=False,
disabled_client_side_validations="",
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
):
"""Constructor
"""
self.host = host
self._base_path = "http://petstore.swagger.io:80/v2" if host is None else host
"""Default Base url
"""
self.server_index = 0 if server_index is None and host is None else server_index
self.server_operation_index = server_operation_index or {}
"""Default server index
"""
self.server_variables = server_variables or {}
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.temp_folder_path = None
"""Temp file folder for downloading files
"""
Expand Down Expand Up @@ -437,14 +456,18 @@ def get_host_settings(self):
}
]

def get_host_from_settings(self, index, variables=None):
def get_host_from_settings(self, index, variables=None, servers=None):
"""Gets host URL based on the index and variables
:param index: array index of the host settings
:param variables: hash of variable and the corresponding value
:param servers: an array of host settings or None
:return: URL based on host settings
"""
if index is None:
return self._base_path

variables = {} if variables is None else variables
servers = self.get_host_settings()
servers = self.get_host_settings() if servers is None else servers

try:
server = servers[index]
Expand All @@ -456,7 +479,7 @@ def get_host_from_settings(self, index, variables=None):
url = server['url']

# go through variables and replace placeholders
for variable_name, variable in server['variables'].items():
for variable_name, variable in server.get('variables', {}).items():
used_value = variables.get(
variable_name, variable['default_value'])

Expand All @@ -471,3 +494,14 @@ def get_host_from_settings(self, index, variables=None):
url = url.replace("{" + variable_name + "}", used_value)

return url

@property
def host(self):
"""Return generated host."""
return self.get_host_from_settings(self.server_index, variables=self.server_variables)

@host.setter
def host(self, value):
"""Fix base path."""
self._base_path = value
self.server_index = None
Loading