Skip to content

Commit

Permalink
openapi3: allow YAML-marshaling invalid specs
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Jul 5, 2024
1 parent 4144c56 commit e1944ed
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
7 changes: 7 additions & 0 deletions maps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,16 @@ EOF


maplike_UnMarsh() {
if [[ "$type" != '*'* ]]; then
echo "TODO: impl non-pointer receiver YAML Marshaler"
exit 2
fi
cat <<EOF >>"$maplike"
// MarshalYAML returns the YAML encoding of ${type#'*'}.
func (${name} ${type}) MarshalYAML() (any, error) {
if ${name} == nil {
return nil, nil
}
m := make(map[string]any, ${name}.Len()+len(${name}.Extensions))
for k, v := range ${name}.Extensions {
m[k] = v
Expand Down
5 changes: 4 additions & 1 deletion openapi3/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func (info Info) MarshalJSON() ([]byte, error) {
}

// MarshalYAML returns the YAML encoding of Info.
func (info Info) MarshalYAML() (any, error) {
func (info *Info) MarshalYAML() (any, error) {
if info == nil {
return nil, nil
}
m := make(map[string]any, 6+len(info.Extensions))
for k, v := range info.Extensions {
m[k] = v
Expand Down
66 changes: 66 additions & 0 deletions openapi3/issue972_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package openapi3

import (
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)

func TestIssue972(t *testing.T) {
type testcase struct {
spec string
validationErrorContains string
}

base := `
openapi: 3.0.2
paths: {}
components: {}
`

for _, tc := range []testcase{{
spec: base,
validationErrorContains: "invalid info: must be an object",
}, {
spec: base + `
info:
title: "Hello World REST APIs"
version: "1.0"
`,
}, {
spec: base + `
info: null
`,
validationErrorContains: "invalid info: must be an object",
}, {
spec: base + `
info: {}
`,
validationErrorContains: "invalid info: value of version must be a non-empty string",
}, {
spec: base + `
info:
title: "Hello World REST APIs"
`,
validationErrorContains: "invalid info: value of version must be a non-empty string",
}} {
t.Logf("spec: %s", tc.spec)

loader := &Loader{}
doc, err := loader.LoadFromData([]byte(tc.spec))
assert.NoError(t, err)
assert.NotNil(t, doc)

err = doc.Validate(loader.Context)
if e := tc.validationErrorContains; e != "" {
assert.ErrorContains(t, err, e)
} else {
assert.NoError(t, err)
}

txt, err := yaml.Marshal(doc)
assert.NoError(t, err)
assert.NotEmpty(t, txt)
}
}
3 changes: 1 addition & 2 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1140,8 +1140,7 @@ func (loader *Loader) resolvePathItemRef(doc *T, pathItem *PathItem, documentPat
*pathItem = p
} else {
var resolved PathItem
doc, documentPath, err = loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
if doc, documentPath, err = loader.resolveComponent(doc, ref, documentPath, &resolved); err != nil {
if err == errMUSTPathItem {
return nil
}
Expand Down
9 changes: 9 additions & 0 deletions openapi3/maplike.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (responses Responses) JSONLookup(token string) (any, error) {

// MarshalYAML returns the YAML encoding of Responses.
func (responses *Responses) MarshalYAML() (any, error) {
if responses == nil {
return nil, nil
}
m := make(map[string]any, responses.Len()+len(responses.Extensions))
for k, v := range responses.Extensions {
m[k] = v
Expand Down Expand Up @@ -206,6 +209,9 @@ func (callback Callback) JSONLookup(token string) (any, error) {

// MarshalYAML returns the YAML encoding of Callback.
func (callback *Callback) MarshalYAML() (any, error) {
if callback == nil {
return nil, nil
}
m := make(map[string]any, callback.Len()+len(callback.Extensions))
for k, v := range callback.Extensions {
m[k] = v
Expand Down Expand Up @@ -334,6 +340,9 @@ func (paths Paths) JSONLookup(token string) (any, error) {

// MarshalYAML returns the YAML encoding of Paths.
func (paths *Paths) MarshalYAML() (any, error) {
if paths == nil {
return nil, nil
}
m := make(map[string]any, paths.Len()+len(paths.Extensions))
for k, v := range paths.Extensions {
m[k] = v
Expand Down
3 changes: 3 additions & 0 deletions openapi3/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (doc *T) MarshalJSON() ([]byte, error) {

// MarshalYAML returns the YAML encoding of T.
func (doc *T) MarshalYAML() (any, error) {
if doc == nil {
return nil, nil
}
m := make(map[string]any, 4+len(doc.Extensions))
for k, v := range doc.Extensions {
m[k] = v
Expand Down

0 comments on commit e1944ed

Please sign in to comment.