From 7afa226d05e04cda66399ec4dfb878871048a6f9 Mon Sep 17 00:00:00 2001 From: ssanjay1 <67482244+ssanjay1@users.noreply.github.com> Date: Thu, 11 Jul 2024 16:20:44 -0400 Subject: [PATCH] Fix action params issue with objects (#458) * fix * added changeset --- .changeset/new-points-sing.md | 5 +++++ packages/client/src/util/isOsdkObject.ts | 22 ++++++++++++++++++++ packages/client/src/util/toDataValue.test.ts | 11 +++++++++- packages/client/src/util/toDataValue.ts | 5 +++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .changeset/new-points-sing.md create mode 100644 packages/client/src/util/isOsdkObject.ts diff --git a/.changeset/new-points-sing.md b/.changeset/new-points-sing.md new file mode 100644 index 000000000..23a881f9f --- /dev/null +++ b/.changeset/new-points-sing.md @@ -0,0 +1,5 @@ +--- +"@osdk/client": patch +--- + +Fix action params that take objects to correctly parse out primary key. diff --git a/packages/client/src/util/isOsdkObject.ts b/packages/client/src/util/isOsdkObject.ts new file mode 100644 index 000000000..8112c42c4 --- /dev/null +++ b/packages/client/src/util/isOsdkObject.ts @@ -0,0 +1,22 @@ +/* + * Copyright 2024 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import type { OsdkBase } from "@osdk/client.api"; + +export function isOsdkBaseObject(o: any): o is OsdkBase { + return o && typeof o === "object" && typeof o.$apiName === "string" + && o.$primaryKey != null; +} diff --git a/packages/client/src/util/toDataValue.test.ts b/packages/client/src/util/toDataValue.test.ts index e0ed256a6..a28f25d6d 100644 --- a/packages/client/src/util/toDataValue.test.ts +++ b/packages/client/src/util/toDataValue.test.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { Ontology } from "@osdk/client.test.ontology"; import { apiServer, MockOntology, stubData } from "@osdk/shared.test"; import type { MockedFunction } from "vitest"; import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; @@ -35,7 +36,7 @@ describe(toDataValue, () => { apiServer.listen(); client = createClient( "https://stack.palantir.com", - MockOntology.metadata.ontologyRid, + Ontology.metadata.ontologyRid, async () => "myAccessToken", ); @@ -105,6 +106,14 @@ describe(toDataValue, () => { ); }); + it("maps an ontology object into just its primary key with osdk wrapper", async () => { + const task = await client(Ontology.objects.Employee).fetchOne(50030); + const ontologyConversion = await toDataValue(task, clientCtx); + expect(ontologyConversion).toEqual( + task.$primaryKey, + ); + }); + it("passes through object set definitions", async () => { const clientObjectSet = client(MockOntology.objects.Task).where({ id: 0 }); const definition = getWireObjectSet(clientObjectSet); diff --git a/packages/client/src/util/toDataValue.ts b/packages/client/src/util/toDataValue.ts index 5a80fe490..2560e6a68 100644 --- a/packages/client/src/util/toDataValue.ts +++ b/packages/client/src/util/toDataValue.ts @@ -19,6 +19,7 @@ import type { MinimalClient } from "../MinimalClientContext.js"; import { isAttachmentUpload } from "../object/AttachmentUpload.js"; import { getWireObjectSet, isObjectSet } from "../objectSet/createObjectSet.js"; import { isOntologyObjectV2 } from "./isOntologyObjectV2.js"; +import { isOsdkBaseObject } from "./isOsdkObject.js"; import { isWireObjectSet } from "./WireObjectSet.js"; /** @@ -65,6 +66,10 @@ export async function toDataValue( return await toDataValue(value.__primaryKey, client); } + if (isOsdkBaseObject(value)) { + return await toDataValue(value.$primaryKey, client); + } + // object set (the rid as a string (passes through the last return), or the ObjectSet definition directly) if (isWireObjectSet(value)) { return value;