Repository: caicloud/nirvana
Pull request #404 (2020-12-01 04:33:25)

fix(rpc): disallow duplicated rpc path
<!--  Thanks for sending a pull request! See below for tips! -->

**What this PR does / why we need it**:

Prevent users from setting duplicated RRC path by mistake.

**Which issue(s) this PR is related to** *(optional, link to 3rd issue(s))*:

Fixes #

Reference to #
<!-- 填在 Fixes，PR 合并就会关 issue。填在 Reference to 会关联 issue，不会联动关闭。-->

**Special notes for your reviewer**:

/cc @iawia002 

<!-- Please answer the following questions during the code freeze, and delete this line.
**Code freeze questions**

1. What causes this PR to not be merged before code freeze?
2. Why this PR is absolutely necessary for this version? Paste a screenshot of smoke testing docs if you could.
3. What's the effects after merging it?
4. Is there anyway we can skip this to not affect the overall process?
-->

**Release note**:
<!--  Write your release note:
1. Enter your extended release note in the below block. If the PR requires additional action from users switching to the new release, include the string "action required".
2. If no release note is required, just write "NONE".
-->

```release-note
NONE
```

<!--  Thanks for sending a pull request! Here are some tips:

1. https://github.com/caicloud/engineering/blob/master/guidelines/review_conventions.md      <-- what is the review process looks like
2. https://github.com/caicloud/engineering/blob/master/guidelines/git_commit_conventions.md  <-- how to structure your git commit
3. https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md            <-- how to work with caicloud bot

Other tips:

If this is your first contribution, read our Getting Started guide https://github.com/caicloud/engineering/blob/master/guidelines/README.md
-->


caicloud-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: 
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: **iawia002**

Assign the PR to them by writing `/assign @iawia002` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#bot-commands).

The pull request process is described [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#typical-workflow)

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/caicloud/nirvana/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["iawia002"]} -->
iawia002: Thanks!
/lgtm
/cherrypick release-0.3
caicloud-bot: @iawia002: once the present PR merges, I will cherry-pick it on top of release-0.3 in a new PR and assign it to you.

<details>

In response to [this](https://github.com/caicloud/nirvana/pull/404#issuecomment-736240105):

>Thanks!
>/lgtm
>/cherrypick release-0.3


Instructions for interacting with me using PR comments are available [here](https://github.com/caicloud/engineering/blob/master/docs/caicloud_bot.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>
caicloud-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/caicloud/nirvana/pull/404#issuecomment-736240105" title="LGTM">iawia002</a>*

The full list of commands accepted by this bot can be found [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#bot-commands).

The pull request process is described [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#typical-workflow)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/caicloud/nirvana/blob/master/OWNERS)~~ [iawia002]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/service/rpc/builder.go b/service/rpc/builder.go
index ab388e8..b90f60a 100644
--- a/service/rpc/builder.go
+++ b/service/rpc/builder.go
@@ -107,7 +107,11 @@ func (b *builder) AddDescriptor(descriptors ...interface{}) error {
 			path = "/"
 		}
 		for _, action := range descriptor.Actions {
-			b.bindings[genRPCPath(path, action.Version, action.Name)] = &binding{
+			rpcPath := genRPCPath(path, action.Version, action.Name)
+			if _, ok := b.bindings[rpcPath]; ok {
+				return fmt.Errorf("duplicated rpc path: %s", rpcPath)
+			}
+			b.bindings[rpcPath] = &binding{
 				middlewares: descriptor.Middlewares,
 				definition:  b.genDefinition(action, descriptor.Consumes, descriptor.Produces, descriptor.Tags),
 			}


Test patch:
diff --git a/service/rpc/builder_test.go b/service/rpc/builder_test.go
index fbd90a8..bc3f42c 100644
--- a/service/rpc/builder_test.go
+++ b/service/rpc/builder_test.go
@@ -303,6 +303,33 @@ func TestDefinitions(t *testing.T) {
 	}
 }
 
+func TestDuplicatedRPCPath(t *testing.T) {
+	const version = "2020-10-10"
+	action := definition.RPCAction{
+		Name:    "GetFoo",
+		Version: version,
+		Function: func() (string, error) {
+			return "", nil
+		},
+		Results: definition.DataErrorResults(""),
+	}
+	desc := definition.RPCDescriptor{
+		Path:        "/",
+		Description: "Test",
+		Consumes:    []string{definition.MIMEAll},
+		Produces:    []string{definition.MIMEAll},
+		Actions:     []definition.RPCAction{action, action},
+	}
+	builder := NewBuilder()
+	err := builder.AddDescriptor(desc)
+	if err == nil {
+		t.Fatal("Unexpected success")
+	}
+	if !strings.Contains(err.Error(), "duplicated rpc path") {
+		t.Fatalf("Unexpected error: %v", err)
+	}
+}
+
 func BenchmarkServer(b *testing.B) {
 	u, _ := url.Parse("/?Action=GetEcho&Version=2020-01-01&name=alice")