Skip to content

Commit 602cbf0

Browse files
committed
Relax enum property value validation according to Boot core
Signed-off-by: BoykoAlex <[email protected]>
1 parent 1dabd89 commit 602cbf0

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2014-2017 Pivotal, Inc.
2+
* Copyright (c) 2014, 2025 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,9 +11,11 @@
1111
package org.springframework.ide.vscode.commons.util;
1212

1313
import java.util.Collection;
14+
import java.util.Set;
1415
import java.util.TreeSet;
1516
import java.util.concurrent.Callable;
1617
import java.util.function.Supplier;
18+
import java.util.stream.Collectors;
1719

1820
import com.google.common.collect.ImmutableSet;
1921

@@ -29,6 +31,7 @@ public class EnumValueParser implements ValueParser {
2931
private Supplier<PartialCollection<String>> values;
3032
private final boolean longRunning;
3133

34+
private transient Set<String> _canonicalValues;
3235

3336
public EnumValueParser(String typeName, String... values) {
3437
this(typeName, ImmutableSet.copyOf(values));
@@ -69,20 +72,15 @@ public Object parse(String str) throws Exception {
6972
throw errorOnBlank(createBlankTextErrorMessage());
7073
}
7174

72-
PartialCollection<String> values = this.values.get();
73-
74-
// If values is not fully known then just assume the str is acceptable.
75-
if (values == null || !values.isComplete() || hasMatchingValue(str, values.getElements())) {
75+
Set<String> canonicalValues = getCanonicalValues();
76+
77+
if (canonicalValues == null || canonicalValues.contains(getCanonicalName(str))) {
7678
return str;
7779
} else {
78-
throw errorOnParse(createErrorMessage(str, values.getElements()));
80+
throw errorOnParse(createErrorMessage(str, this.values.get().getElements()));
7981
}
8082
}
8183

82-
protected boolean hasMatchingValue(String str, Collection<String> values) {
83-
return values.contains(str);
84-
}
85-
8684
protected String createBlankTextErrorMessage() {
8785
return "'" + typeName + "'" + " cannot be blank.";
8886
}
@@ -102,4 +100,21 @@ protected Exception errorOnBlank(String message) {
102100
public boolean longRunning() {
103101
return this.longRunning ;
104102
}
103+
104+
private Set<String> getCanonicalValues() {
105+
PartialCollection<String> partialCollection = this.values.get();
106+
if (partialCollection != null) {
107+
_canonicalValues = partialCollection.getElements().stream().map(EnumValueParser::getCanonicalName).collect(Collectors.toSet());
108+
}
109+
return _canonicalValues;
110+
}
111+
112+
static String getCanonicalName(String name) {
113+
StringBuilder canonicalName = new StringBuilder(name.length());
114+
name.chars()
115+
.filter(Character::isLetterOrDigit)
116+
.map(Character::toLowerCase)
117+
.forEach((c) -> canonicalName.append((char) c));
118+
return canonicalName.toString();
119+
}
105120
}

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)