Skip to content

Commit 980ebf3

Browse files
committed
GH-1665: fix version validation for + wildcard at the end
1 parent bbdfe44 commit 980ebf3

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public boolean visit(NormalAnnotation annotation) {
9393

9494
Expression valueExpression = pair.getValue();
9595
String versionValue = ASTUtils.getExpressionValueAsString(valueExpression, (d) -> {});
96+
versionValue = updateVersion(versionValue);
9697

9798
SemanticApiVersionParser parser = new SemanticApiVersionParser();
9899
try {
@@ -130,6 +131,11 @@ public boolean visit(TypeDeclaration type) {
130131
};
131132
}
132133

134+
private String updateVersion(String version) {
135+
boolean baselineVersion = version.endsWith("+");
136+
return (baselineVersion ? version.substring(0, version.length() - 1) : version);
137+
}
138+
133139
private boolean isApiVersioningConfiguredWithStanardVersionParser(IJavaProject project) {
134140
List<WebConfigIndexElement> webConfigs = springIndex.getNodesOfType(project.getElementName(), WebConfigIndexElement.class);
135141
for (WebConfigIndexElement webConfig : webConfigs) {

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,67 @@ public class A {
8383
assertEquals(0, problems.size());
8484
}
8585

86+
@Test
87+
void parseWildcardVersion() throws Exception {
88+
String source = """
89+
package example.demo;
90+
91+
import org.springframework.web.bind.annotation.RestController;
92+
import org.springframework.web.bind.annotation.RequestMapping;
93+
94+
@RestController
95+
@RequestMapping(path = "mypath", version = "1.1+")
96+
public class A {
97+
}
98+
""";
99+
List<ReconcileProblem> problems = reconcile(() -> {
100+
SpringMetamodelIndex springIndex = new SpringMetamodelIndex();
101+
102+
WebConfigIndexElement webConfig = new WebConfigIndexElement.Builder(ConfigType.WEB_CONFIG)
103+
.versionStrategy("version-strategy-configured", new Range(new Position(1, 1), new Position(1, 4)))
104+
.buildFor(null);
105+
springIndex.updateElements(getProjectName(), "soneURI", new SpringIndexElement[] {webConfig});
106+
107+
WebApiVersionSyntaxReconciler r = new WebApiVersionSyntaxReconciler(springIndex);
108+
return r;
109+
}, "A.java", source, false);
110+
111+
assertEquals(0, problems.size());
112+
}
113+
114+
@Test
115+
void parseWrongWildcardVersion() throws Exception {
116+
String source = """
117+
package example.demo;
118+
119+
import org.springframework.web.bind.annotation.RestController;
120+
import org.springframework.web.bind.annotation.RequestMapping;
121+
122+
@RestController
123+
@RequestMapping(path = "mypath", version = "1.1++")
124+
public class A {
125+
}
126+
""";
127+
List<ReconcileProblem> problems = reconcile(() -> {
128+
SpringMetamodelIndex springIndex = new SpringMetamodelIndex();
129+
130+
WebConfigIndexElement webConfig = new WebConfigIndexElement.Builder(ConfigType.WEB_CONFIG)
131+
.versionStrategy("version-strategy-configured", new Range(new Position(1, 1), new Position(1, 4)))
132+
.buildFor(null);
133+
springIndex.updateElements(getProjectName(), "soneURI", new SpringIndexElement[] {webConfig});
134+
135+
WebApiVersionSyntaxReconciler r = new WebApiVersionSyntaxReconciler(springIndex);
136+
return r;
137+
}, "A.java", source, false);
138+
139+
assertEquals(1, problems.size());
140+
ReconcileProblem problem = problems.get(0);
141+
assertEquals(Boot4JavaProblemType.API_VERSION_SYNTAX_ERROR, problem.getType());
142+
143+
String markedStr = source.substring(problem.getOffset(), problem.getOffset() + problem.getLength());
144+
assertEquals("\"1.1++\"", markedStr);
145+
}
146+
86147
@Test
87148
void parseWithConfiguredDefaultStandardVersionWithoutErrors() throws Exception {
88149
String source = """

0 commit comments

Comments
 (0)