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

openapi3: patch YAML serialization of dates #698

Merged
merged 3 commits into from
Dec 16, 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
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 {
fenollp marked this conversation as resolved.
Show resolved Hide resolved
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: {}