From fd6810fe04800615965d5acd690fee724635dec0 Mon Sep 17 00:00:00 2001 From: Andrei Markov Date: Thu, 7 Nov 2024 21:23:24 +0300 Subject: [PATCH] fix: Guid is an alias to primitive type string --- .../Writers/TypeScript/CodeConstantWriter.cs | 5 ++++- .../Writers/TypeScript/TypeScriptConventionService.cs | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs index 9e4cda57f8..b75590008a 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs @@ -103,6 +103,7 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri var isEnum = executorMethod.ReturnType is CodeType codeType && codeType.TypeDefinition is CodeEnum; var returnTypeWithoutCollectionSymbol = GetReturnTypeWithoutCollectionSymbol(executorMethod, returnType); var isPrimitive = IsPrimitiveType(returnTypeWithoutCollectionSymbol); + var isPrimitiveAlias = GetPrimitiveAlias(returnTypeWithoutCollectionSymbol) is not null; writer.StartBlock($"{executorMethod.Name.ToFirstCharacterLowerCase()}: {{"); var urlTemplateValue = executorMethod.HasUrlTemplateOverride ? $"\"{executorMethod.UrlTemplateOverride}\"" : uriTemplateConstant.Name.ToFirstCharacterUpperCase(); writer.WriteLine($"uriTemplate: {urlTemplateValue},"); @@ -118,7 +119,7 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri } writer.CloseBlock("},"); } - writer.WriteLine($"adapterMethodName: \"{GetSendRequestMethodName(isVoid, isStream, executorMethod.ReturnType.IsCollection, isPrimitive, isEnum)}\","); + writer.WriteLine($"adapterMethodName: \"{GetSendRequestMethodName(isVoid, isStream, executorMethod.ReturnType.IsCollection, isPrimitive || isPrimitiveAlias, isEnum)}\","); if (isEnum) { string enumObjectName = string.Empty; @@ -169,6 +170,8 @@ private string GetTypeFactory(bool isVoid, bool isStream, CodeMethod codeElement if (isVoid) return string.Empty; var typeName = conventions.TranslateType(codeElement.ReturnType); if (isStream || IsPrimitiveType(typeName)) return $" \"{typeName}\""; + if (GetPrimitiveAlias(typeName) is { } alias && !string.IsNullOrEmpty(alias)) + return $" \"{alias}\""; return $" {GetFactoryMethodName(codeElement.ReturnType, codeElement, writer)}"; } private string GetReturnTypeWithoutCollectionSymbol(CodeMethod codeElement, string fullTypeName) diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs index ebb98ab309..3e4bced511 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs @@ -193,7 +193,6 @@ public static string TranslateTypescriptType(CodeTypeBase type) { TYPE_INTEGER or TYPE_INT or TYPE_INT64 or TYPE_FLOAT or TYPE_DOUBLE or TYPE_BYTE or TYPE_SBYTE or TYPE_DECIMAL => TYPE_NUMBER, TYPE_BINARY or TYPE_BASE64 or TYPE_BASE64URL => TYPE_STRING, - TYPE_GUID => TYPE_GUID, TYPE_STRING or TYPE_OBJECT or TYPE_BOOLEAN or TYPE_VOID or TYPE_LOWERCASE_STRING or TYPE_LOWERCASE_OBJECT or TYPE_LOWERCASE_BOOLEAN or TYPE_LOWERCASE_VOID => type.Name.ToFirstCharacterLowerCase(), null => TYPE_OBJECT, _ when type is CodeComposedTypeBase composedType => composedType.Name.ToFirstCharacterUpperCase(), @@ -225,6 +224,15 @@ TYPE_LOWERCASE_BOOLEAN or }; } + public static string? GetPrimitiveAlias(string typeName) + { + return typeName switch + { + TYPE_GUID => TYPE_LOWERCASE_STRING, + _ => null + }; + } + public static bool IsPrimitiveType(CodeType codeType, CodeComposedTypeBase codeComposedTypeBase) => IsPrimitiveType(codeType, codeComposedTypeBase, true); public static bool IsPrimitiveType(CodeType codeType, CodeComposedTypeBase codeComposedTypeBase, bool includeCollectionInformation) => IsPrimitiveType(GetTypescriptTypeString(codeType, codeComposedTypeBase, includeCollectionInformation));