Skip to content

Add dictionary parameter support for QueryParam, FormParam, and HeaderParam attributes#88

Merged
twogood merged 4 commits intomainfrom
copilot/fix-87
Aug 4, 2025
Merged

Add dictionary parameter support for QueryParam, FormParam, and HeaderParam attributes#88
twogood merged 4 commits intomainfrom
copilot/fix-87

Conversation

Copy link
Contributor

Copilot AI commented Aug 4, 2025

This PR implements support for passing IDictionary parameters to QueryParamAttribute, FormParamAttribute, and HeaderParamAttribute. When a dictionary is provided, its key-value pairs are automatically expanded into multiple individual parameters.

Changes Made

  • Modified RequestHandler.GetParams() to check if parameter values implement IDictionary
  • When a dictionary is detected, iterates through all key-value pairs and adds them as separate parameters
  • Maintains full backward compatibility - existing single-value parameters continue to work unchanged
  • Added comprehensive test coverage including edge cases

Usage Examples

Query Parameters:

var filters = new Dictionary<string, string> 
{
    ["category"] = "electronics",
    ["price_min"] = "100",
    ["price_max"] = "500"
};

[Get("/search")]
Task<SearchResult> Search([QueryParam] Dictionary<string, string> filters);
// Creates: /search?category=electronics&price_min=100&price_max=500

Form Parameters:

var formData = new Dictionary<string, string>
{
    ["firstName"] = "John",
    ["lastName"] = "Doe",
    ["email"] = "john@example.com"
};

[Post("/submit")]
Task SubmitForm([FormParam] Dictionary<string, string> data);
// Sends as form-encoded data with all fields

Header Parameters:

var headers = new Dictionary<string, string>
{
    ["X-API-Version"] = "2.0",
    ["X-Client-ID"] = "app123"
};

[Get("/data")]
Task<string> GetData([HeaderParam] Dictionary<string, string> headers);
// Adds all headers to the request

Mixed Parameters:

[Get("/advanced")]
Task<Result> Search(
    [QueryParam] Dictionary<string, string> filters,
    [QueryParam("page")] int page,
    [QueryParam("limit")] int limit);
// Combines dictionary expansion with regular parameters

Features

  • Automatic Expansion: Dictionary entries become individual parameters
  • Null Safety: Handles null keys/values gracefully by converting to empty strings
  • Proper Encoding: Query parameters are URL-encoded correctly
  • Backward Compatibility: Existing code continues to work without changes
  • Mixed Usage: Can combine dictionaries with regular parameters in the same method

The implementation handles edge cases like empty dictionaries and null values, ensuring robust behavior in all scenarios.

Fixes #87.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits August 4, 2025 12:22
… HeaderParam attributes

Co-authored-by: twogood <189982+twogood@users.noreply.github.com>
Co-authored-by: twogood <189982+twogood@users.noreply.github.com>
Copilot AI changed the title [WIP] Allow a dictionary of parameters to be passed to methods Add dictionary parameter support for QueryParam, FormParam, and HeaderParam attributes Aug 4, 2025
Copilot AI requested a review from twogood August 4, 2025 12:26
@twogood twogood marked this pull request as ready for review August 4, 2025 12:28
@twogood twogood merged commit 3391bda into main Aug 4, 2025
8 checks passed
@twogood twogood deleted the copilot/fix-87 branch August 4, 2025 17:07
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.

Allow a dictionary of parameters to be passed to methods

2 participants