question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Body dispatcher - Regex is not working with complex payloads

See original GitHub issue

I 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:closed
  • Created 9 months ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
adrimscommented, Dec 12, 2022

Hey @lbroudoux,

Just for letting you know, everything works fine with the SCRIPT option, thanks!!

BR, Adrián.

1reaction
adrimscommented, Dec 7, 2022

Hey @lbroudoux,

This is the test I was using:

@Test
  public void testRegexpOperatorDispatcherWithRecursiveObjects() throws Exception {
    DispatchCases cases = new DispatchCases();
    Map<String, String> dispatchCases = new HashMap<>();
    dispatchCases.put(".*Peugeot.*", "OK");
    dispatchCases.put("default", "Bad");
    cases.putAll(dispatchCases);

    JsonEvaluationSpecification specifications = new JsonEvaluationSpecification();
    specifications.setExp("/cars");
    specifications.setOperator(EvaluationOperator.regexp);
    specifications.setCases(cases);

    String result = JsonExpressionEvaluator.evaluate(RECURSIVE_OBJECTS, specifications);
    assertEquals("OK", result);

  }

image

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:

{
   exp: "/cars"
   operator: "regexp"
   cases: {
         ".*Peugeot.*": "Response A",
         ".*Volvo.*": "Response B",
         ".*Seat.*": "Response C",
         "Default": "Response A",
   }
}

I hope this helps to clarify 😉

BR, Adrián.

Read more comments on GitHub >

github_iconTop Results From Across the Web

unexpected behavior in RegularExpressionProtection...
I am trying to put regular expression on request json fileds. For this I am using RegularExpressionProtection Policy and in <JSONPayload> ...
Read more >
Chapter 1 - Dispatcher Concepts, Patterns and Antipatterns
Running the Dispatcher in NTFS on Windows can be a challenge. ... An HTTP-body is not required, the “payload” is just the “invalidate-path”...
Read more >
Regex expression format is different in AEM dispatcher
I am not familiar with the AEM Dispatcher, but here is the generic answer to your regular expression question:.
Read more >
Replacement markers in URL dispatcher raise pyramid ...
This issue may be resolved either by providing the maxsplit=1 argument to name.split(':') statement, or by introducing requirements argument to ...
Read more >
Known Issues for Oracle SOA Products 12.1.3
For a list of issues and workarounds pertaining to Oracle SOA Installation, Upgrade, ... The real payload sent through Oracle Mediator is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found