diff --git a/src/adapters/http/schema-registry.ts b/src/adapters/http/schema-registry.ts index ffe12b80..b22ca36b 100644 --- a/src/adapters/http/schema-registry.ts +++ b/src/adapters/http/schema-registry.ts @@ -1,10 +1,16 @@ import { z } from "../../contracts/zod.ts"; import { projectValidationIssues } from "../../contracts/validation-issues.ts"; +/** + * 백엔드 1101/02-02. The paired backend drops a null field together with its + * key (clean-architecture-backend-template docs/web/http-contract.md §1). The + * id keys are absent on an answer written before its request id filter, so + * they are optional here too. + */ const metaSchema = z .object({ - requestId: z.string().regex(/^[A-Za-z0-9._:-]{1,128}$/), - traceId: z.string().regex(/^[A-Za-z0-9._:-]{1,128}$/), + requestId: z.string().regex(/^[A-Za-z0-9._:-]{1,128}$/).optional(), + traceId: z.string().regex(/^[A-Za-z0-9._:-]{1,128}$/).optional(), correlationId: z.string().regex(/^[A-Za-z0-9._:-]{1,128}$/).optional(), }) .strip(); @@ -12,7 +18,8 @@ const metaSchema = z export const successEnvelopeSchema = z .object({ success: z.literal(true), - data: z.unknown(), + // A missing `data` is the null the backend dropped. + data: z.unknown().default(null), meta: metaSchema, }) .strict(); diff --git a/tests/integration/http-client.test.ts b/tests/integration/http-client.test.ts index bb8eb096..44243efe 100644 --- a/tests/integration/http-client.test.ts +++ b/tests/integration/http-client.test.ts @@ -3,6 +3,7 @@ import { setupServer } from "msw/node"; import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; import { createHttpClient } from "../../src/adapters/http/client.ts"; +import { mappingSuccess } from "../../src/contracts/boundary-mapper.ts"; import { TEST_HTTP_CONTRACT, TEST_OPERATIONS, @@ -509,3 +510,67 @@ describe("shared HTTP client", () => { expect(JSON.stringify(result)).not.toContain("raw mapper detail"); }); }); + +// 백엔드 1101/02-02. The paired backend drops a null field together with its +// key (its docs/web/http-contract.md §1). A missing key must read as the null +// it stands for. +describe("envelope keys the paired backend omits", () => { + const meta = { requestId: "request-1", traceId: "trace-1" }; + + it.each([ + { shape: "data: null", body: { success: true, data: null, meta } }, + { shape: "no data key", body: { success: true, meta } }, + ])("reads a success with $shape as null data", async ({ body }) => { + server.use( + http.get("https://api.test/api/entities", () => HttpResponse.json(body)), + ); + const validated: unknown[] = []; + const client = testClient({ + baseUrl: "https://api.test", + clock, + validatePayload: (_schemaId, value) => { + validated.push(value); + return { success: true, data: value }; + }, + mapPayload: (_operationId, value) => mappingSuccess(value), + }); + + await expect(client.execute("LIST_ENTITIES")).resolves.toMatchObject({ + ok: true, + value: null, + }); + expect(validated).toEqual([null]); + }); + + it.each([ + { shape: "with ids and details", meta, details: { reason: "X" } }, + { shape: "without details", meta, details: undefined }, + // Answered before the request id filter: the id keys are absent. + { shape: "without ids", meta: {}, details: undefined }, + ])("keeps the backend code of a failure $shape", async ({ meta, details }) => { + server.use( + http.get("https://api.test/api/entities", () => + HttpResponse.json( + { + success: false, + error: { + code: "ACCESS_DENIED", + category: "FORBIDDEN", + message: "권한이 없습니다.", + retryable: false, + ...(details === undefined ? {} : { details }), + }, + meta, + }, + { status: 403 }, + ), + ), + ); + const client = testClient({ baseUrl: "https://api.test", clock }); + + await expect(client.execute("LIST_ENTITIES")).resolves.toMatchObject({ + ok: false, + error: { kind: "FORBIDDEN", code: "ACCESS_DENIED", httpStatus: 403 }, + }); + }); +});