From 61ef1ab91be74ebd15514763cefa9cb59464353d Mon Sep 17 00:00:00 2001 From: jobayle Date: Fri, 12 Apr 2019 15:09:57 +0200 Subject: [PATCH] TaggedPattern: Fix NPE when using optional maching group --- .../metrics/influxdb/transformer/TaggedPattern.java | 1 + .../influxdb/transformer/TaggedPatternTest.java | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPattern.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPattern.java index 6942203..2c3789c 100644 --- a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPattern.java +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPattern.java @@ -59,6 +59,7 @@ public TaggedPattern(final String pattern, final List tagKeys) { final Matcher matcher = compiledPattern.matcher(input); if (matcher.matches()) { return Optional.of(tagKeys.stream() + .filter(tag -> matcher.group(tag) != null) .filter(tag -> !matcher.group(tag).isEmpty()) .collect(toMap(Function.identity(), matcher::group))); } diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPatternTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPatternTest.java index be1c43b..ea27fd0 100644 --- a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPatternTest.java +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPatternTest.java @@ -45,4 +45,15 @@ public void testTags_matchWithoutTags() { untaggedPattern.tags(".hello.anything.whatever") ); } + + @Test + public void testTags_optionalGroup() { + final TaggedPattern optionalPattern = new TaggedPattern( + "\\.hello\\.(?[A-Za-z]+)\\.(?[0-9]+)?\\.?(?.*)", + "action", "number", "model"); + assertEquals( + Optional.of(ImmutableMap.of("action", "move", "model", "foobar")), + optionalPattern.tags(".hello.move.foobar") + ); + } }