Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add numerical enum generation support across C#, TypeScript, Python, Go, PHP, and Ruby language writers. [#5165](https://github.com/microsoft/kiota/issues/5165)

### Changed

- Python: check inherited-model discriminators once before type dispatch to avoid repeated truthiness checks that cause excessive CodeQL extraction work in large factories.
Expand Down
22 changes: 22 additions & 0 deletions src/Kiota.Builder/CodeDOM/CodeEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ public bool Flags
{
get; set;
}
public bool IsNumeric
{
get; set;
}
public string? Format
{
get; set;
}
private bool? _isFractional;
public bool IsFractional
{
get => _isFractional ?? (IsNumeric && (
string.Equals(Format, "float", StringComparison.OrdinalIgnoreCase) ||
string.Equals(Format, "double", StringComparison.OrdinalIgnoreCase) ||
string.Equals(Format, "decimal", StringComparison.OrdinalIgnoreCase) ||
Options.Any(static x => x.WireName.Contains('.', StringComparison.Ordinal) ||
x.WireName.Contains('e', StringComparison.OrdinalIgnoreCase) ||
(!long.TryParse(x.WireName, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out _) &&
double.TryParse(x.WireName, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out _)))
));
set => _isFractional = value;
}

public CodeDocumentation Documentation { get; set; } = new();
private readonly ConcurrentQueue<CodeEnumOption> OptionsInternal = new(); // this structure is used to maintain the order of the options
Expand Down
4 changes: 4 additions & 0 deletions src/Kiota.Builder/CodeDOM/CodeEnumOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public class CodeEnumOption : CodeElement, IDocumentedElement, ITypeDefinition,
{
/// <inheritdoc/>
public string SerializationName { get; set; } = string.Empty;
public bool IsNumeric
{
get; set;
}
public CodeDocumentation Documentation { get; set; } = new();
/// <inheritdoc/>
public bool IsNameEscaped
Expand Down
19 changes: 17 additions & 2 deletions src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using Kiota.Builder.OpenApiExtensions;
using Microsoft.OpenApi;

Expand Down Expand Up @@ -304,9 +305,23 @@ public static bool IsODataPrimitiveType(this IOpenApiSchema schema)
public static bool IsEnum(this IOpenApiSchema? schema)
{
if (schema is null || schema.Enum is null) return false;
return schema.Enum.Any(static x => x.GetValueKind() is JsonValueKind.String &&
return schema.Enum.Any(static x => (x.GetValueKind() is JsonValueKind.String &&
x.GetValue<string>() is string value &&
!string.IsNullOrEmpty(value)); // number and boolean enums are not supported
!string.IsNullOrEmpty(value)) ||
x.GetValueKind() is JsonValueKind.Number);

Copy link
Copy Markdown
Contributor Author

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.

}
public static bool IsNumericEnum(this IOpenApiSchema? schema)
{
if (schema is null || schema.Enum is null || schema.Enum.Count == 0) return false;
var jsonValues = schema.Enum.OfType<JsonValue>().ToArray();
if (jsonValues.Length == 0 || jsonValues.Length != schema.Enum.Count) return false;

return jsonValues.All(static x =>
x.GetValueKind() is JsonValueKind.Number ||
(x.GetValueKind() is JsonValueKind.String &&
x.TryGetValue<string>(out var s) &&
!string.IsNullOrEmpty(s) &&
decimal.TryParse(s, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out _)));
}
public static bool IsComposedEnum(this IOpenApiSchema? schema)
{
Expand Down
11 changes: 10 additions & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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()) ?
Expand All @@ -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) =>
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/Refiners/CSharpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken
CapitalizeNamespacesFirstLetters(generatedCode);
ReplaceBinaryByNativeType(generatedCode, "Stream", "System.IO");
MakeEnumPropertiesNullable(generatedCode);
RevertFractionalEnumsToPrimitives(generatedCode, "float", "double");
/* Exclude the following as their names will be capitalized making the change unnecessary in this case sensitive language
* code classes, class declarations, property names, using declarations, namespace names
* Exclude CodeMethod as the return type will also be capitalized (excluding the CodeType is not enough since this is evaluated at the code method level)
Expand Down
40 changes: 40 additions & 0 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1720,4 +1720,44 @@ private static void ShortenCodeElementNameIfOversized(CodeNamespace parentNamesp
}
}
}
protected static void RevertFractionalEnumsToPrimitives(CodeElement currentElement, string floatTypeName, string doubleTypeName)
{
if (currentElement is CodeNamespace ns)
{
var fractionalEnums = ns.GetChildElements(true).OfType<CodeEnum>().Where(static x => x.IsFractional).ToArray();
if (fractionalEnums.Length != 0)
{
var fractionalEnumSet = fractionalEnums.ToHashSet();
ReplaceFractionalEnumReferences(currentElement, fractionalEnumSet, floatTypeName, doubleTypeName);
foreach (var fractionalEnum in fractionalEnums)
{
var parent = fractionalEnum.GetImmediateParentOfType<CodeNamespace>();
parent?.RemoveChildElement(fractionalEnum);
}
}
}
}
private static void ReplaceFractionalEnumReferences(CodeElement current, HashSet<CodeEnum> fractionalEnums, string floatTypeName, string doubleTypeName)
{
if (current is CodeProperty prop && prop.Type is CodeType propType && propType.TypeDefinition is CodeEnum propEnum && fractionalEnums.Contains(propEnum))
{
propType.TypeDefinition = null;
propType.Name = string.Equals(propEnum.Format, "float", StringComparison.OrdinalIgnoreCase) ? floatTypeName : doubleTypeName;
propType.IsExternal = true;
prop.DefaultValue = string.Empty;
}
else if (current is CodeParameter param && param.Type is CodeType paramType && paramType.TypeDefinition is CodeEnum paramEnum && fractionalEnums.Contains(paramEnum))
{
paramType.TypeDefinition = null;
paramType.Name = string.Equals(paramEnum.Format, "float", StringComparison.OrdinalIgnoreCase) ? floatTypeName : doubleTypeName;
paramType.IsExternal = true;
}
else if (current is CodeMethod method && method.ReturnType is CodeType returnType && returnType.TypeDefinition is CodeEnum returnEnum && fractionalEnums.Contains(returnEnum))
{
returnType.TypeDefinition = null;
returnType.Name = string.Equals(returnEnum.Format, "float", StringComparison.OrdinalIgnoreCase) ? floatTypeName : doubleTypeName;
returnType.IsExternal = true;
}
CrawlTree(current, x => ReplaceFractionalEnumReferences(x, fractionalEnums, floatTypeName, doubleTypeName));
}
}
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken
RemoveModelPropertiesThatDependOnSubNamespaces(
generatedCode
);
RevertFractionalEnumsToPrimitives(generatedCode, "float32", "float64");
FixConstructorClashes(generatedCode, x => $"{x}Escaped");
ReplaceReservedNames(
generatedCode,
Expand Down Expand Up @@ -825,7 +826,7 @@ x.Type is CodeType pType &&
new (static x => x is CodeMethod @method && @method.IsOfKind(CodeMethodKind.RequestExecutor) && (method.ReturnType.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase) ||
method.Parameters.Any(x => x.Kind is CodeParameterKind.RequestBody && x.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))),
SerializationNamespaceName, KiotaBuilder.UntypedNodeName),
new (static x => x is CodeEnum @enum && @enum.Flags,"", "math"),
new (static x => x is CodeEnum @enum && @enum.Flags && !@enum.IsNumeric, "", "math"),
};
private const string MultipartBodyClassName = "MultipartBody";
private const string AbstractionsNamespaceName = "github.com/microsoft/kiota-abstractions-go";
Expand Down
45 changes: 43 additions & 2 deletions src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
if (!codeElement.Options.Any())
return;

if (codeElement.IsFractional)
throw new InvalidOperationException($"Floating-point enums are not supported in C# ('{codeElement.Name}').");
var codeNamespace = codeElement.Parent as CodeNamespace;
if (codeNamespace != null)
{
Expand All @@ -38,7 +40,18 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
writer.WriteLine("[Flags]");
conventions.WriteDeprecationAttribute(codeElement, writer);
if (!hasDescription) conventions.WritePragmaDisable(writer, CSharpConventionService.CS1591);
writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} enum {codeElement.Name.ToFirstCharacterUpperCase()}");
var numericTypeSuffix = codeElement.IsNumeric && !string.IsNullOrEmpty(codeElement.Format) ? codeElement.Format.ToLowerInvariant() switch
{
"int64" => " : long",
"int8" => " : sbyte",
"uint8" => " : byte",
"int16" => " : short",
"uint16" => " : ushort",
"uint32" => " : uint",
"uint64" => " : ulong",
_ => string.Empty,
} : string.Empty;
writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} enum {codeElement.Name.ToFirstCharacterUpperCase()}{numericTypeSuffix}");
if (!hasDescription) conventions.WritePragmaRestore(writer, CSharpConventionService.CS1591);
writer.StartBlock();
var idx = 0;
Expand All @@ -51,7 +64,16 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
writer.WriteLine($"[EnumMember(Value = \"{option.SerializationName.SanitizeCSharpDoubleQuote()}\")]");
}
if (!hasDescription) conventions.WritePragmaDisable(writer, CSharpConventionService.CS1591);
writer.WriteLine($"{option.Name.ToFirstCharacterUpperCase()}{(codeElement.Flags ? " = " + GetEnumFlag(idx) : string.Empty)},");
if (codeElement.IsNumeric)
{
if (!TryValidateNumericLiteral(codeElement, option.SerializationName, out var validatedValue))
throw new InvalidOperationException($"Invalid integer enum value '{option.SerializationName}' for option '{option.Name}' in enum '{codeElement.Name}'.");
writer.WriteLine($"{option.Name.ToFirstCharacterUpperCase()} = {validatedValue},");
}
else
{
writer.WriteLine($"{option.Name.ToFirstCharacterUpperCase()}{(codeElement.Flags ? " = " + GetEnumFlag(idx) : string.Empty)},");
}
if (!hasDescription) conventions.WritePragmaRestore(writer, CSharpConventionService.CS1591);
idx++;
}
Expand All @@ -60,4 +82,23 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
}
private static readonly Func<int, string> GetEnumFlag = static idx =>
(idx == 0 ? 1 : Math.Pow(2, idx)).ToString(CultureInfo.InvariantCulture);
private static bool TryValidateNumericLiteral(CodeEnum codeElement, string rawValue, out string validatedValue)
{
if (string.Equals(codeElement.Format, "uint64", StringComparison.OrdinalIgnoreCase))
{
if (ulong.TryParse(rawValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out var ulongVal))
{
validatedValue = ulongVal.ToString(CultureInfo.InvariantCulture);
return true;
}
}
else if (long.TryParse(rawValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longVal))
{
validatedValue = longVal.ToString(CultureInfo.InvariantCulture);
return true;
}

validatedValue = string.Empty;
return false;
}
}
14 changes: 12 additions & 2 deletions src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,19 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho
.ThenBy(static x => x.Name))
{
var defaultValue = propWithDefault.DefaultValue;
if (propWithDefault.Type is CodeType { TypeDefinition: CodeEnum })
if (propWithDefault.Type is CodeType { TypeDefinition: CodeEnum enumDefinition })
{
defaultValue = $"{conventions.GetTypeString(propWithDefault.Type, currentMethod).TrimEnd('?')}.{defaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}";
var rawDefault = defaultValue.Trim('"');
var matchingOption = enumDefinition.Options.FirstOrDefault(x =>
x.SerializationName.Equals(rawDefault, StringComparison.OrdinalIgnoreCase) ||
x.WireName.Equals(rawDefault, StringComparison.OrdinalIgnoreCase) ||
x.Name.Equals(rawDefault, StringComparison.OrdinalIgnoreCase));

var memberName = matchingOption is not null
? matchingOption.Name.ToFirstCharacterUpperCase()
: rawDefault.CleanupSymbolName().ToFirstCharacterUpperCase();

defaultValue = $"{conventions.GetTypeString(propWithDefault.Type, currentMethod).TrimEnd('?')}.{memberName}";
}
else if (propWithDefault.Type.IsNullable &&
defaultValue.TrimQuotes().Equals(NullValueString, StringComparison.OrdinalIgnoreCase))
Expand Down
Loading