Skip to content

Commit 607f072

Browse files
committed
GH-1560: avoid duplicate property keys in completion proposala
Fixes GH-1560
1 parent a65c4f8 commit 607f072

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/conditionals/ConditionalOnPropertyCompletionProcessor.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import java.util.ArrayList;
1414
import java.util.Collections;
15+
import java.util.HashSet;
1516
import java.util.List;
1617
import java.util.Set;
1718
import java.util.TreeSet;
@@ -66,10 +67,11 @@ else if (Mode.PREFIX == this.mode) {
6667

6768
private List<AnnotationAttributeProposal> findProperties(IJavaProject project, String prefix) {
6869
List<AnnotationAttributeProposal> result = new ArrayList<>();
70+
Set<String> propertyKeys = new HashSet<>();
6971

7072
// First the 'real' properties, Then also add 'ad-hoc' properties
71-
addPropertyProposals(indexProvider.getIndex(project).getProperties(), prefix, result);
72-
addPropertyProposals(adHocIndexProvider.getIndex(project), prefix, result);
73+
addPropertyProposals(indexProvider.getIndex(project).getProperties(), prefix, result, propertyKeys);
74+
addPropertyProposals(adHocIndexProvider.getIndex(project), prefix, result, propertyKeys);
7375

7476
result.sort((p1, p2) -> p1.getLabel().compareTo(p2.getLabel()));
7577

@@ -86,9 +88,14 @@ private List<AnnotationAttributeProposal> findPrefixes(IJavaProject project) {
8688
return new ArrayList<>(prefixes);
8789
}
8890

89-
private void addPropertyProposals(FuzzyMap<PropertyInfo> properties, String prefix, List<AnnotationAttributeProposal> result) {
91+
private void addPropertyProposals(FuzzyMap<PropertyInfo> properties, String prefix, List<AnnotationAttributeProposal> result, Set<String> propertyKeys) {
9092
properties.forEach(propertyInfo -> {
9193
String propID = propertyInfo.getId();
94+
95+
// avoid duplicate property keys
96+
if (!propertyKeys.add(propID)) {
97+
return;
98+
}
9299

93100
if (prefix != null) {
94101
if (prefix.length() > 0

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/conditionals/test/ConditionalOnPropertyCompletionTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,31 @@ public void testConditionalOnPropertyCompletionWithoutPrefix() throws Exception
147147
assertEquals("spring.boot.prop1", completions.get(2).getLabel());
148148
}
149149

150+
@Test
151+
public void testConditionalOnPropertyCompletionWithAdhocProperties() throws Exception {
152+
adHocProperties.add("adhoc.prop");
153+
154+
List<CompletionItem> completions = getCompletions("@ConditionalOnProperty(<*>)");
155+
assertEquals(4, completions.size());
156+
157+
assertEquals("adhoc.prop", completions.get(0).getLabel());
158+
assertEquals("data.prop2", completions.get(1).getLabel());
159+
assertEquals("else.prop3", completions.get(2).getLabel());
160+
assertEquals("spring.boot.prop1", completions.get(3).getLabel());
161+
}
162+
163+
@Test
164+
public void testConditionalOnPropertyCompletionWithAdhocPropertiesDuplicatedKey() throws Exception {
165+
adHocProperties.add("data.prop2");
166+
167+
List<CompletionItem> completions = getCompletions("@ConditionalOnProperty(<*>)");
168+
assertEquals(3, completions.size());
169+
170+
assertEquals("data.prop2", completions.get(0).getLabel());
171+
assertEquals("else.prop3", completions.get(1).getLabel());
172+
assertEquals("spring.boot.prop1", completions.get(2).getLabel());
173+
}
174+
150175
@Test
151176
public void testConditionalOnPropertyCompletionWithoutPrefixAttributeWithNameAttribute() throws Exception {
152177
List<CompletionItem> completions = getCompletions("@ConditionalOnProperty(name=<*>)");

0 commit comments

Comments
 (0)