diff --git a/codegen/templates/templates.go b/codegen/templates/templates.go index 0c9c104d40e..4b55eedd712 100644 --- a/codegen/templates/templates.go +++ b/codegen/templates/templates.go @@ -706,6 +706,8 @@ func TypeIdentifier(t types.Type) string { res := "" for { switch it := code.Unalias(t).(type) { + case *types.Alias: + return TypeIdentifier(it.Underlying()) case *types.Pointer: t.Underlying() res += "ᚖ" diff --git a/codegen/templates/templates_test.go b/codegen/templates/templates_test.go index d8b8ebf7b3b..47bc73872b2 100644 --- a/codegen/templates/templates_test.go +++ b/codegen/templates/templates_test.go @@ -381,3 +381,26 @@ func TestTypeName(t *testing.T) { assert.Equal(t, test.expected, result) } } + +func TestTypeIdentifierWithAlias(t *testing.T) { + // Create a new package + pkg := types.NewPackage("example.com/pkg", "pkg") + + // Create a basic type (e.g., string) + basicType := types.Typ[types.String] + + // Create an alias type + aliasType := types.NewAlias(types.NewTypeName(0, pkg, "MyAlias", nil), basicType) + + // Create a named type that uses the alias + namedType := types.NewNamed(types.NewTypeName(0, pkg, "MyType", nil), aliasType, nil) + + // Test the TypeIdentifier function + result := TypeIdentifier(namedType) + + // Expected result + // The package path will be encoded, and we expect it to resolve to the underlying type + expected := "exampleᚗcomᚋpkgᚐMyType" + + assert.Equal(t, expected, result, "TypeIdentifier should handle alias types correctly") +}