Skip to content

Commit

Permalink
Don't throw errors when converting wire objects with unknown api names
Browse files Browse the repository at this point in the history
  • Loading branch information
mfedderly committed Feb 2, 2024
1 parent a5c361c commit a8756b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/client/src/object/convertWireToOsdkObjects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
* limitations under the License.
*/

import { createClientContext } from "@osdk/shared.net";
import { apiServer } from "@osdk/shared.test";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import type { Client } from "../Client.js";
import { createClient } from "../createClient.js";
import { Ontology as MockOntology } from "../generatedNoCheck/index.js";
import { Attachment } from "./Attachment.js";
import { convertWireToOsdkObjects } from "./convertWireToOsdkObjects.js";

describe("convertWireToOsdkObjects", () => {
let client: Client<typeof MockOntology>;
Expand Down Expand Up @@ -65,4 +67,23 @@ describe("convertWireToOsdkObjects", () => {
expect(emptyAttachment).toBeUndefined();
expect(emptyAttachmentArray).toBeUndefined();
});

it("works even with unknown apiNames", () => {
const clientCtx = createClientContext(
MockOntology,
"https://stack.palantir.com",
() => "myAccessToken",
"userAgent",
);

const object = {
__apiName: "unknown",
__primaryKey: 0,
} as const;
const prototypeBefore = Object.getPrototypeOf(object);
convertWireToOsdkObjects(clientCtx, [object]);
const prototypeAfter = Object.getPrototypeOf(object);

expect(prototypeBefore).not.toBe(prototypeAfter);
});
});
11 changes: 10 additions & 1 deletion packages/client/src/object/convertWireToOsdkObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function createPrototype<
const objDef = ontology.objects[type];
const proto = {};

if (!objDef) {
return proto;
}

Object.defineProperty(proto, "$link", {
get: function() {
const client = this[OriginClient] as ClientContext<O>;
Expand Down Expand Up @@ -116,10 +120,15 @@ function createConverter<
ontology: O,
type: T,
) {
const objDef = ontology.objects[type];
if (!objDef) {
return false as const;
}

const steps: Array<(o: Record<string, any>) => void> = [];

for (
const [key, value] of Object.entries(ontology.objects[type].properties)
const [key, value] of Object.entries(objDef.properties)
) {
// attachments need a wrapper to provide functionality and to identify them at serialization time
if (value.type === "attachment") {
Expand Down

0 comments on commit a8756b2

Please sign in to comment.