Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't test 2.0 spec so no need to keep this sample folder and yaml config file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated - thanks!

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi @OpenAPITools/generator-core-team

}
}
}
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