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

updated date-time string format regexp to fully comply to standard #493

Merged
merged 1 commit into from
Feb 21, 2022
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
2 changes: 1 addition & 1 deletion openapi3/schema_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func init() {
DefineStringFormat("date", `^[0-9]{4}-(0[0-9]|10|11|12)-([0-2][0-9]|30|31)$`)

// date-time
DefineStringFormat("date-time", `^[0-9]{4}-(0[0-9]|10|11|12)-([0-2][0-9]|30|31)T[0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`)
DefineStringFormat("date-time", `^[0-9]{4}-(0[0-9]|10|11|12)-([0-2][0-9]|30|31)T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`)

}

Expand Down
41 changes: 41 additions & 0 deletions openapi3/schema_issue492_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package openapi3

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestIssue492(t *testing.T) {
spec := []byte(`components:
schemas:
Server:
properties:
time:
$ref: "#/components/schemas/timestamp"
name:
type: string
type: object
timestamp:
type: string
format: date-time
openapi: "3.0.1"
`)

s, err := NewLoader().LoadFromData(spec)
require.NoError(t, err)

// verify that the expected format works
err = s.Components.Schemas["Server"].Value.VisitJSON(map[string]interface{}{
"name": "kin-openapi",
"time": "2001-02-03T04:05:06.789Z",
})
require.NoError(t, err)

// verify that the issue is fixed
err = s.Components.Schemas["Server"].Value.VisitJSON(map[string]interface{}{
"name": "kin-openapi",
"time": "2001-02-03T04:05:06:789Z",
})
require.EqualError(t, err, "Error at \"/time\": string doesn't match the format \"date-time\" (regular expression \"^[0-9]{4}-(0[0-9]|10|11|12)-([0-2][0-9]|30|31)T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]+)?(Z|(\\+|-)[0-9]{2}:[0-9]{2})?$\")\nSchema:\n {\n \"format\": \"date-time\",\n \"type\": \"string\"\n }\n\nValue:\n \"2001-02-03T04:05:06:789Z\"\n")
}