Skip to content

Commit

Permalink
Adds Parsable Info
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev committed Nov 4, 2024
1 parent 7be3b29 commit 9340ce7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
21 changes: 0 additions & 21 deletions src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,31 +169,10 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken
GroupReusableModelsInSingleFile(modelsNamespace);
RemoveSelfReferencingUsings(generatedCode);
AddAliasToCodeFileUsings(generatedCode);
//CorrectSerializerParameters(generatedCode);
cancellationToken.ThrowIfCancellationRequested();
}, cancellationToken);
}

private static void CorrectSerializerParameters(CodeElement currentElement)
{
if (currentElement is CodeFunction currentFunction &&
currentFunction.OriginalLocalMethod.Kind is CodeMethodKind.Serializer)
{
foreach (var parameter in currentFunction.OriginalLocalMethod.Parameters
.Where(p => GetOriginalComposedType(p.Type) is CodeComposedTypeBase composedType &&
composedType.IsComposedOfObjectsAndPrimitives(IsPrimitiveType)))
{
var composedType = GetOriginalComposedType(parameter.Type)!;
var newType = (CodeComposedTypeBase)composedType.Clone();
var nonPrimitiveTypes = composedType.Types.Where(x => !IsPrimitiveType(x, composedType)).ToArray();
newType.SetTypes(nonPrimitiveTypes);
parameter.Type = newType;
}
}

CrawlTree(currentElement, CorrectSerializerParameters);
}

private static void AddAliasToCodeFileUsings(CodeElement currentElement)
{
if (currentElement is CodeFile codeFile)
Expand Down
7 changes: 6 additions & 1 deletion src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ private void WriteComposedTypeDeserializer(CodeFunction codeElement, LanguageWri
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)}"));
writer.WriteLine($"\"\": n => {{ {composedParam.Name.ToFirstCharacterLowerCase()} = {expression}}},");
}
foreach (var mappedType in composedType.Types.Where(x => !IsPrimitiveType(x, composedType, false)))
{
var functionName = GetDeserializerFunctionName(codeElement, mappedType);
Expand Down Expand Up @@ -181,7 +186,7 @@ private void WriteCaseStatementForPrimitiveTypeSerialization(CodeTypeBase type,
writer.StartBlock(type.IsCollection
? $"case Array.isArray({modelParamName}) && ({modelParamName}).every(item => typeof item === '{nodeType}') :"
: $"case typeof {modelParamName} === \"{nodeType}\":");

writer.WriteLine($"writer.{serializationName}({key}, {modelParamName} as {conventions.GetTypeString(type, method)});");
writer.WriteLine("break;");
writer.DecreaseIndent();
Expand Down
29 changes: 18 additions & 11 deletions src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,24 @@ public override string GetAccessModifier(AccessModifier access)
};
}

private static bool ShouldIncludeCollectionInformationForParameter(CodeParameter parameter)
{
return !(GetOriginalComposedType(parameter) is not null
&& parameter.Parent is CodeMethod codeMethod
&& (codeMethod.IsOfKind(CodeMethodKind.Serializer) || codeMethod.IsOfKind(CodeMethodKind.Deserializer)));
}

public override string GetParameterSignature(CodeParameter parameter, CodeElement targetElement, LanguageWriter? writer = null)
{
ArgumentNullException.ThrowIfNull(parameter);
var includeCollectionInformation = ShouldIncludeCollectionInformationForParameter(parameter);
var paramType = GetTypescriptTypeString(parameter.Type, targetElement, includeCollectionInformation: includeCollectionInformation, inlineComposedTypeString: true);
var isComposedOfPrimitives = GetOriginalComposedType(parameter.Type) is CodeComposedTypeBase composedType && composedType.IsComposedOfPrimitives(IsPrimitiveType);
var paramType = GetTypescriptTypeString(parameter.Type, targetElement, includeCollectionInformation: true, inlineComposedTypeString: true);
var composedType = GetOriginalComposedType(parameter.Type);
var isComposedOfPrimitives = composedType != null && composedType.IsComposedOfPrimitives(IsPrimitiveType);

// add a 'Parsable' suffix to composed parameters of primitives only if they are not deserialization targets
var parsableTypes = (
composedType != null && composedType.IsComposedOfPrimitives(IsPrimitiveTypeOrPrimitiveCollection),
parameter.Parent is CodeMethod method && method.IsOfKind(CodeMethodKind.Deserializer)
) switch
{
(true, false) => string.Empty,
(true, true) => "Parsable | ",
_ => string.Empty,
};

var defaultValueSuffix = (string.IsNullOrEmpty(parameter.DefaultValue), parameter.Kind, isComposedOfPrimitives) switch
{
(false, CodeParameterKind.DeserializationTarget, false) when parameter.Parent is CodeMethod codeMethod && codeMethod.Kind is CodeMethodKind.Serializer
Expand All @@ -107,7 +112,7 @@ public override string GetParameterSignature(CodeParameter parameter, CodeElemen
(false, CodeParameterKind.DeserializationTarget) => ("Partial<", ">"),
_ => (string.Empty, string.Empty),
};
return $"{parameter.Name.ToFirstCharacterLowerCase()}{(parameter.Optional && parameter.Type.IsNullable ? "?" : string.Empty)}: {partialPrefix}{paramType}{partialSuffix}{(parameter.Type.IsNullable ? " | undefined" : string.Empty)}{defaultValueSuffix}";
return $"{parameter.Name.ToFirstCharacterLowerCase()}{(parameter.Optional && parameter.Type.IsNullable ? "?" : string.Empty)}: {partialPrefix}{parsableTypes}{paramType}{partialSuffix}{(parameter.Type.IsNullable ? " | undefined" : string.Empty)}{defaultValueSuffix}";
}

public override string GetTypeString(CodeTypeBase code, CodeElement targetElement, bool includeCollectionInformation = true, LanguageWriter? writer = null)
Expand Down Expand Up @@ -227,6 +232,8 @@ TYPE_LOWERCASE_BOOLEAN or

public static bool IsPrimitiveType(CodeType codeType, CodeComposedTypeBase codeComposedTypeBase) => IsPrimitiveType(codeType, codeComposedTypeBase, true);

public static bool IsPrimitiveTypeOrPrimitiveCollection(CodeType codeType, CodeComposedTypeBase codeComposedTypeBase) => IsPrimitiveType(codeType, codeComposedTypeBase, false);

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;
Expand Down

0 comments on commit 9340ce7

Please sign in to comment.