Skip to content

Commit ab3ffcf

Browse files
popomoreatian25
authored andcommitted
fix: customLoader should not overwrite the existing property (#203)
1 parent 01201c3 commit ab3ffcf

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

lib/loader/mixin/custom_loader.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = {
2828

2929
switch (inject) {
3030
case 'ctx': {
31+
assert(!(property in loader.app.context), `customLoader should not override ctx.${property}`);
3132
const defaultConfig = {
3233
caseStyle: 'lower',
3334
fieldClass: `${property}Classes`,
@@ -36,6 +37,7 @@ module.exports = {
3637
break;
3738
}
3839
case 'app': {
40+
assert(!(property in loader.app), `customLoader should not override app.${property}`);
3941
const defaultConfig = {
4042
caseStyle: 'lower',
4143
initializer(Class) {

test/loader/mixin/load_custom_loader.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,42 @@ describe('test/loader/mixin/load_custom_loader.test.js', function() {
9292
app.close();
9393
}
9494
});
95+
96+
it('should not overwrite the existing property', () => {
97+
const app = utils.createApp('custom-loader');
98+
try {
99+
app.loader.config = {
100+
customLoader: {
101+
config: {
102+
directory: 'app/config',
103+
inject: 'app',
104+
},
105+
},
106+
};
107+
app.loader.loadCustomLoader();
108+
throw new Error('should not run');
109+
} catch (err) {
110+
assert(err.message === 'customLoader should not override app.config');
111+
} finally {
112+
app.close();
113+
}
114+
115+
try {
116+
app.loader.config = {
117+
customLoader: {
118+
cookies: {
119+
directory: 'app/cookies',
120+
inject: 'ctx',
121+
},
122+
},
123+
};
124+
app.loader.loadCustomLoader();
125+
throw new Error('should not run');
126+
} catch (err) {
127+
assert(err.message === 'customLoader should not override ctx.cookies');
128+
} finally {
129+
app.close();
130+
}
131+
});
132+
95133
});

0 commit comments

Comments
 (0)