Skip to content

Spring DSL defaultOutputToParentFlow does not behave as describedΒ #3946

Closed

Description

@dobretony

In what version(s) of Spring Integration are you seeing this issue?
5.1.4.RELEASE

Describe the bug

When creating an IntegrationFlow bean using IntegrationFlows.from(CHANNEL), in the route(Function<S, T> router, Consumer<RouterSpec<T, MethodInvokingRouter>> routerConfigurer) method, when configuring the router mappings, the method defaultOutputToParentFlow() does not what the documentation says it does.

Example:

Given this Integration Flow definition:

 @Bean
    public IntegrationFlow orderIntegrationFlow() {
        return IntegrationFlows.from(CHANNEL)
                .<Order, OrderType>route(Order::getType,
                        r -> r.channelMapping(OrderType.DINE_IN, DineInOrderFlow.CHANNEL)
                                .channelMapping(OrderType.ONLINE, OnlineOrderFlow.CHANNEL)
                                .defaultOutputToParentFlow()
                )
                .log("It only reaches here if Order::getType is null.")
                .get();
    }

The log will only get printed if Order::getType is null. This is contrary to the javadoc which says that:

Make a default output mapping of the router to the parent flow. Use the next, after router, parent flow MessageChannel as a AbstractMessageRouter.setDefaultOutputChannel(MessageChannel) of this router.

To Reproduce

Create a Spring project in Java 11, using Spring 5.3.20 and Spring Integration 5.1.4.RELEASE .

Create an IntegrationFlow bean using the default IntegrationFlows.from() method:

    public static final String CHANNEL = "orderInputChannel";

    @Bean(name = CHANNEL)
    public MessageChannel getChannel() {
        DirectChannel dc = new DirectChannel();
        dc.setComponentName(CHANNEL);
        return dc;
    }


    @Bean
    public IntegrationFlow orderIntegrationFlow() {
        return IntegrationFlows.from(CHANNEL)
                .<Order, OrderType>route(Order::getType,
                        r -> r.channelMapping(OrderType.DINE_IN, DineInOrderFlow.CHANNEL)
                                .channelMapping(OrderType.ONLINE, OnlineOrderFlow.CHANNEL)
                                .defaultOutputToParentFlow()
                )
                .log("It only reaches here if Order::getType is null.")
                .get();
    }

Create a class called Order:

public class Order {

    private OrderType orderType;
    public OrderType getType() {
        return orderType;
    }

    public void setType(OrderType orderType) {
        this.orderType = orderType;
    }

}

And an enum called OrderType:

public enum OrderType {
    DINE_IN,
    ONLINE,
    TAKEAWAY;
}

And with everything setup correctly, send a message to CHANNEL of an Order with OrderType.TAKEAWAY.

Expected behavior

The log() at the end of the orderIntegrationFlow will be called because the defaultOutputToParent reached a default state.

** Actual behavior **

An Exception is thrown where TAKEAWAY is not found to be a proper CHANNEL name defined in the Spring Context. Only if Order::getOrderType returns null, then the log() function is called.

Sample

A link to a GitHub repository with a minimal, reproducible sample.

Reports that include a sample will take priority over reports that do not.
At times, we may require a sample, so it is good to try and include a sample up front.

Activity

  1. garyrussell commented on Nov 15, 2022

    @garyrussell
    Contributor

    5.1.x is no longer supported: https://spring.io/projects/spring-integration#support

    See .noChannelKeyFallback().

    	/**
    	 * By default, if a resolved channel key does not exist in the channel map, the key
    	 * itself is used as the channel name, which we will attempt to resolve to a channel.
    	 * Invoke this method to disable this feature. This could be useful to prevent
    	 * malicious actors from generating a message that could cause the message to be
    	 * routed to an unexpected channel, such as one upstream of the router, which would
    	 * cause a stack overflow.
    	 * @return the router spec.
    	 * @since 5.2
    	 */
    	public RouterSpec<K, R> noChannelKeyFallback() {
    		this.handler.setChannelKeyFallback(false);
    		return _this();
    	}
  2. dobretony commented on Nov 15, 2022

    @dobretony
    Author

    All right, thank you for the information. Sorry to bother with an unsupported version.

  3. artembilan commented on Nov 15, 2022

    @artembilan
    Member

    The property for version you use is this:

    	/**
    	 * @param resolutionRequired the resolutionRequired.
    	 * @return the router spec.
    	 * @see AbstractMappingMessageRouter#setResolutionRequired(boolean)
    	 */
    	public RouterSpec<K, R> resolutionRequired(boolean resolutionRequired) {
    

    More JavaDocs are here:

    	/**
    	 * Specify whether this router should ignore any failure to resolve a channel name to
    	 * an actual MessageChannel instance when delegating to the ChannelResolver strategy.
    	 * @param resolutionRequired true if resolution is required.
    	 */
    	public void setResolutionRequired(boolean resolutionRequired) {
    

    And respective docs: https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#router-common-parameters-all.

    Since this defaultOutputToParentFlow() is a bit misleading, I believe we can improve end-user experience at least with more JavaDocs on this method.

    Reopening...

  4. added this to the 6.0.0 milestone on Nov 15, 2022
  5. garyrussell commented on Nov 15, 2022

    @garyrussell
    Contributor

    Good point @artembilan I had forgotten about that property; while polishing the javadocs, we should note that noChannelKeyFallback is a little more efficient because it doesn't attempt resolution at all.

  6. added a commit that references this issue on Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions