Repository navigation
feat: add numerical enum generation support #8532
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
base: main
Are you sure you want to change the base?
Changes from all commits
39dec9a
4bbf711
eea8ceb
6cf55e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2387,10 +2387,13 @@ private CodeElement AddModelDeclarationIfDoesntExist(OpenApiUrlTreeNode currentN | |
| { | ||
| enumFlagsExtension = flagsExtension; | ||
| } | ||
| var isNumeric = schema.IsNumericEnum(); | ||
| var newEnum = new CodeEnum | ||
| { | ||
| Name = declarationName, | ||
| Flags = enumFlagsExtension?.IsFlags ?? false, | ||
| IsNumeric = isNumeric, | ||
|
Contributor
Author
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. Updated CSharp/CodeMethodWriter.cs to resolve default values against the enum's options matching by SerializationName, WireName, or Name. This ensures customized option names (such as those populated via x-ms-enum) are properly selected instead of re-cleaning the raw numeric literal. Added unit tests for default value resolution in constructor emission. |
||
| Format = schema.Format, | ||
| Documentation = new() | ||
| { | ||
| DescriptionTemplate = !string.IsNullOrEmpty(schemaDescription) || !string.IsNullOrEmpty(schema.GetReferenceId()) ? | ||
|
|
@@ -2409,9 +2412,14 @@ private static void SetEnumOptions(IOpenApiSchema schema, CodeEnum target) | |
| OpenApiEnumValuesDescriptionExtension? extensionInformation = null; | ||
| if (schema.Extensions is not null && schema.Extensions.TryGetValue(OpenApiEnumValuesDescriptionExtension.Name, out var rawExtension) && rawExtension is OpenApiEnumValuesDescriptionExtension localExtInfo) | ||
| extensionInformation = localExtInfo; | ||
| if (!target.IsNumeric && schema.IsNumericEnum()) | ||
| { | ||
| target.IsNumeric = true; | ||
| target.Format ??= schema.Format; | ||
| } | ||
| target.AddOption(schema.Enum?.OfType<JsonValue>() | ||
| .Where(static x => x.GetValueKind() is JsonValueKind.String or JsonValueKind.Number) | ||
| .Select(static x => x.GetValueKind() is JsonValueKind.String ? x.GetValue<string>() : x.GetValue<decimal>().ToString(CultureInfo.InvariantCulture)) | ||
| .Select(static x => x.GetValueKind() is JsonValueKind.String ? x.GetValue<string>() : (x.TryGetValue<long>(out var l) ? l.ToString(CultureInfo.InvariantCulture) : (x.TryGetValue<decimal>(out var d) ? d.ToString(CultureInfo.InvariantCulture) : x.ToJsonString()))) | ||
| .Where(static x => !string.IsNullOrEmpty(x)) | ||
| .Distinct(StringComparer.OrdinalIgnoreCase) | ||
| .Select((x) => | ||
|
|
@@ -2423,6 +2431,7 @@ private static void SetEnumOptions(IOpenApiSchema schema, CodeEnum target) | |
| name : | ||
| x).CleanupSymbolName(), | ||
| SerializationName = x, | ||
| IsNumeric = target.IsNumeric, | ||
| Documentation = new() | ||
| { | ||
| DescriptionTemplate = optionDescription?.Description?.CleanupDescription() ?? string.Empty, | ||
|
|
||
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.
Updated the Java constructor default emitter in \Java/CodeMethodWriter.cs\ to resolve the matching enum option and emit double-quoted string literals (e.g. \Status.forValue(\10)), matching the \ValuedEnum\ signature defined on the Java enum. Added a constructor default regression test in \CodeMethodWriterTests.cs.