-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[python-fastapi] Added a base class for the actual implementation (#1…
…4470) It is very difficult to "merge" the changes, made by code generation, and the changes, made by developers. It would be very useful to separate the generated code and the code written by developers. In addition this would remove the necessity to track the generated code. Pyhton (since 3.6) has a hook, __init_subclasses__, that could be used to solve exactly this problem. The classes from *_base.py should be implemented in an ns package that is specified by the additional parameter ("-p fastapiImplementationPackage=example_name"). Signed-off-by: Nikita Vakula <[email protected]>
- Loading branch information
1 parent
b94952b
commit 3db7169
Showing
13 changed files
with
307 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
modules/openapi-generator/src/main/resources/python-fastapi/base_api.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# coding: utf-8 | ||
|
||
from typing import ClassVar, Dict, List, Tuple # noqa: F401 | ||
|
||
{{#imports}} | ||
{{import}} | ||
{{/imports}} | ||
{{#securityImports.0}}from {{packageName}}.security_api import {{#securityImports}}get_token_{{.}}{{^-last}}, {{/-last}}{{/securityImports}}{{/securityImports.0}} | ||
|
||
class Base{{classname}}: | ||
subclasses: ClassVar[Tuple] = () | ||
|
||
def __init_subclass__(cls, **kwargs): | ||
super().__init_subclass__(**kwargs) | ||
Base{{classname}}.subclasses = Base{{classname}}.subclasses + (cls,) | ||
{{#operations}} | ||
{{#operation}} | ||
def {{operationId}}( | ||
self, | ||
{{#allParams}} | ||
{{>impl_argument_definition}}, | ||
{{/allParams}} | ||
) -> {{returnType}}{{^returnType}}None{{/returnType}}: | ||
{{#notes}}"""{{.}}""" | ||
...{{/notes}}{{^notes}}...{{/notes}} | ||
{{^-last}} | ||
|
||
|
||
{{/-last}} | ||
{{/operation}} | ||
{{/operations}} |
1 change: 1 addition & 0 deletions
1
modules/openapi-generator/src/main/resources/python-fastapi/impl_argument.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isPathParam}}{{baseName}}{{/isPathParam}}{{^isPathParam}}{{paramName}}{{/isPathParam}} |
1 change: 1 addition & 0 deletions
1
...les/openapi-generator/src/main/resources/python-fastapi/impl_argument_definition.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isPathParam}}{{baseName}}{{/isPathParam}}{{^isPathParam}}{{paramName}}{{/isPathParam}}: {{>param_type}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
samples/server/petstore/python-fastapi/src/openapi_server/apis/pet_api_base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# coding: utf-8 | ||
|
||
from typing import ClassVar, Dict, List, Tuple # noqa: F401 | ||
|
||
from openapi_server.models.api_response import ApiResponse | ||
from openapi_server.models.pet import Pet | ||
from openapi_server.security_api import get_token_petstore_auth, get_token_api_key | ||
|
||
class BasePetApi: | ||
subclasses: ClassVar[Tuple] = () | ||
|
||
def __init_subclass__(cls, **kwargs): | ||
super().__init_subclass__(**kwargs) | ||
BasePetApi.subclasses = BasePetApi.subclasses + (cls,) | ||
def add_pet( | ||
self, | ||
pet: Pet, | ||
) -> Pet: | ||
"""""" | ||
... | ||
|
||
|
||
def delete_pet( | ||
self, | ||
petId: int, | ||
api_key: str, | ||
) -> None: | ||
"""""" | ||
... | ||
|
||
|
||
def find_pets_by_status( | ||
self, | ||
status: List[str], | ||
) -> List[Pet]: | ||
"""Multiple status values can be provided with comma separated strings""" | ||
... | ||
|
||
|
||
def find_pets_by_tags( | ||
self, | ||
tags: List[str], | ||
) -> List[Pet]: | ||
"""Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.""" | ||
... | ||
|
||
|
||
def get_pet_by_id( | ||
self, | ||
petId: int, | ||
) -> Pet: | ||
"""Returns a single pet""" | ||
... | ||
|
||
|
||
def update_pet( | ||
self, | ||
pet: Pet, | ||
) -> Pet: | ||
"""""" | ||
... | ||
|
||
|
||
def update_pet_with_form( | ||
self, | ||
petId: int, | ||
name: str, | ||
status: str, | ||
) -> None: | ||
"""""" | ||
... | ||
|
||
|
||
def upload_file( | ||
self, | ||
petId: int, | ||
additional_metadata: str, | ||
file: str, | ||
) -> ApiResponse: | ||
"""""" | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.