Skip to content

AbstractMessageSource does not support null as default message anymore [SPR-16127] #20675

Closed
@spring-projects-issues

Description

@spring-projects-issues

Thomas Heigl opened SPR-16127 and commented

I just encountered some issues in my application due to a subtle change in AbstractMessageSource between Spring 4.x and 5.x.

In Spring 4.x is was possible to pass null as defaultMessage, Spring 5.x always returns an empty string.

Spring 4.x:

public final String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
	String msg = getMessageInternal(code, args, locale);
	if (msg != null) {
		return msg;
	}
	if (defaultMessage == null) {
		String fallback = getDefaultMessage(code);
		if (fallback != null) {
			return fallback;
		}
	}
	return renderDefaultMessage(defaultMessage, args, locale);
}

Spring 5.x:

public final String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
	String msg = getMessageInternal(code, args, locale);
	if (msg != null) {
		return msg;
	}
	if (defaultMessage == null) {
		String fallback = getDefaultMessage(code);
		return (fallback != null ? fallback : "");
	}
	return renderDefaultMessage(defaultMessage, args, locale);
}

If the default message is null and the fallback is null Spring now returns "".

This makes it hard to distinguish between properties that are defined but empty and properties that are undefined.


Affects: 5.0.1

Issue Links:

Referenced from: commits e5c8dc0

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions