Skip to content

ObjectStringMessageConverter.toMessage() can't be used with arbitrary payload #8757

Closed
@mrpiggi

Description

@mrpiggi

I am not quite sure, what the original intention of ObjectStringMessageConverter was, but currently using

ObjectStringMessageConverter converter = new ObjectStringMessageConverter();
Integer payload = 1234;
Message<?> converted = converter.toMessage(payload, null);
assertThat(converted).isNotNull();

fails, as Message<?> converted is always null when payload is anything else than String.class as AbstractMessageConverter.canConvertTo()

https://github.com/spring-projects/spring-framework/blob/80646591367b93d220b9dc0b0a126b203edc140c/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java#L231-L235

always returns null due to StringMessageConverter.supports()
https://github.com/spring-projects/spring-framework/blob/80646591367b93d220b9dc0b0a126b203edc140c/spring-messaging/src/main/java/org/springframework/messaging/converter/StringMessageConverter.java#L50-L55
returns false in this case.

The question arises, whether this is intentional. If ObjectStringMessageConverter.toMessage() should not be used with anything else than String payload explicitly throwing an IllegalArgumentException would probably be a better option. For the opposite case I will provide a PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions