mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-06 05:16:10 +00:00
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:
committed by
William Cheng
parent
882186f461
commit
c6a5017a13
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -419,6 +419,7 @@ paths:
|
|||||||
required: true
|
required: true
|
||||||
schema:
|
schema:
|
||||||
type: string
|
type: string
|
||||||
|
pattern: '^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$'
|
||||||
- name: password
|
- name: password
|
||||||
in: query
|
in: query
|
||||||
description: The password for login in clear text
|
description: The password for login in clear text
|
||||||
@@ -606,6 +607,7 @@ components:
|
|||||||
format: int64
|
format: int64
|
||||||
name:
|
name:
|
||||||
type: string
|
type: string
|
||||||
|
pattern: '^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$'
|
||||||
xml:
|
xml:
|
||||||
name: Category
|
name: Category
|
||||||
User:
|
User:
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ import petstore_api
|
|||||||
from petstore_api.rest import ApiException
|
from petstore_api.rest import ApiException
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
|
|
||||||
# create an instance of the API class
|
# create an instance of the API class
|
||||||
api_instance = petstore_api.FakeApi(petstore_api.ApiClient(configuration))
|
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)
|
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
|
- **API key parameter name**: api_key */ ' " =end -- \r\n \n \r
|
||||||
- **Location**: HTTP header
|
- **Location**: HTTP header
|
||||||
|
|
||||||
|
|
||||||
## petstore_auth
|
## petstore_auth
|
||||||
|
|
||||||
- **Type**: OAuth
|
- **Type**: OAuth
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ To test code injection */ ' \" =end -- \\r\\n \\n \\r
|
|||||||
To test code injection */ ' \" =end -- \\r\\n \\n \\r
|
To test code injection */ ' \" =end -- \\r\\n \\n \\r
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import time
|
import time
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ class ApiClient(object):
|
|||||||
query_params=None, header_params=None, body=None, post_params=None,
|
query_params=None, header_params=None, body=None, post_params=None,
|
||||||
files=None, response_type=None, auth_settings=None,
|
files=None, response_type=None, auth_settings=None,
|
||||||
_return_http_data_only=None, collection_formats=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
|
config = self.configuration
|
||||||
|
|
||||||
@@ -157,7 +157,11 @@ class ApiClient(object):
|
|||||||
body = self.sanitize_for_serialization(body)
|
body = self.sanitize_for_serialization(body)
|
||||||
|
|
||||||
# request url
|
# request url
|
||||||
|
if _host is None:
|
||||||
url = self.configuration.host + resource_path
|
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
|
# perform request and return response
|
||||||
response_data = self.request(
|
response_data = self.request(
|
||||||
@@ -290,7 +294,7 @@ class ApiClient(object):
|
|||||||
body=None, post_params=None, files=None,
|
body=None, post_params=None, files=None,
|
||||||
response_type=None, auth_settings=None, async_req=None,
|
response_type=None, auth_settings=None, async_req=None,
|
||||||
_return_http_data_only=None, collection_formats=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.
|
"""Makes the HTTP request (synchronous) and returns deserialized data.
|
||||||
|
|
||||||
To make an async_req request, set the async_req parameter.
|
To make an async_req request, set the async_req parameter.
|
||||||
@@ -333,7 +337,7 @@ class ApiClient(object):
|
|||||||
body, post_params, files,
|
body, post_params, files,
|
||||||
response_type, auth_settings,
|
response_type, auth_settings,
|
||||||
_return_http_data_only, collection_formats,
|
_return_http_data_only, collection_formats,
|
||||||
_preload_content, _request_timeout)
|
_preload_content, _request_timeout, _host)
|
||||||
else:
|
else:
|
||||||
thread = self.pool.apply_async(self.__call_api, (resource_path,
|
thread = self.pool.apply_async(self.__call_api, (resource_path,
|
||||||
method, path_params, query_params,
|
method, path_params, query_params,
|
||||||
@@ -342,7 +346,9 @@ class ApiClient(object):
|
|||||||
response_type, auth_settings,
|
response_type, auth_settings,
|
||||||
_return_http_data_only,
|
_return_http_data_only,
|
||||||
collection_formats,
|
collection_formats,
|
||||||
_preload_content, _request_timeout))
|
_preload_content,
|
||||||
|
_request_timeout,
|
||||||
|
_host))
|
||||||
return thread
|
return thread
|
||||||
|
|
||||||
def request(self, method, url, query_params=None, headers=None,
|
def request(self, method, url, query_params=None, headers=None,
|
||||||
|
|||||||
@@ -60,10 +60,8 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
|
|||||||
self.username = ""
|
self.username = ""
|
||||||
# Password for HTTP basic authentication
|
# Password for HTTP basic authentication
|
||||||
self.password = ""
|
self.password = ""
|
||||||
|
# access token for OAuth/Bearer
|
||||||
# access token for OAuth
|
|
||||||
self.access_token = ""
|
self.access_token = ""
|
||||||
|
|
||||||
# Logging Settings
|
# Logging Settings
|
||||||
self.logger = {}
|
self.logger = {}
|
||||||
self.logger["package_logger"] = logging.getLogger("petstore_api")
|
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',
|
'key': 'api_key */ ' " =end -- \r\n \n \r',
|
||||||
'value': self.get_api_key_with_prefix('api_key */ ' " =end -- \r\n \n \r')
|
'value': self.get_api_key_with_prefix('api_key */ ' " =end -- \r\n \n \r')
|
||||||
},
|
},
|
||||||
|
|
||||||
'petstore_auth':
|
'petstore_auth':
|
||||||
{
|
{
|
||||||
'type': 'oauth2',
|
'type': 'oauth2',
|
||||||
@@ -231,7 +228,6 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
|
|||||||
'key': 'Authorization',
|
'key': 'Authorization',
|
||||||
'value': 'Bearer ' + self.access_token
|
'value': 'Bearer ' + self.access_token
|
||||||
},
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def to_debug_report(self):
|
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"\
|
"Version of the API: 1.0.0 */ ' \" =end -- \\r\\n \\n \\r\n"\
|
||||||
"SDK Package Version: 1.0.0".\
|
"SDK Package Version: 1.0.0".\
|
||||||
format(env=sys.platform, pyversion=sys.version)
|
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
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ from datetime import date, datetime # noqa: F401
|
|||||||
from typing import List, Dict # noqa: F401
|
from typing import List, Dict # noqa: F401
|
||||||
|
|
||||||
from openapi_server.models.base_model_ import Model
|
from openapi_server.models.base_model_ import Model
|
||||||
|
import re
|
||||||
from openapi_server import util
|
from openapi_server import util
|
||||||
|
|
||||||
|
import re # noqa: E501
|
||||||
|
|
||||||
class Category(Model):
|
class Category(Model):
|
||||||
"""NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
"""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.
|
:param name: The name of this Category.
|
||||||
:type name: str
|
: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
|
self._name = name
|
||||||
|
|||||||
@@ -433,6 +433,7 @@ paths:
|
|||||||
name: username
|
name: username
|
||||||
required: true
|
required: true
|
||||||
schema:
|
schema:
|
||||||
|
pattern: ^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$
|
||||||
type: string
|
type: string
|
||||||
style: form
|
style: form
|
||||||
- description: The password for login in clear text
|
- description: The password for login in clear text
|
||||||
@@ -641,6 +642,7 @@ components:
|
|||||||
format: int64
|
format: int64
|
||||||
type: integer
|
type: integer
|
||||||
name:
|
name:
|
||||||
|
pattern: ^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$
|
||||||
type: string
|
type: string
|
||||||
title: Pet category
|
title: Pet category
|
||||||
type: object
|
type: object
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ from datetime import date, datetime # noqa: F401
|
|||||||
from typing import List, Dict # noqa: F401
|
from typing import List, Dict # noqa: F401
|
||||||
|
|
||||||
from openapi_server.models.base_model_ import Model
|
from openapi_server.models.base_model_ import Model
|
||||||
|
import re
|
||||||
from openapi_server import util
|
from openapi_server import util
|
||||||
|
|
||||||
|
import re # noqa: E501
|
||||||
|
|
||||||
class Category(Model):
|
class Category(Model):
|
||||||
"""NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
"""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.
|
:param name: The name of this Category.
|
||||||
:type name: str
|
: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
|
self._name = name
|
||||||
|
|||||||
@@ -433,6 +433,7 @@ paths:
|
|||||||
name: username
|
name: username
|
||||||
required: true
|
required: true
|
||||||
schema:
|
schema:
|
||||||
|
pattern: ^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$
|
||||||
type: string
|
type: string
|
||||||
style: form
|
style: form
|
||||||
- description: The password for login in clear text
|
- description: The password for login in clear text
|
||||||
@@ -641,6 +642,7 @@ components:
|
|||||||
format: int64
|
format: int64
|
||||||
type: integer
|
type: integer
|
||||||
name:
|
name:
|
||||||
|
pattern: ^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$
|
||||||
type: string
|
type: string
|
||||||
title: Pet category
|
title: Pet category
|
||||||
type: object
|
type: object
|
||||||
|
|||||||
Reference in New Issue
Block a user