Skip to content

Commit

Permalink
use alwaysEmitImplicit to match gRPC response
Browse files Browse the repository at this point in the history
  • Loading branch information
gimmyxd committed Dec 19, 2024
1 parent e900444 commit addefeb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 37 deletions.
20 changes: 14 additions & 6 deletions __tests__/directory/v3/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ describe("DirectoryV3", () => {
);
expect(result).toEqual({
check: true,
trace: [],
});

mockCheck.mockReset();
Expand Down Expand Up @@ -504,9 +505,12 @@ describe("DirectoryV3", () => {
const result = await directory.object(params);

expect(result).toEqual({
relations: [],
result: {
id: "123",
type: "user",
displayName: "",
etag: "",
},
});

Expand Down Expand Up @@ -565,7 +569,7 @@ describe("DirectoryV3", () => {
);
const result = await directory.objects(params);

expect(result).toEqual({});
expect(result).toEqual({ results: [] });

mockGetObjects.mockReset();
});
Expand Down Expand Up @@ -598,7 +602,7 @@ describe("DirectoryV3", () => {
);
const result = await directory.objects(params);

expect(result).toEqual({});
expect(result).toEqual({ results: [] });

mockGetObjects.mockReset();
});
Expand Down Expand Up @@ -631,7 +635,7 @@ describe("DirectoryV3", () => {
);
const result = await directory.objects(params);

expect(result).toEqual({});
expect(result).toEqual({ results: [] });

mockGetObjects.mockReset();
});
Expand Down Expand Up @@ -665,7 +669,7 @@ describe("DirectoryV3", () => {
);
const result = await directory.objects(params);

expect(result).toEqual({});
expect(result).toEqual({ results: [] });

mockGetObjects.mockReset();
});
Expand Down Expand Up @@ -815,7 +819,7 @@ describe("DirectoryV3", () => {
const result = await directory.objectMany(params);

expect(result).toEqual({
results: [{ type: "user", id: "123" }],
results: [{ type: "user", id: "123", displayName: "", etag: "" }],
});

mockGetObjectMany.mockReset();
Expand Down Expand Up @@ -866,6 +870,7 @@ describe("DirectoryV3", () => {
);
expect(result).toEqual({
results: [{ objectId: "1234", objectType: "user" }],
trace: [],
});

mockGetGraph.mockReset();
Expand Down Expand Up @@ -950,12 +955,15 @@ describe("DirectoryV3", () => {
const result = await directory.relation(params);

expect(result).toEqual({
objects: {},
result: {
etag: "",
subjectType: "user",
subjectId: "123",
objectType: "identity",
objectId: "identity",
relation: "identifier",
subjectRelation: "",
},
});

Expand Down Expand Up @@ -1009,7 +1017,7 @@ describe("DirectoryV3", () => {
);
const result = await directory.relations(params);

expect(result).toEqual({});
expect(result).toEqual({ objects: {}, results: [] });

mockGetRelations.mockReset();
});
Expand Down
5 changes: 4 additions & 1 deletion __tests__/directory/v3/serializeResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,20 @@ describe("serializeResponse() serializeResponse method", () => {
type: "user",
id: "123",
displayName: "",
etag: "",
etag: "1234",
},
relations: [],
};

const result = serializeResponse(mockResponse);

expect(result).toEqual({
relations: [],
result: {
id: "123",
type: "user",
displayName: "",
etag: "1234",
},
});
});
Expand Down
20 changes: 18 additions & 2 deletions __tests__/integration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe("Integration", () => {
expect(res.status).toBe(200);
expect(res.body).toEqual({
check: true,
trace: [],
});
});

Expand Down Expand Up @@ -258,6 +259,7 @@ describe("Integration", () => {

expect(getObjectRes.status).toBe(200);
expect(getObjectRes.body).toEqual({
relations: [],
result: expect.objectContaining({
id: "object-1",
type: "user",
Expand Down Expand Up @@ -291,6 +293,7 @@ describe("Integration", () => {

expect(getEditedObjectRes.status).toBe(200);
expect(getEditedObjectRes.body).toEqual({
relations: [],
result: expect.objectContaining({
id: "object-1",
type: "user",
Expand Down Expand Up @@ -601,8 +604,8 @@ describe("Integration", () => {
{},
{},
{
object: { recv: "2", set: "2" },
relation: { recv: "1", set: "1" },
object: { recv: "2", set: "2", delete: "0", error: "0" },
relation: { recv: "1", set: "1", delete: "0", error: "0" },
},
]);
});
Expand Down Expand Up @@ -686,6 +689,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "CiRmZDA2MTRkMy1jMzlhLTQ3ODEtYjdiZC04Yjk2ZjVhNTEwMGQSBWxvY2Fs",
displayName: "",
properties: {
kind: "IDENTITY_KIND_PID",
provider: "local",
Expand All @@ -700,6 +704,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "CiRmZDE2MTRkMy1jMzlhLTQ3ODEtYjdiZC04Yjk2ZjVhNTEwMGQSBWxvY2Fs",
displayName: "",
properties: {
kind: "IDENTITY_KIND_PID",
provider: "local",
Expand All @@ -714,6 +719,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "CiRmZDI2MTRkMy1jMzlhLTQ3ODEtYjdiZC04Yjk2ZjVhNTEwMGQSBWxvY2Fs",
displayName: "",
properties: {
kind: "IDENTITY_KIND_PID",
provider: "local",
Expand All @@ -728,6 +734,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "CiRmZDM2MTRkMy1jMzlhLTQ3ODEtYjdiZC04Yjk2ZjVhNTEwMGQSBWxvY2Fs",
displayName: "",
properties: {
kind: "IDENTITY_KIND_PID",
provider: "local",
Expand All @@ -742,6 +749,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "CiRmZDQ2MTRkMy1jMzlhLTQ3ODEtYjdiZC04Yjk2ZjVhNTEwMGQSBWxvY2Fs",
displayName: "",
properties: {
kind: "IDENTITY_KIND_PID",
provider: "local",
Expand All @@ -756,6 +764,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "[email protected]",
displayName: "",
properties: {
kind: "IDENTITY_KIND_EMAIL",
provider: "local",
Expand All @@ -770,6 +779,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "[email protected]",
displayName: "",
properties: {
kind: "IDENTITY_KIND_EMAIL",
provider: "local",
Expand All @@ -784,6 +794,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "[email protected]",
displayName: "",
properties: {
kind: "IDENTITY_KIND_EMAIL",
provider: "local",
Expand All @@ -798,6 +809,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "[email protected]",
displayName: "",
properties: {
verified: true,
kind: "IDENTITY_KIND_EMAIL",
Expand All @@ -812,6 +824,7 @@ describe("Integration", () => {
object: {
type: "identity",
id: "[email protected]",
displayName: "",
properties: {
kind: "IDENTITY_KIND_EMAIL",
provider: "local",
Expand Down Expand Up @@ -1229,6 +1242,7 @@ types:
objectType: "group",
}),
).toEqual({
objects: {},
result: expect.objectContaining({
subjectId: "test-user",
subjectType: "user",
Expand Down Expand Up @@ -1273,6 +1287,7 @@ types:
},
}),
).toEqual({
objects: {},
page: { nextToken: "" },
results: [
expect.objectContaining({
Expand Down Expand Up @@ -1373,6 +1388,7 @@ types:
it("returns [] when there are no objects", async () => {
expect(await directoryClient.objects({ objectType: "user" })).toEqual({
page: { nextToken: "" },
results: [],
});
});

Expand Down
37 changes: 10 additions & 27 deletions lib/directory/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,12 @@ export class DirectoryV3 {
params.page.size ||= 100;
}

const response = serializeResponse(
await this.ReaderClient.getObject(
create(GetObjectRequestSchema, params),
options,
),
const response = await this.ReaderClient.getObject(
create(GetObjectRequestSchema, params),
options,
);

if (response.page && response.page.nextToken === undefined) {
response.page.nextToken = "";
}
return response;
return serializeResponse(response);
} catch (error) {
throw handleError(error, "object");
}
Expand All @@ -331,16 +326,11 @@ export class DirectoryV3 {
if (params.page) {
params.page.size ||= 100;
}
const response = serializeResponse(
await this.ReaderClient.getObjects(
create(GetObjectsRequestSchema, params),
options,
),
const response = await this.ReaderClient.getObjects(
create(GetObjectsRequestSchema, params),
options,
);
if (response.page && response.page.nextToken === undefined) {
response.page.nextToken = "";
}
return response;
return serializeResponse(response);
} catch (error) {
throw handleError(error, "objects");
}
Expand Down Expand Up @@ -457,15 +447,8 @@ export class DirectoryV3 {
params.page.size ||= 100;
}

const response = serializeResponse(
await this.ReaderClient.getRelations(params, options),
);

if (response.page && response.page.nextToken === undefined) {
response.page.nextToken = "";
}

return response;
const response = await this.ReaderClient.getRelations(params, options);
return serializeResponse(response);
} catch (error) {
throw handleError(error, "relations");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/directory/v3/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export function serializeResponse<T extends Message>(
`schema not registered for type: [${response.$typeName}]`,
);
}
return toJson(schema, response) as unknown as T;
return toJson(schema, response, { alwaysEmitImplicit: true }) as unknown as T;
}

0 comments on commit addefeb

Please sign in to comment.