Skip to content

Commit fc51b1f

Browse files
[http-server-javascript] Two small correctness fixes (microsoft#5253)
This fixes two issues that showed up in the widget and petstore REST examples. - Required query parameters were being detected as missing if their value was not exactly `null`. This PR changes this to a falsiness test. - Checking literal range-constrained properties in type differentiation could try to use a property without proving its presence. This PR adds an `in` check to that logic so that range-constrained properties have the same guard that literal-valued properties do. --------- Co-authored-by: Will Temple <[email protected]>
1 parent dddf9c7 commit fc51b1f

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http-server-javascript"
5+
---
6+
7+
Added an additional check for the presence of a property before performing a bounds check on integer properties constrained to a range.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http-server-javascript"
5+
---
6+
7+
Fixed a null check in query parameter requiredness check by replacing it with a falseness check.

packages/http-server-javascript/src/http/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ function* emitQueryParamBinding(
453453
yield `const ${nameCase.camelCase} = __query_params.get(${JSON.stringify(parameter.name)}) ?? undefined;`;
454454

455455
if (!parameter.param.optional) {
456-
yield `if (${nameCase.camelCase} === null) {`;
456+
yield `if (!${nameCase.camelCase}) {`;
457457
// prettier-ignore
458458
yield ` throw new Error("Invalid request: missing required query parameter '${parameter.name}'.");`;
459459
yield "}";

packages/http-server-javascript/src/util/differentiate.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,9 +742,19 @@ export function differentiateModelTypes(
742742

743743
branches.push({
744744
condition: {
745-
kind: "in-range",
746-
expr: { kind: "model-property", property },
747-
range,
745+
kind: "binary-op",
746+
left: {
747+
kind: "binary-op",
748+
left: { kind: "literal", value: renderPropertyName(property) },
749+
operator: "in",
750+
right: SUBJECT,
751+
},
752+
operator: "&&",
753+
right: {
754+
kind: "in-range",
755+
expr: { kind: "model-property", property },
756+
range,
757+
},
748758
},
749759
body: { kind: "result", type: model },
750760
});

0 commit comments

Comments
 (0)