From e49a554b4318f76b89133f8f0f63d3d9390dfb5d Mon Sep 17 00:00:00 2001 From: ssanjay1 <67482244+ssanjay1@users.noreply.github.com> Date: Wed, 24 Jul 2024 16:44:03 -0400 Subject: [PATCH] Fix geo types (#520) * types fix * add changeset --- .changeset/eleven-planets-taste.md | 6 + etc/client.api.report.api.md | 10 + .../client.api/src/aggregate/WhereClause.ts | 10 + .../modernToLegacyWhereClause.test.ts | 199 +++++++++++------- .../conversions/modernToLegacyWhereClause.ts | 10 +- 5 files changed, 161 insertions(+), 74 deletions(-) create mode 100644 .changeset/eleven-planets-taste.md diff --git a/.changeset/eleven-planets-taste.md b/.changeset/eleven-planets-taste.md new file mode 100644 index 000000000..42f254ae9 --- /dev/null +++ b/.changeset/eleven-planets-taste.md @@ -0,0 +1,6 @@ +--- +"@osdk/client.api": patch +"@osdk/client": patch +--- + +Fixing types for within and intersects so they don't take more than the permitted keys diff --git a/etc/client.api.report.api.md b/etc/client.api.report.api.md index 25e1ff9b0..389397451 100644 --- a/etc/client.api.report.api.md +++ b/etc/client.api.report.api.md @@ -361,8 +361,10 @@ export type GeoFilter = GeoFilter_Within | GeoFilter_Intersects; export type GeoFilter_Intersects = { "$intersects": { $bbox: BBox; + $polygon?: never; } | BBox | { $polygon: Polygon["coordinates"]; + $bbox?: never; } | Polygon; }; @@ -371,10 +373,18 @@ export type GeoFilter_Within = { "$within": { $distance: [number, keyof typeof DistanceUnitMapping]; $of: [number, number] | Readonly; + $bbox?: never; + $polygon?: never; } | { $bbox: BBox; + $distance?: never; + $of?: never; + $polygon?: never; } | BBox | { $polygon: Polygon["coordinates"]; + $bbox?: never; + $distance?: never; + $of?: never; } | Polygon; }; diff --git a/packages/client.api/src/aggregate/WhereClause.ts b/packages/client.api/src/aggregate/WhereClause.ts index 4577727f3..58d0a3c33 100644 --- a/packages/client.api/src/aggregate/WhereClause.ts +++ b/packages/client.api/src/aggregate/WhereClause.ts @@ -113,13 +113,21 @@ export type GeoFilter_Within = { | { $distance: [number, keyof typeof DistanceUnitMapping]; $of: [number, number] | Readonly; + $bbox?: never; + $polygon?: never; } | { $bbox: BBox; + $distance?: never; + $of?: never; + $polygon?: never; } | BBox | { $polygon: Polygon["coordinates"]; + $bbox?: never; + $distance?: never; + $of?: never; } | Polygon; }; @@ -128,10 +136,12 @@ export type GeoFilter_Intersects = { "$intersects": | { $bbox: BBox; + $polygon?: never; } | BBox | { $polygon: Polygon["coordinates"]; + $bbox?: never; } | Polygon; }; diff --git a/packages/client/src/internal/conversions/modernToLegacyWhereClause.test.ts b/packages/client/src/internal/conversions/modernToLegacyWhereClause.test.ts index 1e155f8f3..7847b4e1c 100644 --- a/packages/client/src/internal/conversions/modernToLegacyWhereClause.test.ts +++ b/packages/client/src/internal/conversions/modernToLegacyWhereClause.test.ts @@ -14,8 +14,10 @@ * limitations under the License. */ +import type { WhereClause } from "@osdk/client.api"; import type { MockOntology } from "@osdk/shared.test"; import type { Point } from "geojson"; +import { expectType } from "ts-expect"; import { describe, expect, it } from "vitest"; import { modernToLegacyWhereClause } from "./modernToLegacyWhereClause.js"; @@ -219,16 +221,70 @@ describe(modernToLegacyWhereClause, () => { } `); }); - }); - describe("$intersects", () => { - it("properly generates bbox shortcut", async () => { - expect(modernToLegacyWhereClause( - { - geoShape: { - $intersects: [-5, 5, -10, 10], + it("check types", async () => { + expectType>({ + geoPoint: { + $within: [-5, 5, -10, 10], + }, + }); + + expectType>({ + geoPoint: { + $within: { $distance: [2, "centimeter"], $of: [2, 2] }, + }, + }); + + expectType>({ + geoPoint: { + $within: { $polygon: [[[0, 1], [3, 2], [0, 1]]] }, + }, + }); + + expectType>({ + geoPoint: { + $within: { + type: "Polygon", + coordinates: [[[0, 1], [3, 2], [0, 1]]], }, }, - )).toMatchInlineSnapshot(` + }); + + expectType>({ + geoPoint: { + $within: { $bbox: [-5, 5, -10, 10] }, + }, + }); + + expectType>({ + geoPoint: { + $within: { + $bbox: [-5, 5, -10, 10], + // @ts-expect-error + $distance: [2, "centimeter"], + // @ts-expect-error + $of: [2, 2], + }, + }, + }); + expectType>({ + geoPoint: { + $within: { + $polygon: [[[0, 1], [3, 2], [0, 1]]], + // @ts-expect-error + $bbox: [2, 2, 2, 2], + }, + }, + }); + }); + describe("$intersects", () => { + it("properly generates bbox shortcut", async () => { + expect(modernToLegacyWhereClause( + { + geoShape: { + $intersects: [-5, 5, -10, 10], + }, + }, + )).toMatchInlineSnapshot(` { "field": "geoShape", "type": "intersectsBoundingBox", @@ -250,17 +306,17 @@ describe(modernToLegacyWhereClause, () => { }, } `); - }); - it("properly generates bbox long form", async () => { - expect(modernToLegacyWhereClause( - { - geoShape: { - $intersects: { - $bbox: [-5, 5, -10, 10], + }); + it("properly generates bbox long form", async () => { + expect(modernToLegacyWhereClause( + { + geoShape: { + $intersects: { + $bbox: [-5, 5, -10, 10], + }, }, }, - }, - )).toMatchInlineSnapshot(` + )).toMatchInlineSnapshot(` { "field": "geoShape", "type": "intersectsBoundingBox", @@ -282,16 +338,16 @@ describe(modernToLegacyWhereClause, () => { }, } `); - }); + }); - it("properly generates intersects polygon", async () => { - expect(modernToLegacyWhereClause( - { - geoShape: { - $intersects: { $polygon: [[[0, 1], [3, 2], [0, 1]]] }, + it("properly generates intersects polygon", async () => { + expect(modernToLegacyWhereClause( + { + geoShape: { + $intersects: { $polygon: [[[0, 1], [3, 2], [0, 1]]] }, + }, }, - }, - )).toMatchInlineSnapshot(` + )).toMatchInlineSnapshot(` { "field": "geoShape", "type": "intersectsPolygon", @@ -316,19 +372,19 @@ describe(modernToLegacyWhereClause, () => { }, } `); - }); + }); - it("properly generates within polygon geojson", async () => { - expect(modernToLegacyWhereClause( - { - geoShape: { - $intersects: { - type: "Polygon", - coordinates: [[[0, 1], [3, 2], [0, 1]]], + it("properly generates within polygon geojson", async () => { + expect(modernToLegacyWhereClause( + { + geoShape: { + $intersects: { + type: "Polygon", + coordinates: [[[0, 1], [3, 2], [0, 1]]], + }, }, }, - }, - )).toMatchInlineSnapshot(` + )).toMatchInlineSnapshot(` { "field": "geoShape", "type": "intersectsPolygon", @@ -353,13 +409,13 @@ describe(modernToLegacyWhereClause, () => { }, } `); + }); }); - }); - it("inverts ne short hand properly", () => { - expect(modernToLegacyWhereClause({ - integer: { $ne: 5 }, - })).toMatchInlineSnapshot(` + it("inverts ne short hand properly", () => { + expect(modernToLegacyWhereClause({ + integer: { $ne: 5 }, + })).toMatchInlineSnapshot(` { "type": "not", "value": { @@ -369,17 +425,17 @@ describe(modernToLegacyWhereClause, () => { }, } `); + }); }); - }); - describe("multiple checks", () => { - it("properly handles multiple simple where checks", () => { - expect(modernToLegacyWhereClause( - { - decimal: 5, - integer: 10, - }, - )).toMatchInlineSnapshot(` + describe("multiple checks", () => { + it("properly handles multiple simple where checks", () => { + expect(modernToLegacyWhereClause( + { + decimal: 5, + integer: 10, + }, + )).toMatchInlineSnapshot(` { "type": "and", "value": [ @@ -396,18 +452,18 @@ describe(modernToLegacyWhereClause, () => { ], } `); - }); + }); - it("properly handles $and", () => { - expect(modernToLegacyWhereClause( - { - $and: [{ - decimal: 5, - }, { - integer: 10, - }], - }, - )).toMatchInlineSnapshot(` + it("properly handles $and", () => { + expect(modernToLegacyWhereClause( + { + $and: [{ + decimal: 5, + }, { + integer: 10, + }], + }, + )).toMatchInlineSnapshot(` { "type": "and", "value": [ @@ -424,18 +480,18 @@ describe(modernToLegacyWhereClause, () => { ], } `); - }); + }); - it("properly handles $or", () => { - expect(modernToLegacyWhereClause( - { - $or: [{ - decimal: 5, - }, { - integer: 10, - }], - }, - )).toMatchInlineSnapshot(` + it("properly handles $or", () => { + expect(modernToLegacyWhereClause( + { + $or: [{ + decimal: 5, + }, { + integer: 10, + }], + }, + )).toMatchInlineSnapshot(` { "type": "or", "value": [ @@ -452,6 +508,7 @@ describe(modernToLegacyWhereClause, () => { ], } `); + }); }); }); }); diff --git a/packages/client/src/internal/conversions/modernToLegacyWhereClause.ts b/packages/client/src/internal/conversions/modernToLegacyWhereClause.ts index 3d4ae4e4d..c09cec132 100644 --- a/packages/client/src/internal/conversions/modernToLegacyWhereClause.ts +++ b/packages/client/src/internal/conversions/modernToLegacyWhereClause.ts @@ -163,9 +163,13 @@ function handleWherePair([field, filter]: [string, any]): SearchJsonQueryV2 { if (Array.isArray(withinBody)) { return makeGeoFilterBbox(field, withinBody, firstKey); - } else if ("$bbox" in withinBody) { + } else if ("$bbox" in withinBody && withinBody.$bbox != null) { return makeGeoFilterBbox(field, withinBody.$bbox, firstKey); - } else if ("$distance" in withinBody && "$of" in withinBody) { + } else if ( + ("$distance" in withinBody && "$of" in withinBody) + && withinBody.$distance != null + && withinBody.$of != null + ) { return { type: "withinDistanceOf", field, @@ -194,7 +198,7 @@ function handleWherePair([field, filter]: [string, any]): SearchJsonQueryV2 { filter[firstKey] as GeoFilter_Intersects["$intersects"]; if (Array.isArray(intersectsBody)) { return makeGeoFilterBbox(field, intersectsBody, firstKey); - } else if ("$bbox" in intersectsBody) { + } else if ("$bbox" in intersectsBody && intersectsBody.$bbox != null) { return makeGeoFilterBbox(field, intersectsBody.$bbox, firstKey); } else { const coordinates = ("$polygon" in intersectsBody)