Repository navigation
fix(rpc): keep repeated query values #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package rpc | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "reflect" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/flanksource/clicky/entity" | ||
| "github.com/flanksource/clicky/flags" | ||
| ) | ||
|
|
||
| // A query key repeated on a GET must reach the operation the way a repeated | ||
| // flag reaches it on the command line: ?kind=a&kind=b is --kind a --kind b. | ||
| var _ = Describe("repeated query parameters", func() { | ||
| var ( | ||
| executor *CommandExecutor | ||
| operation *RPCOperation | ||
| kinds []string | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| kinds = nil | ||
| cmd := &cobra.Command{ | ||
| Use: "targets", | ||
| RunE: func(*cobra.Command, []string) error { return nil }, | ||
| } | ||
| cmd.Flags().StringSliceVar(&kinds, "kind", nil, "kinds to include") | ||
| cmd.Flags().String("name", "", "target name") | ||
| var err error | ||
| operation, err = NewConverter(nil).ConvertCommand(cmd) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| executor = NewCommandExecutor(&RPCService{Operations: []RPCOperation{*operation}}, | ||
| &ExecutorConfig{Enabled: true, SkipPreRun: true, PathPrefix: "/api/v1"}) | ||
| }) | ||
|
|
||
| DescribeTable("a slice flag receives every value, as the CLI would", | ||
| func(target string, want []string) { | ||
| request, err := executor.ExtractRequestFromHTTP(httptest.NewRequest(http.MethodGet, target, nil), operation) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| _, _, err = executor.ExecuteCommand(operation, request) | ||
|
|
||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(kinds).To(Equal(want)) | ||
| }, | ||
| Entry("repeated key", "/api/v1/targets?kind=table&kind=!column", []string{"table", "!column"}), | ||
| Entry("comma-separated single value", "/api/v1/targets?kind=table,!column", []string{"table", "!column"}), | ||
| Entry("repeated key mixing comma values", "/api/v1/targets?kind=table,view&kind=!column", []string{"table", "view", "!column"}), | ||
| ) | ||
|
|
||
| It("binds every repeated value onto a MultiFilter field", func() { | ||
| type targetFlags struct { | ||
| Kind entity.MultiFilter `flag:"kind"` | ||
| } | ||
| request, err := executor.ExtractRequestFromHTTP(httptest.NewRequest(http.MethodGet, "/api/v1/targets?kind=table&kind=!column", nil), operation) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| fields, err := flags.ParseStructFields(reflect.TypeOf(targetFlags{})) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| var decoded targetFlags | ||
| Expect(flags.PopulateFromRequest(reflect.ValueOf(&decoded).Elem(), fields, request.Flags, nil)).To(Succeed()) | ||
|
|
||
| Expect(decoded.Kind).To(Equal(entity.MultiFilter{"table", "!column"})) | ||
| }) | ||
|
|
||
| It("refuses a scalar flag given more than once with a 400 naming it", func() { | ||
| _, err := executor.ExtractRequestFromHTTP(httptest.NewRequest(http.MethodGet, "/api/v1/targets?name=a&name=b", nil), operation) | ||
|
|
||
| Expect(err).To(MatchError(ContainSubstring("name"))) | ||
| var status *entity.StatusError | ||
| Expect(errors.As(err, &status)).To(BeTrue()) | ||
| Expect(status.StatusCode()).To(Equal(http.StatusBadRequest)) | ||
| }) | ||
|
|
||
| It("passes every repeated positional value as args", func() { | ||
| request, err := executor.ExtractRequestFromHTTP(httptest.NewRequest(http.MethodGet, "/api/v1/targets?args=a&args=b,c", nil), operation) | ||
|
|
||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(request.Args).To(Equal([]string{"a", "b", "c"})) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reject repeats for scalar positional query parameters.
When an operation declares a named positional query parameter of type
string,?id=a&id=bappends both values toreq.Args. The scalar-repeat check inqueryFlagValuenever runs for positional parameters. Reject this request before buildingreq.Args, while continuing to accept repeats for positional parameters of typearray. (raw.githubusercontent.com)