Skip to content

Fix StringIndexOutOfBoundsException when resolving a self $ref '#' - #149

Merged
pk-work merged 1 commit into
eclipse-vertx:masterfrom
SiteNetSoft:fix-root-ref-resolution
Aug 7, 2026
Merged

Fix StringIndexOutOfBoundsException when resolving a self $ref '#'#149
pk-work merged 1 commit into
eclipse-vertx:masterfrom
SiteNetSoft:fix-root-ref-resolution

Conversation

@jnbdz

@jnbdz jnbdz commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #141

Motivation

A schema containing a self-reference $ref: '#' (e.g. a recursive tree structure) throws StringIndexOutOfBoundsException during resolution:

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
	at io.vertx.json.schema.impl.JsonRef.resolveUri(JsonRef.java:328)

In JsonRef.resolve, a $ref of # becomes id + "#" (just # when there is no $id). resolveUri then splits it into ["", ""], so hashPresent was true while path was empty, and path.charAt(0) threw.

Changes

Treat an empty fragment (# or id#) the same as no fragment at all, so the reference resolves to the schema identified by the prefix — the root schema for a bare #. This is "Option 1" from the issue: it removes the invalid empty-path state entirely instead of guarding the charAt call, and an empty fragment never carries pointer segments to reduce() anyway.

Added tests covering the reproducer from the issue: direct JsonRef.resolve, through SchemaRepository.resolve (the path in the reported stack trace), and with a $id present.

Note: the follow-up comment in #141 about $ref: '#/' failing later in SchemaValidatorImpl is a separate lookup-normalization issue and is not addressed here; with # fixed that workaround is no longer needed.

A $ref pointing at the schema itself ('#', or 'id#' after contextual
resolution) produces an empty fragment in JsonRef.resolveUri, which then
fails on path.charAt(0). Treat an empty fragment the same as no fragment
so the ref resolves to the schema identified by the prefix.

Fixes eclipse-vertx#141
@pk-work
pk-work merged commit e29bc7b into eclipse-vertx:master Aug 7, 2026
5 checks passed
@pk-work

pk-work commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Hi @jnbdz thank you for the fix! I really appreciate it.

Do you want this also in 5.1? Then I can downport it.

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.

Recursive Ref Causes "String index out of range"

2 participants