Skip to content

Commit 9b62b9b

Browse files
authored
fix: ExampleGenerator correctly produces YYYY-MM-dd format for date with examples (#17495)
When an example was specified for a property with the `date` format, it's example would be generated to something like `2024-01-01T00:00:00.000+00:00` or `Sat Jan 30 01:00:00 CET 2021`. It's because the resolved type for a Date example is a DateSchema, and a DateSchema uses `java.util.Date` to represents the Date. Which gets printed using .toString() not in the YYYY-MM-dd format. Fixes #17494 #15342
1 parent e69c526 commit 9b62b9b

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.slf4j.LoggerFactory;
2828

2929
import java.math.BigDecimal;
30+
import java.text.SimpleDateFormat;
3031
import java.util.*;
3132

3233
public class ExampleGenerator {
@@ -36,6 +37,8 @@ public class ExampleGenerator {
3637
private static final String MIME_TYPE_JSON = "application/json";
3738
private static final String MIME_TYPE_XML = "application/xml";
3839

40+
protected final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
41+
3942
private static final String EXAMPLE = "example";
4043
private static final String CONTENT_TYPE = "contentType";
4144
private static final String GENERATED_CONTENT_TYPE = "generatedContentType";
@@ -54,6 +57,7 @@ public ExampleGenerator(Map<String, Schema> examples, OpenAPI openAPI) {
5457
this.openAPI = openAPI;
5558
// use a fixed seed to make the "random" numbers reproducible.
5659
this.random = new Random("ExampleGenerator".hashCode());
60+
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
5761
}
5862

5963
public List<Map<String, String>> generateFromResponseSchema(String statusCode, Schema responseSchema, Set<String> producesInfo) {
@@ -231,6 +235,11 @@ private Object resolvePropertyToExample(String propertyName, String mediaType, S
231235
LOGGER.debug("Resolving example for property {}...", property);
232236
if (property.getExample() != null) {
233237
LOGGER.debug("Example set in openapi spec, returning example: '{}'", property.getExample().toString());
238+
// When a property is of type Date, we want to ensure that we're returning a formatted Date.
239+
// And not returning the Date object directly.
240+
if (property.getExample() instanceof Date) {
241+
return DATE_FORMAT.format(property.getExample());
242+
}
234243
return property.getExample();
235244
} else if (ModelUtils.isBooleanSchema(property)) {
236245
Object defaultValue = property.getDefault();

modules/openapi-generator/src/test/java/org/openapitools/codegen/ExampleGeneratorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,35 @@ public void generateFromResponseSchemaWithPrimitiveType() {
3939
assertEquals("200", examples.get(0).get("statusCode"));
4040
}
4141

42+
@Test
43+
public void generateFromResponseSchemaWithDateFormat() {
44+
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/example_generator_test.yaml");
45+
46+
new InlineModelResolver().flatten(openAPI);
47+
48+
ExampleGenerator exampleGenerator = new ExampleGenerator(openAPI.getComponents().getSchemas(), openAPI);
49+
Set<String> mediaTypeKeys = new TreeSet<>();
50+
mediaTypeKeys.add("application/json");
51+
List<Map<String, String>> examples = exampleGenerator.generateFromResponseSchema(
52+
"200",
53+
openAPI
54+
.getPaths()
55+
.get("/generate_from_response_schema_with_date_format")
56+
.getGet()
57+
.getResponses()
58+
.get("200")
59+
.getContent()
60+
.get("application/json")
61+
.getSchema(),
62+
mediaTypeKeys
63+
);
64+
65+
assertEquals(1, examples.size());
66+
assertEquals("application/json", examples.get(0).get("contentType"));
67+
assertEquals(String.format(Locale.ROOT, "{%n \"date_with_example\" : \"2024-01-01\",%n \"date_without_example\" : \"2000-01-23\"%n}"), examples.get(0).get("example"));
68+
assertEquals("200", examples.get(0).get("statusCode"));
69+
}
70+
4271
@Test
4372
public void generateFromResponseSchemaWithNoExample() {
4473
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/example_generator_test.yaml");

modules/openapi-generator/src/test/resources/3_0/example_generator_test.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ paths:
2626
schema:
2727
type: string
2828
example: primitive type example value
29+
/generate_from_response_schema_with_date_format:
30+
get:
31+
operationId: generateFromResponseSchemaWithDateFormat
32+
responses:
33+
'200':
34+
description: successful operation
35+
content:
36+
application/json:
37+
schema:
38+
$ref: '#/components/schemas/DateSchema'
2939
/generate_from_response_schema_with_array_of_model:
3040
get:
3141
operationId: generateFromResponseSchemaWithArrayOfModel
@@ -93,6 +103,16 @@ paths:
93103
$ref: '#/components/schemas/ExampleOneOfSchema'
94104
components:
95105
schemas:
106+
DateSchema:
107+
type: object
108+
properties:
109+
date_with_example:
110+
type: string
111+
format: date
112+
example: 2024-01-01
113+
date_without_example:
114+
type: string
115+
format: date
96116
StringSchema:
97117
type: string
98118
example: string schema example value

0 commit comments

Comments
 (0)