Skip to content

Commit 9170e10

Browse files
authored
fix enum in python flask (#16576)
1 parent 4260c7a commit 9170e10

10 files changed

Lines changed: 774 additions & 33 deletions

File tree

bin/configs/python-flask.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
generatorName: python-flask
22
outputDir: samples/server/petstore/python-flask
3-
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
3+
inputSpec: modules/openapi-generator/src/test/resources/2_0/python-flask/petstore.yaml
44
templateDir: modules/openapi-generator/src/main/resources/python-flask

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,4 +1981,17 @@ public String toEnumValue(String value, String datatype) {
19811981
public String toEnumDefaultValue(String value, String datatype) {
19821982
return value;
19831983
}
1984+
1985+
/**
1986+
* checks if the data should be classified as "string" in enum
1987+
* e.g. double in C# needs to be double-quoted (e.g. "2.8") by treating it as a string
1988+
* In the future, we may rename this function to "isEnumString"
1989+
*
1990+
* @param dataType data type
1991+
* @return true if it's a enum string
1992+
*/
1993+
@Override
1994+
public boolean isDataTypeString(String dataType) {
1995+
return "str".equals(dataType);
1996+
}
19841997
}

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,9 @@
1616

1717
package org.openapitools.codegen.languages;
1818

19-
import static org.openapitools.codegen.utils.StringUtils.camelize;
20-
2119
import java.io.File;
2220
import java.io.IOException;
23-
import java.util.ArrayList;
24-
import java.util.Collection;
25-
import java.util.Collections;
26-
import java.util.HashMap;
27-
import java.util.List;
28-
import java.util.Map;
29-
import java.util.Set;
21+
import java.util.*;
3022

3123
import org.apache.commons.lang3.StringUtils;
3224
import org.openapitools.codegen.CliOption;
@@ -69,6 +61,8 @@
6961
import io.swagger.v3.oas.models.parameters.RequestBody;
7062
import io.swagger.v3.oas.models.security.SecurityScheme;
7163

64+
import static org.openapitools.codegen.utils.StringUtils.*;
65+
7266
public abstract class AbstractPythonConnexionServerCodegen extends AbstractPythonCodegen implements CodegenConfig {
7367
private static class PythonBooleanSerializer extends JsonSerializer<Boolean> {
7468
@Override
@@ -178,7 +172,7 @@ public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fi
178172
cliOptions.add(new CliOption(USE_PYTHON_SRC_ROOT_IN_IMPORTS, "include pythonSrcRoot in import namespaces.").
179173
defaultValue("false"));
180174
cliOptions.add(new CliOption(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "generates test under the pythonSrcRoot folder.")
181-
.defaultValue("false"));
175+
.defaultValue("false"));
182176
}
183177

184178
protected void addSupportingFiles() {
@@ -333,7 +327,6 @@ public String toApiName(String name) {
333327
}
334328

335329

336-
337330
@Override
338331
public String toApiTestFilename(String name) {
339332
return "test_" + toApiFilename(name);
@@ -636,12 +629,6 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
636629
postProcessPattern(property.pattern, property.vendorExtensions);
637630
}
638631

639-
@Override
640-
public ModelsMap postProcessModels(ModelsMap objs) {
641-
// process enum in models
642-
return postProcessModelsEnum(objs);
643-
}
644-
645632
@Override
646633
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
647634
Map<String, ModelsMap> result = super.postProcessAllModels(objs);

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -464,20 +464,6 @@ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Sc
464464
}
465465
}
466466

467-
468-
/**
469-
* checks if the data should be classified as "string" in enum
470-
* e.g. double in C# needs to be double-quoted (e.g. "2.8") by treating it as a string
471-
* In the future, we may rename this function to "isEnumString"
472-
*
473-
* @param dataType data type
474-
* @return true if it's a enum string
475-
*/
476-
@Override
477-
public boolean isDataTypeString(String dataType) {
478-
return "str".equals(dataType);
479-
}
480-
481467
@Override
482468
public String escapeReservedWord(String name) {
483469
if (this.reservedWordsMappings().containsKey(name)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class {{classname}}(Model):
2929
allowed enum values
3030
"""
3131
{{#enumVars}}
32-
{{name}} = {{{value}}}{{^-last}}
32+
{{{name}}} = {{{value}}}{{^-last}}
3333
{{/-last}}
3434
{{/enumVars}}
3535
{{/allowableValues}}

0 commit comments

Comments
 (0)