Skip to content

Commit

Permalink
- fixes missing return type bug in TS
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Jan 24, 2024
1 parent 56c42d0 commit 167eaea
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed a bug where scalar error mappings would be generated even though it's not supported by the http request adapter. [#4018](https://github.com/microsoft/kiota/issues/4018)
- Switched to proxy generation for TypeScript, leading to about ~44% bundle sizes reduction. [#3642](https://github.com/microsoft/kiota/issues/3642)
- Fixed a bug where TypeScript models factory methods would be missing return types.

## [1.10.1] - 2024-01-12

Expand Down
4 changes: 4 additions & 0 deletions src/Kiota.Builder/CodeDOM/CodeParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public enum CodeParameterKind
/// The content type of the request body when it couldn't be inferred from the description.
/// </summary>
RequestBodyContentType,
/// <summary>
/// When the deserialization method is replaced as a function, this is the parameter representing instance we're deserializing into.
/// </summary>
DeserializationTarget,
}

public class CodeParameter : CodeTerminalWithKind<CodeParameterKind>, ICloneable, IDocumentedElement, IDeprecableElement
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,8 @@ private static void AddInterfaceParamToSerializer(CodeInterface modelInterface,
{
Name = ReturnFinalInterfaceName(modelInterface.Name), // remove the interface suffix
DefaultValue = "{}",
Type = new CodeType { Name = ReturnFinalInterfaceName(modelInterface.Name), TypeDefinition = modelInterface }
Type = new CodeType { Name = ReturnFinalInterfaceName(modelInterface.Name), TypeDefinition = modelInterface },
Kind = CodeParameterKind.DeserializationTarget,
});

if (modelInterface.Parent is not null)
Expand Down
4 changes: 3 additions & 1 deletion src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public override void WriteCodeElement(CodeFunction codeElement, LanguageWriter w

var codeMethod = codeElement.OriginalLocalMethod;

var returnType = codeMethod.Kind != CodeMethodKind.Factory ? conventions.GetTypeString(codeMethod.ReturnType, codeElement) : string.Empty;
var returnType = codeMethod.Kind is CodeMethodKind.Factory ?
"((instance?: Parsable) => Record<string, (node: ParseNode) => void>)" :
conventions.GetTypeString(codeMethod.ReturnType, codeElement);
var isVoid = "void".EqualsIgnoreCase(returnType);
CodeMethodWriter.WriteMethodDocumentationInternal(codeElement.OriginalLocalMethod, writer, isVoid, conventions);
CodeMethodWriter.WriteMethodPrototypeInternal(codeElement.OriginalLocalMethod, writer, returnType, isVoid, conventions, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@ public override string GetParameterSignature(CodeParameter parameter, CodeElemen
{
ArgumentNullException.ThrowIfNull(parameter);
var paramType = GetTypeString(parameter.Type, targetElement);
var defaultValueSuffix = string.IsNullOrEmpty(parameter.DefaultValue) ? string.Empty : $" = {parameter.DefaultValue} as {paramType}";
return $"{parameter.Name.ToFirstCharacterLowerCase()}{(parameter.Optional && parameter.Type.IsNullable ? "?" : string.Empty)}: {paramType}{(parameter.Type.IsNullable ? " | undefined" : string.Empty)}{defaultValueSuffix}";
var defaultValueSuffix = (string.IsNullOrEmpty(parameter.DefaultValue), parameter.Kind) switch
{
(false, CodeParameterKind.DeserializationTarget) => $" = {parameter.DefaultValue}",
(false, _) => $" = {parameter.DefaultValue} as {paramType}",
(true, _) => string.Empty,
};
var (partialPrefix, partialSuffix) = parameter.Kind switch
{
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}";
}
public override string GetTypeString(CodeTypeBase code, CodeElement targetElement, bool includeCollectionInformation = true, LanguageWriter? writer = null)
{
Expand Down

0 comments on commit 167eaea

Please sign in to comment.