Fix optional query/header parameters with constant values being hardcoded#3812
Fix optional query/header parameters with constant values being hardcoded#3812
Conversation
Optional query parameters with constant literal types were being hardcoded instead of reading from options. This prevented users from omitting these optional parameters. The fix checks if a constant parameter is also optional before hardcoding its value. Optional constants now flow through the standard optional parameter handling path, which reads from options and allows the parameter to be omitted. Co-authored-by: JialinHuang803 <139532647+JialinHuang803@users.noreply.github.com>
|
@copilot Update the unit test to cover header and body parameters. |
Extended the unit test to cover three scenarios: 1. Optional constant query params with body 2. Optional constant header params (required + optional) with body 3. Combined optional constant query + header params with body Co-authored-by: JialinHuang803 <139532647+JialinHuang803@users.noreply.github.com>
Added two more test scenarios in commit 73cacc7:
All 538 modular unit tests pass. |
|
@copilot the test is in the queryParam folder, while it contains header param test scenarios. Can you remove the test "Required constant body with optional constant query and header" and move to header test scenario to a correct place? Also add a test for optional constant body parameter. |
- Remove combined query+header test and header test from queryParam/optionalConstantQueryParam.md - Move optional constant header test to headerParam/optionalConstantHeaderParam.md - Add optional constant body parameter test in bodyParam/optionalConstantBodyParam.md Co-authored-by: JialinHuang803 <139532647+JialinHuang803@users.noreply.github.com>
Done in ba603b4:
All 538 modular + 309 RLC unit tests pass. |
...typespec-ts/test/modularUnit/scenarios/operations/headerParam/optionalConstantHeaderParam.md
Show resolved
Hide resolved
MaryGao
left a comment
There was a problem hiding this comment.
Approved and left small comments.
getParameterMapin the modular emitter unconditionally hardcoded constant-type parameters, ignoring optionality. This meant optional params like@query("name") foo?: "literal"or@header("name") bar?: "literal"always emitted the literal value instead of reading from options, making them impossible to omit.Changes
operationHelpers.ts: Gate the constant-value early return on!isOptional(param)so optional constants flow through the standardgetOptionalpath, which reads from the options bag and preservesundefinedfor URL template omission and header conditional spreading.Before (generated):
After (generated):
queryParam/optionalConstantQueryParam.md— Optional constant query parameters with path and body paramsheaderParam/optionalConstantHeaderParam.md— Required constant, optional constant, and optional string header parameters with bodybodyParam/optionalConstantBodyParam.md— Optional constant body parameterOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.