Skip to content

Commit e7e6823

Browse files
authored
Relax enum property value validation according to Boot core (#1609)
* Relax enum property value validation according to Boot core Signed-off-by: BoykoAlex <[email protected]> * Introduce BootEnumValueParser Signed-off-by: BoykoAlex <[email protected]> --------- Signed-off-by: BoykoAlex <[email protected]>
1 parent db2bd2d commit e7e6823

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-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,55 @@
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+
public class BootEnumValueParser extends EnumValueParser {
21+
22+
private Set<String> canonicalValues;
23+
24+
private static String getCanonicalName(String name) {
25+
StringBuilder canonicalName = new StringBuilder(name.length());
26+
name.chars()
27+
.filter(Character::isLetterOrDigit)
28+
.map(Character::toLowerCase)
29+
.forEach((c) -> canonicalName.append((char) c));
30+
return canonicalName.toString();
31+
}
32+
33+
public BootEnumValueParser(String typeName, String[] values) {
34+
super(typeName, values);
35+
this.canonicalValues = Arrays.stream(values).map(BootEnumValueParser::getCanonicalName).collect(Collectors.toSet());
36+
}
37+
38+
@Override
39+
public Object parse(String str) throws Exception {
40+
// IMPORTANT: check the text FIRST before fetching values
41+
// from the hints provider, as the hints provider may be expensive when
42+
// resolving values
43+
if (!StringUtil.hasText(str)) {
44+
throw errorOnBlank(createBlankTextErrorMessage());
45+
}
46+
47+
// If values is not fully known then just assume the str is acceptable.
48+
if (canonicalValues.contains(getCanonicalName(str))) {
49+
return str;
50+
} else {
51+
throw errorOnParse(createErrorMessage(str, getAllKnownValues()));
52+
}
53+
}
54+
55+
}

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)