diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java index 2e29750d05c6..c466730a1d74 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java @@ -31,6 +31,7 @@ import io.swagger.v3.oas.models.parameters.RequestBody; import io.swagger.v3.oas.models.servers.Server; import io.swagger.v3.parser.util.SchemaTypeUtil; +import java.text.SimpleDateFormat; import lombok.Getter; import lombok.Setter; import org.apache.commons.io.FilenameUtils; @@ -71,6 +72,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code private final Logger LOGGER = LoggerFactory.getLogger(AbstractJavaCodegen.class); private static final String ARTIFACT_VERSION_DEFAULT_VALUE = "1.0.0"; + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT); public static final String DEFAULT_LIBRARY = ""; public static final String DATE_LIBRARY = "dateLibrary"; @@ -1638,6 +1640,9 @@ public void setParameterExampleValue(CodegenParameter p) { @Override public String toExampleValue(Schema p) { if (p.getExample() != null) { + if (p.getExample() instanceof Date) { + return DATE_FORMAT.format(p.getExample()); + } return escapeText(p.getExample().toString()); } else { return null; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java index 8e2a0bb556e1..5a004f257cbe 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java @@ -965,4 +965,11 @@ public void removeAnnotationsTest() { Assert.assertEquals(codegen.removeAnnotations("List<@Valid Pet>"), "List"); } + @Test(description = "test generated example values for string properties") + public void testGeneratedExampleValues() { + final OpenAPI openAPI = FLATTENED_SPEC.get("3_0/spring/date-time-parameter-types-for-testing"); + codegen.setOpenAPI(openAPI); + DateSchema dateSchema = (DateSchema) openAPI.getPaths().get("/thingy/{date}").getPost().getParameters().get(0).getSchema(); + Assert.assertTrue(codegen.escapeQuotationMark(codegen.toExampleValue(dateSchema)).matches("2021-01-01")); + } }