Commit c7c0bf7
fix(csharp): use numeric JSON operations for numeric enum converters (OpenAPITools#22917)
* fix(csharp): use numeric JSON operations for integer enum converters in C# generichost
Integer enums now correctly use reader.GetInt32()/GetInt64() and
writer.WriteNumberValue() instead of reader.GetString() and
writer.WriteStringValue() in the generated JsonConverter classes.
This fixes System.InvalidOperationException when deserializing integer
enum values from JSON numbers.
Co-authored-by: copilot-swe-agent[bot] <198982749+copilot@users.noreply.github.com>
* fix: handle double/float/decimal enums correctly in JSON converters
For floating-point enums (double, float, decimal), C# enums can't have
non-integral underlying types, so:
- ToJsonValue now returns actual enum values via switch-case instead of
casting (which gave wrong values like 2.0 instead of -1.2)
- Read method uses reader.GetDouble() etc. and converts to string with
InvariantCulture for matching via FromStringOrDefault
- Write method uses WriteNumberValue with the corrected ToJsonValue
Integer enums (int32, int64) still use direct cast as before.
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* test: use actual int64 values for LongEnum test spec
Use 2147483648 (int32 max + 1) and 9223372036854775807 (int64 max) to
ensure the int64 branch is properly exercised with values that exceed
int32 range.
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* fix: eliminate extra blank lines in generated enum converters
Replace triple-negation pattern ({{^isFloat}}{{^isDouble}}{{^isDecimal}})
with positive flags ({{#isInteger}}, {{#isLong}}) for integer enum
branches. The triple-negation caused the Java Mustache engine to produce
spurious blank lines and incorrect indentation in the generated C# code.
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* fix: validate integer/long enum values during deserialization
Use FromStringOrDefault for all numeric enum types (int, long, float,
double, decimal), not just float/double/decimal. This ensures invalid
values throw JsonException instead of being silently cast to the enum.
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* fix: remove InvariantCulture from enum ToString calls in Read methods
Remove System.Globalization.CultureInfo.InvariantCulture from all
.ToString() calls in the enum JsonConverter Read methods. This ensures
the culture used for ToString matches the culture used in
FromStringOrDefault comparisons (both now use system default culture).
Since the Write path outputs the numeric value directly, the JSON
output is always correct regardless of culture.
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* fix: nullable enum converters - use WriteNullValue() and handle null token in Read
Nullable string enum Write now calls WriteNullValue() when HasValue is
false, instead of writing the string literal "null".
Nullable enum Read (all types) now checks reader.TokenType ==
JsonTokenType.Null and returns null before attempting to read the value.
Previously this check was only applied to non-string enum types.
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* fix: emit `: long` for int64 enum declarations and add test assertions
The enum declaration template now checks `{{#isLong}}` to emit `: long`
as the underlying type, preventing overflow for int64 enum values.
Added test assertions verifying:
- LongEnum has `: long` in its declaration
- IntegerEnum does NOT have `: long` or `: byte`
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* fix: add space before colon in enum type declaration per C# convention
Changes `enum Name: long` to `enum Name : long` (and same for `: byte`)
to match standard C# formatting.
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* fix: delegate enum property deserialization to JsonSerializer.Deserialize
Model-level enum property reading now delegates to the enum's own
registered JsonConverter via JsonSerializer.Deserialize<EnumType>()
instead of inlining reader.GetString()/GetInt32() calls. This ensures
the already-fixed enum converters handle all type branching correctly.
Also propagate isNumeric/isInteger/isLong/isFloat/isDouble/isDecimal
flags from referenced enum models to properties in patchProperty(),
so the Write path correctly uses WriteNumber vs WriteString.
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* fix: use ValueConverter.ToJsonValue for non-inner numeric enum properties
The WriteNumber path in JsonConverter.mustache was missing the
'ValueConverter.' prefix for non-inner enum properties, generating
broken calls like `EnumTypeToJsonValue(...)` instead of
`EnumTypeValueConverter.ToJsonValue(...)`.
This caused CS0103 compile errors in CI for models with numeric enum
properties defined as $ref (not inline).
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* fix: don't propagate isString from enum models to properties
Setting property.isString on enum ref properties caused duplicate
WriteString statements in anyOf models (e.g., IconsSizeParameter).
String enum properties are already handled through the isEnum template
path, not the isString path, so propagating isString is both unnecessary
and harmful.
Co-authored-by: alexaka1 <22166651+alexaka1@users.noreply.github.com>
* remove AI comment
* fix(csharp): refresh numeric enum converter templates
Co-authored-by: Alex Martossy <github@mail.martossy.hu>
* test(csharp): cover numeric enum formats
Co-authored-by: Alex Martossy <github@mail.martossy.hu>
* fix(csharp): classify decimal enum models
Co-authored-by: Alex Martossy <github@mail.martossy.hu>
* fix(csharp): preserve numeric enum precision
Co-authored-by: Alex Martossy <github@mail.martossy.hu>
* chore(csharp): regenerate generichost enum samples
Co-authored-by: Alex Martossy <github@mail.martossy.hu>
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+copilot@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>1 parent f7191ab commit c7c0bf7
385 files changed
Lines changed: 5614 additions & 3668 deletions
File tree
- modules/openapi-generator/src
- main
- java/org/openapitools/codegen/languages
- resources/csharp
- libraries/generichost
- test
- java/org/openapitools/codegen/csharpnetcore
- resources/3_0/csharp
- samples/client/petstore/csharp/generichost
- latest
- ComposedEnum/src/Org.OpenAPITools/Model
- InlineEnumAnyOf/src/Org.OpenAPITools/Model
- UseDateTimeOffset/src/Org.OpenAPITools/Model
- net10
- FormModels/src/Org.OpenAPITools/Model
- NullReferenceTypes/src/Org.OpenAPITools/Model
- Petstore/src/Org.OpenAPITools/Model
- SourceGeneration/src/Org.OpenAPITools/Model
- net4.7
- FormModels/src/Org.OpenAPITools/Model
- Petstore/src/Org.OpenAPITools/Model
- net4.8
- FormModels/src/Org.OpenAPITools/Model
- Petstore/src/Org.OpenAPITools/Model
- net8
- FormModels/src/Org.OpenAPITools/Model
- NullReferenceTypes/src/Org.OpenAPITools/Model
- Petstore/src/Org.OpenAPITools/Model
- SourceGeneration/src/Org.OpenAPITools/Model
- net9
- FormModels/src/Org.OpenAPITools/Model
- NullReferenceTypes/src/Org.OpenAPITools/Model
- Petstore/src/Org.OpenAPITools/Model
- SourceGeneration/src/Org.OpenAPITools/Model
- standard2.0/Petstore/src/Org.OpenAPITools/Model
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
594 | 594 | | |
595 | 595 | | |
596 | 596 | | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
597 | 602 | | |
598 | 603 | | |
599 | 604 | | |
| |||
853 | 858 | | |
854 | 859 | | |
855 | 860 | | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
856 | 870 | | |
857 | 871 | | |
858 | 872 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
Lines changed: 30 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
238 | 252 | | |
239 | | - | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
240 | 263 | | |
241 | 264 | | |
242 | 265 | | |
243 | 266 | | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | 267 | | |
255 | 268 | | |
256 | 269 | | |
257 | 270 | | |
258 | 271 | | |
259 | 272 | | |
260 | 273 | | |
261 | | - | |
262 | 274 | | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
263 | 280 | | |
264 | 281 | | |
265 | 282 | | |
| |||
524 | 541 | | |
525 | 542 | | |
526 | 543 | | |
527 | | - | |
| 544 | + | |
528 | 545 | | |
529 | 546 | | |
530 | 547 | | |
| |||
0 commit comments