Skip to content

WW-3666 Trim surrounding whitespace before converting to non-String types - #2039

Merged
lukaszlenart merged 2 commits into
mainfrom
feature/WW-3666-trim-before-conversion
Oct 17, 2026
Merged

lukaszlenart merged 2 commits into
mainfrom
feature/WW-3666-trim-before-conversion

Conversation

@lukaszlenart

Copy link
Copy Markdown
Member

Fixes WW-3666

Summary

Request values with surrounding whitespace (" 42 ", " ACTIVE ", "\t2001-01-10\n") were rejected by most built-in converters. Only primitive int/long already trimmed, so long and Long disagreed on the same input. Boolean and Character silently misread padded input, with no conversion error.

XWorkBasicConverter.convertValue now trims String and String[] values before converting to a non-String scalar target:

  • numbers (boxed, double/float, BigInteger, BigDecimal), enums, booleans, Date/java.time, Class
  • arrays and collections are not trimmed at the entry; their elements re-enter convertValue and are trimmed per element
  • String/Object targets keep the value untouched (they return before the trim), and so do List<String> elements
  • a converter registered for a type still receives the raw value — it is looked up before the default converter
  • Character/char: " x" → 'x'; whitespace-only input keeps its first character (" " → ' '), preserving the existing testConvertChar behaviour

Whitespace-only input now follows the existing "" path.

Point 3 of the ticket ("errors should bubble up") is already covered: conversion failures are recorded and ConversionErrorInterceptor turns them into field errors.

Behaviour changes worth a migration note (7.5.0)

Input Target Before After
" true " Boolean / boolean false true
" x" Character / char ' ' 'x'
"" or " " Boolean false null
" " boxed numbers, enums, java.time conversion error null (a required validator now reports it instead)
" 42 " etc. boxed numbers, enums, java.time, arrays conversion error converted value

Unchanged: " " into int/long is still a conversion error. " " into double/float gives null, which is what "" already gives there (a pre-existing quirk in NumberConverter.convertToDouble/convertToFloat, left alone).

Test plan

  • New XWorkConverterTest cases for numbers, enums, booleans, characters, java.time, String[] → Long[], whitespace-only boxed → null, integral primitives → conversion error — the behaviour cases watched failing before the change
  • Guard cases (String/Object/String[] targets untouched, custom converter receives the raw value) checked by mutation: trimming before the early return, or in XWorkConverter before the lookup, fails them (and the existing testConvertChar)
  • ./mvnw test -DskipAssembly -pl core — 3520 tests, 0 failures
  • ./mvnw test -DskipAssembly -DskipITs (full reactor) — 4860 tests, 0 failures

🤖 Generated with Claude Code

…non-String types

Padded request values such as " 42 " or " ACTIVE " were rejected by the
locale-aware number, enum and java.time paths, while primitive int/long
already trimmed. Boolean and Character silently misread them (" true " was
false, " x" was ' ').

XWorkBasicConverter now trims String and String[] values for scalar targets.
String/Object targets and converters registered for a type still receive the
raw value; arrays and collections trim per element. Whitespace-only input
follows the existing "" path, so boxed Boolean blank becomes null and a
whitespace-only Character keeps its first character.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@lukaszlenart
lukaszlenart marked this pull request as ready for review October 17, 2026 08:22
return value;
}

value = trimSurroundingWhitespace(value, toType);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reassigning to method parameter is a bad idea

…reassigning the parameter

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sonarqubecloud

Copy link
Copy Markdown

@lukaszlenart
lukaszlenart merged commit 6b2c315 into main Oct 17, 2026
8 checks passed
@lukaszlenart
lukaszlenart deleted the feature/WW-3666-trim-before-conversion branch October 17, 2026 08:43
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.

1 participant