Skip to content

Commit efabf30

Browse files
authored
fix(core): format all Date, LocalDate and OffsetDateTimes in iso format (#722)
1 parent 1d1d081 commit efabf30

File tree

7 files changed

+53
-7
lines changed

7 files changed

+53
-7
lines changed

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalker.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
import lombok.RequiredArgsConstructor;
88
import lombok.extern.slf4j.Slf4j;
99

10+
import java.text.SimpleDateFormat;
11+
import java.time.OffsetDateTime;
12+
import java.time.format.DateTimeFormatter;
1013
import java.util.Comparator;
14+
import java.util.Date;
1115
import java.util.HashSet;
1216
import java.util.List;
1317
import java.util.Map;
@@ -19,6 +23,8 @@
1923
@RequiredArgsConstructor
2024
public class DefaultSchemaWalker<T, R> implements SchemaWalker<R> {
2125

26+
private static final SimpleDateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
27+
2228
Boolean DEFAULT_BOOLEAN_EXAMPLE = true;
2329

2430
String DEFAULT_STRING_EXAMPLE = "string";
@@ -110,10 +116,12 @@ private Optional<T> getExampleValueFromSchemaAnnotation(Schema schema) {
110116
}
111117

112118
// value is represented in their native type
113-
if (exampleValue instanceof Boolean) {
114-
return exampleValueGenerator.createBooleanExample((Boolean) exampleValue, schema);
115-
} else if (exampleValue instanceof Number) {
116-
double doubleValue = ((Number) exampleValue).doubleValue();
119+
if (exampleValue instanceof Boolean booleanValue) {
120+
return exampleValueGenerator.createBooleanExample(booleanValue, schema);
121+
}
122+
123+
if (exampleValue instanceof Number numberValue) {
124+
double doubleValue = numberValue.doubleValue();
117125

118126
// Check if it's an integer (whole number)
119127
if (doubleValue == (int) doubleValue) {
@@ -123,6 +131,16 @@ private Optional<T> getExampleValueFromSchemaAnnotation(Schema schema) {
123131
return exampleValueGenerator.createDoubleExample(doubleValue, schema);
124132
}
125133

134+
if (exampleValue instanceof Date exampleDate) { // in case of LocalDate, swagger-parser converts it into a Date
135+
String formatted = ISO_DATE_FORMAT.format(exampleDate);
136+
return exampleValueGenerator.createStringExample(formatted, schema);
137+
}
138+
139+
if (exampleValue instanceof OffsetDateTime exampleOffsetDateTime) {
140+
String formatted = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(exampleOffsetDateTime);
141+
return exampleValueGenerator.createStringExample(formatted, schema);
142+
}
143+
126144
try {
127145
// value (i.e. OffsetDateTime) is represented as string
128146
return exampleValueGenerator.createStringExample(exampleValue.toString(), schema);

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultJsonComponentsServiceTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.InputStream;
2525
import java.math.BigInteger;
2626
import java.nio.charset.StandardCharsets;
27+
import java.time.LocalDate;
2728
import java.time.OffsetDateTime;
2829
import java.util.ArrayList;
2930
import java.util.List;
@@ -132,6 +133,9 @@ private static class DocumentedSimpleFoo {
132133
@Schema(example = "2000-01-01T02:00:00+02:00", requiredMode = Schema.RequiredMode.REQUIRED)
133134
private OffsetDateTime dt;
134135

136+
@Schema(example = "2024-04-24")
137+
private LocalDate ld;
138+
135139
@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
136140
private SimpleFoo f;
137141

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultXmlComponentsServiceTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.InputStream;
2828
import java.math.BigInteger;
2929
import java.nio.charset.StandardCharsets;
30+
import java.time.LocalDate;
3031
import java.time.OffsetDateTime;
3132
import java.util.ArrayList;
3233
import java.util.List;
@@ -146,6 +147,9 @@ private static class DocumentedSimpleFoo {
146147
@Schema(example = "2000-01-01T02:00:00+02:00", requiredMode = Schema.RequiredMode.REQUIRED)
147148
private OffsetDateTime dt;
148149

150+
@Schema(example = "2024-04-24")
151+
private LocalDate ld;
152+
149153
@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
150154
private SimpleFoo f;
151155

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultYamlComponentsServiceTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.InputStream;
2727
import java.math.BigInteger;
2828
import java.nio.charset.StandardCharsets;
29+
import java.time.LocalDate;
2930
import java.time.OffsetDateTime;
3031
import java.util.ArrayList;
3132
import java.util.List;
@@ -137,6 +138,9 @@ private static class DocumentedSimpleFoo {
137138
@Schema(example = "2000-01-01T02:00:00+02:00", requiredMode = Schema.RequiredMode.REQUIRED)
138139
private OffsetDateTime dt;
139140

141+
@Schema(example = "2024-04-24")
142+
private LocalDate ld;
143+
140144
@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
141145
private SimpleFoo f;
142146

springwolf-core/src/test/resources/schemas/documented-definitions.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"f" : {
1414
"$ref" : "#/components/schemas/io.github.springwolf.core.asyncapi.components.DefaultJsonComponentsServiceTest$SimpleFoo"
1515
},
16+
"ld" : {
17+
"type" : "string",
18+
"format" : "date",
19+
"examples" : [ "2024-04-24T00:00:00.000+00:00" ]
20+
},
1621
"ls_plain" : {
1722
"type" : "array",
1823
"description" : "List without example",
@@ -50,11 +55,12 @@
5055
"description" : "foo model",
5156
"examples" : [ {
5257
"bi" : 0,
53-
"dt" : "2000-01-01T02:00+02:00",
58+
"dt" : "2000-01-01T02:00:00+02:00",
5459
"f" : {
5560
"b" : true,
5661
"s" : "string"
5762
},
63+
"ld" : "2024-04-24",
5864
"ls_plain" : [ "string" ],
5965
"mss" : {
6066
"key1" : "value1"

springwolf-core/src/test/resources/schemas/xml/documented-definitions-xml.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"f" : {
1414
"$ref" : "#/components/schemas/io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$SimpleFoo"
1515
},
16+
"ld" : {
17+
"type" : "string",
18+
"format" : "date",
19+
"examples" : [ "2024-04-24T00:00:00.000+00:00" ]
20+
},
1621
"ls_plain" : {
1722
"type" : "array",
1823
"description" : "List without example",
@@ -46,7 +51,7 @@
4651
}
4752
},
4853
"description" : "foo model",
49-
"examples" : [ "<DocumentedSimpleFoo><bi>0</bi><dt>2000-01-01T02:00+02:00</dt><f><b>true</b><s>string</s></f><ls_plain>string</ls_plain><mss/><mss_plain/><s>s value</s></DocumentedSimpleFoo>" ],
54+
"examples" : [ "<DocumentedSimpleFoo><bi>0</bi><dt>2000-01-01T02:00:00+02:00</dt><f><b>true</b><s>string</s></f><ld>2024-04-24</ld><ls_plain>string</ls_plain><mss/><mss_plain/><s>s value</s></DocumentedSimpleFoo>" ],
5055
"required" : [ "dt", "f", "s" ]
5156
},
5257
"io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$SimpleFoo" : {

springwolf-core/src/test/resources/schemas/yaml/documented-definitions-yaml.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"f" : {
1414
"$ref" : "#/components/schemas/io.github.springwolf.core.asyncapi.components.DefaultYamlComponentsServiceTest$SimpleFoo"
1515
},
16+
"ld" : {
17+
"type" : "string",
18+
"format" : "date",
19+
"examples" : [ "2024-04-24T00:00:00.000+00:00" ]
20+
},
1621
"ls_plain" : {
1722
"type" : "array",
1823
"description" : "List without example",
@@ -48,7 +53,7 @@
4853
}
4954
},
5055
"description" : "foo model",
51-
"examples" : [ "bi: 0\ndt: 2000-01-01T02:00+02:00\nf:\n b: true\n s: string\nls_plain:\n- string\nmss:\n key1: value1\nmss_plain: {}\ns: s value\n" ],
56+
"examples" : [ "bi: 0\ndt: 2000-01-01T02:00:00+02:00\nf:\n b: true\n s: string\nld: 2024-04-24\nls_plain:\n- string\nmss:\n key1: value1\nmss_plain: {}\ns: s value\n" ],
5257
"required" : [ "dt", "f", "s" ]
5358
},
5459
"io.github.springwolf.core.asyncapi.components.DefaultYamlComponentsServiceTest$SimpleFoo" : {

0 commit comments

Comments
 (0)