Skip to content

Commit

Permalink
[Python] Add enum support when building default values for model prop…
Browse files Browse the repository at this point in the history
…erties when using `$ref` (OpenAPITools#18796)

* Add enum support when building default values for model properties

* Update enum handling for Python for enum references

* Remove unused method

* Update mustaches for FastAPI, Pydantic, and Python for default values

* Address PR feedback and rebase main

* Remove old 2_0 samples
  • Loading branch information
welshm authored Jun 8, 2024
1 parent 5adf1ff commit a5a9958
Show file tree
Hide file tree
Showing 27 changed files with 1,441 additions and 841 deletions.
2 changes: 1 addition & 1 deletion bin/configs/python-flask.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
generatorName: python-flask
outputDir: samples/server/petstore/python-flask
inputSpec: modules/openapi-generator/src/test/resources/2_0/python-flask/petstore.yaml
inputSpec: modules/openapi-generator/src/test/resources/3_0/python-flask/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/python-flask
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,18 @@ public String toEnumDefaultValue(String value, String datatype) {
return datatype + "." + value;
}

/**
* Return the enum default value in the language specified format
*
* @param property The codegen property to create the default for.
* @param value Enum variable name
* @return the default value for the enum
*/
public String toEnumDefaultValue(CodegenProperty property, String value) {
// Use the datatype with the value.
return toEnumDefaultValue(value, property.datatypeWithEnum);
}

/**
* Return the enum value in the language specified format
* e.g. status becomes "status"
Expand Down Expand Up @@ -6645,7 +6657,7 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
}
}
if (enumName != null) {
var.defaultValue = toEnumDefaultValue(enumName, var.datatypeWithEnum);
var.defaultValue = toEnumDefaultValue(var, enumName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ public String escapeReservedWord(String name) {
*/
@Override
public String toDefaultValue(Schema p) {
// If the Schema is a $ref, get the default value from the reference.
String ref = ModelUtils.getSimpleRef(p.get$ref());
if (ref != null) {
Schema referencedSchema = ModelUtils.getSchemas(this.openAPI).get(ref);
if (referencedSchema != null) {
String defaultValue = toDefaultValue(referencedSchema);
if (defaultValue != null) {
return defaultValue;
}
// No default was found for the $ref, so see if one has been defined locally.
}
}
if (ModelUtils.isBooleanSchema(p)) {
if (p.getDefault() != null) {
if (!Boolean.valueOf(p.getDefault().toString()))
Expand All @@ -217,6 +229,14 @@ public String toDefaultValue(Schema p) {
if (p.getDefault() != null) {
return p.getDefault().toString();
}
} else if (ModelUtils.isEnumSchema(p)) {
// Enum must come before string to use enum reference!
if (p.getDefault() != null) {
String defaultValue = String.valueOf(p.getDefault());
if (defaultValue != null) {
return defaultValue;
}
}
} else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) {
String defaultValue = String.valueOf(p.getDefault());
Expand All @@ -237,7 +257,6 @@ public String toDefaultValue(Schema p) {
return null;
}
}

return null;
}

Expand Down Expand Up @@ -1085,7 +1104,9 @@ public String toEnumVariableName(String name, String datatype) {
}

// remove quote e.g. 'abc' => abc
name = name.substring(1, name.length() - 1);
if (name.startsWith("'") && name.endsWith("'")) {
name = name.substring(1, name.length() - 1);
}

if (name.length() == 0) {
return "EMPTY";
Expand Down Expand Up @@ -1396,7 +1417,13 @@ public String toEnumValue(String value, String datatype) {
}

@Override
public String toEnumDefaultValue(String value, String datatype) {
public String toEnumDefaultValue(CodegenProperty property, String value) {
if (property.isEnumRef) {
// Determine if it's a string by checking if the value has already been encapsulated in single quotes.
String dataType = (value.startsWith("'") && value.endsWith("'")) ? "string" : "int";
// If the property is an enum reference, then use the fully qualified name with the data type.
return property.dataType + "." + toEnumVariableName(value, dataType);
}
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
"{{{baseName}}}": {{{dataType}}}.from_dict(obj.get("{{{baseName}}}")) if obj.get("{{{baseName}}}") is not None else None{{^-last}},{{/-last}}
{{/isEnumOrRef}}
{{#isEnumOrRef}}
"{{{baseName}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
"{{{baseName}}}": obj.get("{{{baseName}}}"){{#defaultValue}} if obj.get("{{baseName}}") is not None else {{defaultValue}}{{/defaultValue}}{{^-last}},{{/-last}}
{{/isEnumOrRef}}
{{/isPrimitiveType}}
{{#isPrimitiveType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
"{{{name}}}": {{{dataType}}}.from_dict(obj.get("{{{baseName}}}")) if obj.get("{{{baseName}}}") is not None else None{{^-last}},{{/-last}}
{{/isEnumOrRef}}
{{#isEnumOrRef}}
"{{{name}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
"{{{name}}}": obj.get("{{{baseName}}}"){{#defaultValue}} if obj.get("{{baseName}}") is not None else {{defaultValue}}{{/defaultValue}}{{^-last}},{{/-last}}
{{/isEnumOrRef}}
{{/isPrimitiveType}}
{{#isPrimitiveType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
"{{{baseName}}}": {{{dataType}}}.from_dict(obj["{{{baseName}}}"]) if obj.get("{{{baseName}}}") is not None else None{{^-last}},{{/-last}}
{{/isEnumOrRef}}
{{#isEnumOrRef}}
"{{{baseName}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
"{{{baseName}}}": obj.get("{{{baseName}}}"){{#defaultValue}} if obj.get("{{baseName}}") is not None else {{defaultValue}}{{/defaultValue}}{{^-last}},{{/-last}}
{{/isEnumOrRef}}
{{/isPrimitiveType}}
{{#isPrimitiveType}}
Expand Down
Loading

0 comments on commit a5a9958

Please sign in to comment.