Skip to content

fix: write negative int, short and enum constants with the invariant culture - #1040

Merged
DocSvartz merged 1 commit into
MapsterMapper:developmentfrom
menes97:fix/translator-negative-constants
Nov 23, 2026
Merged

DocSvartz merged 1 commit into
MapsterMapper:developmentfrom
menes97:fix/translator-negative-constants

Conversation

@menes97

@menes97 menes97 commented Nov 21, 2026

Copy link
Copy Markdown

Follow-up to #1035, which made ExpressionTranslator.WriteValue write decimal, long, uint and ulong constants with the invariant culture. Three branches still used value.ToString():

  • int;
  • byte/sbyte/short/ushort, written as ((short)...);
  • enum values without a name, written as (Type)....

The ICU data for Finnish, Swedish and Norwegian Bokmål sets NegativeSign to U+2212 MINUS SIGN. Under fi-FI, sv-SE or nb-NO, Expression.Constant(-5).ToScript() therefore contains −5, which the C# compiler rejects. So code generated by Mapster.Tool or compiled by ExpressionDebugger breaks on those machines as soon as a mapping has a negative constant.

The int and short-integer branches now use Convert.ToString(value, CultureInfo.InvariantCulture). The unnamed-enum branch converts to the underlying type first and formats that with the invariant culture. As a side effect, a flags combination without a name now becomes its number instead of its "A, B" name list, which wasn't valid C# after a cast anyway.

Test: TestNegativeIntegerConstantsDoNotDependOnCurrentCulture sets fi-FI and checks -5, ((short)-3) and (DayOfWeek)-2. It also checks that the output contains no U+2212.

  • before: fails with Assert.IsTrue failed. −5;
  • after: ExpressionDebugger.Tests 45/45 (net7.0), Mapster.Tests 410 passed / 6 skipped (net10.0), Mapster.Tool.Tests 5/5 (net10.0).

This PR was prepared with AI assistance (Claude Code); I reviewed the change and ran the tests above.

🤖 Generated with Claude Code

…culture

int, byte/sbyte/short/ushort and unnamed enum constants were written with
value.ToString(), so under fi-FI, sv-SE or nb-NO a negative value got the
U+2212 MINUS SIGN ("−5"), which is not valid C#.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DocSvartz
DocSvartz merged commit 706867f into MapsterMapper:development Nov 23, 2026
2 checks passed
@DocSvartz

Copy link
Copy Markdown
Contributor

@menes97 Thanks!
Added in v10.0.15-pre04

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