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

Try decoding as JSON first then YAML, for speed #693

Merged
merged 1 commit into from Dec 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/go.yml
Expand Up @@ -97,6 +97,11 @@ jobs:
run: |
! grep -IErn '\s$' --exclude-dir={.git,target,pgdata}

- if: runner.os == 'Linux'
name: Ensure use of unmarshal
run: |
[[ "$(git grep -F yaml. -- openapi3/ | grep -v _test.go | wc -l)" = 1 ]]

- if: runner.os == 'Linux'
name: Missing specification object link to definition
run: |
Expand Down
16 changes: 12 additions & 4 deletions openapi3/loader.go
Expand Up @@ -115,7 +115,7 @@ func (loader *Loader) loadSingleElementFromURI(ref string, rootPath *url.URL, el
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(data, element); err != nil {
if err := unmarshal(data, element); err != nil {
return nil, err
}

Expand All @@ -133,7 +133,7 @@ func (loader *Loader) readURL(location *url.URL) ([]byte, error) {
func (loader *Loader) LoadFromData(data []byte) (*T, error) {
loader.resetVisitedPathItemRefs()
doc := &T{}
if err := yaml.Unmarshal(data, doc); err != nil {
if err := unmarshal(data, doc); err != nil {
return nil, err
}
if err := loader.ResolveRefsIn(doc, nil); err != nil {
Expand Down Expand Up @@ -162,7 +162,7 @@ func (loader *Loader) loadFromDataWithPathInternal(data []byte, location *url.UR
doc := &T{}
loader.visitedDocuments[uri] = doc

if err := yaml.Unmarshal(data, doc); err != nil {
if err := unmarshal(data, doc); err != nil {
return nil, err
}
if err := loader.ResolveRefsIn(doc, location); err != nil {
Expand All @@ -172,6 +172,14 @@ func (loader *Loader) loadFromDataWithPathInternal(data []byte, location *url.UR
return doc, nil
}

func unmarshal(data []byte, v interface{}) error {
// See https://github.com/getkin/kin-openapi/issues/680
if err := json.Unmarshal(data, v); err != nil {
return yaml.Unmarshal(data, v)
}
return nil
}

// ResolveRefsIn expands references if for instance spec was just unmarshalled
func (loader *Loader) ResolveRefsIn(doc *T, location *url.URL) (err error) {
if loader.Context == nil {
Expand Down Expand Up @@ -319,7 +327,7 @@ func (loader *Loader) resolveComponent(
if err2 != nil {
return nil, err
}
if err2 = yaml.Unmarshal(data, &cursor); err2 != nil {
if err2 = unmarshal(data, &cursor); err2 != nil {
return nil, err
}
if cursor, err2 = drill(cursor); err2 != nil || cursor == nil {
Expand Down