From 1d9fb2e9c77002c946fef0de10ff2e1b13a671f8 Mon Sep 17 00:00:00 2001 From: Rodrigo Merino Date: Thu, 19 Dec 2024 19:02:27 -0300 Subject: [PATCH] W-17475148: Simple type params handler not handling certain expression (#14097) param values --- .../dsl/spring/BeanDefinitionCreator.java | 11 ++- .../CreateComponentBeanDefinitionRequest.java | 5 ++ ...ateDslParamGroupBeanDefinitionRequest.java | 7 ++ .../CreateParamBeanDefinitionRequest.java | 8 ++ .../SimpleTypeBeanBaseDefinitionCreator.java | 14 +++- .../SimpleTypeBeanParamDefinitionCreator.java | 5 ++ ...ypeBeanParamDefinitionCreatorTestCase.java | 75 +++++++++++++++++++ 7 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 modules/spring-config/src/test/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanParamDefinitionCreatorTestCase.java diff --git a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/BeanDefinitionCreator.java b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/BeanDefinitionCreator.java index a29369f76a75..edb73fe95d36 100644 --- a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/BeanDefinitionCreator.java +++ b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/BeanDefinitionCreator.java @@ -6,8 +6,11 @@ */ package org.mule.runtime.config.internal.dsl.spring; +import static org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage; + import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition; +import org.mule.runtime.api.exception.MuleRuntimeException; import org.mule.runtime.ast.api.ComponentAst; import org.mule.runtime.config.internal.dsl.model.SpringComponentModel; import org.mule.runtime.config.internal.factories.ConstantFactoryBean; @@ -41,8 +44,12 @@ public void setNext(BeanDefinitionCreator nextBeanDefinitionCreator) { * @param request */ public final void processRequest(Map springComponentModels, R request) { - if (handleRequest(springComponentModels, request)) { - return; + try { + if (handleRequest(springComponentModels, request)) { + return; + } + } catch (Exception e) { + throw new MuleRuntimeException(createStaticMessage("Exception processing " + request.toString()), e); } if (next != null) { next.processRequest(springComponentModels, request); diff --git a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateComponentBeanDefinitionRequest.java b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateComponentBeanDefinitionRequest.java index 1d4f65a25ffd..1b037148f59e 100644 --- a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateComponentBeanDefinitionRequest.java +++ b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateComponentBeanDefinitionRequest.java @@ -34,4 +34,9 @@ public ComponentAst resolveConfigurationComponent() { public Consumer getNestedComponentParamProcessor() { return nestedComponentParamProcessor; } + + @Override + public String toString() { + return "component request for `" + getComponent().toString(); + } } diff --git a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateDslParamGroupBeanDefinitionRequest.java b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateDslParamGroupBeanDefinitionRequest.java index 37a218746a6a..2ba6fc0b63f5 100644 --- a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateDslParamGroupBeanDefinitionRequest.java +++ b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateDslParamGroupBeanDefinitionRequest.java @@ -49,4 +49,11 @@ public ParameterGroupModel getParamGroupModel() { public ComponentParameterAst getParameter(String parameterName) { return paramOwnerComponent.getParameter(paramGroupModel.getName(), parameterName); } + + @Override + public String toString() { + return "DSL param group request for `" + + getParamGroupModel().getName() + "` in component `" + + getParamOwnerComponent().toString(); + } } diff --git a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateParamBeanDefinitionRequest.java b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateParamBeanDefinitionRequest.java index 3b951f182da7..5f464861686f 100644 --- a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateParamBeanDefinitionRequest.java +++ b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/CreateParamBeanDefinitionRequest.java @@ -65,4 +65,12 @@ public ComponentParameterAst getParameter(String parameterName) { .findFirst() .orElse(null); } + + @Override + public String toString() { + return "param request for `" + + getParam().getGroupModel().getName() + "." + + getParam().getModel().getName() + "` in component `" + + getParamOwnerComponent().toString(); + } } diff --git a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanBaseDefinitionCreator.java b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanBaseDefinitionCreator.java index 5a0c90659625..24de05a9a4b9 100644 --- a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanBaseDefinitionCreator.java +++ b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanBaseDefinitionCreator.java @@ -31,12 +31,18 @@ protected final boolean handleRequest(Map sp R createBeanDefinitionRequest) { Class type = createBeanDefinitionRequest.getSpringComponentModel().getType(); - if (!isSimpleType(type)) { - return false; + if (isSimpleType(type) + // Expressions are String, which are simple values for the spring bean definitions + || isExpressionValue(createBeanDefinitionRequest)) { + createBeanDefinitionRequest.getSpringComponentModel().setType(type); + return doHandleRequest(createBeanDefinitionRequest, type); } - createBeanDefinitionRequest.getSpringComponentModel().setType(type); - return doHandleRequest(createBeanDefinitionRequest, type); + return false; + } + + protected boolean isExpressionValue(R request) { + return false; } protected abstract boolean doHandleRequest(R createBeanDefinitionRequest, Class type); diff --git a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanParamDefinitionCreator.java b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanParamDefinitionCreator.java index c80262636958..cc6b726c3b9c 100644 --- a/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanParamDefinitionCreator.java +++ b/modules/spring-config/src/main/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanParamDefinitionCreator.java @@ -41,6 +41,11 @@ protected boolean doHandleRequest(CreateParamBeanDefinitionRequest createBeanDef return true; } + @Override + protected boolean isExpressionValue(CreateParamBeanDefinitionRequest request) { + return request.getParam().getValue().isLeft(); + } + static Object resolveParamValue(final ComponentParameterAst param, boolean disableTrimWhitespaces, boolean disablePojoCdataTrimWhitespaces) { return param.getValue() diff --git a/modules/spring-config/src/test/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanParamDefinitionCreatorTestCase.java b/modules/spring-config/src/test/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanParamDefinitionCreatorTestCase.java new file mode 100644 index 000000000000..0348dae9425f --- /dev/null +++ b/modules/spring-config/src/test/java/org/mule/runtime/config/internal/dsl/spring/SimpleTypeBeanParamDefinitionCreatorTestCase.java @@ -0,0 +1,75 @@ +/* + * Copyright 2023 Salesforce, Inc. All rights reserved. + * The software in this package is published under the terms of the CPAL v1.0 + * license, a copy of which has been included with this distribution in the + * LICENSE.txt file. + */ +package org.mule.runtime.config.internal.dsl.spring; + +import static org.mule.runtime.api.functional.Either.left; +import static org.mule.runtime.api.functional.Either.right; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.mule.runtime.ast.api.ComponentAst; +import org.mule.runtime.ast.api.ComponentParameterAst; +import org.mule.runtime.config.internal.dsl.model.SpringComponentModel; +import org.mule.runtime.dsl.api.component.ComponentBuildingDefinition; +import org.mule.tck.junit4.AbstractMuleTestCase; + +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import io.qameta.allure.Issue; + +public class SimpleTypeBeanParamDefinitionCreatorTestCase extends AbstractMuleTestCase { + + private SimpleTypeBeanParamDefinitionCreator simpleTypeParamDefinitionCreator; + private CreateParamBeanDefinitionRequest createParamDefinitionRequest; + private Map springComponentModels; + + @Before + public void setUp() { + simpleTypeParamDefinitionCreator = new SimpleTypeBeanParamDefinitionCreator(true, true); + createParamDefinitionRequest = mock(CreateParamBeanDefinitionRequest.class); + SpringComponentModel springComponentModel = new SpringComponentModel(); + when(createParamDefinitionRequest.getSpringComponentModel()).thenReturn(springComponentModel); + when(createParamDefinitionRequest.getComponentBuildingDefinition()).thenReturn(mock(ComponentBuildingDefinition.class)); + } + + @Test + @Issue("W-17475148") + public void complexObjectFromExpression() { + SpringComponentModel springComponentModel = createParamDefinitionRequest.getSpringComponentModel(); + springComponentModel.setType(Map.class); + + final ComponentParameterAst param = mock(ComponentParameterAst.class); + when(param.getValue()).thenReturn(left("'some DW expression'")); + + when(createParamDefinitionRequest.getParam()).thenReturn(param); + + boolean result = simpleTypeParamDefinitionCreator.handleRequest(springComponentModels, createParamDefinitionRequest); + assertThat("request not handled when it must", result, is(true)); + } + + @Test + @Issue("W-17475148") + public void complexObjectInlineNotHandled() { + SpringComponentModel springComponentModel = createParamDefinitionRequest.getSpringComponentModel(); + springComponentModel.setType(Map.class); + + final ComponentParameterAst param = mock(ComponentParameterAst.class); + when(param.getValue()).thenReturn(right(mock(ComponentAst.class))); + + when(createParamDefinitionRequest.getParam()).thenReturn(param); + + boolean result = simpleTypeParamDefinitionCreator.handleRequest(springComponentModels, createParamDefinitionRequest); + assertThat("request handled when it must not", result, is(false)); + } + +}