Skip to content

Commit

Permalink
fix missing refrences
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev committed Nov 11, 2024
1 parent c81440e commit b77ac28
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
39 changes: 20 additions & 19 deletions src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeFile>(), codeElement.OriginalLocalMethod, writer, isVoid, conventions);
var codeFile = codeElement.GetImmediateParentOfType<CodeFile>();
CodeMethodWriter.WriteMethodDocumentationInternal(codeFile, codeElement.OriginalLocalMethod, writer, isVoid, conventions);
CodeMethodWriter.WriteMethodTypecheckIgnoreInternal(codeElement.OriginalLocalMethod, writer);
CodeMethodWriter.WriteMethodPrototypeInternal(codeElement.OriginalLocalMethod, writer, returnType, isVoid, conventions, true);

Expand All @@ -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);
Expand All @@ -53,26 +54,26 @@ 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)

Check warning on line 57 in src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'codeElement'. (https://rules.sonarsource.com/csharp/RSPEC-1172)
{
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)
{
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;

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)))
Expand Down Expand Up @@ -222,24 +223,24 @@ private static void WriteSerializationRegistration(HashSet<string> 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);

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:
Expand Down Expand Up @@ -524,27 +525,27 @@ _ 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
{
throw new InvalidOperationException($"Model interface for deserializer function {codeFunction.Name} is not available");
}
}

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);

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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} }},");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,12 @@ public static string GetFactoryMethodName(CodeTypeBase targetClassType, CodeElem
return definitionClass.GetImmediateParentOfType<CodeFile>(definitionClass)?.FindChildByName<CodeFunction>(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;

Expand All @@ -339,19 +339,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)})"
};
}
}

0 comments on commit b77ac28

Please sign in to comment.