Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
koros committed Jun 13, 2024
1 parent 111b0e1 commit 8f0f5d6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private static void ReplaceFactoryMethodForComposedType(CodeInterface codeInterf
RemoveOldCodeFunctionAndAddNewOne(children, codeInterface, codeNamespace, factoryMethod, factoryFunction);

// Remove the deserializer import statement if its not being used
if (composedType is CodeUnionType || ConventionServiceInstance.IsComposedOfPrimitives(composedType))
if (composedType is CodeUnionType || IsComposedOfPrimitives(composedType))
{
RemoveUnusedDeserializerImport(children, factoryFunction);
}
Expand All @@ -363,7 +363,7 @@ private static void ReplaceDeserializerMethodForComposedType(CodeInterface codeI
if (deserializerMethod is null) return;

// For code union Deserializer is not required, however its needed for Object Intersection types
if (composedType is CodeIntersectionType && !ConventionServiceInstance.IsComposedOfPrimitives(composedType))
if (composedType is CodeIntersectionType && !IsComposedOfPrimitives(composedType))
{
var method = CreateDeserializerMethodForComposedType(codeInterface, deserializerMethod);
var deserializerFunction = new CodeFunction(method) { Name = method.Name };
Expand All @@ -384,7 +384,7 @@ private static void RemoveChildElementFromInterfaceAndNamespace(CodeInterface co
private static CodeMethod CreateFactoryMethodForComposedType(CodeInterface codeInterface, CodeComposedTypeBase composedType, CodeFunction function)
{
var method = CreateCodeMethod(codeInterface, function);
if (composedType is not null && ConventionServiceInstance.IsComposedOfPrimitives(composedType))
if (composedType is not null && IsComposedOfPrimitives(composedType))
method.ReturnType = composedType;
return method;
}
Expand All @@ -393,7 +393,7 @@ private static CodeMethod CreateSerializerMethodForComposedType(CodeInterface co
{
var method = CreateCodeMethod(codeInterface, function);
// Add the key parameter if the composed type is a union of primitive values
if (ConventionServiceInstance.IsComposedOfPrimitives(composedType))
if (IsComposedOfPrimitives(composedType))
method.AddParameter(CreateKeyParameter());
return method;
}
Expand Down
50 changes: 29 additions & 21 deletions src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override void WriteCodeElement(CodeFunction codeElement, LanguageWriter w

var codeMethod = codeElement.OriginalLocalMethod;

var isComposedOfPrimitives = GetOriginalComposedType(codeMethod.ReturnType) is CodeComposedTypeBase composedType && conventions.IsComposedOfPrimitives(composedType);
var isComposedOfPrimitives = GetOriginalComposedType(codeMethod.ReturnType) is CodeComposedTypeBase composedType && IsComposedOfPrimitives(composedType);

var returnType = codeMethod.Kind is CodeMethodKind.Factory || (codeMethod.Kind is CodeMethodKind.ComposedTypeFactory && !isComposedOfPrimitives) ?
FactoryMethodReturnType :
Expand Down Expand Up @@ -94,7 +94,7 @@ private void WriteComposedTypeSerializer(CodeFunction codeElement, LanguageWrite

if (composedParam is null || GetOriginalComposedType(composedParam) is not { } composedType) return;

if (conventions.IsComposedOfPrimitives(composedType))
if (IsComposedOfPrimitives(composedType))
{
WriteComposedTypeSerializationForPrimitives(composedType, composedParam, codeElement, writer);
return;
Expand Down Expand Up @@ -209,26 +209,21 @@ private void WriteFactoryMethodBody(CodeFunction codeElement, string returnType,

switch (composedType)
{
case CodeComposedTypeBase type when ConventionServiceInstance.IsComposedOfPrimitives(type):
case CodeComposedTypeBase type when IsComposedOfPrimitives(type):
WriteFactoryMethodBodyForPrimitives(type, codeElement, writer, parseNodeParameter);
break;
case CodeUnionType _ when parseNodeParameter != null:
WriteFactoryMethodBodyForCodeUnionType(codeElement, writer, parseNodeParameter);
break;
case CodeIntersectionType _ when parseNodeParameter != null:
WriteFactoryMethodBodyForCodeIntersectionType(codeElement, returnType, writer, parseNodeParameter);
WriteDefaultDiscriminator(codeElement, returnType, writer, parseNodeParameter);
break;
default:
WriteNormalFactoryMethodBody(codeElement, returnType, writer);
break;
}
}

private void WriteFactoryMethodBodyForCodeIntersectionType(CodeFunction codeElement, string returnType, LanguageWriter writer, CodeParameter parseNodeParameter)
{
WriteDefaultDiscriminator(codeElement, returnType, writer, parseNodeParameter);
}

private void WriteFactoryMethodBodyForCodeUnionType(CodeFunction codeElement, LanguageWriter writer, CodeParameter parseNodeParameter)
{
WriteDiscriminatorInformation(codeElement, parseNodeParameter, writer);
Expand Down Expand Up @@ -281,7 +276,7 @@ private void WriteDiscriminatorInformation(CodeFunction codeElement, CodeParamet

private string GetParseNodeParameterForPrimitiveValues(CodeFunction codeElement, CodeParameter? parseNodeParameter)
{
if (GetOriginalComposedType(codeElement.OriginalLocalMethod.ReturnType) is CodeComposedTypeBase composedType && conventions.IsComposedOfPrimitives(composedType) && parseNodeParameter is not null)
if (GetOriginalComposedType(codeElement.OriginalLocalMethod.ReturnType) is CodeComposedTypeBase composedType && IsComposedOfPrimitives(composedType) && parseNodeParameter is not null)
{
return $"({parseNodeParameter.Name.ToFirstCharacterLowerCase()})";
}
Expand Down Expand Up @@ -370,30 +365,43 @@ private void WritePropertySerializer(string modelParamName, CodeProperty codePro
{
writer.WriteLine($"if({modelParamName}.{codePropertyName})");
}
if (composedType is not null && conventions.IsComposedOfPrimitives(composedType))
if (composedType is not null && IsComposedOfPrimitives(composedType))
writer.WriteLine($"{serializationName}(writer, \"{codeProperty.WireName}\", {spreadOperator}{modelParamName}.{codePropertyName}{defaultValueSuffix});");
else
writer.WriteLine($"writer.{serializationName}(\"{codeProperty.WireName}\", {spreadOperator}{modelParamName}.{codePropertyName}{defaultValueSuffix});");
}
}


public static string GetSerializationMethodName(CodeTypeBase propertyType, CodeMethod method)
{
ArgumentNullException.ThrowIfNull(propertyType);
ArgumentNullException.ThrowIfNull(method);
var composedType = GetOriginalComposedType(propertyType);

if (composedType is not null && ConventionServiceInstance.IsComposedOfPrimitives(composedType))
var composedType = GetOriginalComposedType(propertyType);
if (composedType is not null && IsComposedOfPrimitives(composedType))
{
return $"serialize{composedType.Name.ToFirstCharacterUpperCase()}";
}

var propertyTypeName = ConventionServiceInstance.TranslateType(propertyType);
CodeType? currentType = composedType is not null ? GetCodeTypeForComposedType(composedType) : propertyType as CodeType;
return (currentType, propertyTypeName) switch

if (currentType != null && !string.IsNullOrEmpty(propertyTypeName))
{
(CodeType type, string prop) when !string.IsNullOrEmpty(prop) && GetSerializationMethodNameForCodeType(type, prop) is string result && !string.IsNullOrWhiteSpace(result) => result,
(_, "string" or "boolean" or "number" or "Guid" or "Date" or "DateOnly" or "TimeOnly" or "Duration") => $"write{propertyTypeName.ToFirstCharacterUpperCase()}Value",
_ => "writeObjectValue"
};
var result = GetSerializationMethodNameForCodeType(currentType, propertyTypeName);
if (!string.IsNullOrWhiteSpace(result))
{
return result;
}
}

if (propertyTypeName is TYPE_LOWERCASE_STRING or TYPE_LOWERCASE_BOOLEAN or TYPE_NUMBER or TYPE_GUID or TYPE_DATE or TYPE_DATE_ONLY or TYPE_TIME_ONLY or TYPE_DURATION)
{
return $"write{propertyTypeName.ToFirstCharacterUpperCase()}Value";
}

return "writeObjectValue";
}

private static CodeType GetCodeTypeForComposedType(CodeComposedTypeBase composedType)
Expand All @@ -419,7 +427,8 @@ _ when ConventionServiceInstance.StreamTypeName.Equals(propertyType, StringCompa

private void WriteDeserializerFunction(CodeFunction codeFunction, LanguageWriter writer)
{
if (codeFunction.OriginalLocalMethod.Parameters.FirstOrDefault() is CodeParameter param && param.Type is CodeType codeType && codeType.TypeDefinition is CodeInterface codeInterface)
var param = codeFunction.OriginalLocalMethod.Parameters.FirstOrDefault();
if (param?.Type is CodeType codeType && codeType.TypeDefinition is CodeInterface codeInterface)
{
var properties = codeInterface.Properties.Where(static x => x.IsOfKind(CodePropertyKind.Custom, CodePropertyKind.BackingStore) && !x.ExistsInBaseType);

Expand All @@ -434,8 +443,7 @@ private void WriteDeserializerFunction(CodeFunction codeFunction, LanguageWriter

writer.CloseBlock();
}
else
throw new InvalidOperationException($"Model interface for deserializer function {codeFunction.Name} is not available");
else throw new InvalidOperationException($"Model interface for deserializer function {codeFunction.Name} is not available");
}

private void WriteInheritsBlock(CodeInterface codeInterface, CodeParameter param, LanguageWriter writer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public override string GetAccessModifier(AccessModifier access)
};
}

public bool IsComposedOfPrimitives(CodeComposedTypeBase composedType)
public static bool IsComposedOfPrimitives(CodeComposedTypeBase composedType)
{
// Primitive values don't have a discriminator property so it should be handled differently if any of the values is primitive
return composedType?.Types.All(x => IsPrimitiveType(GetTypeString(x, composedType))) ?? false;
return composedType?.Types.All(x => IsPrimitiveType(GetTypescriptTypeString(x, composedType))) ?? false;
}

public override string GetParameterSignature(CodeParameter parameter, CodeElement targetElement, LanguageWriter? writer = null)
Expand Down

0 comments on commit 8f0f5d6

Please sign in to comment.