Skip to content

Implement AllowReserved support for path parameters with URL encoding#550

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-82
Draft

Implement AllowReserved support for path parameters with URL encoding#550
Copilot wants to merge 3 commits intomainfrom
copilot/fix-82

Conversation

Copy link
Contributor

Copilot AI commented Aug 8, 2025

This PR implements proper support for the allowReserved property in path parameters by adding URL encoding logic to the TypeSpec Rust emitter.

Problem

The TypeSpec Rust emitter was not handling the allowReserved property for path parameters correctly. The issue manifested in two ways:

  1. Semantic inversion: The param.allowReserved value was being passed directly as the encoded parameter, when it should be inverted (!param.allowReserved)
  2. Missing URL encoding logic: The encoded property was defined but never used in code generation, so path parameters were always replaced using simple string substitution without any URL encoding

This meant that reserved characters (like /, ?, #, [, ], @, etc.) in path parameter values were not being properly URL-encoded, which could lead to malformed URLs and incorrect API calls.

Solution

1. Fixed Semantic Inversion

Updated src/tcgcadapter/adapter.ts to correctly map allowReserved to the encoded property:

  • When allowReserved=false (default): encoded=true (apply URL encoding)
  • When allowReserved=true: encoded=false (skip URL encoding)

2. Implemented URL Encoding Logic

Added comprehensive URL encoding support in src/codegen/clients.ts:

  • Scalar path parameters: Apply percent_encode() to the entire parameter value when encoded=true
  • Collection path parameters: Apply encoding to each individual item in the collection
  • HashMap path parameters: Apply encoding to both keys and values individually
  • All parameter styles: Handles simple, path, label, and matrix styles correctly

The implementation uses url::percent_encoding::percent_encode with the CONTROLS encode set for proper URL path encoding.

3. Added Test Coverage

Created test/allowreserved.test.ts with comprehensive tests that verify:

  • Path parameters are correctly marked as encoded/unencoded based on allowReserved value
  • URL encoding imports are properly added when encoding is needed

Example

Before this change, a path parameter with value "foo/bar" would be inserted directly:

path = path.replace("{param}", &param_value); // Results in malformed URL

After this change, with allowReserved=false (default):

path = path.replace("{param}", &percent_encode(param_value.as_bytes(), CONTROLS).to_string()); // Properly encoded

With allowReserved=true:

path = path.replace("{param}", &param_value); // Intentionally unencoded

Fixes #82.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits August 8, 2025 16:02
Co-authored-by: RickWinter <4430337+RickWinter@users.noreply.github.com>
Co-authored-by: RickWinter <4430337+RickWinter@users.noreply.github.com>
Copilot AI changed the title [WIP] Support AllowReserved in Path Implementation Implement AllowReserved support for path parameters with URL encoding Aug 8, 2025
Copilot AI requested a review from RickWinter August 8, 2025 16:05
expect(useText).toContain('percent_encode');
expect(useText).toContain('CONTROLS');
});
}); No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
});
});

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 AllowReserved in Path Implementation

2 participants