Repository navigation
GH-1832: put the API version into the version path segment of live request mapping URLs - #2071
Conversation
…ment of live request mapping URLs Signed-off-by: seonwoo_jung <[email protected]>
There was a problem hiding this comment.
Generally the changes make sense.
I didn't like the ApiVersionPathSegment class... it is just a bunch of static utilities. Wonder if it can be replaced with VersionedPaths class of some sort that takes LiveRequestMapping, SpringProcessLiveData andCollection<WebConfigIndexElement> as fields and has a getter for splitPath, produces the versioned path etc. Some OOD around that would be nice to hide index computations, the path computations, looking at index or live properties as implementation details of the class.
I'm also yet to try the changes manually... (struggling to reanimate my dev environment)
…a VersionedPaths class Signed-off-by: seonwoo_jung <[email protected]>
|
Replaced ApiVersionPathSegment with a VersionedPaths class in 1c898d7. It takes the LiveRequestMapping, SpringProcessLiveData and the project's web configs, exposes getSplitPath() (including the unconstrained-path fallback that was duplicated in three places) and getVersionedPath(path), and keeps the index lookup from live properties/web configs and the segment replacement private. |
| * <code>org.springframework.web.accept.PathApiVersionResolver</code> does, i.e. within the application path) | ||
| * with the version, but only if that segment is a URI variable. | ||
| */ | ||
| private static String insertVersion(String path, String version, int index) { |
There was a problem hiding this comment.
With spring-petclinic /owners/find request mapping. I enter this method with path = /owners/find, version = "1.2.0" and index = 0.
This ends up having 3 segments where the segment at index 0 is and empty string.
There was a problem hiding this comment.
The split does yield 3 segments, but empty ones are skipped when counting (the same way PathApiVersionResolver counts within the application path), so index 0 is owners; since that is not a URI variable the path is left unchanged on purpose. With usePathSegment(0) Spring itself would resolve owners as the version for a /owners/find mapping, so the version has to be in the mapped path, e.g. /{version}/owners/find or a /{version} path prefix (which shows up in the live mapping path too). How is the petclinic mapping set up on your side? I have not tried it in Eclipse or VS Code yet, only via the unit tests and the test fixture project.
There was a problem hiding this comment.
Yes, I was missing the /{version}/owners/find for the path pattern. We should probably add that to the quick fix in some way.
|
Thanks a lot for your contribution! |

Fixes #1832
With path segment API versioning (e.g.
addPathPrefix("/{version}", ...)+usePathSegment(0)), the mappings actuator reports the pattern/{version}/greeting, so the live hover, live code lens and live URL symbols linked tohttp://host:port/{version}/greeting, and opening it gives a 400 because{version}isn't a valid version.Root cause: the version of a mapping is never read from the live data, so the version path segment stays a URI variable. Spring Framework 7 adds the version condition to the mapping's
predicate({GET [/{version}/greeting], version [1.1+]}), but the structuredrequestMappingConditionsdon't include it.Changes:
LiveRequestMapping.getApiVersion()(defaultnull). The Boot 2+ mapping reads it from the predicate. A baseline1.1+becomes1.1, which the mapping matches.New
ApiVersionPathSegmenthelper. It replaces the path segment at the configured index with the version, but only when that segment is a URI variable, and counts segments within the application path the wayPathApiVersionResolverdoes.Where the index comes from:
spring.mvc|webflux.apiversion.use.path-segmentfrom the running app's live properties, if the value can be read (it's ignored when the env endpoint sanitizes it).usePathSegment(n)) and properties/YAML, using the same sources as the web config code lens.If the index is unknown or configs disagree, the URL is left unchanged, the same as today.
This is wired into the request mapping live hover and code lenses.
LiveAppURLSymbolProviderhas no project, so it only uses the live property.Other strategies (header, query parameter, media type) are not covered here, since the version can't be expressed in the path for those.
Tests:
ApiVersionLiveHoverTestuses a new small fixture projecttest-api-version-path-segment(/api/{version}prefix,usePathSegment(1)) with Boot 4 shaped mappings JSON. Hover and code lens show/api/1/greetingand/api/1.1/greeting. An unversioned mapping keeps its path. This test fails without the change (the hover shows/api/{version}/greeting).ApiVersionPathSegmentTest: parsing the predicate, segment replacement (regex variables,{*path}, out of range), index resolution from web configs (conflicting indices are ignored), and live property precedence and sanitized values.I didn't reuse
test-web-config-supportbecause it configures conflicting indices on purpose (usePathSegment(0)in Java andpath-segment: 3inapplication.yml), so with it the URL is correctly left unchanged.Verification done:
./mvnw test -pl spring-boot-language-serverwith-Dtestcovering therequestmapping/test/**andlivehover/**packages. 225 tests ran, 0 failures, on JDK 25.