Repository: caicloud/nirvana
Pull request #402 (2020-11-30 04:05:06)

fix(*): make modifier ProduceAllIfProducesIsEmpty also modify ErrorProduces
<!--  Thanks for sending a pull request! See below for tips! -->

**What this PR does / why we need it**:

The value of `ErrorProduces` field of `Definition` is the same as `Produces` field by default:
1. https://github.com/caicloud/nirvana/blob/b561a068104cef53a92bd475c326231132ec6158/service/rpc/builder.go#L129
2. https://github.com/caicloud/nirvana/blob/b561a068104cef53a92bd475c326231132ec6158/service/rest/builder.go#L157

Currently the modifier  `ProduceAllIfProducesIsEmpty` only modify the `Produces` field, so `ErrorProduces` would be empty If `Produces` is empty, then nirvana will throw an error when building executor:
https://github.com/caicloud/nirvana/blob/b561a068104cef53a92bd475c326231132ec6158/service/executor/executor.go#L59-L61

**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: **ddysher**

Assign the PR to them by writing `/assign @ddysher` 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":["ddysher"]} -->
iawia002: /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/402#issuecomment-735538968):

>/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>
iawia002: /lgtm
caicloud-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/caicloud/nirvana/pull/402#issuecomment-735545242" 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/modifier.go b/service/modifier.go
index 53eee95..73dadc1 100644
--- a/service/modifier.go
+++ b/service/modifier.go
@@ -69,13 +69,16 @@ func ConsumeAllIfConsumesIsEmpty() DefinitionModifier {
 	}
 }
 
-// ProduceAllIfProducesIsEmpty adds definition.MIMEAll to consumes if consumes
-// is empty.
+// ProduceAllIfProducesIsEmpty adds definition.MIMEAll to produces and errorProduces if
+// they are empty.
 func ProduceAllIfProducesIsEmpty() DefinitionModifier {
 	return func(d *definition.Definition) {
-		if len(d.Produces) <= 0 {
+		if len(d.Produces) == 0 {
 			d.Produces = []string{definition.MIMEAll}
 		}
+		if len(d.ErrorProduces) == 0 {
+			d.ErrorProduces = []string{definition.MIMEAll}
+		}
 	}
 }
 


Test patch:
diff --git a/service/modfier_test.go b/service/modfier_test.go
new file mode 100644
index 0000000..56314ad
--- /dev/null
+++ b/service/modfier_test.go
@@ -0,0 +1,42 @@
+/*
+Copyright 2020 Caicloud Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package service
+
+import (
+	"testing"
+
+	"github.com/caicloud/nirvana/definition"
+)
+
+func TestProduceAllIfProducesIsEmpty(t *testing.T) {
+	d := definition.Definition{
+		Summary:     "Test",
+		Method:      definition.Create,
+		Description: "test",
+		Results:     definition.DataErrorResults("data"),
+		Function: func() (interface{}, error) {
+			return nil, nil
+		},
+	}
+	ProduceAllIfProducesIsEmpty()(&d)
+	if len(d.Produces) == 0 {
+		t.Errorf("Produces should not be empty")
+	}
+	if len(d.ErrorProduces) == 0 {
+		t.Errorf("ErrorProduces should not be empty")
+	}
+}