From 13f631b46f9d59bdb17dad4f1b7a6d99b87fd2b9 Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Tue, 5 Mar 2024 21:10:08 +0100 Subject: [PATCH] OpenAPI: aliases to basic types --- openapi3/to_schema.go | 2 +- openapi3/to_schema_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/openapi3/to_schema.go b/openapi3/to_schema.go index e824ee10..47abf931 100644 --- a/openapi3/to_schema.go +++ b/openapi3/to_schema.go @@ -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" diff --git a/openapi3/to_schema_test.go b/openapi3/to_schema_test.go index afc93b4b..ba40c153 100644 --- a/openapi3/to_schema_test.go +++ b/openapi3/to_schema_test.go @@ -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)