Repository: caicloud/nirvana
Pull request #461 (2021-07-04 11:31:04)

add openapi body name
<!--  Thanks for sending a pull request! See below for tips! -->

**What this PR does / why we need it**:

Add openapi v2 specs field, body.name.
Specs docs here, https://swagger.io/docs/specification/2-0/describing-request-body/ ,search `name: `.

**Which issue(s) this PR is related to** *(optional, link to 3rd issue(s))*:

<!-- 填在 Fixes，PR 合并就会关 issue。填在 Reference to 会关联 issue，不会联动关闭。-->

**Special notes for your reviewer**:

/cc @your-reviewer

<!-- 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: **kdada**

Assign the PR to them by writing `/assign @kdada` 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":["kdada"]} -->
alswl: Forced pushing fixed parent from my featured branch to origin master branch.
iawia002: /lgtm
caicloud-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/caicloud/nirvana/pull/461#issuecomment-873743262" 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/definition/helper.go b/definition/helper.go
index aa7754b..33ccda2 100644
--- a/definition/helper.go
+++ b/definition/helper.go
@@ -93,7 +93,12 @@ func FileParameterFor(name string, description string, operators ...Operator) Pa
 
 // BodyParameterFor creates a body parameter
 func BodyParameterFor(description string, operators ...Operator) Parameter {
-	return ParameterFor(Body, "", description, operators...)
+	return BodyParameterWithNameFor("", description, operators...)
+}
+
+// BodyParameterWithNameFor creates a body parameter with body name
+func BodyParameterWithNameFor(name string, description string, operators ...Operator) Parameter {
+	return ParameterFor(Body, name, description, operators...)
 }
 
 // PrefabParameterFor creates a prefab parameter
diff --git a/examples/api-basic/api/v2/app.go b/examples/api-basic/api/v2/app.go
index 0cb6de5..fdd5d53 100644
--- a/examples/api-basic/api/v2/app.go
+++ b/examples/api-basic/api/v2/app.go
@@ -36,7 +36,7 @@ var app = definition.Descriptor{
 			Consumes:    []string{definition.MIMEJSON},
 			Produces:    []string{definition.MIMEJSON},
 			Parameters: []definition.Parameter{
-				definition.BodyParameterFor("Application V2 json object"),
+				definition.BodyParameterWithNameFor("Application V2 json object", "description"),
 			},
 			Results: definition.DataErrorResults("Application V2 json object"),
 		},


Test patch:
diff --git a/definition/definition_test.go b/definition/definition_test.go
index e8aab7b..7dfdf33 100644
--- a/definition/definition_test.go
+++ b/definition/definition_test.go
@@ -42,7 +42,8 @@ func TestParameter(t *testing.T) {
 		{Source: Header, Name: "test", Description: "For test"},
 		{Source: Form, Name: "test", Description: "For test"},
 		{Source: File, Name: "test", Description: "For test"},
-		{Source: Body, Description: "For test"},
+		{Source: Body, Name: "", Description: "For test"},
+		{Source: Body, Name: "For test", Description: "For test"},
 		{Source: Auto, Description: "For test"},
 		{Source: Prefab, Name: "test", Description: "For test"},
 	}
@@ -53,6 +54,7 @@ func TestParameter(t *testing.T) {
 		FormParameterFor("test", "For test"),
 		FileParameterFor("test", "For test"),
 		BodyParameterFor("For test"),
+		BodyParameterWithNameFor("For test", "For test"),
 		AutoParameterFor("For test"),
 		PrefabParameterFor("test", "For test"),
 	}