Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- fixes missing return type bug in TS #4049

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading