Skip to content

Commit 3cf480a

Browse files
authored
Relax enum property value validation according to Boot core (#1610)
* Relax enum property value validation according to Boot core Signed-off-by: BoykoAlex <[email protected]> * Introduce BootEnumValueParser Signed-off-by: BoykoAlex <[email protected]> * Comments adjustments Signed-off-by: BoykoAlex <[email protected]> --------- Signed-off-by: BoykoAlex <[email protected]>
1 parent 4848010 commit 3cf480a

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

headless-services/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/EnumValueParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package org.springframework.ide.vscode.commons.util;
1212

1313
import java.util.Collection;
14+
import java.util.Collections;
1415
import java.util.TreeSet;
1516
import java.util.concurrent.Callable;
1617
import java.util.function.Supplier;
@@ -78,6 +79,11 @@ public Object parse(String str) throws Exception {
7879
throw errorOnParse(createErrorMessage(str, values.getElements()));
7980
}
8081
}
82+
83+
protected final Collection<String> getAllKnownValues() {
84+
PartialCollection<String> partialCollection = this.values.get();
85+
return partialCollection == null ? Collections.emptyList() : this.values.get().getElements();
86+
}
8187

8288
protected boolean hasMatchingValue(String str, Collection<String> values) {
8389
return values.contains(str);

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/metadata/types/TypeUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.ide.vscode.boot.metadata.hints.StsValueHint;
4545
import org.springframework.ide.vscode.boot.metadata.util.DeprecationUtil;
4646
import org.springframework.ide.vscode.boot.metadata.util.PropertyDocUtils;
47+
import org.springframework.ide.vscode.boot.properties.reconcile.BootEnumValueParser;
4748
import org.springframework.ide.vscode.commons.java.Flags;
4849
import org.springframework.ide.vscode.commons.java.IField;
4950
import org.springframework.ide.vscode.commons.java.IJavaElement;
@@ -55,7 +56,6 @@
5556
import org.springframework.ide.vscode.commons.util.ArrayUtils;
5657
import org.springframework.ide.vscode.commons.util.Assert;
5758
import org.springframework.ide.vscode.commons.util.CollectionUtil;
58-
import org.springframework.ide.vscode.commons.util.EnumValueParser;
5959
import org.springframework.ide.vscode.commons.util.LazyProvider;
6060
import org.springframework.ide.vscode.commons.util.MimeTypes;
6161
import org.springframework.ide.vscode.commons.util.Renderables;
@@ -272,7 +272,7 @@ public ValueParser getValueParser(Type type) {
272272
//Note, technically if 'enumValues is empty array' this means something different
273273
// from when it is null. An empty array means a type that has no values, so
274274
// assigning anything to it is an error.
275-
return new EnumValueParser(niceTypeName(type), getBareValues(enumValues));
275+
return new BootEnumValueParser(niceTypeName(type), getBareValues(enumValues));
276276
}
277277
if (isMap(type)) {
278278
//Trying to parse map types from scalars is not possible. Thus we
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Broadcom, Inc.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Broadcom, Inc. - initial API and implementation
10+
*******************************************************************************/
11+
package org.springframework.ide.vscode.boot.properties.reconcile;
12+
13+
import java.util.Arrays;
14+
import java.util.Set;
15+
import java.util.stream.Collectors;
16+
17+
import org.springframework.ide.vscode.commons.util.EnumValueParser;
18+
import org.springframework.ide.vscode.commons.util.StringUtil;
19+
20+
/**
21+
* Boot property values allow relaxed parsing via canonical names.
22+
*
23+
* @author Alex Boyko
24+
*/
25+
public class BootEnumValueParser extends EnumValueParser {
26+
27+
private Set<String> canonicalValues;
28+
29+
private static String getCanonicalName(String name) {
30+
StringBuilder canonicalName = new StringBuilder(name.length());
31+
name.chars()
32+
.filter(Character::isLetterOrDigit)
33+
.map(Character::toLowerCase)
34+
.forEach((c) -> canonicalName.append((char) c));
35+
return canonicalName.toString();
36+
}
37+
38+
public BootEnumValueParser(String typeName, String[] values) {
39+
super(typeName, values);
40+
this.canonicalValues = Arrays.stream(values).map(BootEnumValueParser::getCanonicalName).collect(Collectors.toSet());
41+
}
42+
43+
@Override
44+
public Object parse(String str) throws Exception {
45+
// IMPORTANT: check the text FIRST before fetching values
46+
// from the hints provider, as the hints provider may be expensive when
47+
// resolving values
48+
if (!StringUtil.hasText(str)) {
49+
throw errorOnBlank(createBlankTextErrorMessage());
50+
}
51+
52+
if (canonicalValues.contains(getCanonicalName(str))) {
53+
return str;
54+
} else {
55+
throw errorOnParse(createErrorMessage(str, getAllKnownValues()));
56+
}
57+
}
58+
59+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5014,6 +5014,7 @@ private void doCollectionOfEnumReconcileTest(String collectionType) throws Excep
50145014
"my:\n" +
50155015
" colors:\n" +
50165016
" - red\n" +
5017+
" - r-e-d\n" + // Canonical name is "red" from "r-e-d" hence it is okay in Boot as well
50175018
" - green\n" +
50185019
" - BLUE\n" +
50195020
" - not-a-color\n"

0 commit comments

Comments
 (0)