-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathissue597_test.go
More file actions
82 lines (72 loc) · 2.01 KB
/
Copy pathissue597_test.go
File metadata and controls
82 lines (72 loc) · 2.01 KB
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
// Copyright 2026 Princess Beef Heavy Industries / Dave Shanley
// https://pb33f.io
// SPDX-License-Identifier: MIT
package libopenapi
import (
"testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/testify/require"
"go.yaml.in/yaml/v4"
)
const issue597Spec = `openapi: 3.1.0
info:
title: Common schemas
version: 0.0.1
servers:
- url: http://localhost:8080/
components:
schemas:
foo:
type: object
properties:
hello:
type: string
world:
type: string
x-custom: true
`
func TestIssue597SchemaExtensionMarshalYAMLDoesNotChangeLowHash(t *testing.T) {
tests := []struct {
name string
marshal func(*orderedmap.Map[string, *yaml.Node]) error
}{
{
name: "direct MarshalYAML",
marshal: func(extensions *orderedmap.Map[string, *yaml.Node]) error {
_, err := extensions.MarshalYAML()
return err
},
},
{
name: "yaml Marshal",
marshal: func(extensions *orderedmap.Map[string, *yaml.Node]) error {
_, err := yaml.Marshal(extensions)
return err
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc, err := NewDocumentWithConfiguration([]byte(issue597Spec), &datamodel.DocumentConfiguration{
ExtractRefsSequentially: true,
})
require.NoError(t, err)
v3Model, err := doc.BuildV3Model()
require.NoError(t, err)
schema := v3Model.Model.Components.Schemas.Value("foo").Schema()
lowSchema := schema.GoLow()
lowExtension := low.FindItemInOrderedMap[*yaml.Node]("x-custom", lowSchema.GetExtensions())
require.NotNil(t, lowExtension)
require.Equal(t, "!!bool", lowExtension.Value.Tag)
initialHash := lowSchema.Hash()
low.ClearHashCache()
require.NoError(t, tt.marshal(schema.Extensions))
require.Equal(t, "!!bool", lowExtension.Value.Tag)
require.Equal(t, "true", lowExtension.Value.Value)
low.ClearHashCache()
require.Equal(t, initialHash, lowSchema.Hash())
})
}
}