Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,11 @@ func defineTypeOfExample(schemaType, arrayType, exampleValue string) (interface{

return result, nil
case OBJECT:
// Handle empty object "{}" as a special case
if exampleValue == "{}" {
return map[string]any{}, nil
}

if arrayType == "" {
return nil, fmt.Errorf("%s is unsupported type in example value `%s`", schemaType, exampleValue)
}
Expand Down
14 changes: 13 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3924,7 +3924,12 @@ func TestDefineTypeOfExample(t *testing.T) {
t.Run("Object type", func(t *testing.T) {
t.Parallel()

example, err := defineTypeOfExample("object", "", "key_one:one,key_two:two,key_three:three")
// Test empty object "{}" - issue #2018
example, err := defineTypeOfExample("object", "", "{}")
assert.NoError(t, err)
assert.Equal(t, map[string]any{}, example)

example, err = defineTypeOfExample("object", "", "key_one:one,key_two:two,key_three:three")
assert.Error(t, err)
assert.Nil(t, example)

Expand All @@ -3945,6 +3950,13 @@ func TestDefineTypeOfExample(t *testing.T) {
}

assert.Equal(t, obj, map[string]string{"key_one": "one", "key_two": "two", "key_three": "three"})

// Test map[string]struct{} with empty object values - issue #2018
example, err = defineTypeOfExample("object", "object", "key1:{},key2:{}")
assert.NoError(t, err)
objMap := example.(map[string]interface{})
assert.Equal(t, map[string]any{}, objMap["key1"])
assert.Equal(t, map[string]any{}, objMap["key2"])
})

t.Run("Invalid type", func(t *testing.T) {
Expand Down