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 b24452ad9b1..556afd426fb 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 @@ -1046,14 +1046,70 @@ public class DefaultCodegen implements CodegenConfig { * * @param codegenParameter Codegen parameter */ - public void setParameterExampleValue(CodegenParameter codegenParameter) { + protected void setParameterExampleValueFromVendorExtensions(CodegenParameter codegenParameter) { + if (codegenParameter.vendorExtensions == null || !codegenParameter.vendorExtensions.containsKey("x-example")) { + return; + } + + codegenParameter.example = Json.pretty(codegenParameter.vendorExtensions.get("x-example")); + } + + /** + * Return the example value of the parameter. + * + * @param codegenParameter Codegen parameter + * @param parameter parameter + */ + protected void setParameterExampleValue(CodegenParameter codegenParameter, Parameter parameter) { + setParameterExampleValueFromVendorExtensions(codegenParameter); + if (codegenParameter.example != null) { + return; + } + + if (parameter.getExample() != null) { + codegenParameter.example = parameter.getExample().toString(); + return; + } + + setParameterExampleValue(codegenParameter); + } + + /** + * Return the example value of the parameter. + * + * @param codegenParameter Codegen parameter + * @param schema schema + */ + protected void setParameterExampleValue(CodegenParameter codegenParameter, Schema schema) { + setParameterExampleValueFromVendorExtensions(codegenParameter); + if (codegenParameter.example != null) { + return; + } + + if (schema.getExample() != null) { + codegenParameter.example = schema.getExample().toString(); + return; + } + + setParameterExampleValue(codegenParameter); + } + + /** + * Return the example value of the parameter. + * + * @param codegenParameter Codegen parameter + */ + protected void setParameterExampleValue(CodegenParameter codegenParameter) { // set the example value // if not specified in x-example, generate a default value + setParameterExampleValueFromVendorExtensions(codegenParameter); + if (codegenParameter.example != null) { + return; + } + // TODO need to revise how to obtain the example value - if (codegenParameter.vendorExtensions != null && codegenParameter.vendorExtensions.containsKey("x-example")) { - codegenParameter.example = Json.pretty(codegenParameter.vendorExtensions.get("x-example")); - } else if (Boolean.TRUE.equals(codegenParameter.isBoolean)) { + if (Boolean.TRUE.equals(codegenParameter.isBoolean)) { codegenParameter.example = "true"; } else if (Boolean.TRUE.equals(codegenParameter.isLong)) { codegenParameter.example = "789"; @@ -2744,7 +2800,7 @@ public class DefaultCodegen implements CodegenConfig { // set the parameter excample value // should be overridden by lang codegen - setParameterExampleValue(codegenParameter); + setParameterExampleValue(codegenParameter, parameter); postProcessParameter(codegenParameter); LOGGER.debug("debugging codegenParameter return: " + codegenParameter); @@ -3985,7 +4041,7 @@ public class DefaultCodegen implements CodegenConfig { /** * returns the list of MIME types the APIs can produce * - * @param openAPI + * @param openAPI OpenAPI * @param operation Operation * @return a set of MIME types */ @@ -4211,7 +4267,7 @@ public class DefaultCodegen implements CodegenConfig { } setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty); - setParameterExampleValue(codegenParameter); + setParameterExampleValue(codegenParameter, propertySchema); //TODO collectionFormat for form parameter not yet supported //codegenParameter.collectionFormat = getCollectionFormat(propertySchema); @@ -4369,7 +4425,7 @@ public class DefaultCodegen implements CodegenConfig { // set the parameter's example value // should be overridden by lang codegen - setParameterExampleValue(codegenParameter); + setParameterExampleValue(codegenParameter, schema); return codegenParameter; } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 99ed3225203..1baa382416e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -348,7 +348,7 @@ public class ModelUtils { /** * If a Schema contains a reference to an other Schema with '$ref', returns the referenced Schema or the actual Schema in the other cases. - * @param openAPI + * @param openAPI OpenAPI * @param schema potentially containing a '$ref' * @return schema without '$ref' */ @@ -374,7 +374,7 @@ public class ModelUtils { /** * If a RequestBody contains a reference to an other RequestBody with '$ref', returns the referenced RequestBody or the actual RequestBody in the other cases. - * @param openAPI + * @param openAPI OpenAPI * @param requestBody potentially containing a '$ref' * @return requestBody without '$ref' */ @@ -399,7 +399,7 @@ public class ModelUtils { /** * If a ApiResponse contains a reference to an other ApiResponse with '$ref', returns the referenced ApiResponse or the actual ApiResponse in the other cases. - * @param openAPI + * @param openAPI OpenAPI * @param apiResponse potentially containing a '$ref' * @return apiResponse without '$ref' */ diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java index 8da1965393c..bd63cedf6a4 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java @@ -72,9 +72,9 @@ public class URLPathUtils { /** * Return the port, example value 8080 - * @param url + * @param url URL * @param defaultPort if the port is not set - * @return + * @return port */ public static String getPort(URL url, int defaultPort) { return getPort(url, String.valueOf(defaultPort)); @@ -82,9 +82,9 @@ public class URLPathUtils { /** * Return the port, example value 8080 - * @param url + * @param url URL * @param defaultPort if the port is not set - * @return + * @return port */ public static String getPort(URL url, String defaultPort) { if (url == null || url.getPort() == -1) { @@ -96,7 +96,7 @@ public class URLPathUtils { /** * Return the path, example value /abcdef/xyz - * @param url + * @param url URL * @param defaultPath if the path is not empty * @return path */ @@ -110,7 +110,7 @@ public class URLPathUtils { /** * Get the protocol and the host, example value https://www.abcdef.xyz - * @param url + * @param url URL * @return protocolAndHost */ public static String getProtocolAndHost(URL url) { @@ -124,7 +124,7 @@ public class URLPathUtils { /** * Return the first complete URL from the OpenAPI specification - * @param openAPI + * @param openAPI OpenAPI * @return host */ public static String getHost(OpenAPI openAPI) {