Skip to content

Commit

Permalink
try harder
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Jan 22, 2024
1 parent 30adaee commit bbca606
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
97 changes: 69 additions & 28 deletions openapi3/issue883_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package openapi3_test
import (
"testing"

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

"github.com/getkin/kin-openapi/openapi3"
)
Expand All @@ -19,11 +20,13 @@ info:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths: {}
components:
schemas:
Kitten:
type: string
paths:
/simple:
get:
summary: A simple GET request
responses:
'200':
description: OK
`[1:]

sl := openapi3.NewLoader()
Expand All @@ -35,27 +38,65 @@ components:
require.NoError(t, err)
require.NotNil(t, doc.Paths)

marshalledJson, err := doc.MarshalJSON()
require.NoError(t, err)
require.JSONEq(t, `{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {"name": "MIT"}
},
"servers": [{"url": "http://petstore.swagger.io/v1"}],
"paths": {},
"components": {
"schemas": {
"Kitten": {"type": "string"}
}
}
}`, string(marshalledJson))
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 := yaml.Marshal(&doc)
require.NoError(t, err)
require.YAMLEq(t, spec, string(marshalledYaml))
require.NotNil(t, doc.Paths)
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))

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
Original file line number Diff line number Diff line change
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 bbca606

Please sign in to comment.