Skip to content

Commit

Permalink
[http-server-javascript] Two small correctness fixes (#5253)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
witemple-msft and willmtemple authored Dec 5, 2024
1 parent dddf9c7 commit fc51b1f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-server-javascript"
---

Added an additional check for the presence of a property before performing a bounds check on integer properties constrained to a range.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-server-javascript"
---

Fixed a null check in query parameter requiredness check by replacing it with a falseness check.
2 changes: 1 addition & 1 deletion packages/http-server-javascript/src/http/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ function* emitQueryParamBinding(
yield `const ${nameCase.camelCase} = __query_params.get(${JSON.stringify(parameter.name)}) ?? undefined;`;

if (!parameter.param.optional) {
yield `if (${nameCase.camelCase} === null) {`;
yield `if (!${nameCase.camelCase}) {`;
// prettier-ignore
yield ` throw new Error("Invalid request: missing required query parameter '${parameter.name}'.");`;
yield "}";
Expand Down
16 changes: 13 additions & 3 deletions packages/http-server-javascript/src/util/differentiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,19 @@ export function differentiateModelTypes(

branches.push({
condition: {
kind: "in-range",
expr: { kind: "model-property", property },
range,
kind: "binary-op",
left: {
kind: "binary-op",
left: { kind: "literal", value: renderPropertyName(property) },
operator: "in",
right: SUBJECT,
},
operator: "&&",
right: {
kind: "in-range",
expr: { kind: "model-property", property },
range,
},
},
body: { kind: "result", type: model },
});
Expand Down

0 comments on commit fc51b1f

Please sign in to comment.