Skip to content

Commit

Permalink
[params] make request body optional, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lispyclouds committed Dec 20, 2024
1 parent a0db4d0 commit 4125a41
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ paths:
schema:
type: boolean

requestBody:
description: The requestBody
required: true
x-cli-name: req-body

components:
schemas:
NumbersMap:
Expand Down
4 changes: 2 additions & 2 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type HandlerData struct {
QueryParams []ParamMeta // List of query params
HeaderParams []ParamMeta // List of header params
CookieParams []ParamMeta // List of cookie params
RequestBodyParam ParamMeta // The request body
RequestBodyParam *ParamMeta // The optional request body
}

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

if req := body.Required; req != nil && *req {
Expand Down
16 changes: 15 additions & 1 deletion lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestBootstrapV3(t *testing.T) {
assert.Equal(t, data.QueryParams, []ParamMeta{{Name: "p2", Type: String}})
assert.Equal(t, data.HeaderParams, []ParamMeta{{Name: "p3", Type: Number}})
assert.Equal(t, data.CookieParams, []ParamMeta{{Name: "p4", Type: Boolean}})
assert.Equal(t, data.RequestBodyParam, &ParamMeta{Name: "req-body", Type: String})
}
rootCmd := &cobra.Command{
Use: "calc",
Expand Down Expand Up @@ -160,6 +161,19 @@ func TestBootstrapV3(t *testing.T) {

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

rootCmd.SetArgs([]string{"info", "GetInfo", "--p1", "420", "--p2", "yes", "--p3", "420.69", "--p4", "true"})
rootCmd.SetArgs([]string{
"info",
"GetInfo",
"--p1",
"420",
"--p2",
"yes",
"--p3",
"420.69",
"--p4",
"true",
"--req-body",
"the string body",
})
rootCmd.Execute()
}

0 comments on commit 4125a41

Please sign in to comment.