Skip to content

Commit

Permalink
fix(template): Proxy Compile Input loses values from arrays (#21943)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekerson committed May 3, 2023
1 parent 8bdae52 commit 0913787
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/util/template/index.spec.ts
Expand Up @@ -116,22 +116,25 @@ describe('util/template/index', () => {

describe('proxyCompileInput', () => {
const allowedField = 'body';
const allowedArrayField = 'prBodyNotes';
const forbiddenField = 'foobar';

type TestCompileInput = Record<
typeof allowedField | typeof forbiddenField,
typeof allowedField | typeof allowedArrayField | typeof forbiddenField,
unknown
>;

const compileInput: TestCompileInput = {
[allowedField]: 'allowed',
[allowedArrayField]: ['allowed'],
[forbiddenField]: 'forbidden',
};

it('accessing allowed files', () => {
it('accessing allowed fields', () => {
const p = template.proxyCompileInput(compileInput);

expect(p[allowedField]).toBe('allowed');
expect(p[allowedArrayField]).toStrictEqual(['allowed']);
expect(p[forbiddenField]).toBeUndefined();
});

Expand All @@ -153,6 +156,7 @@ describe('util/template/index', () => {
const arr = proxy[allowedField] as TestCompileInput[];
const obj = arr[0];
expect(obj[allowedField]).toBe('allowed');
expect(obj[allowedArrayField]).toStrictEqual(['allowed']);
expect(obj[forbiddenField]).toBeUndefined();
});
});
Expand Down
8 changes: 5 additions & 3 deletions lib/util/template/index.ts
Expand Up @@ -185,9 +185,11 @@ const compileInputProxyHandler: ProxyHandler<CompileInput> = {
const value = target[prop];

if (is.array(value)) {
return value
.filter(is.plainObject)
.map((element) => proxyCompileInput(element as CompileInput));
return value.map((element) =>
is.primitive(element)
? element
: proxyCompileInput(element as CompileInput)
);
}

if (is.plainObject(value)) {
Expand Down

0 comments on commit 0913787

Please sign in to comment.