Skip to content

Commit d6f31c7

Browse files
authored
Fix PropertyNamesMatcher.get returning wrong value for null-valued pattern matches (#1506)
1 parent 3c0b086 commit d6f31c7

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

implementation/src/main/java/io/smallrye/config/PropertyNamesMatcher.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ private boolean matches(final Node<T> current, final NameIterator ni) {
8888
return false;
8989
}
9090

91+
// Sentinel to distinguish "matched with null value" from "no match" in the recursive get traversal
92+
private static final Object NO_MATCH = new Object();
93+
9194
/**
9295
* Returns the value matching a name with any of the names contained in this matcher.
9396
*
@@ -100,21 +103,26 @@ public T get(final String name) {
100103
return result;
101104
}
102105

106+
if (properties.containsKey(name)) {
107+
return null;
108+
}
109+
103110
NameIterator ni = new NameIterator(name);
104-
return get(wildcards, ni);
111+
result = get(wildcards, ni);
112+
return result == noMatch() ? null : result;
105113
}
106114

107115
private T get(final Node<T> current, final NameIterator ni) {
108116
if (!ni.hasNext()) {
109-
return current.terminal ? current.value : null;
117+
return current.terminal ? current.value : noMatch();
110118
}
111119

112120
int position = ni.getPosition();
113121
Node<T> child = current.find(ni);
114122
if (child != null) {
115123
ni.next();
116124
T result = get(child, ni);
117-
if (result != null) {
125+
if (result != noMatch()) {
118126
return result;
119127
}
120128
}
@@ -127,7 +135,7 @@ private T get(final Node<T> current, final NameIterator ni) {
127135
ni.setPosition(position);
128136
ni.next();
129137
T result = get(current.wildcard, ni);
130-
if (result != null) {
138+
if (result != noMatch()) {
131139
return result;
132140
}
133141
if (current.wildcard.terminal && !current.greedy) {
@@ -139,7 +147,7 @@ private T get(final Node<T> current, final NameIterator ni) {
139147
return current.value;
140148
}
141149

142-
return null;
150+
return noMatch();
143151
}
144152

145153
protected void add(final String name) {
@@ -209,6 +217,11 @@ protected void put(final String name, final T value) {
209217
}
210218
}
211219

220+
@SuppressWarnings("unchecked")
221+
private static <T> T noMatch() {
222+
return (T) NO_MATCH;
223+
}
224+
212225
public static final class Node<T> {
213226
String path;
214227
T value;

implementation/src/test/java/io/smallrye/config/PropertyNamesMatcherTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ void conflicts() {
3434
matcher = new PropertyNamesMatcher<>();
3535
matcher.add(configClass(ComposedNames.class).getProperties());
3636
assertNull(matcher.get("map.*.*"));
37+
38+
matcher = new PropertyNamesMatcher<>();
39+
matcher.add(configClass(WithParentNameSingle.class).getProperties());
40+
assertNull(matcher.get("map.key.another"));
3741
}
3842

3943
@Test
@@ -157,4 +161,17 @@ interface Nested {
157161
String value();
158162
}
159163
}
164+
165+
@ConfigMapping
166+
interface WithParentNameSingle {
167+
Map<String, Nested> map();
168+
169+
interface Nested {
170+
@WithParentName
171+
@WithDefault("value")
172+
String value();
173+
174+
String another();
175+
}
176+
}
160177
}

0 commit comments

Comments
 (0)