Skip to content

Commit 1594afe

Browse files
authored
add runtime deprecation warning (#7451)
1 parent 05882bc commit 1594afe

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ ___
135135
- Fix incorrect LiveQuery events triggered for multiple subscriptions on the same class with different events [#7341](https://github.com/parse-community/parse-server/pull/7341)
136136
- Fix select and excludeKey queries to properly accept JSON string arrays. Also allow nested fields in exclude (Corey Baker) [#7242](https://github.com/parse-community/parse-server/pull/7242)
137137
- Fix LiveQuery server crash when using $all query operator on a missing object key (Jason Posthuma) [#7421](https://github.com/parse-community/parse-server/pull/7421)
138-
138+
- Added runtime deprecation warnings (Manuel Trezza) [#7451](https://github.com/parse-community/parse-server/pull/7451)
139139
___
140140
## 4.5.0
141141
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0)

CONTRIBUTING.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ If you change or remove an existing feature that would lead to a breaking change
182182
- Use a default value that falls back to existing behavior.
183183
- Add a deprecation definition in `Deprecator/Deprecations.js` that will output a deprecation warning log message on Parse Server launch, for example:
184184
> DeprecationWarning: The Parse Server option 'example' will be removed in a future release.
185-
185+
186+
For deprecations that can only be determined ad-hoc during runtime, for example Parse Query syntax deprecations, use the `Deprecator.logRuntimeDeprecation()` method.
187+
186188
Deprecations become breaking changes after notifying developers through deprecation warnings for at least one entire previous major release. For example:
187189
- `4.5.0` is the current version
188190
- `4.6.0` adds a new optional feature and a deprecation warning for the existing feature

spec/Deprecator.spec.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,28 @@ describe('Deprecator', () => {
2121
const logSpy = spyOn(logger, 'warn').and.callFake(() => {});
2222

2323
await reconfigureServer();
24-
expect(logSpy.calls.all()[0].args[0]).toContain(deprecations[0].optionKey);
25-
expect(logSpy.calls.all()[0].args[0]).toContain(deprecations[0].changeNewDefault);
24+
expect(logSpy.calls.all()[0].args[0]).toEqual(
25+
`DeprecationWarning: The Parse Server option '${deprecations[0].optionKey}' default will change to '${deprecations[0].changeNewDefault}' in a future version.`
26+
);
2627
});
2728

2829
it('does not log deprecation for new default if option is set manually', async () => {
2930
deprecations = [{ optionKey: 'exampleKey', changeNewDefault: 'exampleNewDefault' }];
3031

3132
spyOn(Deprecator, '_getDeprecations').and.callFake(() => deprecations);
32-
const logSpy = spyOn(Deprecator, '_log').and.callFake(() => {});
33+
const logSpy = spyOn(Deprecator, '_logOption').and.callFake(() => {});
3334
await reconfigureServer({ [deprecations[0].optionKey]: 'manuallySet' });
3435
expect(logSpy).not.toHaveBeenCalled();
3536
});
37+
38+
it('logs runtime deprecation', async () => {
39+
const logger = require('../lib/logger').logger;
40+
const logSpy = spyOn(logger, 'warn').and.callFake(() => {});
41+
const options = { usage: 'Doing this', solution: 'Do that instead.' };
42+
43+
Deprecator.logRuntimeDeprecation(options);
44+
expect(logSpy.calls.all()[0].args[0]).toEqual(
45+
`DeprecationWarning: ${options.usage} is deprecated and will be removed in a future version. ${options.solution}`
46+
);
47+
});
3648
});

src/Deprecator/Deprecator.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,41 @@ class Deprecator {
2222

2323
// If default will change, only throw a warning if option is not set
2424
if (changeNewDefault != null && options[optionKey] == null) {
25-
Deprecator._log({ optionKey, changeNewDefault, solution });
25+
Deprecator._logOption({ optionKey, changeNewDefault, solution });
2626
}
2727
}
2828
}
2929

30+
/**
31+
* Logs a deprecation warning for a parameter that can only be determined dynamically
32+
* during runtime.
33+
*
34+
* Note: Do not use this to log deprecations of Parse Server options, but add such
35+
* deprecations to `Deprecations.js` instead. See the contribution docs for more
36+
* details.
37+
*
38+
* For consistency, the deprecation warning is composed of the following parts:
39+
*
40+
* > DeprecationWarning: `usage` is deprecated and will be removed in a future version.
41+
* `solution`.
42+
*
43+
* - `usage`: The deprecated usage.
44+
* - `solution`: The instruction to resolve this deprecation warning.
45+
*
46+
* For example:
47+
* > DeprecationWarning: `Prefixing field names with dollar sign ($) in aggregation query`
48+
* is deprecated and will be removed in a future version. `Reference field names without
49+
* dollar sign prefix.`
50+
*
51+
* @param {Object} options The deprecation options.
52+
* @param {String} options.usage The usage that is deprecated.
53+
* @param {String} [options.solution] The instruction to resolve this deprecation warning.
54+
* Optional. It is recommended to add an instruction for the convenience of the developer.
55+
*/
56+
static logRuntimeDeprecation(options) {
57+
Deprecator._logGeneric(options);
58+
}
59+
3060
/**
3161
* Returns the deprecation definitions.
3262
* @returns {Array<Object>} The deprecations.
@@ -35,8 +65,24 @@ class Deprecator {
3565
return Deprecations;
3666
}
3767

68+
/**
69+
* Logs a generic deprecation warning.
70+
*
71+
* @param {Object} options The deprecation options.
72+
* @param {String} options.usage The usage that is deprecated.
73+
* @param {String} [options.solution] The instruction to resolve this deprecation warning.
74+
* Optional. It is recommended to add an instruction for the convenience of the developer.
75+
*/
76+
static _logGeneric({ usage, solution }) {
77+
// Compose message
78+
let output = `DeprecationWarning: ${usage} is deprecated and will be removed in a future version.`;
79+
output += solution ? ` ${solution}` : '';
80+
logger.warn(output);
81+
}
82+
3883
/**
3984
* Logs a deprecation warning for a Parse Server option.
85+
*
4086
* @param {String} optionKey The option key incl. its path, e.g. `security.enableCheck`.
4187
* @param {String} envKey The environment key, e.g. `PARSE_SERVER_SECURITY`.
4288
* @param {String} changeNewKey Set the new key name if the current key will be replaced,
@@ -48,7 +94,7 @@ class Deprecator {
4894
* automatically added to the message. It should only contain the instruction on how
4995
* to resolve this warning.
5096
*/
51-
static _log({ optionKey, envKey, changeNewKey, changeNewDefault, solution }) {
97+
static _logOption({ optionKey, envKey, changeNewKey, changeNewDefault, solution }) {
5298
const type = optionKey ? 'option' : 'environment key';
5399
const key = optionKey ? optionKey : envKey;
54100
const keyAction =

0 commit comments

Comments
 (0)