Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): format all Date, LocalDate and OffsetDateTimes in iso format #722

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -19,6 +23,8 @@
@RequiredArgsConstructor
public class DefaultSchemaWalker<T, R> implements SchemaWalker<R> {

private static final SimpleDateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

Boolean DEFAULT_BOOLEAN_EXAMPLE = true;

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

// value is represented in their native type
if (exampleValue instanceof Boolean) {
return exampleValueGenerator.createBooleanExample((Boolean) exampleValue, schema);
} else if (exampleValue instanceof Number) {
double doubleValue = ((Number) exampleValue).doubleValue();
if (exampleValue instanceof Boolean booleanValue) {
return exampleValueGenerator.createBooleanExample(booleanValue, schema);
}

if (exampleValue instanceof Number numberValue) {
double doubleValue = numberValue.doubleValue();

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

if (exampleValue instanceof Date exampleDate) { // in case of LocalDate, swagger-parser converts it into a Date
String formatted = ISO_DATE_FORMAT.format(exampleDate);
return exampleValueGenerator.createStringExample(formatted, schema);
}

if (exampleValue instanceof OffsetDateTime exampleOffsetDateTime) {
String formatted = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(exampleOffsetDateTime);
return exampleValueGenerator.createStringExample(formatted, schema);
}

try {
// value (i.e. OffsetDateTime) is represented as string
return exampleValueGenerator.createStringExample(exampleValue.toString(), schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -132,6 +133,9 @@ private static class DocumentedSimpleFoo {
@Schema(example = "2000-01-01T02:00:00+02:00", requiredMode = Schema.RequiredMode.REQUIRED)
private OffsetDateTime dt;

@Schema(example = "2024-04-24")
private LocalDate ld;

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
private SimpleFoo f;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -146,6 +147,9 @@ private static class DocumentedSimpleFoo {
@Schema(example = "2000-01-01T02:00:00+02:00", requiredMode = Schema.RequiredMode.REQUIRED)
private OffsetDateTime dt;

@Schema(example = "2024-04-24")
private LocalDate ld;

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
private SimpleFoo f;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -137,6 +138,9 @@ private static class DocumentedSimpleFoo {
@Schema(example = "2000-01-01T02:00:00+02:00", requiredMode = Schema.RequiredMode.REQUIRED)
private OffsetDateTime dt;

@Schema(example = "2024-04-24")
private LocalDate ld;

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
private SimpleFoo f;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
"f" : {
"$ref" : "#/components/schemas/io.github.springwolf.core.asyncapi.components.DefaultJsonComponentsServiceTest$SimpleFoo"
},
"ld" : {
"type" : "string",
"format" : "date",
"examples" : [ "2024-04-24T00:00:00.000+00:00" ]
},
"ls_plain" : {
"type" : "array",
"description" : "List without example",
Expand Down Expand Up @@ -50,11 +55,12 @@
"description" : "foo model",
"examples" : [ {
"bi" : 0,
"dt" : "2000-01-01T02:00+02:00",
"dt" : "2000-01-01T02:00:00+02:00",
"f" : {
"b" : true,
"s" : "string"
},
"ld" : "2024-04-24",
"ls_plain" : [ "string" ],
"mss" : {
"key1" : "value1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
"f" : {
"$ref" : "#/components/schemas/io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$SimpleFoo"
},
"ld" : {
"type" : "string",
"format" : "date",
"examples" : [ "2024-04-24T00:00:00.000+00:00" ]
},
"ls_plain" : {
"type" : "array",
"description" : "List without example",
Expand Down Expand Up @@ -46,7 +51,7 @@
}
},
"description" : "foo model",
"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>" ],
"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>" ],
"required" : [ "dt", "f", "s" ]
},
"io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$SimpleFoo" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
"f" : {
"$ref" : "#/components/schemas/io.github.springwolf.core.asyncapi.components.DefaultYamlComponentsServiceTest$SimpleFoo"
},
"ld" : {
"type" : "string",
"format" : "date",
"examples" : [ "2024-04-24T00:00:00.000+00:00" ]
},
"ls_plain" : {
"type" : "array",
"description" : "List without example",
Expand Down Expand Up @@ -48,7 +53,7 @@
}
},
"description" : "foo model",
"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" ],
"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" ],
"required" : [ "dt", "f", "s" ]
},
"io.github.springwolf.core.asyncapi.components.DefaultYamlComponentsServiceTest$SimpleFoo" : {
Expand Down