Skip to content

Commit 7b059ad

Browse files
RandomByteGerrit Code Review
authored andcommitted
Merge "[FIX] sap.base.strings.formatMessage: Accept nullish arguments"
2 parents 77b9190 + 8606049 commit 7b059ad

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/sap.ui.core/src/sap/base/strings/formatMessage.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ sap.ui.define(["sap/base/assert"], function(assert) {
6666
* @public
6767
*/
6868
var fnFormatMessage = function(sPattern, aValues) {
69+
if (sPattern == null) {
70+
// Binding formatter case: arguments might be nullish while data is still being populated
71+
// Return an empty string in those cases
72+
return "";
73+
}
6974
assert(typeof sPattern === "string" || sPattern instanceof String, "pattern must be string");
7075
if (arguments.length > 2 || (aValues != null && !Array.isArray(aValues))) {
7176
aValues = Array.prototype.slice.call(arguments, 1);

src/sap.ui.core/test/sap/ui/core/qunit/base/strings/formatMessage.qunit.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ sap.ui.define(["sap/base/strings/formatMessage"], function(formatMessage) {
4040
assert.equal(formatMessage("Say '{0}'", "Hello"), "Say {0}", "quoted placeholder should be ignored");
4141
});
4242

43+
QUnit.test("nullish pattern", function(assert) {
44+
assert.equal(formatMessage(undefined, ["Hello"]), "", "should return empty string");
45+
assert.equal(formatMessage(undefined, 0), "", "should return empty string");
46+
assert.equal(formatMessage(null, ["Hello"]), "", "should return empty string");
47+
assert.equal(formatMessage(null, 0), "", "should return empty string");
48+
});
49+
4350
QUnit.test("pattern syntax errors", function(assert) {
44-
assert.expect(5);
51+
assert.expect(6);
4552
try {
4653
formatMessage("Say }", [12.4]);
4754
} catch (e) {
@@ -67,5 +74,10 @@ sap.ui.define(["sap/base/strings/formatMessage"], function(formatMessage) {
6774
} catch (e) {
6875
assert.ok(true, "nested placeholders raise an error");
6976
}
77+
try {
78+
formatMessage(false, [5]);
79+
} catch (e) {
80+
assert.ok(true, "boolean instead of string raise an error");
81+
}
7082
});
7183
});

0 commit comments

Comments
 (0)