Skip to content

Commit

Permalink
openapi3: patch YAML serialization of dates (#698)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
cosnicolaou and fenollp authored Dec 16, 2022
1 parent 35bb627 commit 2975a21
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
15 changes: 15 additions & 0 deletions openapi3/issue697_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 9 additions & 1 deletion openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"regexp"
"sort"
"strconv"
"strings"
"unicode/utf16"

"github.com/go-openapi/jsonpointer"
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions openapi3/testdata/issue697.yml
Original file line number Diff line number Diff line change
@@ -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: {}

0 comments on commit 2975a21

Please sign in to comment.