Skip to content

Commit

Permalink
OpenAPI: aliases to basic types
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Mar 5, 2024
1 parent 2250f12 commit 13f631b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion openapi3/to_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ToSchema(v any) *Schema {
s.Properties[fieldName] = *ToSchema(field.Interface())
} else {
// If the field is a basic type, we can just add it to the properties
fieldTypeType := fieldType.Type.Name()
fieldTypeType := fieldType.Type.Kind().String()
format := fieldType.Tag.Get("format")
if strings.Contains(fieldTypeType, "int") {
fieldTypeType = "integer"
Expand Down
17 changes: 17 additions & 0 deletions openapi3/to_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ func TestToSchema(t *testing.T) {
require.Equal(t, "string", s.Type)
})

t.Run("alias to string", func(t *testing.T) {
type S string
s := ToSchema(S(""))
require.Equal(t, "string", s.Type)
})

t.Run("struct with a field alias to string", func(t *testing.T) {
type MyAlias string
type S struct {
A MyAlias
}

s := ToSchema(S{})
require.Equal(t, "object", s.Type)
require.Equal(t, "string", s.Properties["A"].Type)
})

t.Run("int", func(t *testing.T) {
s := ToSchema(0)
require.Equal(t, "integer", s.Type)
Expand Down

0 comments on commit 13f631b

Please sign in to comment.