Skip to content

Commit 716f304

Browse files
committed
Fix RFC3339 validation
OpenAPI specifies the format `date-time` as RFC3339 compliant. In a RFC3339 `date-time` the `time-offset` is a required component. The current RFC3339 validation regex defines the `time-offset` as an optional component. This leads to errors where a timestamp is considered valid by the library but the value still cannot be parsed into a `time.Time`. This change makes the `time-offset` a required component. Relevant tests are changed accordingly. Links: - https://datatracker.ietf.org/doc/html/rfc3339#section-5.6 Fixes: #1031
1 parent f53e403 commit 716f304

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

.github/docs/openapi3.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838
FormatOfStringDate = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])$`
3939

4040
// FormatOfStringDateTime is a RFC3339 date-time format regexp, for example "2017-07-21T17:32:28Z".
41-
FormatOfStringDateTime = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])T([0-1][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`
41+
FormatOfStringDateTime = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])T([0-1][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})$`
4242
)
4343
const (
4444
SerializationSimple = "simple"

openapi3/schema_formats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const (
4444
FormatOfStringDate = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])$`
4545

4646
// FormatOfStringDateTime is a RFC3339 date-time format regexp, for example "2017-07-21T17:32:28Z".
47-
FormatOfStringDateTime = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])T([0-1][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`
47+
FormatOfStringDateTime = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])T([0-1][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})$`
4848
)
4949

5050
func init() {

openapi3/schema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ var schemaExamples = []schemaExample{
489489
"format": "date-time",
490490
},
491491
AllValid: []any{
492-
"2017-12-31T11:59:59",
493492
"2017-12-31T11:59:59Z",
494493
"2017-12-31T11:59:59-11:30",
495494
"2017-12-31T11:59:59+11:30",
@@ -503,6 +502,7 @@ var schemaExamples = []schemaExample{
503502
"2017-12-31T11:59:59\n",
504503
"2017-12-31T11:59:59.+11:30",
505504
"2017-12-31T11:59:59.Z",
505+
"2017-12-31T11:59:59",
506506
},
507507
},
508508

openapi3filter/validation_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,14 @@ func TestFilter(t *testing.T) {
248248
// Test query parameter openapi3filter
249249
req = ExampleRequest{
250250
Method: "POST",
251-
URL: "http://example.com/api/prefix/v/suffix?queryArgAnyOf=ae&queryArgOneOf=ac&queryArgAllOf=2017-12-31T11:59:59",
251+
URL: "http://example.com/api/prefix/v/suffix?queryArgAnyOf=ae&queryArgOneOf=ac&queryArgAllOf=2017-12-31T11:59:59Z",
252252
}
253253
err = expect(req, resp)
254254
require.NoError(t, err)
255255

256256
req = ExampleRequest{
257257
Method: "POST",
258-
URL: "http://example.com/api/prefix/v/suffix?queryArgAnyOf=2017-12-31T11:59:59",
258+
URL: "http://example.com/api/prefix/v/suffix?queryArgAnyOf=2017-12-31T11:59:59Z",
259259
}
260260
err = expect(req, resp)
261261
require.NoError(t, err)
@@ -269,7 +269,7 @@ func TestFilter(t *testing.T) {
269269

270270
req = ExampleRequest{
271271
Method: "POST",
272-
URL: "http://example.com/api/prefix/v/suffix?queryArgOneOf=2017-12-31T11:59:59",
272+
URL: "http://example.com/api/prefix/v/suffix?queryArgOneOf=2017-12-31T11:59:59Z",
273273
}
274274
err = expect(req, resp)
275275
require.IsType(t, &RequestError{}, err)

0 commit comments

Comments
 (0)