Skip to content

Commit 20b8eff

Browse files
GuillaumeSmahawing328
authored andcommitted
[python-server] Support python 3.7 for all server-generators (#2884)
* Support python 3.7 for all server-generators Signed-off-by: Guillaume Smaha <guillaume.smaha@gmail.com> * Rename typing_patch.py to typing_utils.py * Renaming typing_patch.mustache to typing_utils.mustache * Fix comparaison in typing_utils.is_dict for python3.7
1 parent 1411880 commit 20b8eff

31 files changed

Lines changed: 377 additions & 59 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public void processOpts() {
202202
}
203203
supportingFiles.add(new SupportingFile("__main__.mustache", packagePath(), "__main__.py"));
204204
supportingFiles.add(new SupportingFile("util.mustache", packagePath(), "util.py"));
205+
supportingFiles.add(new SupportingFile("typing_utils.mustache", packagePath(), "typing_utils.py"));
205206
supportingFiles.add(new SupportingFile("__init__.mustache", packagePath() + File.separatorChar + packageToPath(controllerPackage), "__init__.py"));
206207
supportingFiles.add(new SupportingFile("security_controller_.mustache", packagePath() + File.separatorChar + packageToPath(controllerPackage), "security_controller_.py"));
207208
supportingFiles.add(new SupportingFile("__init__model.mustache", packagePath() + File.separatorChar + packageToPath(modelPackage), "__init__.py"));

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonBluePlanetServerCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public void processOpts() {
118118
supportingFiles.add(new SupportingFile("app/{{packageName}}/__main__.mustache", APP_PACKAGE_PATH, "__main__.py"));
119119
supportingFiles.add(new SupportingFile("app/{{packageName}}/encoder.mustache", APP_PACKAGE_PATH, "encoder.py"));
120120
supportingFiles.add(new SupportingFile("app/{{packageName}}/util.mustache", APP_PACKAGE_PATH, "util.py"));
121+
supportingFiles.add(new SupportingFile("app/{{packageName}}/typing_utils.mustache", APP_PACKAGE_PATH, "typing_utils.py"));
121122

122123
supportingFiles.add(new SupportingFile("app/{{packageName}}/controllers/__init__.mustache", CONTROLLER_PATH, "__init__.py"));
123124

@@ -192,6 +193,7 @@ protected void addSupportingFiles() {
192193
supportingFiles.add(new SupportingFile("app/{{packageName}}/__main__.mustache", APP_PACKAGE_PATH, "__main__.py"));
193194
supportingFiles.add(new SupportingFile("app/{{packageName}}/encoder.mustache", APP_PACKAGE_PATH, "encoder.py"));
194195
supportingFiles.add(new SupportingFile("app/{{packageName}}/util.mustache", APP_PACKAGE_PATH, "util.py"));
196+
supportingFiles.add(new SupportingFile("app/{{packageName}}/typing_utils.mustache", APP_PACKAGE_PATH, "typing_utils.py"));
195197

196198
supportingFiles.add(new SupportingFile("app/{{packageName}}/controllers/__init__.mustache", CONTROLLER_PATH, "__init__.py"));
197199

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# coding: utf-8
2+
3+
import sys
4+
5+
if sys.version_info < (3, 7):
6+
import typing
7+
8+
def is_generic(klass):
9+
""" Determine whether klass is a generic class """
10+
return type(klass) == typing.GenericMeta
11+
12+
def is_dict(klass):
13+
""" Determine whether klass is a Dict """
14+
return klass.__extra__ == dict
15+
16+
def is_list(klass):
17+
""" Determine whether klass is a List """
18+
return klass.__extra__ == list
19+
20+
else:
21+
22+
def is_generic(klass):
23+
""" Determine whether klass is a generic class """
24+
return hasattr(klass, '__origin__')
25+
26+
def is_dict(klass):
27+
""" Determine whether klass is a Dict """
28+
return klass.__origin__ == dict
29+
30+
def is_list(klass):
31+
""" Determine whether klass is a List """
32+
return klass.__origin__ == list

modules/openapi-generator/src/main/resources/python-aiohttp/util.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import datetime
22

33
import typing
44
from typing import Union
5+
from {{packageName}} import typing_utils
56

67
T = typing.TypeVar('T')
78
Class = typing.Type[T]
@@ -26,10 +27,10 @@ def _deserialize(data: Union[dict, list, str], klass: Union[Class, str]) -> Unio
2627
return deserialize_date(data)
2728
elif klass == datetime.datetime:
2829
return deserialize_datetime(data)
29-
elif type(klass) == typing.GenericMeta:
30-
if klass.__extra__ == list:
30+
elif typing_utils.is_generic(klass):
31+
if typing_utils.is_list(klass):
3132
return _deserialize_list(data, klass.__args__[0])
32-
if klass.__extra__ == dict:
33+
if typing_utils.is_dict(klass):
3334
return _deserialize_dict(data, klass.__args__[1])
3435
else:
3536
return deserialize_model(data, klass)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# coding: utf-8
2+
3+
import sys
4+
5+
if sys.version_info < (3, 7):
6+
import typing
7+
8+
def is_generic(klass):
9+
""" Determine whether klass is a generic class """
10+
return type(klass) == typing.GenericMeta
11+
12+
def is_dict(klass):
13+
""" Determine whether klass is a Dict """
14+
return klass.__extra__ == dict
15+
16+
def is_list(klass):
17+
""" Determine whether klass is a List """
18+
return klass.__extra__ == list
19+
20+
else:
21+
22+
def is_generic(klass):
23+
""" Determine whether klass is a generic class """
24+
return hasattr(klass, '__origin__')
25+
26+
def is_dict(klass):
27+
""" Determine whether klass is a Dict """
28+
return klass.__origin__ == dict
29+
30+
def is_list(klass):
31+
""" Determine whether klass is a List """
32+
return klass.__origin__ == list

modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/util.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import datetime
22

33
import six
44
import typing
5+
from {{packageName}} import typing_utils
56

67

78
def _deserialize(data, klass):
@@ -23,10 +24,10 @@ def _deserialize(data, klass):
2324
return deserialize_date(data)
2425
elif klass == datetime.datetime:
2526
return deserialize_datetime(data)
26-
elif type(klass) == typing.GenericMeta:
27-
if klass.__extra__ == list:
27+
elif typing_utils.is_generic(klass):
28+
if typing_utils.is_list(klass):
2829
return _deserialize_list(data, klass.__args__[0])
29-
if klass.__extra__ == dict:
30+
if typing_utils.is_dict(klass):
3031
return _deserialize_dict(data, klass.__args__[1])
3132
else:
3233
return deserialize_model(data, klass)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# coding: utf-8
2+
3+
import sys
4+
5+
if sys.version_info < (3, 7):
6+
import typing
7+
8+
def is_generic(klass):
9+
""" Determine whether klass is a generic class """
10+
return type(klass) == typing.GenericMeta
11+
12+
def is_dict(klass):
13+
""" Determine whether klass is a Dict """
14+
return klass.__extra__ == dict
15+
16+
def is_list(klass):
17+
""" Determine whether klass is a List """
18+
return klass.__extra__ == list
19+
20+
else:
21+
22+
def is_generic(klass):
23+
""" Determine whether klass is a generic class """
24+
return hasattr(klass, '__origin__')
25+
26+
def is_dict(klass):
27+
""" Determine whether klass is a Dict """
28+
return klass.__origin__ == dict
29+
30+
def is_list(klass):
31+
""" Determine whether klass is a List """
32+
return klass.__origin__ == list

modules/openapi-generator/src/main/resources/python-flask/util.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import datetime
22

33
import six
44
import typing
5+
from {{packageName}} import typing_utils
56

67

78
def _deserialize(data, klass):
@@ -23,10 +24,10 @@ def _deserialize(data, klass):
2324
return deserialize_date(data)
2425
elif klass == datetime.datetime:
2526
return deserialize_datetime(data)
26-
elif type(klass) == typing.GenericMeta:
27-
if klass.__extra__ == list:
27+
elif typing_utils.is_generic(klass):
28+
if typing_utils.is_list(klass):
2829
return _deserialize_list(data, klass.__args__[0])
29-
if klass.__extra__ == dict:
30+
if typing_utils.is_dict(klass):
3031
return _deserialize_dict(data, klass.__args__[1])
3132
else:
3233
return deserialize_model(data, klass)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.0-SNAPSHOT
1+
4.0.0

samples/server/openapi3/petstore/python-flask-python2/openapi_server/openapi/openapi.yaml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
openapi: 3.0.0
22
info:
3-
description: This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
3+
description: This is a sample server Petstore server. For this sample, you can use
4+
the api key `special-key` to test the authorization filters.
45
license:
56
name: Apache-2.0
67
url: http://www.apache.org/licenses/LICENSE-2.0.html
@@ -92,7 +93,6 @@ paths:
9293
description: Invalid status value
9394
security:
9495
- petstore_auth:
95-
- write:pets
9696
- read:pets
9797
summary: Finds Pets by status
9898
tags:
@@ -101,7 +101,8 @@ paths:
101101
/pet/findByTags:
102102
get:
103103
deprecated: true
104-
description: Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
104+
description: Multiple tags can be provided with comma separated strings. Use
105+
tag1, tag2, tag3 for testing.
105106
operationId: find_pets_by_tags
106107
parameters:
107108
- description: Tags to filter by
@@ -141,7 +142,6 @@ paths:
141142
description: Invalid tag value
142143
security:
143144
- petstore_auth:
144-
- write:pets
145145
- read:pets
146146
summary: Finds Pets by tags
147147
tags:
@@ -337,7 +337,8 @@ paths:
337337
x-openapi-router-controller: openapi_server.controllers.store_controller
338338
/store/order/{orderId}:
339339
delete:
340-
description: For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
340+
description: For valid response try integer IDs with value < 1000. Anything
341+
above 1000 or nonintegers will generate API errors
341342
operationId: delete_order
342343
parameters:
343344
- description: ID of the order that needs to be deleted
@@ -358,7 +359,8 @@ paths:
358359
- store
359360
x-openapi-router-controller: openapi_server.controllers.store_controller
360361
get:
361-
description: For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
362+
description: For valid response try integer IDs with value <= 5 or > 10. Other
363+
values will generated exceptions
362364
operationId: get_order_by_id
363365
parameters:
364366
- description: ID of pet that needs to be fetched
@@ -471,7 +473,8 @@ paths:
471473
description: successful operation
472474
headers:
473475
Set-Cookie:
474-
description: Cookie authentication key for use with the `auth_cookie` apiKey authentication.
476+
description: Cookie authentication key for use with the `auth_cookie`
477+
apiKey authentication.
475478
explode: false
476479
schema:
477480
example: AUTH_KEY=abcde12345; Path=/; HttpOnly

0 commit comments

Comments
 (0)