-
-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openapi3: update date schema formats to not match months or days of '…
…00' (#1042) * Update date schema formats to not match months or days of '00' * Update schema_issue492_test.go to look for correct DateTime format in error output * Add test cases for '00' months and days in date and date-time objects * Change date/time validation test cases to use EqualError * Fix hour/minute/second matches as well - also update tests
- Loading branch information
1 parent
d819171
commit b82c647
Showing
4 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package openapi3 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var DateSpec = []byte(` | ||
components: | ||
schemas: | ||
Server: | ||
properties: | ||
date: | ||
$ref: "#/components/schemas/timestamp" | ||
name: | ||
type: string | ||
type: object | ||
timestamp: | ||
type: string | ||
format: date | ||
openapi: "3.0.1" | ||
paths: {} | ||
info: | ||
version: 1.1.1 | ||
title: title | ||
`[1:]) | ||
|
||
var DateTimeSpec = []byte(` | ||
components: | ||
schemas: | ||
Server: | ||
properties: | ||
datetime: | ||
$ref: "#/components/schemas/timestamp" | ||
name: | ||
type: string | ||
type: object | ||
timestamp: | ||
type: string | ||
format: date-time | ||
openapi: "3.0.1" | ||
paths: {} | ||
info: | ||
version: 1.1.1 | ||
title: title | ||
`[1:]) | ||
|
||
func TestDateZeroMonth(t *testing.T) { | ||
loader := NewLoader() | ||
doc, err := loader.LoadFromData(DateSpec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(loader.Context) | ||
require.NoError(t, err) | ||
|
||
err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{ | ||
"name": "kin-openapi", | ||
"date": "2001-00-03", | ||
}) | ||
require.EqualError(t, err, `Error at "/date": string doesn't match the format "date": string doesn't match pattern "`+FormatOfStringDate+`"`) | ||
} | ||
|
||
func TestDateZeroDay(t *testing.T) { | ||
loader := NewLoader() | ||
doc, err := loader.LoadFromData(DateSpec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(loader.Context) | ||
require.NoError(t, err) | ||
|
||
err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{ | ||
"name": "kin-openapi", | ||
"date": "2001-02-00", | ||
}) | ||
require.EqualError(t, err, `Error at "/date": string doesn't match the format "date": string doesn't match pattern "`+FormatOfStringDate+`"`) | ||
} | ||
|
||
func TestDateTimeZeroMonth(t *testing.T) { | ||
loader := NewLoader() | ||
doc, err := loader.LoadFromData(DateTimeSpec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(loader.Context) | ||
require.NoError(t, err) | ||
|
||
err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{ | ||
"name": "kin-openapi", | ||
"datetime": "2001-00-03T04:05:06.789Z", | ||
}) | ||
require.EqualError(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`) | ||
} | ||
|
||
func TestDateTimeZeroDay(t *testing.T) { | ||
loader := NewLoader() | ||
doc, err := loader.LoadFromData(DateTimeSpec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(loader.Context) | ||
require.NoError(t, err) | ||
|
||
err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{ | ||
"name": "kin-openapi", | ||
"datetime": "2001-02-00T04:05:06.789Z", | ||
}) | ||
require.EqualError(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`) | ||
} | ||
|
||
func TestDateTimeLeapSecond(t *testing.T) { | ||
loader := NewLoader() | ||
doc, err := loader.LoadFromData(DateTimeSpec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(loader.Context) | ||
require.NoError(t, err) | ||
|
||
err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{ | ||
"name": "kin-openapi", | ||
"datetime": "2016-12-31T23:59:60.000Z", // exact time of the most recent leap second | ||
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestDateTimeHourOutOfBounds(t *testing.T) { | ||
loader := NewLoader() | ||
doc, err := loader.LoadFromData(DateTimeSpec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(loader.Context) | ||
require.NoError(t, err) | ||
|
||
err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{ | ||
"name": "kin-openapi", | ||
"datetime": "2016-12-31T24:00:00.000Z", | ||
}) | ||
require.EqualError(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`) | ||
} | ||
|
||
func TestDateTimeMinuteOutOfBounds(t *testing.T) { | ||
loader := NewLoader() | ||
doc, err := loader.LoadFromData(DateTimeSpec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(loader.Context) | ||
require.NoError(t, err) | ||
|
||
err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{ | ||
"name": "kin-openapi", | ||
"datetime": "2016-12-31T23:60:00.000Z", | ||
}) | ||
require.EqualError(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`) | ||
} | ||
|
||
func TestDateTimeSecondOutOfBounds(t *testing.T) { | ||
loader := NewLoader() | ||
doc, err := loader.LoadFromData(DateTimeSpec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(loader.Context) | ||
require.NoError(t, err) | ||
|
||
err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{ | ||
"name": "kin-openapi", | ||
"datetime": "2016-12-31T23:59:61.000Z", | ||
}) | ||
require.EqualError(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters