added test for require <-> omitempty

This commit is contained in:
Shreyas Goenka 2023-01-18 13:12:01 +01:00
parent 7e00a80109
commit 2789b61e40
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
1 changed files with 63 additions and 0 deletions

View File

@ -918,6 +918,69 @@ func TestObjectSchema(t *testing.T) {
assert.Equal(t, expected, string(jsonSchema))
}
func TestFieldsWithoutOmitEmptyAreRequired(t *testing.T) {
type Papaya struct {
A int `json:"a,string,omitempty"`
B string `json:"b"`
}
type MyStruct struct {
Foo string `json:"-,omitempty"`
Bar int `json:"bar"`
Apple int `json:"apple,omitempty"`
Mango int `json:",omitempty"`
Guava int `json:","`
Papaya *Papaya `json:"papaya,"`
}
elem := MyStruct{}
schema, err := NewSchema(reflect.TypeOf(elem))
require.NoError(t, err)
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)
expectedSchema :=
`{
"type": "object",
"properties": {
"apple": {
"type": "number"
},
"bar": {
"type": "number"
},
"papaya": {
"type": "object",
"properties": {
"a": {
"type": "number"
},
"b": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"b"
]
}
},
"additionalProperties": false,
"required": [
"bar",
"papaya"
]
}`
t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expectedSchema)
assert.Equal(t, expectedSchema, string(jsonSchema))
}
// // Only for testing bundle, will be removed
// func TestBundleSchema(t *testing.T) {
// elem := config.Root{}