Skip to content
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

Implement exploded query parameters #498

Merged

Conversation

csmarchbanks
Copy link
Contributor

@csmarchbanks csmarchbanks commented Jul 11, 2024

Allow query parameters such as /foo?bar=baz&bar=qux rather than requiring multiple values to be comma separated. This has worse performance than comma seprated so is not enabled by default and requires adding the explode tag to the query parameter.

Closes: #452

Summary by CodeRabbit

  • New Features
    • Introduced support for specifying the same query parameter multiple times using the explode tag in URI query strings, offering an alternative to comma-separated lists for multiple values.
  • Documentation
    • Added documentation for the new query parameter explode feature.

Allow query parameters such as /foo?bar=baz&bar=qux rather than
requiring multiple values to be comma separated. This has worse
performance than comma seprated so is not enabled by default and
requires adding the `explode` tag to the query parameter.

Signed-off-by: Chris Marchbanks <[email protected]>
Copy link

coderabbitai bot commented Jul 11, 2024

Walkthrough

The changes introduce the ability to handle multiple query parameters with the same name using the explode tag in URI query strings. This enhancement allows for an alternative to comma-separated lists, improving parsing and ensuring that values containing commas are correctly handled.

Changes

File Change Summary
docs/docs/features/... Updated documentation to include information on the new explode tag for handling multiple query parameters with the same name.
huma.go Added a new Explode field to the paramFieldInfo struct and updated query parameter parsing logic to support the explode tag.
huma_test.go Modified the Features struct to include the QueryExploded field and updated test assertions and URLs to validate the new query parameter handling functionality.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant Huma
    Client->>Server: GET /path?foo=bar&foo=baz
    Server->>Huma: Parse query parameters
    Huma->>Server: Parsed parameters {foo: ["bar", "baz"]}
    Server->>Client: Response
Loading

Assessment against linked issues

Objective Addressed Explanation
Support multiple query parameters with the same name (#452)

Poem

In the realm of code, a quest began,
To parse query strings, a better plan.
No longer commas causing a plight,
Explode the tags, make it right.
Now foo=bar&foo=baz shall reign,
Huma’s parsing, free from strain.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b8be41a and 6961c0c.

Files selected for processing (3)
  • docs/docs/features/request-inputs.md (1 hunks)
  • huma.go (13 hunks)
  • huma_test.go (2 hunks)
Additional comments not posted (6)
docs/docs/features/request-inputs.md (1)

36-36: LGTM!

The documentation explains the explode tag for query parameters clearly.

huma.go (2)

105-105: LGTM!

The addition of the Explode field to the paramFieldInfo struct is straightforward and aligns with the new feature.


140-146: LGTM!

The parsing logic for query parameters correctly accommodates the explode tag.

huma_test.go (3)

370-370: Ensure proper field tagging.

The new field QueryExploded is correctly tagged with query:"exploded,explode".


405-405: Validate assert statement.

The assertion assert.Equal(t, []string{"foo", "bar"}, input.QueryExploded) correctly tests the functionality of the explode tag.


416-416: Ensure test URL correctness.

The test URL includes the exploded query parameters correctly: exploded=foo&exploded=bar.

Copy link

@ARolek ARolek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really clean in there. I left a minor suggestion inline. I was trying to come up with alternative names for Explode, but didn't come up with anything better. The name works, but could maybe be more specific. Thanks for tackling this issue. It will help me get through a blocker.

nope := false
explode = &nope
// easier if we use comma-separated values, so we disable explode by default.
if slicesContains(split[1:], "explode") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the built in slices package: https://pkg.go.dev/slices#Contains

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to support Go 1.20 FYI. Once Go 1.23 is released I will probably make the minimum required version 1.21 and we can start using the slices package.

@csmarchbanks
Copy link
Contributor Author

I was trying to come up with alternative names for Explode, but didn't come up with anything better. The name works, but could maybe be more specific

Thinking about this some more over the weekend, maybe repeated would be better?

@ARolek
Copy link

ARolek commented Jul 15, 2024

Thinking about this some more over the weekend, maybe repeated would be better?

That could work. I know it's a bit verbose, but "Support Multiples" is potentially more specific.

Copy link

codecov bot commented Jul 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.84%. Comparing base (b8be41a) to head (6961c0c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #498      +/-   ##
==========================================
- Coverage   92.85%   92.84%   -0.01%     
==========================================
  Files          22       22              
  Lines        3791     3787       -4     
==========================================
- Hits         3520     3516       -4     
  Misses        227      227              
  Partials       44       44              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for making this optional!

Edit: I'm fine with explode as that matches the OpenAPI spec terminology, so anyone familiar with OpenAPI will find this straightforward.

@danielgtaylor danielgtaylor merged commit 6ff17c6 into danielgtaylor:main Jul 15, 2024
2 of 3 checks passed
@csmarchbanks csmarchbanks deleted the exploded-query-parameters branch July 15, 2024 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support multiple query parameters with the same name
3 participants