@digitraffic/common 2026.10.30-2 → 2026.11.2-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -131,11 +131,15 @@ describe("createKyHooks", () => {
|
|
|
131
131
|
expect(httpLogger.logResponse).toHaveBeenCalledWith(200, 5);
|
|
132
132
|
expect(cloneSpy).not.toHaveBeenCalled();
|
|
133
133
|
});
|
|
134
|
-
test("afterResponse
|
|
134
|
+
test("afterResponse leaves length unknown when header is missing and keeps body readable", async () => {
|
|
135
135
|
const { httpLogger, fn } = hook("afterResponse");
|
|
136
136
|
const response = new Response("hello", { status: 201 });
|
|
137
|
+
const cloneSpy = vi.spyOn(response, "clone");
|
|
137
138
|
const result = (await fn({ response }));
|
|
138
|
-
|
|
139
|
+
// The body must not be read just for logging. Buffering it would hold
|
|
140
|
+
// the whole payload in memory and block streaming consumers.
|
|
141
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(201, undefined);
|
|
142
|
+
expect(cloneSpy).not.toHaveBeenCalled();
|
|
139
143
|
expect(await result.text()).toBe("hello");
|
|
140
144
|
});
|
|
141
145
|
test("beforeError logs and returns non-HTTP errors", () => {
|
|
@@ -86,11 +86,24 @@ describe("convertJsonSchemaNullableToOneOfDeep", () => {
|
|
|
86
86
|
});
|
|
87
87
|
test("keeps null allowed when nullable schema is only a reference", () => {
|
|
88
88
|
const schema = convertJsonSchemaNullableToOneOfDeep({
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
type: "object",
|
|
90
|
+
definitions: {
|
|
91
|
+
Vessel: { type: "object" },
|
|
92
|
+
},
|
|
93
|
+
properties: {
|
|
94
|
+
vessel: { ref: "#/definitions/Vessel", nullable: true },
|
|
95
|
+
},
|
|
91
96
|
});
|
|
92
97
|
expect(schema).toEqual({
|
|
93
|
-
|
|
98
|
+
type: "object",
|
|
99
|
+
definitions: {
|
|
100
|
+
Vessel: { type: "object" },
|
|
101
|
+
},
|
|
102
|
+
properties: {
|
|
103
|
+
vessel: {
|
|
104
|
+
anyOf: [{ ref: "#/definitions/Vessel" }, { type: "null" }],
|
|
105
|
+
},
|
|
106
|
+
},
|
|
94
107
|
});
|
|
95
108
|
});
|
|
96
109
|
test("keeps null allowed when nullable schema is empty", () => {
|
|
@@ -280,6 +293,121 @@ describe("convertJsonSchemaNullableToOneOfDeep", () => {
|
|
|
280
293
|
],
|
|
281
294
|
});
|
|
282
295
|
});
|
|
296
|
+
test("throws when a sibling reference points inside a wrapped schema", () => {
|
|
297
|
+
// The reference target moves under oneOf[0] when address is wrapped,
|
|
298
|
+
// so the pointer would be left dangling
|
|
299
|
+
expect(() => convertJsonSchemaNullableToOneOfDeep({
|
|
300
|
+
type: "object",
|
|
301
|
+
properties: {
|
|
302
|
+
address: {
|
|
303
|
+
type: "object",
|
|
304
|
+
nullable: true,
|
|
305
|
+
properties: { code: { type: "string" } },
|
|
306
|
+
},
|
|
307
|
+
billingCode: { ref: "#/properties/address/properties/code" },
|
|
308
|
+
},
|
|
309
|
+
})).toThrow(/#\/properties\/address\/properties\/code/);
|
|
310
|
+
});
|
|
311
|
+
test("throws when a definitions reference points inside a wrapped schema", () => {
|
|
312
|
+
expect(() => convertJsonSchemaNullableToOneOfDeep({
|
|
313
|
+
type: "object",
|
|
314
|
+
definitions: {
|
|
315
|
+
Shortcut: { ref: "#/properties/address/properties/code" },
|
|
316
|
+
},
|
|
317
|
+
properties: {
|
|
318
|
+
address: {
|
|
319
|
+
type: "object",
|
|
320
|
+
nullable: true,
|
|
321
|
+
properties: { code: { type: "string" } },
|
|
322
|
+
},
|
|
323
|
+
link: { ref: "#/definitions/Shortcut" },
|
|
324
|
+
},
|
|
325
|
+
})).toThrow(/#\/properties\/address\/properties\/code/);
|
|
326
|
+
});
|
|
327
|
+
test("allows references whose target does not move", () => {
|
|
328
|
+
// address is wrapped, but the reference points to plain, which stays put
|
|
329
|
+
const schema = convertJsonSchemaNullableToOneOfDeep({
|
|
330
|
+
type: "object",
|
|
331
|
+
properties: {
|
|
332
|
+
plain: { type: "string" },
|
|
333
|
+
address: { type: "object", nullable: true },
|
|
334
|
+
copy: { ref: "#/properties/plain" },
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
expect(schema).toEqual({
|
|
338
|
+
type: "object",
|
|
339
|
+
properties: {
|
|
340
|
+
plain: { type: "string" },
|
|
341
|
+
address: { oneOf: [{ type: "object" }, { type: "null" }] },
|
|
342
|
+
copy: { ref: "#/properties/plain" },
|
|
343
|
+
},
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
test("accepts percent-encoded references", () => {
|
|
347
|
+
// "#/definitions/Postal%20Code" is a valid URI fragment pointer to
|
|
348
|
+
// a definition named "Postal Code"
|
|
349
|
+
const schema = {
|
|
350
|
+
type: "object",
|
|
351
|
+
definitions: {
|
|
352
|
+
"Postal Code": { type: "string" },
|
|
353
|
+
},
|
|
354
|
+
properties: {
|
|
355
|
+
code: { ref: "#/definitions/Postal%20Code" },
|
|
356
|
+
name: { type: "string", nullable: true },
|
|
357
|
+
},
|
|
358
|
+
};
|
|
359
|
+
expect(() => convertJsonSchemaNullableToOneOfDeep(schema)).not.toThrow();
|
|
360
|
+
});
|
|
361
|
+
test("rejects an array index with a leading zero", () => {
|
|
362
|
+
// "01" is not a valid JSON Pointer array index, so the reference
|
|
363
|
+
// cannot be resolved even though Number("01") is 1
|
|
364
|
+
expect(() => convertJsonSchemaNullableToOneOfDeep({
|
|
365
|
+
type: "object",
|
|
366
|
+
properties: {
|
|
367
|
+
choice: { anyOf: [{ type: "string" }, { type: "number" }] },
|
|
368
|
+
copy: { ref: "#/properties/choice/anyOf/01" },
|
|
369
|
+
name: { type: "string", nullable: true },
|
|
370
|
+
},
|
|
371
|
+
})).toThrow(/#\/properties\/choice\/anyOf\/01/);
|
|
372
|
+
});
|
|
373
|
+
test("rejects references to inherited object properties", () => {
|
|
374
|
+
// toString exists on Object.prototype but not in the JSON document
|
|
375
|
+
expect(() => convertJsonSchemaNullableToOneOfDeep({
|
|
376
|
+
type: "object",
|
|
377
|
+
definitions: {},
|
|
378
|
+
properties: {
|
|
379
|
+
bad: { ref: "#/definitions/toString" },
|
|
380
|
+
name: { type: "string", nullable: true },
|
|
381
|
+
},
|
|
382
|
+
})).toThrow(/#\/definitions\/toString/);
|
|
383
|
+
});
|
|
384
|
+
test("treats an encoded slash as a separator per RFC 6901", () => {
|
|
385
|
+
// RFC 6901 section 6: the whole fragment is percent-decoded first and
|
|
386
|
+
// then tokenized, so %2F becomes a separator. A slash inside a key must
|
|
387
|
+
// be escaped as ~1 instead.
|
|
388
|
+
expect(() => convertJsonSchemaNullableToOneOfDeep({
|
|
389
|
+
type: "object",
|
|
390
|
+
definitions: {
|
|
391
|
+
"a/b": { type: "string" },
|
|
392
|
+
},
|
|
393
|
+
properties: {
|
|
394
|
+
bad: { ref: "#/definitions/a%2Fb" },
|
|
395
|
+
name: { type: "string", nullable: true },
|
|
396
|
+
},
|
|
397
|
+
})).toThrow(/#\/definitions\/a%2Fb/);
|
|
398
|
+
});
|
|
399
|
+
test("resolves a slash in a key escaped as ~1", () => {
|
|
400
|
+
expect(() => convertJsonSchemaNullableToOneOfDeep({
|
|
401
|
+
type: "object",
|
|
402
|
+
definitions: {
|
|
403
|
+
"a/b": { type: "string" },
|
|
404
|
+
},
|
|
405
|
+
properties: {
|
|
406
|
+
good: { ref: "#/definitions/a~1b" },
|
|
407
|
+
name: { type: "string", nullable: true },
|
|
408
|
+
},
|
|
409
|
+
})).not.toThrow();
|
|
410
|
+
});
|
|
283
411
|
test("creates an API Gateway schema from Zod", () => {
|
|
284
412
|
const schema = toApiGatewayJsonSchema(z.object({
|
|
285
413
|
reason: z.string().nullable(),
|
|
@@ -59,17 +59,12 @@ export function createKyHooks(httpLogger) {
|
|
|
59
59
|
},
|
|
60
60
|
],
|
|
61
61
|
afterResponse: [
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
// create clone from the response, so we can read its body without affecting the original response
|
|
70
|
-
const resp = state.response.clone();
|
|
71
|
-
const contentLength = (await resp.bytes()).length;
|
|
72
|
-
httpLogger.logResponse(resp.status, contentLength);
|
|
62
|
+
(state) => {
|
|
63
|
+
// Content length is taken from the header only. When the header is
|
|
64
|
+
// missing, for example with chunked responses, the length is left
|
|
65
|
+
// unknown: reading the body here would buffer the whole payload in
|
|
66
|
+
// memory and block streaming consumers until EOF.
|
|
67
|
+
httpLogger.logResponse(state.response.status, parseContentLengthFromHeader(state.response.headers.get("content-length")));
|
|
73
68
|
return state.response;
|
|
74
69
|
},
|
|
75
70
|
],
|
|
@@ -30,10 +30,12 @@ type JsonSchemaWithNullable = JsonSchema & {
|
|
|
30
30
|
* allowed. The conversion is applied recursively to nested schemas. Field
|
|
31
31
|
* names and literal values such as defaults are left untouched.
|
|
32
32
|
*
|
|
33
|
-
* Limitation:
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
33
|
+
* Limitation: wrapping moves schema content under oneOf/anyOf, which can
|
|
34
|
+
* break local references such as `#/properties/...` pointing at the old
|
|
35
|
+
* location. After conversion every local reference is checked against the
|
|
36
|
+
* result, and an error is thrown instead of producing a model with dangling
|
|
37
|
+
* references. References to `#/definitions/...` targets stay valid because
|
|
38
|
+
* definitions are kept at the root. Zod output never contains such
|
|
37
39
|
* references, so this only matters for hand-written schemas.
|
|
38
40
|
*/
|
|
39
41
|
export declare function convertJsonSchemaNullableToOneOfDeep(schema: JsonSchemaWithNullable): JsonSchemaWithNullable;
|
package/dist/utils/api-model.js
CHANGED
|
@@ -90,21 +90,77 @@ function findLocalReferences(schema) {
|
|
|
90
90
|
}
|
|
91
91
|
return references;
|
|
92
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Checks whether a local JSON Pointer such as "#/definitions/Code" resolves
|
|
95
|
+
* to a value in the document. The pointer is a URI fragment: per RFC 6901
|
|
96
|
+
* section 6 the whole fragment is percent-decoded first and then tokenized,
|
|
97
|
+
* so an encoded slash (%2F) is a separator. A slash inside a key must be
|
|
98
|
+
* escaped as ~1.
|
|
99
|
+
*/
|
|
100
|
+
function resolvesInDocument(root, pointer) {
|
|
101
|
+
let decoded;
|
|
102
|
+
try {
|
|
103
|
+
decoded = decodeURIComponent(pointer.slice(1));
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// malformed percent-encoding cannot resolve
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
const parts = decoded
|
|
110
|
+
.slice(1)
|
|
111
|
+
.split("/")
|
|
112
|
+
.map((part) => part.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
113
|
+
let current = root;
|
|
114
|
+
for (const part of parts) {
|
|
115
|
+
if (Array.isArray(current)) {
|
|
116
|
+
// RFC 6901: an array index is "0" or digits without a leading zero
|
|
117
|
+
if (!/^(0|[1-9][0-9]*)$/.test(part)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
const index = Number(part);
|
|
121
|
+
if (index >= current.length) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
current = current[index];
|
|
125
|
+
}
|
|
126
|
+
else if (isObject(current)) {
|
|
127
|
+
// own property only: inherited ones such as toString are not
|
|
128
|
+
// members of the JSON document
|
|
129
|
+
if (!Object.hasOwn(current, part)) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
current = current[part];
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Throws if the converted schema contains local references that no longer
|
|
142
|
+
* resolve. Wrapping nullable schemas moves their content under oneOf/anyOf,
|
|
143
|
+
* which can leave references pointing at the old location. Failing at synth
|
|
144
|
+
* time is safer than deploying a model with dangling references.
|
|
145
|
+
*
|
|
146
|
+
* This is an existence check only. In the unlikely case that a reference
|
|
147
|
+
* path stays occupied but its content changes, for example "#/oneOf/1"
|
|
148
|
+
* pointing at a union that gets a null alternative inserted, the change is
|
|
149
|
+
* not detected. Do not reference union alternatives by index.
|
|
150
|
+
*/
|
|
151
|
+
function assertLocalReferencesResolve(schema) {
|
|
152
|
+
const broken = findLocalReferences(schema).filter((reference) => !resolvesInDocument(schema, reference));
|
|
153
|
+
if (broken.length > 0) {
|
|
154
|
+
throw new Error(`Converting nullable schemas broke local references: ${broken.join(", ")}. Restructure the schema, for example move the referenced schema to definitions.`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
93
157
|
/**
|
|
94
158
|
* Wraps a schema and a null alternative into a union. Definitions are kept at
|
|
95
159
|
* the root, otherwise local references like "#/definitions/X" would point to
|
|
96
160
|
* nothing after wrapping.
|
|
97
|
-
*
|
|
98
|
-
* Throws if the schema contains local references to other locations, such as
|
|
99
|
-
* "#/properties/...". Wrapping would silently break them, so failing at synth
|
|
100
|
-
* time is safer than deploying a broken model.
|
|
101
161
|
*/
|
|
102
162
|
function wrapSchemaWithNull(schema, unionKeyword) {
|
|
103
163
|
const { definitions, ...rest } = schema;
|
|
104
|
-
const brokenReferences = findLocalReferences(rest).filter((reference) => !reference.startsWith("#/definitions/"));
|
|
105
|
-
if (brokenReferences.length > 0) {
|
|
106
|
-
throw new Error(`Cannot convert nullable schema: wrapping would break local references: ${brokenReferences.join(", ")}. Only #/definitions/ references are supported.`);
|
|
107
|
-
}
|
|
108
164
|
const wrapped = {
|
|
109
165
|
[unionKeyword]: [rest, { type: "null" }],
|
|
110
166
|
};
|
|
@@ -184,14 +240,18 @@ function removePatternKeyword(schema) {
|
|
|
184
240
|
* allowed. The conversion is applied recursively to nested schemas. Field
|
|
185
241
|
* names and literal values such as defaults are left untouched.
|
|
186
242
|
*
|
|
187
|
-
* Limitation:
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
243
|
+
* Limitation: wrapping moves schema content under oneOf/anyOf, which can
|
|
244
|
+
* break local references such as `#/properties/...` pointing at the old
|
|
245
|
+
* location. After conversion every local reference is checked against the
|
|
246
|
+
* result, and an error is thrown instead of producing a model with dangling
|
|
247
|
+
* references. References to `#/definitions/...` targets stay valid because
|
|
248
|
+
* definitions are kept at the root. Zod output never contains such
|
|
191
249
|
* references, so this only matters for hand-written schemas.
|
|
192
250
|
*/
|
|
193
251
|
export function convertJsonSchemaNullableToOneOfDeep(schema) {
|
|
194
|
-
|
|
252
|
+
const converted = transformSchemaDeep(schema, convertNullableSchema);
|
|
253
|
+
assertLocalReferencesResolve(converted);
|
|
254
|
+
return converted;
|
|
195
255
|
}
|
|
196
256
|
/**
|
|
197
257
|
* Creates a JSON Schema that can be used as an API Gateway model from a Zod schema.
|
|
@@ -226,7 +286,9 @@ export function toApiGatewayJsonSchema(schema) {
|
|
|
226
286
|
target: "openapi-3.0",
|
|
227
287
|
unrepresentable: "any",
|
|
228
288
|
});
|
|
229
|
-
|
|
289
|
+
const converted = transformSchemaDeep(jsonSchema, (schemaObject) => convertNullableSchema(removePatternKeyword(schemaObject)));
|
|
290
|
+
assertLocalReferencesResolve(converted);
|
|
291
|
+
return converted;
|
|
230
292
|
}
|
|
231
293
|
/**
|
|
232
294
|
* Get a reference to an OpenAPI model object in a REST API.
|