@@ -5,12 +5,13 @@ import (
5
5
"testing"
6
6
7
7
"github.com/oapi-codegen/nullable"
8
+ "gopkg.in/yaml.v3"
8
9
9
10
"github.com/stretchr/testify/require"
10
11
)
11
12
12
13
type Obj struct {
13
- Foo nullable.Nullable [string ] `json:"foo,omitempty"` // note "omitempty" is important for fields that are optional
14
+ Foo nullable.Nullable [string ] `json:"foo,omitempty",yaml:"foo,omitempty" ` // note "omitempty" is important for fields that are optional
14
15
}
15
16
16
17
func TestNullable (t * testing.T ) {
@@ -88,3 +89,79 @@ func serialize(o Obj, t *testing.T) string {
88
89
require .NoError (t , err )
89
90
return string (data )
90
91
}
92
+
93
+ func TestNullableYAML (t * testing.T ) {
94
+ // --- parsing from json and serializing back to JSON
95
+
96
+ // -- case where there is an actual value
97
+ data := `foo: bar`
98
+ // deserialize from json
99
+ myObj := parseYAML (data , t )
100
+ require .Equal (t , myObj , Obj {Foo : nullable.Nullable [string ]{true : "bar" }})
101
+ require .False (t , myObj .Foo .IsNull ())
102
+ require .True (t , myObj .Foo .IsSpecified ())
103
+ value , err := myObj .Foo .Get ()
104
+ require .NoError (t , err )
105
+ require .Equal (t , "bar" , value )
106
+ require .Equal (t , "bar" , myObj .Foo .MustGet ())
107
+ // serialize back to json: leads to the same data
108
+ require .Equal (t , data , serializeYAML (myObj , t ))
109
+
110
+ // -- case where no value is specified: parsed from JSON
111
+ data = ``
112
+ // deserialize from json
113
+ myObj = parseYAML (data , t )
114
+ require .Equal (t , myObj , Obj {Foo : nil })
115
+ require .False (t , myObj .Foo .IsNull ())
116
+ require .False (t , myObj .Foo .IsSpecified ())
117
+ _ , err = myObj .Foo .Get ()
118
+ require .ErrorContains (t , err , "value is not specified" )
119
+ // serialize back to json: leads to the same data
120
+ require .Equal (t , data , serializeYAML (myObj , t ))
121
+
122
+ // -- case where the specified value is explicitly null
123
+ data = `foo:null`
124
+ // deserialize from json
125
+ myObj = parseYAML (data , t )
126
+ require .Equal (t , myObj , Obj {Foo : nullable.Nullable [string ]{false : "" }})
127
+ require .True (t , myObj .Foo .IsNull ())
128
+ require .True (t , myObj .Foo .IsSpecified ())
129
+ _ , err = myObj .Foo .Get ()
130
+ require .ErrorContains (t , err , "value is null" )
131
+ require .Panics (t , func () { myObj .Foo .MustGet () })
132
+ // serialize back to json: leads to the same data
133
+ require .Equal (t , data , serializeYAML (myObj , t ))
134
+
135
+ // --- building objects from a Go client
136
+
137
+ // - case where there is an actual value
138
+ myObj = Obj {}
139
+ myObj .Foo .Set ("bar" )
140
+ require .Equal (t , `foo:"bar"` , serialize (myObj , t ))
141
+
142
+ // - case where the value should be unspecified
143
+ myObj = Obj {}
144
+ // do nothing: unspecified by default
145
+ require .Equal (t , `` , serializeYAML (myObj , t ))
146
+ // explicitly mark unspecified
147
+ myObj .Foo .SetUnspecified ()
148
+ require .Equal (t , `` , serializeYAML (myObj , t ))
149
+
150
+ // - case where the value should be null
151
+ myObj = Obj {}
152
+ myObj .Foo .SetNull ()
153
+ require .Equal (t , `foo:null` , serialize (myObj , t ))
154
+ }
155
+
156
+ func parseYAML (data string , t * testing.T ) Obj {
157
+ var myObj Obj
158
+ err := yaml .Unmarshal ([]byte (data ), & myObj )
159
+ require .NoError (t , err )
160
+ return myObj
161
+ }
162
+
163
+ func serializeYAML (o Obj , t * testing.T ) string {
164
+ data , err := yaml .Marshal (o )
165
+ require .NoError (t , err )
166
+ return string (data )
167
+ }
0 commit comments