Skip to content

Fix ISIN country-code check using untrimmed input#399

Open
sahvx655-wq wants to merge 2 commits into
apache:masterfrom
sahvx655-wq:isin-country-code-trimmed
Open

Fix ISIN country-code check using untrimmed input#399
sahvx655-wq wants to merge 2 commits into
apache:masterfrom
sahvx655-wq:isin-country-code-trimmed

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

ISIN country code read from the raw argument

Whilst checking the country-code path I noticed that isValid/validate disagree with the underlying CodeValidator for inputs with surrounding whitespace. The validator trims the argument before matching, so validate(" US0378331005") returns the trimmed US0378331005, but the ISO-3166 lookup then takes code.substring(0, 2) from the original untrimmed string and reads " U", which is not a country code. The upshot is that the same valid ISIN passes with getInstance(false) yet is rejected by getInstance(true).

The root cause is the country code being derived from the raw argument rather than from the code the validator actually accepted. Reading it from the validated value keeps both code paths consistent. Left unfixed, callers that enable the country check silently reject otherwise-valid codes purely on the presence of leading whitespace. Added a regression test for the trimmed lookup.

@garydgregory garydgregory changed the title fix ISIN country-code check using untrimmed input Fix ISIN country-code check using untrimmed input Jun 15, 2026

@garydgregory garydgregory left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hello @sahvx655-wq

Thank you for the PR.

Please see my comment.

// The underlying CodeValidator trims the input, so the country code must be
// derived from the trimmed code. Otherwise a valid ISIN with leading whitespace
// is accepted without the country check but rejected with it.
final String leading = " US0378331005";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about trailing whitespace?

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.

Trailing whitespace is handled the same way. CodeValidator.validate trims both ends before matching, and since the fix now reads the country code from the validated/trimmed value rather than the raw argument, the substring(0, 2) lookup sees "US" regardless of where the whitespace sits.

I have extended the regression test to loop over leading, trailing and surrounding whitespace so all three paths are covered. Full ISINValidatorTest still passes (Tests run: 5, Failures: 0).

@garydgregory garydgregory left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@sahvx655-wq
Please see my comment.
TY.

// The underlying CodeValidator trims the input, so the country code must be
// derived from the trimmed code. Otherwise a valid ISIN with surrounding whitespace
// is accepted without the country check but rejected with it.
for (final String code : new String[] { " US0378331005", "US0378331005 ", " US0378331005 " }) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use @ParameterizedTest instead of a loop.

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.

2 participants