mapWithDefault is useful to handle those cases where the source message field is expected, but might not be existed.
Example in previous firstname lastname to fullname example, both firstname and lastname is expected to be concatenated.
Before implement mapWithDefault:

Case 1:
Both firstname and lastname segment is existed and with values. Display queue show value “John”. Mapping test run-time success.
Case 2:
Firstname is existed but with empty string value. Display queue show empty string value “”. Mapping test run-time success.
Case 3:
Firstname is not existed. Display queue show empty context and only have <null> displayed. Mapping test run-time error.
The concat function expected first parameter must feed in value, but none provided and error out.
After implement mapWithDefault:
Case 4:
Firstname is not existed. Display queue show empty context and only have <null> displayed. Mapping test run-time success.mapwithdefault_case4
Let’s see the queue of mapWithDefault.
Firstname’s mapWithDefault received input value “SUPPRESS” because firstname not existed. MapWithDefault mapped it with empty string value “”.
Lastname’s mapWithDefault received input value”Locke” because lastname existed and with value “Locke”. Since field is existed, so take existed value “Locke”.
General advise, if in situation where the expected field might or might not existed but is mandatory in mapping, or the source field is not primary and optional in source system, then it is advisable to include mapWithDefault. This will avoid the mapping failed badly in run-time. Remember, all input data is evil!

Handle Empty Context with mapWithDefault
Tagged on:         

4 thoughts on “Handle Empty Context with mapWithDefault

  • January 23, 2015 at 12:11 pm

    Nice Explanation>..

  • April 17, 2015 at 3:16 pm

    I like the example. easy to follow and understand.

  • June 18, 2015 at 11:39 pm

    nic eexplanation 🙂

  • March 2, 2023 at 6:04 pm

    Great explanation thanks for your efforts and explanation.


Leave a Reply

Your email address will not be published. Required fields are marked *