Repository navigation
replacePattern fails when the first group does not participate in a match - #1086
Open
chuuminggg wants to merge 1 commit into
Open
chuuminggg wants to merge 1 commit into
chuuminggg wants to merge 1 commit into
Conversation
…cipate Previously, when a pattern with a capturing group was used with replacePattern and a match occurred in which the first group did not participate, for example because the group was optional or in an alternative branch, a StringIndexOutOfBoundsException was thrown. Such matches are now left unchanged. Signed-off-by: chuuminggg <rorrxr@naver.com>
This branch has not been deployed
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.
When the pattern passed to
replacePattern(…)has a capturing group, only the first group of each match is replaced. If a match occurs in which that group doesn't participate,matcher.start(1)returns-1andPatternReplacingContentModifierfails with aStringIndexOutOfBoundsException:This happens with two common kinds of pattern:
[0-9]+(-SNAPSHOT)?. The first match,1, has no-SNAPSHOT."id":"([^"]+)"|"created":"[^"]+". A match of the second branch fails.As a result, documenting the request or response fails, even though most matches could be replaced.
This PR skips matches in which the first group doesn't participate, so that their content is left unchanged. This keeps the existing behavior of replacing only the first group when it does participate. With the fix, the example above produces
1.0 2.0-RELEASE.An alternative would be to replace the whole match when the group doesn't participate. I chose to leave such matches unchanged, as replacing text outside the group would be surprising when the pattern was written to replace only the group. I'm happy to change it if you'd prefer the alternative.
The tests cover the existing first-group behavior, which wasn't tested before, and the two cases above.