From d160b827dea303648c1674ebe90ad881fb1c89ef Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 21 Jun 2023 16:36:45 +0800 Subject: [PATCH] Allow better default value in parameters using codegen property (#15882) * allow better default value in parameters using codegen property * update --- .../openapitools/codegen/DefaultCodegen.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 59260c803e2..10050e57963 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -2179,6 +2179,21 @@ public class DefaultCodegen implements CodegenConfig { return toDefaultValue(schema); } + /** + * Return the default value of the parameter + *

+ * Return null if you do NOT want a default value. + * Any non-null value will cause {{#defaultValue} check to pass. + * + * @param codegenProperty Codegen Property + * @param schema Parameter schema + * @return string presentation of the default value of the parameter + */ + public String toDefaultParameterValue(CodegenProperty codegenProperty, Schema schema) { + // by default works as original method to be backward compatible + return toDefaultParameterValue(schema); + } + /** * Return property value depending on property type. * @@ -5120,9 +5135,6 @@ public class DefaultCodegen implements CodegenConfig { codegenParameter.isNullable = true; } - // set default value - codegenParameter.defaultValue = toDefaultParameterValue(parameterSchema); - if (parameter.getStyle() != null) { codegenParameter.style = parameter.getStyle().toString(); codegenParameter.isDeepObject = Parameter.StyleEnum.DEEPOBJECT == parameter.getStyle(); @@ -5290,6 +5302,9 @@ public class DefaultCodegen implements CodegenConfig { } } + // set default value + codegenParameter.defaultValue = toDefaultParameterValue(codegenProperty, parameterSchema); + finishUpdatingParameter(codegenParameter, parameter); return codegenParameter; } @@ -6901,7 +6916,6 @@ public class DefaultCodegen implements CodegenConfig { codegenParameter.baseType = codegenProperty.baseType; codegenParameter.dataType = codegenProperty.dataType; - codegenParameter.defaultValue = toDefaultParameterValue(propertySchema); codegenParameter.baseName = codegenProperty.baseName; codegenParameter.paramName = toParamName(codegenParameter.baseName); codegenParameter.dataFormat = codegenProperty.dataFormat; @@ -6912,6 +6926,9 @@ public class DefaultCodegen implements CodegenConfig { codegenParameter._enum = codegenProperty._enum; codegenParameter.allowableValues = codegenProperty.allowableValues; + // set default value + codegenParameter.defaultValue = toDefaultParameterValue(codegenProperty, propertySchema); + if (ModelUtils.isFileSchema(ps) && !ModelUtils.isStringSchema(ps)) { // swagger v2 only, type file codegenParameter.isFile = true; @@ -7046,7 +7063,10 @@ public class DefaultCodegen implements CodegenConfig { imports.add(codegenProperty.complexType); } } + + // set example value setParameterExampleValue(codegenParameter); + // set nullable setParameterNullable(codegenParameter, codegenProperty);