fix(csharp): use numeric JSON operations for numeric enum converters
Integer enums now correctly use reader.GetInt32()/GetInt64() and writer.WriteNumberValue() instead of reader.GetString() and writer.WriteStringValue(value ?? "null") in the generated JsonConverter classes.
This fixes System.InvalidOperationException when deserializing integer enum values from JSON numbers.
Furthermore, even if the previous implementation worked for reading the enums, serializing them back to json was broken as it just serialized the enum value (always integer) as a string into the json. This was also incorrect as it would have become {"key": "1"} instead of {"key": 1}, but it would also lose the original value eg. -1.2. This has been also fixed as a natural consequence of my issue.
Fixes #21618
All CI passes in my fork against master: https://github.com/alexaka1/openapi-generator/pull/1/checks
PR checklist
- Read the contribution guidelines.
- Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having
"fixes #123"present in the PR description) - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Mentioning @mandrean @shibayan @Blackclaws @lucamazzanti @iBicha
Summary by cubic
C# generichost enum JsonConverters now use numeric reads/writes for integer and floating‑point enums, preserve precision, validate inputs, and handle nulls. Model enum properties now delegate to JsonSerializer, and enum declarations emit the correct underlying type (e.g., " : long", " : byte").
- Bug Fixes
- Numeric enums: Read via
GetInt32/GetInt64/GetSingle/GetDouble/GetDecimal/GetByte; write withWriteNumberValue; floating‑pointToJsonValuereturns exact values; all numeric enums validate viaFromStringOrDefaultand throwJsonExceptionon invalid input. - Nullable enums: Return null on
JsonTokenType.Null; write nulls withWriteNullValue(). - Model enums: Properties read via
JsonSerializer.Deserialize<EnumType>(); propagate onlyisNumeric/isInteger/isLong/isFloat/isDouble/isDecimal; do not propagateisString; fix non‑inline numeric enums to callEnumTypeValueConverter.ToJsonValue(...). - Templates/declarations: Add
EnumJsonReaderMethod.mustache; emit " : long" for int64 and " : byte" forx-enum-byte(with spacing); classify decimal enums; eliminate extra blank lines; removeInvariantCulturefrom Read; add int64 test values; regenerate generichost samples. Fixes #21618.
- Numeric enums: Read via