Skip to content

Commit

Permalink
openapi3: tests for #931 (#887)
Browse files Browse the repository at this point in the history
* repro #883

Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>

* try harder

Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>

* skip yaml unmarshaler for maplike types for now

Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>

* docs.sh

Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>

---------

Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Apr 6, 2024
1 parent 5a6afbe commit b6f165a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/docs/openapi3.txt
Expand Up @@ -1696,7 +1696,7 @@ func (doc *T) JSONLookup(token string) (interface{}, error)
JSONLookup implements
https://pkg.go.dev/github.com/go-openapi/jsonpointer#JSONPointable

func (doc T) MarshalJSON() ([]byte, error)
func (doc *T) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of T.

func (doc *T) UnmarshalJSON(data []byte) error
Expand Down
103 changes: 103 additions & 0 deletions openapi3/issue883_test.go
@@ -0,0 +1,103 @@
package openapi3_test

import (
"testing"

invopopYaml "github.com/invopop/yaml"
"github.com/stretchr/testify/require"
v3 "gopkg.in/yaml.v3"

"github.com/getkin/kin-openapi/openapi3"
)

func TestIssue883(t *testing.T) {
spec := `
openapi: '3.0.0'
info:
version: '1.0.0'
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/simple:
get:
summary: A simple GET request
responses:
'200':
description: OK
`[1:]

sl := openapi3.NewLoader()
doc, err := sl.LoadFromData([]byte(spec))
require.NoError(t, err)
require.NotNil(t, doc.Paths)

err = doc.Validate(sl.Context)
require.NoError(t, err)
require.NotNil(t, doc.Paths)

t.Run("Roundtrip invopop/yaml", func(t *testing.T) {
justPaths, err := invopopYaml.Marshal(doc.Paths)
require.NoError(t, err)
require.NotNil(t, doc.Paths)
require.YAMLEq(t, `
/simple:
get:
summary: A simple GET request
responses:
'200':
description: OK
`[1:], string(justPaths))

marshalledYaml, err := invopopYaml.Marshal(doc)
require.NoError(t, err)
require.NotNil(t, doc.Paths)
require.YAMLEq(t, spec, string(marshalledYaml))

var newDoc openapi3.T
err = invopopYaml.Unmarshal(marshalledYaml, &newDoc)
require.NoError(t, err)
require.NotNil(t, newDoc.Paths)
require.Equal(t, doc, &newDoc)
})

t.Run("Roundtrip yaml.v3", func(t *testing.T) {
justPaths, err := doc.Paths.MarshalJSON()
require.NoError(t, err)
require.NotNil(t, doc.Paths)
require.YAMLEq(t, `
/simple:
get:
summary: A simple GET request
responses:
'200':
description: OK
`[1:], string(justPaths))

justPaths, err = v3.Marshal(doc.Paths)
require.NoError(t, err)
require.NotNil(t, doc.Paths)
require.YAMLEq(t, `
/simple:
get:
summary: A simple GET request
responses:
'200':
description: OK
`[1:], string(justPaths))

marshalledYaml, err := v3.Marshal(doc)
require.NoError(t, err)
require.NotNil(t, doc.Paths)
require.YAMLEq(t, spec, string(marshalledYaml))

t.Skip("TODO: impl https://pkg.go.dev/gopkg.in/yaml.v3#Unmarshaler on maplike types")
var newDoc openapi3.T
err = v3.Unmarshal(marshalledYaml, &newDoc)
require.NoError(t, err)
require.NotNil(t, newDoc.Paths)
require.Equal(t, doc, &newDoc)
})
}
2 changes: 1 addition & 1 deletion openapi3/openapi3.go
Expand Up @@ -54,7 +54,7 @@ func (doc *T) JSONLookup(token string) (interface{}, error) {
}

// MarshalJSON returns the JSON encoding of T.
func (doc T) MarshalJSON() ([]byte, error) {
func (doc *T) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{}, 4+len(doc.Extensions))
for k, v := range doc.Extensions {
m[k] = v
Expand Down

0 comments on commit b6f165a

Please sign in to comment.