forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
[BUG][csharp] Fix integer/numeric enum JSON converters using string operations instead of numeric #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
9
commits into
master
Choose a base branch
from
copilot/fix-enum-string-generation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[BUG][csharp] Fix integer/numeric enum JSON converters using string operations instead of numeric #1
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
29ce4b6
fix(csharp): use numeric JSON operations for integer enum converters …
alexaka1 45f833b
fix: handle double/float/decimal enums correctly in JSON converters
Copilot b672dfd
test: use actual int64 values for LongEnum test spec
Copilot 6932a22
fix: eliminate extra blank lines in generated enum converters
Copilot 5233c7c
fix: validate integer/long enum values during deserialization
Copilot d8ab8ee
fix: remove InvariantCulture from enum ToString calls in Read methods
Copilot 383ea38
fix: nullable enum converters - use WriteNullValue() and handle null …
Copilot b0b2835
fix: emit `: long` for int64 enum declarations and add test assertions
Copilot bd903e8
fix: add space before colon in enum type declaration per C# convention
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...i-generator/src/main/resources/csharp/libraries/generichost/EnumJsonReaderMethod.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {{#allowableValues}}{{#enumVars}}{{#-first}}{{#isNumeric}}{{#isLong}}GetInt64{{/isLong}}{{#isFloat}}GetSingle{{/isFloat}}{{#isDouble}}GetDouble{{/isDouble}}{{#isDecimal}}GetDecimal{{/isDecimal}}{{^isLong}}{{^isFloat}}{{^isDouble}}{{^isDecimal}}GetInt32{{/isDecimal}}{{/isDouble}}{{/isFloat}}{{/isLong}}{{/isNumeric}}{{/-first}}{{/enumVars}}{{/allowableValues}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,4 +260,82 @@ private List<String> getNames(List<CodegenProperty> props) { | |
| if (props == null) return null; | ||
| return props.stream().map(v -> v.name).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIntegerEnumJsonConverterUsesNumericOperations() throws IOException { | ||
| File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); | ||
| output.deleteOnExit(); | ||
| final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/csharp/integer-enum.yaml"); | ||
| final DefaultGenerator defaultGenerator = new DefaultGenerator(); | ||
| final ClientOptInput clientOptInput = new ClientOptInput(); | ||
| clientOptInput.openAPI(openAPI); | ||
| CSharpClientCodegen cSharpClientCodegen = new CSharpClientCodegen(); | ||
| cSharpClientCodegen.setLibrary("generichost"); | ||
| cSharpClientCodegen.setOutputDir(output.getAbsolutePath()); | ||
| clientOptInput.config(cSharpClientCodegen); | ||
| defaultGenerator.opts(clientOptInput); | ||
|
|
||
| Map<String, File> files = defaultGenerator.generate().stream() | ||
| .collect(Collectors.toMap(File::getPath, Function.identity())); | ||
|
|
||
| // Verify integer enum uses numeric JSON reader with validation | ||
| File intEnumFile = files.get(Paths | ||
| .get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Model", "IntegerEnum.cs") | ||
| .toString() | ||
| ); | ||
| assertNotNull(intEnumFile, "Could not find file for model: IntegerEnum"); | ||
| assertFileContains(intEnumFile.toPath(), | ||
| "reader.GetInt32().ToString()", | ||
| "FromStringOrDefault(rawValue)", | ||
| "throw new JsonException()", | ||
| "writer.WriteNumberValue(", | ||
| "public static int ToJsonValue(IntegerEnum value)" | ||
| ); | ||
| assertFileNotContains(intEnumFile.toPath(), | ||
| "reader.GetString()", | ||
| "writer.WriteStringValue(", | ||
| ": long", | ||
| ": byte" | ||
| ); | ||
|
|
||
| // Verify long enum uses int64 reader with validation and actual int64 values | ||
| File longEnumFile = files.get(Paths | ||
| .get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Model", "LongEnum.cs") | ||
| .toString() | ||
| ); | ||
| assertNotNull(longEnumFile, "Could not find file for model: LongEnum"); | ||
| assertFileContains(longEnumFile.toPath(), | ||
| "enum LongEnum : long", | ||
| "reader.GetInt64().ToString()", | ||
| "FromStringOrDefault(rawValue)", | ||
| "throw new JsonException()", | ||
| "writer.WriteNumberValue(", | ||
| "public static long ToJsonValue(LongEnum value)", | ||
| "AboveInt32Max = 2147483648", | ||
| "Int64Max = 9223372036854775807" | ||
| ); | ||
| assertFileNotContains(longEnumFile.toPath(), | ||
| "reader.GetString()", | ||
| "writer.WriteStringValue(" | ||
| ); | ||
|
|
||
| // Verify double enum reads numeric value and converts to string for matching, writes as number | ||
| File doubleEnumFile = files.get(Paths | ||
| .get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Model", "DoubleEnum.cs") | ||
| .toString() | ||
| ); | ||
| assertNotNull(doubleEnumFile, "Could not find file for model: DoubleEnum"); | ||
| assertFileContains(doubleEnumFile.toPath(), | ||
| "reader.GetDouble().ToString()", | ||
| "writer.WriteNumberValue(", | ||
| "public static double ToJsonValue(DoubleEnum value)", | ||
| "return 1.1d;", | ||
| "return -1.2d;" | ||
| ); | ||
| assertFileNotContains(doubleEnumFile.toPath(), | ||
| "reader.GetString()", | ||
| "writer.WriteStringValue(", | ||
| "return (double) value" | ||
| ); | ||
|
Comment on lines
282
to
339
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot test the datatype of the generated enum as well. Long and byte should have |
||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
modules/openapi-generator/src/test/resources/3_0/csharp/integer-enum.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| openapi: 3.0.1 | ||
| info: | ||
| title: Integer Enum Test | ||
| version: v1 | ||
| paths: | ||
| /some/api: | ||
| get: | ||
| tags: | ||
| - Example | ||
| operationId: ExampleApi | ||
| responses: | ||
| '200': | ||
| description: OK | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: '#/components/schemas/IntegerEnum' | ||
| components: | ||
| schemas: | ||
| IntegerEnum: | ||
| enum: | ||
| - 0 | ||
| - 1 | ||
| type: integer | ||
| format: int32 | ||
| x-enum-varnames: | ||
| - None | ||
| - Some | ||
| LongEnum: | ||
| enum: | ||
| - 2147483648 | ||
| - 9223372036854775807 | ||
| type: integer | ||
| format: int64 | ||
| x-enum-varnames: | ||
| - AboveInt32Max | ||
| - Int64Max | ||
| DoubleEnum: | ||
| enum: | ||
| - 1.1 | ||
| - -1.2 | ||
| type: number | ||
| format: double |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot
In
@tools/openapi/templates/csharp/modelEnum.mustachearound lines 277 - 288,The NullableJsonConverter.Read implementation for string-backed enums should
detect a JSON null token before calling reader.GetString and return null rather
than throwing; update the isString branch in the NullableJsonConverter.Read
method to check reader.TokenType == JsonTokenType.Null (or check rawValue ==
null immediately after GetString) and return null when appropriate, then only
call ValueConverter.FromStringOrDefault(rawValue) for non-null rawValue so that
deserializing JSON null yields a null nullable enum instead of raising
JsonException.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in
383ea387. TheJsonTokenType.Nullcheck is now applied to ALL enum types (including string) in the nullable converter Read method. Previously it was gated behind{{^isString}}, so string enums would fall through toreader.GetString()and then throwJsonExceptionwhen the result was null. Now it returnsnullimmediately for null tokens.