fix: normalize raw local path base URI for OpenAPI 3.1 ref resolution (#2365)#2366
Open
seonwooj0810 wants to merge 1 commit into
Open
Conversation
A raw local filesystem path accepted by OpenAPIV3Parser.readLocation is used as the base URI for reference resolution, where ReferenceUtils and Visitor#readURI build java.net.URI instances from it. A path containing characters illegal in a URI (most commonly a space) makes those constructions throw "Illegal character in path", which surfaces as a parse error even for a self-contained single-file spec resolving same-file refs. Normalize the location to a proper file: URI when it is not already a valid URI, so the base is URI-safe everywhere downstream. Valid URIs (including scheme-less relative paths) are left untouched. Fixes swagger-api#2365 Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2365
Root cause
OpenAPIV3Parser.readLocation(...)accepts a raw local filesystem path, and that path becomes the base URI for OpenAPI 3.1 reference resolution. Resolution then buildsjava.net.URIinstances from the base in several places (ReferenceUtils.resolve/toBaseURIandVisitor#readURI). When the raw path contains a character that is illegal in a URI — most commonly a space — thosenew URI(...)calls throwURISyntaxException: Illegal character in path. The exception is caught inReferenceVisitorand recorded as a parse message, so a self-contained single-file spec resolving same-file refs (e.g.#/components/responses/...) returns a result polluted with anIllegal character in patherror.Fix
Normalize the location once, at the entry to
resolve(...): if the location is not already a valid URI, convert it to a properfile:URI viaPaths.get(location).toUri()so the base is URI-safe everywhere downstream. Locations that already parse as a URI (including scheme-less relative paths without illegal characters) are left untouched, so existing behavior is unchanged.Test evidence
Added a regression test
OpenAPIV31RawPathWithSpacesTest#resolveSameFileReferenceFromRawPathWithSpacesthat writes a single-file 3.1 spec with same-file response/schema$refs into a temp directory whose path contains spaces and asserts the parse result carries noIllegal character in pathmessage. It fails before the change (Illegal character in path at index 12: ...) and passes after.Verification done: full
swagger-parser-v3module test suite green (584 + 25 tests, 0 failures) with the fix; the new regression test fails before / passes after. (Thesafe-url-resolvermodule has pre-existing JDK 25 mock failures unrelated to this change.)