OpenAPITools / OpenAPITools/openapi-generator
Python: Allow nested classes to choice if they need to update typing import for operation responses type
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Hello. I need to create my own generator for Python, inherited from the AbstractPythonCodegen class. In the generated code, I noticed that not all models required for API functions are being imported. In particular, I was missing the 400 and 422 responses.
I found out that disabling the following condition in AbstractPythonCodegen
// update typing import for operation responses type
// only python-fastapi needs this at the moment
if (this instanceof PythonFastAPIServerCodegen) {
(to make it working for all derived classes) solves my problem. I think it would make sense to allow derived classes to decide whether this piece of code is needed, instead of having it hardcoded to PythonFastAPIServerCodegen. Example patch:
index e7740fe2..2fde467e 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java
@@ -54,6 +54,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
protected boolean hasModelsToImport = Boolean.FALSE;
protected String mapNumberTo = "Union[StrictFloat, StrictInt]";
protected Map<Character, String> regexModifiers;
+ protected boolean haveToUpdateApiResponsesImports = false;
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
// map of set (model imports)
@@ -1241,7 +1242,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
// update typing import for operation responses type
// only python-fastapi needs this at the moment
- if (this instanceof PythonFastAPIServerCodegen) {
+ if (haveToUpdateApiResponsesImports) {
for (CodegenResponse response : operation.responses) {
// Not interested in the result, only in the update of the imports
getPydanticType(
diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFastAPIServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFastAPIServerCodegen.java
index 802cd435..736282eb 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFastAPIServerCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFastAPIServerCodegen.java
@@ -117,6 +117,7 @@ public class PythonFastAPIServerCodegen extends AbstractPythonCodegen {
typeMapping.put("array", "List");
typeMapping.put("map", "Dict");
+ haveToUpdateApiResponsesImports = true;
outputFolder = "generated-code" + File.separator + NAME;
modelTemplateFiles.put("model.mustache", ".py");
apiTemplateFiles.put("api.mustache", ".py");
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java, around the operation-response import handling, then compare PythonFastAPIServerCodegen.java. Check how derived Python generators handle missing 400 and 422 response model imports. Done means derived classes can select this behavior and generated code imports all models required by API responses.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100