Skip to content

Commit 0232ea9

Browse files
fixed isLibrary flag in fastapi python
1 parent b796620 commit 0232ea9

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void serialize(Boolean value, JsonGenerator gen, SerializerProvider seria
6565

6666
private String implPackage;
6767
protected String sourceFolder;
68-
protected boolean libraryMode = false;
68+
protected boolean isLibrary = false;
6969

7070
private static final String BASE_CLASS_SUFFIX = "base";
7171
private static final String SERVER_PORT = "serverPort";
@@ -74,8 +74,7 @@ public void serialize(Boolean value, JsonGenerator gen, SerializerProvider seria
7474
private static final String DEFAULT_PACKAGE_NAME = "openapi_server";
7575
private static final String DEFAULT_SOURCE_FOLDER = "src";
7676
private static final String DEFAULT_PACKAGE_VERSION = "1.0.0";
77-
private static final String LIBRARY_MODE = "libraryMode";
78-
private static final String DEFAULT_LIBRARY_MODE = "false";
77+
private static final String IS_LIBRARY = "isLibrary";
7978

8079
@Override
8180
public CodegenType getTag() {
@@ -87,8 +86,8 @@ public String getHelp() {
8786
return "Generates a Python FastAPI server (beta). Models are defined with the pydantic library";
8887
}
8988

90-
public void setLibraryMode(final boolean mode) {
91-
this.libraryMode = mode;
89+
public void setIsLibrary(final boolean isLibrary) {
90+
this.isLibrary = isLibrary;
9291
}
9392

9493
public void setImplPackage(final String implPackage) {
@@ -124,7 +123,6 @@ public PythonFastAPIServerCodegen() {
124123
additionalProperties.put(CodegenConstants.SOURCE_FOLDER, DEFAULT_SOURCE_FOLDER);
125124
additionalProperties.put(CodegenConstants.PACKAGE_NAME, DEFAULT_PACKAGE_NAME);
126125
additionalProperties.put(CodegenConstants.FASTAPI_IMPLEMENTATION_PACKAGE, DEFAULT_PACKAGE_NAME.concat(".impl"));
127-
additionalProperties.put(LIBRARY_MODE, DEFAULT_LIBRARY_MODE);
128126

129127
languageSpecificPrimitives.add("List");
130128
languageSpecificPrimitives.add("Dict");
@@ -163,9 +161,9 @@ public PythonFastAPIServerCodegen() {
163161
"python package name for the implementation code (convention: snake_case).",
164162
implPackage);
165163

166-
addSwitch(LIBRARY_MODE,
167-
"whether to generate minimal python code to be published as a separate library",
168-
libraryMode);
164+
addSwitch(IS_LIBRARY,
165+
"whether to generate minimal python code to be published as a separate library or not",
166+
isLibrary);
169167
}
170168

171169
@Override
@@ -176,7 +174,7 @@ public void processOpts() {
176174
convertPropertyToStringAndWriteBack(CodegenConstants.PACKAGE_NAME, this::setPackageName);
177175
convertPropertyToStringAndWriteBack(CodegenConstants.SOURCE_FOLDER, this::setSourceFolder);
178176
convertPropertyToStringAndWriteBack(CodegenConstants.FASTAPI_IMPLEMENTATION_PACKAGE, this::setImplPackage);
179-
convertPropertyToBooleanAndWriteBack(LIBRARY_MODE, this::setLibraryMode);
177+
convertPropertyToBooleanAndWriteBack(IS_LIBRARY, this::setIsLibrary);
180178

181179
modelPackage = packageName + "." + modelPackage;
182180
apiPackage = packageName + "." + apiPackage;
@@ -185,7 +183,7 @@ public void processOpts() {
185183
supportingFiles.add(new SupportingFile("openapi.mustache", "", "openapi.yaml"));
186184
supportingFiles.add(new SupportingFile("main.mustache", String.join(File.separator, new String[]{sourceFolder, packageName.replace('.', File.separatorChar)}), "main.py"));
187185

188-
if (!this.libraryMode) {
186+
if (!this.isLibrary) {
189187
supportingFiles.add(new SupportingFile("docker-compose.mustache", "", "docker-compose.yaml"));
190188
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
191189
}
@@ -200,12 +198,15 @@ public void processOpts() {
200198
namespacePackagePath.append(File.separator).append(tmp);
201199
supportingFiles.add(new SupportingFile("__init__.mustache", namespacePackagePath.toString(), "__init__.py"));
202200
}
201+
203202
supportingFiles.add(new SupportingFile("__init__.mustache", StringUtils.substringAfter(modelFileFolder(), outputFolder), "__init__.py"));
204203
supportingFiles.add(new SupportingFile("__init__.mustache", StringUtils.substringAfter(apiFileFolder(), outputFolder), "__init__.py"));
205-
supportingFiles.add(new SupportingFile("__init__.mustache", StringUtils.substringAfter(apiImplFileFolder(), outputFolder), "__init__.py"));
206204

205+
if (!this.isLibrary) {
206+
supportingFiles.add(new SupportingFile("__init__.mustache", StringUtils.substringAfter(apiImplFileFolder(), outputFolder), "__init__.py"));
207+
}
208+
207209
supportingFiles.add(new SupportingFile("conftest.mustache", testPackage.replace('.', File.separatorChar), "conftest.py"));
208-
209210
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
210211
supportingFiles.add(new SupportingFile("pyproject_toml.mustache", "", "pyproject.toml"));
211212
supportingFiles.add(new SupportingFile("setup_cfg.mustache", "", "setup.cfg"));

modules/openapi-generator/src/main/resources/python-fastapi/api.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import importlib
55
import pkgutil
66

77
from {{apiPackage}}.{{classFilename}}_{{baseSuffix}} import Base{{classname}}
8+
{{^isLibrary}}
89
{{#fastapiImplementationPackage}}
910
import {{fastapiImplementationPackage}}
1011
{{/fastapiImplementationPackage}}
12+
{{/isLibrary}}
1113

1214

1315
from fastapi import ( # noqa: F401
@@ -33,11 +35,13 @@ from {{modelPackage}}.extra_models import TokenModel # noqa: F401
3335

3436
router = APIRouter()
3537

38+
{{^isLibrary}}
3639
{{#fastapiImplementationPackage}}
3740
ns_pkg = {{fastapiImplementationPackage}}
3841
for _, name, _ in pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + "."):
3942
importlib.import_module(name)
4043
{{/fastapiImplementationPackage}}
44+
{{/isLibrary}}
4145

4246
{{#operations}}
4347
{{#operation}}

modules/openapi-generator/src/main/resources/python-fastapi/base_api.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
# coding: utf-8
22

33
from typing import ClassVar, Dict, List, Tuple # noqa: F401
4+
{{#isLibrary}}
5+
from abc import ABC, abstractmethod
6+
{{/isLibrary}}
47

58
{{#imports}}
69
{{import}}
710
{{/imports}}
811
{{#securityImports.0}}from {{packageName}}.security_api import {{#securityImports}}get_token_{{.}}{{^-last}}, {{/-last}}{{/securityImports}}{{/securityImports.0}}
912

10-
class Base{{classname}}:
13+
class Base{{classname}}{{#isLibrary}}(ABC){{/isLibrary}}:
1114
subclasses: ClassVar[Tuple] = ()
1215

1316
def __init_subclass__(cls, **kwargs):
1417
super().__init_subclass__(**kwargs)
1518
Base{{classname}}.subclasses = Base{{classname}}.subclasses + (cls,)
19+
1620
{{#operations}}
1721
{{#operation}}
22+
{{#isLibrary}}@abstractmethod{{/isLibrary}}
1823
async def {{operationId}}(
1924
self,
2025
{{#allParams}}

0 commit comments

Comments
 (0)