From d871badc150be100972f093fd17c15206ac5868b Mon Sep 17 00:00:00 2001 From: Cosmos Nicolaou Date: Sun, 11 Dec 2022 18:09:50 -0800 Subject: [PATCH 1/3] . --- openapi3/issue697_test.go | 15 +++++++++++++++ openapi3/schema.go | 10 +++++++++- openapi3/testdata/issue697.yml | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 openapi3/issue697_test.go create mode 100644 openapi3/testdata/issue697.yml diff --git a/openapi3/issue697_test.go b/openapi3/issue697_test.go new file mode 100644 index 000000000..c7317584a --- /dev/null +++ b/openapi3/issue697_test.go @@ -0,0 +1,15 @@ +package openapi3 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIssue697(t *testing.T) { + loader := NewLoader() + doc, err := loader.LoadFromFile("testdata/issue697.yml") + require.NoError(t, err) + err = doc.Validate(loader.Context) + require.NoError(t, err) +} diff --git a/openapi3/schema.go b/openapi3/schema.go index d2cd31c5f..193a8651a 100644 --- a/openapi3/schema.go +++ b/openapi3/schema.go @@ -12,6 +12,7 @@ import ( "regexp" "sort" "strconv" + "strings" "unicode/utf16" "github.com/go-openapi/jsonpointer" @@ -180,7 +181,14 @@ func (schema *Schema) MarshalJSON() ([]byte, error) { // UnmarshalJSON sets Schema to a copy of data. func (schema *Schema) UnmarshalJSON(data []byte) error { - return jsoninfo.UnmarshalStrictStruct(data, schema) + err := jsoninfo.UnmarshalStrictStruct(data, schema) + if schema.Format == "date" { + if eg, ok := schema.Example.(string); ok { + eg = strings.TrimSuffix(eg, "T00:00:00Z") + schema.Example = eg + } + } + return err } // JSONLookup implements github.com/go-openapi/jsonpointer#JSONPointable diff --git a/openapi3/testdata/issue697.yml b/openapi3/testdata/issue697.yml new file mode 100644 index 000000000..71a4b2ae2 --- /dev/null +++ b/openapi3/testdata/issue697.yml @@ -0,0 +1,14 @@ +openapi: 3.0.1 +components: + schemas: + API: + properties: + dateExample: + type: string + format: date + example: 2019-09-12 +info: + title: sample + version: version not set +paths: {} + From b5db4e0f5af5fa5dcf2115f47665c4ccd11f06eb Mon Sep 17 00:00:00 2001 From: Cosmos Nicolaou Date: Fri, 16 Dec 2022 09:23:10 -0800 Subject: [PATCH 2/3] . --- openapi3/schema.go | 1 + 1 file changed, 1 insertion(+) diff --git a/openapi3/schema.go b/openapi3/schema.go index 193a8651a..19300aaa2 100644 --- a/openapi3/schema.go +++ b/openapi3/schema.go @@ -183,6 +183,7 @@ func (schema *Schema) MarshalJSON() ([]byte, error) { func (schema *Schema) UnmarshalJSON(data []byte) error { err := jsoninfo.UnmarshalStrictStruct(data, schema) if schema.Format == "date" { + // This is a fix for: https://github.com/getkin/kin-openapi/issues/697 if eg, ok := schema.Example.(string); ok { eg = strings.TrimSuffix(eg, "T00:00:00Z") schema.Example = eg From 249bba1a5f97c35634a6b768128b44d28f1ee894 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Fri, 16 Dec 2022 19:31:48 +0100 Subject: [PATCH 3/3] Update openapi3/schema.go --- openapi3/schema.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/openapi3/schema.go b/openapi3/schema.go index 19300aaa2..2a90d2828 100644 --- a/openapi3/schema.go +++ b/openapi3/schema.go @@ -185,8 +185,7 @@ func (schema *Schema) UnmarshalJSON(data []byte) error { if schema.Format == "date" { // This is a fix for: https://github.com/getkin/kin-openapi/issues/697 if eg, ok := schema.Example.(string); ok { - eg = strings.TrimSuffix(eg, "T00:00:00Z") - schema.Example = eg + schema.Example = strings.TrimSuffix(eg, "T00:00:00Z") } } return err