diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs index 3af93b703e..41857b0d3f 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs @@ -29,7 +29,8 @@ public override void WriteCodeElement(CodeFunction codeElement, LanguageWriter w FactoryMethodReturnType : GetTypescriptTypeString(codeMethod.ReturnType, codeElement, inlineComposedTypeString: true); var isVoid = "void".EqualsIgnoreCase(returnType); - CodeMethodWriter.WriteMethodDocumentationInternal(codeElement.GetImmediateParentOfType(), codeElement.OriginalLocalMethod, writer, isVoid, conventions); + var codeFile = codeElement.GetImmediateParentOfType(); + CodeMethodWriter.WriteMethodDocumentationInternal(codeFile, codeElement.OriginalLocalMethod, writer, isVoid, conventions); CodeMethodWriter.WriteMethodTypecheckIgnoreInternal(codeElement.OriginalLocalMethod, writer); CodeMethodWriter.WriteMethodPrototypeInternal(codeElement.OriginalLocalMethod, writer, returnType, isVoid, conventions, true); @@ -38,13 +39,13 @@ public override void WriteCodeElement(CodeFunction codeElement, LanguageWriter w switch (codeMethod.Kind) { case CodeMethodKind.Deserializer: - WriteDeserializerFunction(codeElement, writer); + WriteDeserializerFunction(codeElement, codeFile, writer); break; case CodeMethodKind.Serializer: WriteSerializerFunction(codeElement, writer); break; case CodeMethodKind.Factory: - WriteFactoryMethod(codeElement, writer); + WriteFactoryMethod(codeElement, codeFile, writer); break; case CodeMethodKind.ClientConstructor: WriteApiConstructorBody(parentFile, codeMethod, writer); @@ -53,10 +54,10 @@ public override void WriteCodeElement(CodeFunction codeElement, LanguageWriter w } } - private string GetSerializationMethodsForPrimitiveUnionTypes(CodeComposedTypeBase composedType, string parseNodeParameterName, CodeFunction codeElement, bool nodeParameterCanBeNull = true) + private string GetSerializationMethodsForPrimitiveUnionTypes(CodeComposedTypeBase composedType, string parseNodeParameterName, CodeFunction codeElement, CodeFile codeFile, bool nodeParameterCanBeNull = true) { var optionalChainingSymbol = nodeParameterCanBeNull ? "?" : string.Empty; - return string.Join(" ?? ", composedType.Types.Where(x => IsPrimitiveType(x, composedType)).Select(x => $"{parseNodeParameterName}{optionalChainingSymbol}." + conventions.GetDeserializationMethodName(x, codeElement.OriginalLocalMethod))); + return string.Join(" ?? ", composedType.Types.Where(x => IsPrimitiveType(x, composedType)).Select(x => $"{parseNodeParameterName}{optionalChainingSymbol}." + conventions.GetDeserializationMethodName(x, codeFile))); } private static CodeParameter? GetComposedTypeParameter(CodeFunction codeElement) @@ -64,7 +65,7 @@ private string GetSerializationMethodsForPrimitiveUnionTypes(CodeComposedTypeBas return codeElement.OriginalLocalMethod.Parameters.FirstOrDefault(x => GetOriginalComposedType(x) is not null); } - private void WriteComposedTypeDeserializer(CodeFunction codeElement, LanguageWriter writer, CodeParameter composedParam) + private void WriteComposedTypeDeserializer(CodeFunction codeElement, LanguageWriter writer, CodeParameter composedParam, CodeFile codeFile) { if (GetOriginalComposedType(composedParam) is not { } composedType) return; @@ -72,7 +73,7 @@ private void WriteComposedTypeDeserializer(CodeFunction codeElement, LanguageWri writer.StartBlock("return {"); if (composedType.Types.Any(x => IsPrimitiveType(x, composedType, false))) { - var expression = string.Join(" ?? ", composedType.Types.Where(x => IsPrimitiveType(x, composedType, false)).Select(codeType => $"n.{conventions.GetDeserializationMethodName(codeType, codeElement.OriginalLocalMethod, composedType.IsCollection)}")); + var expression = string.Join(" ?? ", composedType.Types.Where(x => IsPrimitiveType(x, composedType, false)).Select(codeType => $"n.{conventions.GetDeserializationMethodName(codeType, codeFile, composedType.IsCollection)}")); writer.WriteLine($"\"\" : n => {{ {composedParam.Name.ToFirstCharacterLowerCase()} = {expression}}},"); } foreach (var mappedType in composedType.Types.Where(x => !IsPrimitiveType(x, composedType, false))) @@ -222,16 +223,16 @@ private static void WriteSerializationRegistration(HashSet serialization writer.WriteLine($"{methodName}({module});"); } - private void WriteFactoryMethod(CodeFunction codeElement, LanguageWriter writer) + private void WriteFactoryMethod(CodeFunction codeElement, CodeFile codeFile, LanguageWriter writer) { var returnType = conventions.GetTypeString(codeElement.OriginalLocalMethod.ReturnType, codeElement); if (codeElement.OriginalMethodParentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForInheritedType) WriteDefensiveStatements(codeElement.OriginalLocalMethod, writer); - WriteFactoryMethodBody(codeElement, returnType, writer); + WriteFactoryMethodBody(codeElement, returnType, codeFile, writer); } - private void WriteFactoryMethodBody(CodeFunction codeElement, string returnType, LanguageWriter writer) + private void WriteFactoryMethodBody(CodeFunction codeElement, string returnType, CodeFile codeFile, LanguageWriter writer) { var parseNodeParameter = codeElement.OriginalLocalMethod.Parameters.OfKind(CodeParameterKind.ParseNode); var composedType = GetOriginalComposedType(codeElement.OriginalLocalMethod.ReturnType); @@ -239,7 +240,7 @@ private void WriteFactoryMethodBody(CodeFunction codeElement, string returnType, switch (composedType) { case CodeComposedTypeBase type when type.IsComposedOfPrimitives(IsPrimitiveType): - string primitiveValuesUnionString = GetSerializationMethodsForPrimitiveUnionTypes(composedType, parseNodeParameter!.Name.ToFirstCharacterLowerCase(), codeElement); + string primitiveValuesUnionString = GetSerializationMethodsForPrimitiveUnionTypes(composedType, parseNodeParameter!.Name.ToFirstCharacterLowerCase(), codeElement, codeFile); writer.WriteLine($"return {primitiveValuesUnionString};"); break; case CodeUnionType _ when parseNodeParameter != null: @@ -524,19 +525,19 @@ _ when conventions.StreamTypeName.Equals(propertyType, StringComparison.OrdinalI }; } - private void WriteDeserializerFunction(CodeFunction codeFunction, LanguageWriter writer) + private void WriteDeserializerFunction(CodeFunction codeFunction, CodeFile codeFile, LanguageWriter writer) { var composedParam = GetComposedTypeParameter(codeFunction); if (composedParam is not null) { - WriteComposedTypeDeserializer(codeFunction, writer, composedParam); + WriteComposedTypeDeserializer(codeFunction, writer, composedParam, codeFile); return; } var param = codeFunction.OriginalLocalMethod.Parameters.FirstOrDefault(); if (param?.Type is CodeType codeType && codeType.TypeDefinition is CodeInterface codeInterface) { - WriteDeserializerFunctionProperties(param, codeInterface, codeFunction, writer); + WriteDeserializerFunctionProperties(param, codeInterface, codeFunction, codeFile, writer); } else { @@ -544,7 +545,7 @@ private void WriteDeserializerFunction(CodeFunction codeFunction, LanguageWriter } } - private void WriteDeserializerFunctionProperties(CodeParameter param, CodeInterface codeInterface, CodeFunction codeFunction, LanguageWriter writer) + private void WriteDeserializerFunctionProperties(CodeParameter param, CodeInterface codeInterface, CodeFunction codeFunction, CodeFile codeFile, LanguageWriter writer) { var properties = codeInterface.Properties.Where(static x => x.IsOfKind(CodePropertyKind.Custom, CodePropertyKind.BackingStore) && !x.ExistsInBaseType); @@ -557,7 +558,7 @@ private void WriteDeserializerFunctionProperties(CodeParameter param, CodeInterf foreach (var otherProp in properties) { - WritePropertyDeserializationBlock(otherProp, param, primaryErrorMapping, primaryErrorMappingKey, codeFunction, writer); + WritePropertyDeserializationBlock(otherProp, param, primaryErrorMapping, primaryErrorMappingKey, codeFile, writer); } writer.CloseBlock(); @@ -578,7 +579,7 @@ private static (string, string) GetPrimaryErrorMapping(CodeFunction codeFunction return (primaryErrorMapping, primaryErrorMappingKey); } - private void WritePropertyDeserializationBlock(CodeProperty otherProp, CodeParameter param, string primaryErrorMapping, string primaryErrorMappingKey, CodeFunction codeFunction, LanguageWriter writer) + private void WritePropertyDeserializationBlock(CodeProperty otherProp, CodeParameter param, string primaryErrorMapping, string primaryErrorMappingKey, CodeFile codeFile, LanguageWriter writer) { var suffix = otherProp.Name.Equals(primaryErrorMappingKey, StringComparison.Ordinal) ? primaryErrorMapping : string.Empty; var paramName = param.Name.ToFirstCharacterLowerCase(); @@ -590,12 +591,12 @@ private void WritePropertyDeserializationBlock(CodeProperty otherProp, CodeParam } else if (GetOriginalComposedType(otherProp.Type) is { } composedType) { - var expression = string.Join(" ?? ", composedType.Types.Select(codeType => $"n.{conventions.GetDeserializationMethodName(codeType, codeFunction.OriginalLocalMethod, composedType.IsCollection)}")); + var expression = string.Join(" ?? ", composedType.Types.Select(codeType => $"n.{conventions.GetDeserializationMethodName(codeType, codeFile, composedType.IsCollection)}")); writer.WriteLine($"\"{otherProp.WireName}\": n => {{ {paramName}.{propName} = {expression};{suffix} }},"); } else { - var objectSerializationMethodName = conventions.GetDeserializationMethodName(otherProp.Type, codeFunction.OriginalLocalMethod); + var objectSerializationMethodName = conventions.GetDeserializationMethodName(otherProp.Type, codeFile); var defaultValueSuffix = GetDefaultValueSuffix(otherProp); writer.WriteLine($"\"{otherProp.WireName}\": n => {{ {paramName}.{propName} = n.{objectSerializationMethodName}{defaultValueSuffix};{suffix} }},"); } diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs index 0587dcc071..54de4264cf 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs @@ -315,12 +315,12 @@ public static string GetFactoryMethodName(CodeTypeBase targetClassType, CodeElem return definitionClass.GetImmediateParentOfType(definitionClass)?.FindChildByName(factoryMethodName); } - public string GetDeserializationMethodName(CodeTypeBase codeType, CodeMethod method, bool? IsCollection = null) + public string GetDeserializationMethodName(CodeTypeBase codeType, CodeElement targetElement, bool? IsCollection = null) { ArgumentNullException.ThrowIfNull(codeType); - ArgumentNullException.ThrowIfNull(method); + ArgumentNullException.ThrowIfNull(targetElement); var isCollection = IsCollection == true || codeType.IsCollection; - var propertyType = GetTypescriptTypeString(codeType, method, false); + var propertyType = GetTypescriptTypeString(codeType, targetElement, false); CodeTypeBase _codeType = GetOriginalComposedType(codeType) is CodeComposedTypeBase composedType ? new CodeType() { Name = composedType.Name, TypeDefinition = composedType } : codeType; @@ -331,19 +331,19 @@ public string GetDeserializationMethodName(CodeTypeBase codeType, CodeMethod met (CodeEnum currentEnum, _, _) when currentEnum.CodeEnumObject is not null => $"{(currentEnum.Flags || isCollection ? "getCollectionOfEnumValues" : "getEnumValue")}<{currentEnum.Name.ToFirstCharacterUpperCase()}>({currentEnum.CodeEnumObject.Name.ToFirstCharacterUpperCase()})", (_, _, _) when StreamTypeName.Equals(propertyType, StringComparison.OrdinalIgnoreCase) => "getByteArrayValue", (_, true, _) when currentType.TypeDefinition is null => $"getCollectionOfPrimitiveValues<{propertyType}>()", - (_, true, _) => $"getCollectionOfObjectValues<{propertyType.ToFirstCharacterUpperCase()}>({GetFactoryMethodName(_codeType, method)})", - _ => GetDeserializationMethodNameForPrimitiveOrObject(_codeType, propertyType, method) + (_, true, _) => $"getCollectionOfObjectValues<{propertyType.ToFirstCharacterUpperCase()}>({GetFactoryMethodName(_codeType, targetElement)})", + _ => GetDeserializationMethodNameForPrimitiveOrObject(_codeType, propertyType, targetElement) }; } - return GetDeserializationMethodNameForPrimitiveOrObject(_codeType, propertyType, method); + return GetDeserializationMethodNameForPrimitiveOrObject(_codeType, propertyType, targetElement); } - private static string GetDeserializationMethodNameForPrimitiveOrObject(CodeTypeBase propType, string propertyTypeName, CodeMethod method) + private static string GetDeserializationMethodNameForPrimitiveOrObject(CodeTypeBase propType, string propertyTypeName, CodeElement targetElement) { return propertyTypeName switch { TYPE_LOWERCASE_STRING or TYPE_STRING or TYPE_LOWERCASE_BOOLEAN or TYPE_BOOLEAN or TYPE_NUMBER or TYPE_GUID or TYPE_DATE or TYPE_DATE_ONLY or TYPE_TIME_ONLY or TYPE_DURATION => $"get{propertyTypeName.ToFirstCharacterUpperCase()}Value()", - _ => $"getObjectValue<{propertyTypeName.ToFirstCharacterUpperCase()}>({GetFactoryMethodName(propType, method)})" + _ => $"getObjectValue<{propertyTypeName.ToFirstCharacterUpperCase()}>({GetFactoryMethodName(propType, targetElement)})" }; } }