Skip to content

Commit 4125a41

Browse files
committed
[params] make request body optional, tests
1 parent a0db4d0 commit 4125a41

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

api.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ paths:
8686
schema:
8787
type: boolean
8888

89+
requestBody:
90+
description: The requestBody
91+
required: true
92+
x-cli-name: req-body
93+
8994
components:
9095
schemas:
9196
NumbersMap:

lib.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type HandlerData struct {
3838
QueryParams []ParamMeta // List of query params
3939
HeaderParams []ParamMeta // List of header params
4040
CookieParams []ParamMeta // List of cookie params
41-
RequestBodyParam ParamMeta // The request body
41+
RequestBodyParam *ParamMeta // The optional request body
4242
}
4343

4444
// The handler signature
@@ -154,7 +154,7 @@ func addRequestBody(cmd *cobra.Command, op *v3.Operation, handlerData *HandlerDa
154154
// TODO: Handle all the different MIME types and schemas from body.Content
155155
// maybe assert the shape if mime is json and schema is an object
156156
// Treats all request body content as a string as of now
157-
handlerData.RequestBodyParam = ParamMeta{Name: paramName, Type: String}
157+
handlerData.RequestBodyParam = &ParamMeta{Name: paramName, Type: String}
158158
cmd.Flags().String(paramName, "", body.Description)
159159

160160
if req := body.Required; req != nil && *req {

lib_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func TestBootstrapV3(t *testing.T) {
8888
assert.Equal(t, data.QueryParams, []ParamMeta{{Name: "p2", Type: String}})
8989
assert.Equal(t, data.HeaderParams, []ParamMeta{{Name: "p3", Type: Number}})
9090
assert.Equal(t, data.CookieParams, []ParamMeta{{Name: "p4", Type: Boolean}})
91+
assert.Equal(t, data.RequestBodyParam, &ParamMeta{Name: "req-body", Type: String})
9192
}
9293
rootCmd := &cobra.Command{
9394
Use: "calc",
@@ -160,6 +161,19 @@ func TestBootstrapV3(t *testing.T) {
160161

161162
assertCmdTree(t, rootCmd, assertConf, rootCmd.Use)
162163

163-
rootCmd.SetArgs([]string{"info", "GetInfo", "--p1", "420", "--p2", "yes", "--p3", "420.69", "--p4", "true"})
164+
rootCmd.SetArgs([]string{
165+
"info",
166+
"GetInfo",
167+
"--p1",
168+
"420",
169+
"--p2",
170+
"yes",
171+
"--p3",
172+
"420.69",
173+
"--p4",
174+
"true",
175+
"--req-body",
176+
"the string body",
177+
})
164178
rootCmd.Execute()
165179
}

0 commit comments

Comments
 (0)