Body dispatcher - Regex is not working with complex payloads
See original GitHub issueI was trying to create a mock with different responses depending on the body, I tried to use body dispatcher with regexp operator but the problem is when I try to evaluate a regex pattern with a payload that has objects inside objects, I always receive the default response, I reviewed the code and I found in the class “JsonExpressionEvaluator.java” line 70:
String caseKey = evaluatedNode.asText();
This “asText()” is returning a void string for payloads as:
private static final String RECURSIVE_OBJECTS = "{\"cars\":{\"Peugeot\":[ " + "{\"name\": \"307\", \"model\": \"Peugeot 307\", \"year\": 2003}, " + "{\"name\": \"308\", \"model\": \"Peugeot 308\", \"year\": 2014}, " + "{\"name\": \"508\", \"model\": \"Peugeot 508\", \"year\": 2015}, " + "{\"name\": \"3008\", \"model\": \"Peugeot 3008\", \"year\": 2016}, " + "{\"name\": \"5008\", \"model\": \"Peugeot 5008\", \"year\": 2017}]}} ";
Not sure if there any other way to make this work, just in case, an easy fix is change the lane 93 for:
if (Pattern.matches(choiceKey, evaluatedNode.toString()))
Thanks in advance!
BR, Adrián
Issue Analytics
- State:
- Created 9 months ago
- Comments:8 (4 by maintainers)
Hey @lbroudoux,
Just for letting you know, everything works fine with the SCRIPT option, thanks!!
BR, Adrián.
Hey @lbroudoux,
This is the test I was using:
I need to identify different objects inside the message, also I tried with the operator “presence”, but if I’m not wrong is only binary option for 1 item.
Just for give you more context, my use case is something similar to the message I share above, but sometimes I’m gonna receive the object Peugot and I need to answer with response A, other times I’m gonna receive Seat, Volvo, etc… and for each case I need to answer with a different response. So I need to evaluate if the object is inside the message, and when I try to point to the “parent node” of the payload is when I receive the void string.
Let’s say I want to have something like this:
I hope this helps to clarify 😉
BR, Adrián.