Fix regex in Python server model code (#2314)

* - Fix regex in Python server model code.

* - Adding (regular expression) `pattern` to user name and category name.

* - Update Python server related generated code samples.
This commit is contained in:
Tom Ghyselinck 2019-03-08 10:10:52 +01:00 committed by William Cheng
parent 882186f461
commit c6a5017a13
10 changed files with 89 additions and 10 deletions

View File

@ -908,4 +908,13 @@ public class PythonAbstractConnexionServerCodegen extends DefaultCodegen impleme
}
}
/*
* We don't want to run `escapeText` on the pattern
* but forward it directly to the Python implementation.
*/
@Override
public String toRegularExpression(String pattern) {
return addRegularExpressionDelimiter(pattern);
}
}

View File

@ -419,6 +419,7 @@ paths:
required: true
schema:
type: string
pattern: '^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$'
- name: password
in: query
description: The password for login in clear text
@ -606,6 +607,7 @@ components:
format: int64
name:
type: string
pattern: '^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$'
xml:
name: Category
User:

View File

@ -51,6 +51,7 @@ import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# create an instance of the API class
api_instance = petstore_api.FakeApi(petstore_api.ApiClient(configuration))
test_code_inject____end____rn_n_r = 'test_code_inject____end____rn_n_r_example' # str | To test code injection */ ' \\\" =end -- \\\\r\\\\n \\\\n \\\\r (optional)
@ -86,6 +87,7 @@ Class | Method | HTTP request | Description
- **API key parameter name**: api_key */ ' " =end -- \r\n \n \r
- **Location**: HTTP header
## petstore_auth
- **Type**: OAuth

View File

@ -15,6 +15,7 @@ To test code injection */ ' \" =end -- \\r\\n \\n \\r
To test code injection */ ' \" =end -- \\r\\n \\n \\r
### Example
```python
from __future__ import print_function
import time

View File

@ -110,7 +110,7 @@ class ApiClient(object):
query_params=None, header_params=None, body=None, post_params=None,
files=None, response_type=None, auth_settings=None,
_return_http_data_only=None, collection_formats=None,
_preload_content=True, _request_timeout=None):
_preload_content=True, _request_timeout=None, _host=None):
config = self.configuration
@ -157,7 +157,11 @@ class ApiClient(object):
body = self.sanitize_for_serialization(body)
# request url
url = self.configuration.host + resource_path
if _host is None:
url = self.configuration.host + resource_path
else:
# use server/host defined in path or operation instead
url = _host + resource_path
# perform request and return response
response_data = self.request(
@ -290,7 +294,7 @@ class ApiClient(object):
body=None, post_params=None, files=None,
response_type=None, auth_settings=None, async_req=None,
_return_http_data_only=None, collection_formats=None,
_preload_content=True, _request_timeout=None):
_preload_content=True, _request_timeout=None, _host=None):
"""Makes the HTTP request (synchronous) and returns deserialized data.
To make an async_req request, set the async_req parameter.
@ -333,7 +337,7 @@ class ApiClient(object):
body, post_params, files,
response_type, auth_settings,
_return_http_data_only, collection_formats,
_preload_content, _request_timeout)
_preload_content, _request_timeout, _host)
else:
thread = self.pool.apply_async(self.__call_api, (resource_path,
method, path_params, query_params,
@ -342,7 +346,9 @@ class ApiClient(object):
response_type, auth_settings,
_return_http_data_only,
collection_formats,
_preload_content, _request_timeout))
_preload_content,
_request_timeout,
_host))
return thread
def request(self, method, url, query_params=None, headers=None,

View File

@ -60,10 +60,8 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
self.username = ""
# Password for HTTP basic authentication
self.password = ""
# access token for OAuth
# access token for OAuth/Bearer
self.access_token = ""
# Logging Settings
self.logger = {}
self.logger["package_logger"] = logging.getLogger("petstore_api")
@ -223,7 +221,6 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
'key': 'api_key */ ' " =end -- \r\n \n \r',
'value': self.get_api_key_with_prefix('api_key */ ' " =end -- \r\n \n \r')
},
'petstore_auth':
{
'type': 'oauth2',
@ -231,7 +228,6 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
'key': 'Authorization',
'value': 'Bearer ' + self.access_token
},
}
def to_debug_report(self):
@ -245,3 +241,54 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
"Version of the API: 1.0.0 */ ' \" =end -- \\r\\n \\n \\r\n"\
"SDK Package Version: 1.0.0".\
format(env=sys.platform, pyversion=sys.version)
def get_host_settings(self):
"""Gets an array of host settings
:return: An array of host settings
"""
return [
{
'url': "//petstore.swagger.io */ ' " =end -- \r\n \n \r/v2 */ ' " =end -- \r\n \n \r",
'description': "No description provided",
}
]
def get_host_from_settings(self, index, variables={}):
"""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
:return: URL based on host settings
"""
servers = self.get_host_settings()
# check array index out of bound
if index < 0 or index >= len(servers):
raise ValueError(
"Invalid index {} when selecting the host settings. Must be less than {}" # noqa: E501
.format(index, len(servers)))
server = servers[index]
url = server['url']
# go through variable and assign a value
for variable_name in server['variables']:
if variable_name in variables:
if variables[variable_name] in server['variables'][
variable_name]['enum_values']:
url = url.replace("{" + variable_name + "}",
variables[variable_name])
else:
raise ValueError(
"The variable `{}` in the host URL has invalid value {}. Must be {}." # noqa: E501
.format(
variable_name, variables[variable_name],
server['variables'][variable_name]['enum_values']))
else:
# use default value
url = url.replace(
"{" + variable_name + "}",
server['variables'][variable_name]['default_value'])
return url

View File

@ -6,8 +6,10 @@ from datetime import date, datetime # noqa: F401
from typing import List, Dict # noqa: F401
from openapi_server.models.base_model_ import Model
import re
from openapi_server import util
import re # noqa: E501
class Category(Model):
"""NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
@ -86,5 +88,7 @@ class Category(Model):
:param name: The name of this Category.
:type name: str
"""
if name is not None and not re.search(r'^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$', name): # noqa: E501
raise ValueError("Invalid value for `name`, must be a follow pattern or equal to `/^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/`") # noqa: E501
self._name = name

View File

@ -433,6 +433,7 @@ paths:
name: username
required: true
schema:
pattern: ^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$
type: string
style: form
- description: The password for login in clear text
@ -641,6 +642,7 @@ components:
format: int64
type: integer
name:
pattern: ^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$
type: string
title: Pet category
type: object

View File

@ -6,8 +6,10 @@ from datetime import date, datetime # noqa: F401
from typing import List, Dict # noqa: F401
from openapi_server.models.base_model_ import Model
import re
from openapi_server import util
import re # noqa: E501
class Category(Model):
"""NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
@ -86,5 +88,7 @@ class Category(Model):
:param name: The name of this Category.
:type name: str
"""
if name is not None and not re.search(r'^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$', name): # noqa: E501
raise ValueError("Invalid value for `name`, must be a follow pattern or equal to `/^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/`") # noqa: E501
self._name = name

View File

@ -433,6 +433,7 @@ paths:
name: username
required: true
schema:
pattern: ^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$
type: string
style: form
- description: The password for login in clear text
@ -641,6 +642,7 @@ components:
format: int64
type: integer
name:
pattern: ^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$
type: string
title: Pet category
type: object