Skip to content

Commit

Permalink
fix: patches typescript composed type serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Sep 6, 2024
1 parent 643de49 commit 0122648
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 4 additions & 7 deletions src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void WriteComposedTypeDeserializer(CodeFunction codeElement, LanguageWri
if (GetOriginalComposedType(composedParam) is not { } composedType) return;

writer.StartBlock("return {");
foreach (var mappedType in composedType.Types.Where(x => !IsPrimitiveType(x, composedType)))
foreach (var mappedType in composedType.Types.Where(x => !IsPrimitiveType(x, composedType, false)))
{
var functionName = GetDeserializerFunctionName(codeElement, mappedType);
var variableName = composedParam.Name.ToFirstCharacterLowerCase();
Expand All @@ -86,12 +86,12 @@ private void WriteComposedTypeSerializer(CodeFunction codeElement, LanguageWrite
{
if (GetOriginalComposedType(composedParam) is not { } composedType) return;

if (composedType.IsComposedOfPrimitives(IsPrimitiveType))
if (composedType.IsComposedOfPrimitives((x, y) => IsPrimitiveType(x, y, false)))
{
var paramName = composedParam.Name.ToFirstCharacterLowerCase();
writer.WriteLine($"if ({paramName} === undefined || {paramName} === null) return;");
writer.StartBlock($"switch (typeof {paramName}) {{");
foreach (var type in composedType.Types.Where(x => IsPrimitiveType(x, composedType)))
foreach (var type in composedType.Types.Where(x => IsPrimitiveType(x, composedType, false)))
{
WriteCaseStatementForPrimitiveTypeSerialization(type, "key", paramName, codeElement, writer);
}
Expand Down Expand Up @@ -327,11 +327,8 @@ private string FindFunctionInNameSpace(string functionName, CodeElement codeElem
CodeFunction[] codeFunctions = myNamespace.FindChildrenByName<CodeFunction>(functionName).ToArray();

var codeFunction = Array.Find(codeFunctions,
func => func.GetImmediateParentOfType<CodeNamespace>().Name == myNamespace.Name);

if (codeFunction == null)
func => func.GetImmediateParentOfType<CodeNamespace>().Name == myNamespace.Name) ??
throw new InvalidOperationException($"Function {functionName} not found in namespace {myNamespace.Name}");

return conventions.GetTypeString(new CodeType { TypeDefinition = codeFunction }, codeElement, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ TYPE_LOWERCASE_BOOLEAN or
};
}

public static bool IsPrimitiveType(CodeType codeType, CodeComposedTypeBase codeComposedTypeBase) => IsPrimitiveType(GetTypescriptTypeString(codeType, codeComposedTypeBase));
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));

internal static string RemoveInvalidDescriptionCharacters(string originalDescription) => originalDescription?.Replace("\\", "/", StringComparison.OrdinalIgnoreCase) ?? string.Empty;
public override bool WriteShortDescription(IDocumentedElement element, LanguageWriter writer, string prefix = "", string suffix = "")
Expand Down

0 comments on commit 0122648

Please sign in to comment.