Skip to content

fix #5481#5482

Merged
kevwan merged 1 commit intozeromicro:masterfrom
kesonan:feature/issue_5481
Mar 15, 2026
Merged

fix #5481#5482
kevwan merged 1 commit intozeromicro:masterfrom
kesonan:feature/issue_5481

Conversation

@kesonan
Copy link
Collaborator

@kesonan kesonan commented Mar 15, 2026

No description provided.

@codecov
Copy link

codecov bot commented Mar 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@kevwan kevwan force-pushed the feature/issue_5481 branch from 0a85b10 to 2071ca0 Compare March 15, 2026 13:57
@kevwan
Copy link
Contributor

kevwan commented Mar 15, 2026

The fix looks correct. Could you add a test to guard against regression? Here's one that exercises genCallGroup directly with two services that have disjoint message sets — it fails against the unfixed code and passes with this PR's change.

Please add this as tools/goctl/rpc/generator/gencall_test.go:

package generator

import (
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/emicklei/proto"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	conf "github.com/zeromicro/go-zero/tools/goctl/config"
	"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
	"github.com/zeromicro/go-zero/tools/goctl/util/stringx"
)

// mockDirContext is a minimal DirContext for unit-testing genCallGroup.
type mockDirContext struct {
	callDir Dir
	pbDir   Dir
	protoGo Dir
}

func (m *mockDirContext) GetCall() Dir                   { return m.callDir }
func (m *mockDirContext) GetEtc() Dir                    { return Dir{} }
func (m *mockDirContext) GetInternal() Dir               { return Dir{} }
func (m *mockDirContext) GetConfig() Dir                 { return Dir{} }
func (m *mockDirContext) GetLogic() Dir                  { return Dir{} }
func (m *mockDirContext) GetServer() Dir                 { return Dir{} }
func (m *mockDirContext) GetSvc() Dir                    { return Dir{} }
func (m *mockDirContext) GetPb() Dir                     { return m.pbDir }
func (m *mockDirContext) GetProtoGo() Dir                { return m.protoGo }
func (m *mockDirContext) GetMain() Dir                   { return Dir{} }
func (m *mockDirContext) GetServiceName() stringx.String { return stringx.From("test") }
func (m *mockDirContext) SetPbDir(pbDir, grpcDir string) {}

// TestGenCallGroup_OnlyUsedTypesAliased verifies that in multi-service mode each
// generated client file contains type aliases only for the message types actually
// used by that service's RPCs (fix for issue #5481).
func TestGenCallGroup_OnlyUsedTypesAliased(t *testing.T) {
	tmpDir := t.TempDir()
	callBase := filepath.Join(tmpDir, "call")
	pbBase := filepath.Join(tmpDir, "pb")

	// Pre-create subdirs that genCallGroup will write into.
	require.NoError(t, os.MkdirAll(filepath.Join(callBase, "servicea"), 0755))
	require.NoError(t, os.MkdirAll(filepath.Join(callBase, "serviceb"), 0755))
	require.NoError(t, os.MkdirAll(pbBase, 0755))

	mctx := &mockDirContext{
		callDir: Dir{
			Filename: callBase,
			Package:  "example.com/multitest/call",
			Base:     "call",
			GetChildPackage: func(childPath string) (string, error) {
				// Return a package path whose Base() is the lowercase service name.
				return filepath.Join(callBase, strings.ToLower(childPath)), nil
			},
		},
		pbDir: Dir{
			Filename: pbBase,
			Package:  "example.com/multitest/pb",
			Base:     "pb",
		},
		protoGo: Dir{
			// Must differ from "servicea"/"serviceb" so isCallPkgSameToPbPkg stays false
			// and alias generation is triggered.
			Filename: pbBase,
			Package:  "example.com/multitest/pb",
			Base:     "pb",
		},
	}

	// Proto with two services that use completely disjoint message types.
	protoData := parser.Proto{
		Name:      "multi.proto",
		PbPackage: "pb",
		Message: []parser.Message{
			{Message: &proto.Message{Name: "AReq"}},
			{Message: &proto.Message{Name: "AResp"}},
			{Message: &proto.Message{Name: "BReq"}},
			{Message: &proto.Message{Name: "BResp"}},
		},
		Service: parser.Services{
			{
				Service: &proto.Service{Name: "ServiceA"},
				RPC: []*parser.RPC{
					{RPC: &proto.RPC{Name: "DoA", RequestType: "AReq", ReturnsType: "AResp"}},
				},
			},
			{
				Service: &proto.Service{Name: "ServiceB"},
				RPC: []*parser.RPC{
					{RPC: &proto.RPC{Name: "DoB", RequestType: "BReq", ReturnsType: "BResp"}},
				},
			},
		},
	}

	cfg, err := conf.NewConfig("")
	require.NoError(t, err)

	g := NewGenerator("gozero", false)
	require.NoError(t, g.genCallGroup(mctx, protoData, cfg))

	// servicea/servicea.go — aliases for AReq/AResp only
	aContent, err := os.ReadFile(filepath.Join(callBase, "servicea", "servicea.go"))
	require.NoError(t, err)
	aFile := string(aContent)

	assert.Contains(t, aFile, "AReq", "ServiceA file should alias AReq")
	assert.Contains(t, aFile, "AResp", "ServiceA file should alias AResp")
	assert.NotContains(t, aFile, "BReq", "ServiceA file must not alias BReq")
	assert.NotContains(t, aFile, "BResp", "ServiceA file must not alias BResp")

	// serviceb/serviceb.go — aliases for BReq/BResp only
	bContent, err := os.ReadFile(filepath.Join(callBase, "serviceb", "serviceb.go"))
	require.NoError(t, err)
	bFile := string(bContent)

	assert.Contains(t, bFile, "BReq", "ServiceB file should alias BReq")
	assert.Contains(t, bFile, "BResp", "ServiceB file should alias BResp")
	assert.NotContains(t, bFile, "AReq", "ServiceB file must not alias AReq")
	assert.NotContains(t, bFile, "AResp", "ServiceB file must not alias AResp")
}

@kevwan kevwan added this pull request to the merge queue Mar 15, 2026
Merged via the queue into zeromicro:master with commit 4d2e64a Mar 15, 2026
7 of 8 checks passed
kevwan added a commit to kevwan/go-zero that referenced this pull request Mar 15, 2026
Verify that genCallGroup only emits type aliases for the request/response
messages actually used by each service's RPCs, not all messages in the
proto file (issue zeromicro#5481, fixed in zeromicro#5482).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants