Skip to content

GH-1832: put the API version into the version path segment of live request mapping URLs - #2071

Merged
BoykoAlex merged 2 commits into
spring-projects:mainfrom
seonwooj0810:fix/GH-1832-api-version-in-live-urls
Nov 5, 2026
Merged

BoykoAlex merged 2 commits into
spring-projects:mainfrom
seonwooj0810:fix/GH-1832-api-version-in-live-urls

Conversation

@seonwooj0810

Copy link
Copy Markdown
Contributor

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 to http://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 structured requestMappingConditions don't include it.

Changes:

  • LiveRequestMapping.getApiVersion() (default null). The Boot 2+ mapping reads it from the predicate. A baseline 1.1+ becomes 1.1, which the mapping matches.

  • New ApiVersionPathSegment helper. 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 way PathApiVersionResolver does.

  • Where the index comes from:

    1. spring.mvc|webflux.apiversion.use.path-segment from the running app's live properties, if the value can be read (it's ignored when the env endpoint sanitizes it).
    2. Otherwise, the project's web configs (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. LiveAppURLSymbolProvider has 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:

  • ApiVersionLiveHoverTest uses a new small fixture project test-api-version-path-segment (/api/{version} prefix, usePathSegment(1)) with Boot 4 shaped mappings JSON. Hover and code lens show /api/1/greeting and /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-support because it configures conflicting indices on purpose (usePathSegment(0) in Java and path-segment: 3 in application.yml), so with it the URL is correctly left unchanged.

Verification done: ./mvnw test -pl spring-boot-language-server with -Dtest covering the requestmapping/test/** and livehover/** packages. 225 tests ran, 0 failures, on JDK 25.

…ment of live request mapping URLs

Signed-off-by: seonwoo_jung <[email protected]>

@BoykoAlex BoykoAlex left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@seonwooj0810

Copy link
Copy Markdown
Contributor Author

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.

@BoykoAlex BoykoAlex left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried this with spring-petclinic where version is path segment at index 0.

Image

The version isn't showing. Looks like the case with segment 0 isn't quite working. Did you try this in Eclipse or VSCode manually?

* <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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was missing the /{version}/owners/find for the path pattern. We should probably add that to the quick fix in some way.

@BoykoAlex
BoykoAlex merged commit 828073f into spring-projects:main Nov 5, 2026
1 check passed
@BoykoAlex

Copy link
Copy Markdown
Contributor

Thanks a lot for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

REST API version is in the live URL

3 participants