-
-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathissue883_test.go
103 lines (90 loc) · 2.27 KB
/
issue883_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package openapi3_test
import (
"testing"
yaml "github.com/oasdiff/yaml"
yamlv3 "github.com/oasdiff/yaml3"
"github.com/stretchr/testify/require"
"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 using yaml pkg", func(t *testing.T) {
justPaths, err := yaml.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.NotNil(t, doc.Paths)
require.YAMLEq(t, spec, string(marshalledYaml))
var newDoc openapi3.T
err = yaml.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 = yamlv3.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 := yamlv3.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/github.com/oasdiff/yaml3#Unmarshaler on maplike types")
var newDoc openapi3.T
err = yamlv3.Unmarshal(marshalledYaml, &newDoc)
require.NoError(t, err)
require.NotNil(t, newDoc.Paths)
require.Equal(t, doc, &newDoc)
})
}