mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
fix: transform pattern regression
This commit is contained in:
@@ -124,14 +124,14 @@ describe('transformPatternToRoute', () => {
|
|||||||
describe('when gets value exceeding max depth or max keys', () => {
|
describe('when gets value exceeding max depth or max keys', () => {
|
||||||
it('should return special string indicating the limit was reached', () => {
|
it('should return special string indicating the limit was reached', () => {
|
||||||
const deepNestedPattern = {
|
const deepNestedPattern = {
|
||||||
a: { b: { c: { d: { e: { f: 'too deep' } } } } },
|
a: { b: { c: { d: { e: { f: { g: 'too deep' } } } } } },
|
||||||
};
|
};
|
||||||
const tooManyKeysPattern = Object.fromEntries(
|
const tooManyKeysPattern = Object.fromEntries(
|
||||||
Array.from({ length: 25 }, (_, i) => [`key${i}`, `value${i}`]),
|
Array.from({ length: 25 }, (_, i) => [`key${i}`, `value${i}`]),
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(transformPatternToRoute(deepNestedPattern)).to.be.equal(
|
expect(transformPatternToRoute(deepNestedPattern)).to.be.equal(
|
||||||
'{"a":{"b":{"c":{"d":{"e":"[MAX_DEPTH_REACHED]"}}}}}',
|
'{"a":{"b":{"c":{"d":{"e":{"f":[MAX_DEPTH_REACHED]}}}}}}',
|
||||||
);
|
);
|
||||||
expect(transformPatternToRoute(tooManyKeysPattern)).to.be.equal(
|
expect(transformPatternToRoute(tooManyKeysPattern)).to.be.equal(
|
||||||
'"[TOO_MANY_KEYS]"',
|
'"[TOO_MANY_KEYS]"',
|
||||||
|
|||||||
@@ -23,38 +23,45 @@ export function transformPatternToRoute(
|
|||||||
maxDepth = DEFAULT_MAX_DEPTH,
|
maxDepth = DEFAULT_MAX_DEPTH,
|
||||||
maxKeys = DEFAULT_MAX_KEYS,
|
maxKeys = DEFAULT_MAX_KEYS,
|
||||||
): string {
|
): string {
|
||||||
// Prevent excessively deep recursion
|
|
||||||
if (depth > maxDepth) {
|
|
||||||
return '"[MAX_DEPTH_REACHED]"';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isString(pattern) || isNumber(pattern)) {
|
if (isString(pattern) || isNumber(pattern)) {
|
||||||
return `${pattern}`;
|
return `${pattern}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isObject(pattern)) {
|
if (!isObject(pattern)) {
|
||||||
return `"${String(pattern)}"`;
|
// For non-string, non-number, non-object values
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (depth > maxDepth) {
|
||||||
|
return '[MAX_DEPTH_REACHED]';
|
||||||
}
|
}
|
||||||
|
|
||||||
const keys = Object.keys(pattern);
|
const keys = Object.keys(pattern);
|
||||||
|
|
||||||
// Limit number of keys to prevent huge objects
|
|
||||||
if (keys.length > maxKeys) {
|
if (keys.length > maxKeys) {
|
||||||
return '"[TOO_MANY_KEYS]"';
|
return '[TOO_MANY_KEYS]';
|
||||||
}
|
}
|
||||||
|
|
||||||
const sortedKeys = keys.sort((a, b) => ('' + a).localeCompare(b));
|
const sortedKeys = keys.sort((a, b) => ('' + a).localeCompare(b));
|
||||||
|
|
||||||
const sortedPatternParams = sortedKeys.map(key => {
|
const parts = sortedKeys.map(key => {
|
||||||
const value = pattern[key];
|
const value = pattern[key];
|
||||||
const partialRoute = `"${key}":${transformPatternToRoute(
|
let partialRoute = `"${key}":`;
|
||||||
value,
|
|
||||||
depth + 1,
|
// Only quote strings, numbers and objects are handled recursively
|
||||||
maxDepth,
|
if (isString(value)) {
|
||||||
maxKeys,
|
partialRoute += `"${transformPatternToRoute(value, depth + 1, maxDepth, maxKeys)}"`;
|
||||||
)}`;
|
} else {
|
||||||
|
partialRoute += transformPatternToRoute(
|
||||||
|
value,
|
||||||
|
depth + 1,
|
||||||
|
maxDepth,
|
||||||
|
maxKeys,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return partialRoute;
|
return partialRoute;
|
||||||
});
|
});
|
||||||
|
|
||||||
return `{${sortedPatternParams.join(',')}}`;
|
return `{${parts.join(',')}}`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user