Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W-17475148: Simple type params handler not handling certain expression param values (#14097) #14103

Open
wants to merge 1 commit into
base: support/4.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,8 +44,12 @@ public void setNext(BeanDefinitionCreator<R> nextBeanDefinitionCreator) {
* @param request
*/
public final void processRequest(Map<ComponentAst, SpringComponentModel> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public ComponentAst resolveConfigurationComponent() {
public Consumer<ComponentAst> getNestedComponentParamProcessor() {
return nestedComponentParamProcessor;
}

@Override
public String toString() {
return "component request for `" + getComponent().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ protected final boolean handleRequest(Map<ComponentAst, SpringComponentModel> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ComponentAst, SpringComponentModel> 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));
}

}