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-server] Support python 3.5+ for all server-generators #2884

Merged
merged 4 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public void processOpts() {
}
supportingFiles.add(new SupportingFile("__main__.mustache", packagePath(), "__main__.py"));
supportingFiles.add(new SupportingFile("util.mustache", packagePath(), "util.py"));
supportingFiles.add(new SupportingFile("typing_patch.mustache", packagePath(), "typing_patch.py"));
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: what about naming it as typing_utils.py instead?

Patch sounds like the code is more like a workaround and not production ready

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's is better with typing_utils.py

supportingFiles.add(new SupportingFile("__init__.mustache", packagePath() + File.separatorChar + packageToPath(controllerPackage), "__init__.py"));
supportingFiles.add(new SupportingFile("security_controller_.mustache", packagePath() + File.separatorChar + packageToPath(controllerPackage), "security_controller_.py"));
supportingFiles.add(new SupportingFile("__init__model.mustache", packagePath() + File.separatorChar + packageToPath(modelPackage), "__init__.py"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("app/{{packageName}}/__main__.mustache", APP_PACKAGE_PATH, "__main__.py"));
supportingFiles.add(new SupportingFile("app/{{packageName}}/encoder.mustache", APP_PACKAGE_PATH, "encoder.py"));
supportingFiles.add(new SupportingFile("app/{{packageName}}/util.mustache", APP_PACKAGE_PATH, "util.py"));
supportingFiles.add(new SupportingFile("app/{{packageName}}/typing_patch.mustache", APP_PACKAGE_PATH, "typing_patch.py"));

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

Expand Down Expand Up @@ -192,6 +193,7 @@ protected void addSupportingFiles() {
supportingFiles.add(new SupportingFile("app/{{packageName}}/__main__.mustache", APP_PACKAGE_PATH, "__main__.py"));
supportingFiles.add(new SupportingFile("app/{{packageName}}/encoder.mustache", APP_PACKAGE_PATH, "encoder.py"));
supportingFiles.add(new SupportingFile("app/{{packageName}}/util.mustache", APP_PACKAGE_PATH, "util.py"));
supportingFiles.add(new SupportingFile("app/{{packageName}}/typing_patch.mustache", APP_PACKAGE_PATH, "typing_patch.py"));

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# coding: utf-8

import sys

if sys.version_info < (3, 7):
import typing

def is_generic(klass):
""" Determine whether klass is a generic class """
return type(klass) == typing.GenericMeta

def is_dict(klass):
""" Determine whether klass is a Dict """
return klass.__extra__ == dict

def is_list(klass):
""" Determine whether klass is a List """
return klass.__extra__ == list

else:

def is_generic(klass):
""" Determine whether klass is a generic class """
return hasattr(klass, '__origin__')

def is_dict(klass):
""" Determine whether klass is a Dict """
return klass.__origin__ == list

def is_list(klass):
""" Determine whether klass is a List """
return klass.__origin__ == list
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import datetime

import typing
from typing import Union
from {{packageName}} import typing_patch

T = typing.TypeVar('T')
Class = typing.Type[T]
Expand All @@ -26,10 +27,10 @@ def _deserialize(data: Union[dict, list, str], klass: Union[Class, str]) -> Unio
return deserialize_date(data)
elif klass == datetime.datetime:
return deserialize_datetime(data)
elif type(klass) == typing.GenericMeta:
if klass.__extra__ == list:
elif typing_patch.is_generic(klass):
if typing_patch.is_list(klass):
return _deserialize_list(data, klass.__args__[0])
if klass.__extra__ == dict:
if typing_patch.is_dict(klass):
return _deserialize_dict(data, klass.__args__[1])
else:
return deserialize_model(data, klass)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# coding: utf-8

import sys

if sys.version_info < (3, 7):
import typing

def is_generic(klass):
""" Determine whether klass is a generic class """
return type(klass) == typing.GenericMeta

def is_dict(klass):
""" Determine whether klass is a Dict """
return klass.__extra__ == dict

def is_list(klass):
""" Determine whether klass is a List """
return klass.__extra__ == list

else:

def is_generic(klass):
""" Determine whether klass is a generic class """
return hasattr(klass, '__origin__')

def is_dict(klass):
""" Determine whether klass is a Dict """
return klass.__origin__ == list

def is_list(klass):
""" Determine whether klass is a List """
return klass.__origin__ == list
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import datetime

import six
import typing
from {{packageName}} import typing_patch


def _deserialize(data, klass):
Expand All @@ -23,10 +24,10 @@ def _deserialize(data, klass):
return deserialize_date(data)
elif klass == datetime.datetime:
return deserialize_datetime(data)
elif type(klass) == typing.GenericMeta:
if klass.__extra__ == list:
elif typing_patch.is_generic(klass):
if typing_patch.is_list(klass):
return _deserialize_list(data, klass.__args__[0])
if klass.__extra__ == dict:
if typing_patch.is_dict(klass):
return _deserialize_dict(data, klass.__args__[1])
else:
return deserialize_model(data, klass)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# coding: utf-8

import sys

if sys.version_info < (3, 7):
import typing

def is_generic(klass):
""" Determine whether klass is a generic class """
return type(klass) == typing.GenericMeta

def is_dict(klass):
""" Determine whether klass is a Dict """
return klass.__extra__ == dict

def is_list(klass):
""" Determine whether klass is a List """
return klass.__extra__ == list

else:

def is_generic(klass):
""" Determine whether klass is a generic class """
return hasattr(klass, '__origin__')

def is_dict(klass):
""" Determine whether klass is a Dict """
return klass.__origin__ == list

def is_list(klass):
""" Determine whether klass is a List """
return klass.__origin__ == list
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import datetime

import six
import typing
from {{packageName}} import typing_patch


def _deserialize(data, klass):
Expand All @@ -23,10 +24,10 @@ def _deserialize(data, klass):
return deserialize_date(data)
elif klass == datetime.datetime:
return deserialize_datetime(data)
elif type(klass) == typing.GenericMeta:
if klass.__extra__ == list:
elif typing_patch.is_generic(klass):
if typing_patch.is_list(klass):
return _deserialize_list(data, klass.__args__[0])
if klass.__extra__ == dict:
if typing_patch.is_dict(klass):
return _deserialize_dict(data, klass.__args__[1])
else:
return deserialize_model(data, klass)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.0-SNAPSHOT
4.0.0
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
openapi: 3.0.0
info:
description: This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
description: This is a sample server Petstore server. For this sample, you can use
the api key `special-key` to test the authorization filters.
license:
name: Apache-2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
Expand Down Expand Up @@ -92,7 +93,6 @@ paths:
description: Invalid status value
security:
- petstore_auth:
- write:pets
- read:pets
summary: Finds Pets by status
tags:
Expand All @@ -101,7 +101,8 @@ paths:
/pet/findByTags:
get:
deprecated: true
description: Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
description: Multiple tags can be provided with comma separated strings. Use
tag1, tag2, tag3 for testing.
operationId: find_pets_by_tags
parameters:
- description: Tags to filter by
Expand Down Expand Up @@ -141,7 +142,6 @@ paths:
description: Invalid tag value
security:
- petstore_auth:
- write:pets
- read:pets
summary: Finds Pets by tags
tags:
Expand Down Expand Up @@ -337,7 +337,8 @@ paths:
x-openapi-router-controller: openapi_server.controllers.store_controller
/store/order/{orderId}:
delete:
description: For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
description: For valid response try integer IDs with value < 1000. Anything
above 1000 or nonintegers will generate API errors
operationId: delete_order
parameters:
- description: ID of the order that needs to be deleted
Expand All @@ -358,7 +359,8 @@ paths:
- store
x-openapi-router-controller: openapi_server.controllers.store_controller
get:
description: For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
description: For valid response try integer IDs with value <= 5 or > 10. Other
values will generated exceptions
operationId: get_order_by_id
parameters:
- description: ID of pet that needs to be fetched
Expand Down Expand Up @@ -471,7 +473,8 @@ paths:
description: successful operation
headers:
Set-Cookie:
description: Cookie authentication key for use with the `auth_cookie` apiKey authentication.
description: Cookie authentication key for use with the `auth_cookie`
apiKey authentication.
explode: false
schema:
example: AUTH_KEY=abcde12345; Path=/; HttpOnly
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# coding: utf-8

import sys

if sys.version_info < (3, 7):
import typing

def is_generic(klass):
""" Determine whether klass is a generic class """
return type(klass) == typing.GenericMeta

def is_dict(klass):
""" Determine whether klass is a Dict """
return klass.__extra__ == dict

def is_list(klass):
""" Determine whether klass is a List """
return klass.__extra__ == list

else:

def is_generic(klass):
""" Determine whether klass is a generic class """
return hasattr(klass, '__origin__')

def is_dict(klass):
""" Determine whether klass is a Dict """
return klass.__origin__ == list

def is_list(klass):
""" Determine whether klass is a List """
return klass.__origin__ == list
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import six
import typing
from openapi_server import typing_patch


def _deserialize(data, klass):
Expand All @@ -23,10 +24,10 @@ def _deserialize(data, klass):
return deserialize_date(data)
elif klass == datetime.datetime:
return deserialize_datetime(data)
elif type(klass) == typing.GenericMeta:
if klass.__extra__ == list:
elif typing_patch.is_generic(klass):
if typing_patch.is_list(klass):
return _deserialize_list(data, klass.__args__[0])
if klass.__extra__ == dict:
if typing_patch.is_dict(klass):
return _deserialize_dict(data, klass.__args__[1])
else:
return deserialize_model(data, klass)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.0-SNAPSHOT
4.0.0
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
openapi: 3.0.0
info:
description: This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
description: This is a sample server Petstore server. For this sample, you can use
the api key `special-key` to test the authorization filters.
license:
name: Apache-2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
Expand Down Expand Up @@ -92,7 +93,6 @@ paths:
description: Invalid status value
security:
- petstore_auth:
- write:pets
- read:pets
summary: Finds Pets by status
tags:
Expand All @@ -101,7 +101,8 @@ paths:
/pet/findByTags:
get:
deprecated: true
description: Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
description: Multiple tags can be provided with comma separated strings. Use
tag1, tag2, tag3 for testing.
operationId: find_pets_by_tags
parameters:
- description: Tags to filter by
Expand Down Expand Up @@ -141,7 +142,6 @@ paths:
description: Invalid tag value
security:
- petstore_auth:
- write:pets
- read:pets
summary: Finds Pets by tags
tags:
Expand Down Expand Up @@ -337,7 +337,8 @@ paths:
x-openapi-router-controller: openapi_server.controllers.store_controller
/store/order/{orderId}:
delete:
description: For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
description: For valid response try integer IDs with value < 1000. Anything
above 1000 or nonintegers will generate API errors
operationId: delete_order
parameters:
- description: ID of the order that needs to be deleted
Expand All @@ -358,7 +359,8 @@ paths:
- store
x-openapi-router-controller: openapi_server.controllers.store_controller
get:
description: For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
description: For valid response try integer IDs with value <= 5 or > 10. Other
values will generated exceptions
operationId: get_order_by_id
parameters:
- description: ID of pet that needs to be fetched
Expand Down Expand Up @@ -471,7 +473,8 @@ paths:
description: successful operation
headers:
Set-Cookie:
description: Cookie authentication key for use with the `auth_cookie` apiKey authentication.
description: Cookie authentication key for use with the `auth_cookie`
apiKey authentication.
explode: false
schema:
example: AUTH_KEY=abcde12345; Path=/; HttpOnly
Expand Down
Loading