From 2975a21ed6b57ea97427c31a297c368fb1ce35de Mon Sep 17 00:00:00 2001 From: Cosmos Nicolaou Date: Fri, 16 Dec 2022 10:58:33 -0800 Subject: [PATCH] openapi3: patch YAML serialization of dates (#698) Co-authored-by: Pierre Fenoll --- 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 c5af8536e..41ccaafef 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" { + // This is a fix for: https://github.com/getkin/kin-openapi/issues/697 + if eg, ok := schema.Example.(string); ok { + schema.Example = strings.TrimSuffix(eg, "T00:00:00Z") + } + } + 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: {} +