From 1ba28747ec716124e5394deddd0f8c6716e9cb94 Mon Sep 17 00:00:00 2001 From: koros Date: Tue, 19 Mar 2024 23:27:16 +0300 Subject: [PATCH] add support for enums or collection of enums as root payload --- CHANGELOG.md | 1 + .../Writers/TypeScript/CodeConstantWriter.cs | 33 ++++-- .../TypeScript/CodeConstantWriterTests.cs | 102 ++++++++++++++++++ 3 files changed, 126 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2224a66040..898e381fc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added the init command as part of the experience revamp of [#3356](https://github.com/microsoft/kiota/issues/3356) - Added uri-form encoded serialization for Python. [#2075](https://github.com/microsoft/kiota/issues/2075) - Added support for multipart form data request body in Python. [#3030](https://github.com/microsoft/kiota/issues/3030) +- Added support for Enums or collection of enum values as root of the payload. [#1042](https://github.com/microsoft/kiota-typescript/issues/1042) ### Changed diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs index bdcf9460ba..9e8879b3eb 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs @@ -99,7 +99,9 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri var returnType = conventions.GetTypeString(executorMethod.ReturnType, codeElement); var isVoid = "void".EqualsIgnoreCase(returnType); var isStream = conventions.StreamTypeName.Equals(returnType, StringComparison.OrdinalIgnoreCase); + var isEnum = executorMethod.ReturnType is CodeType codeType && codeType.TypeDefinition is CodeEnum; var returnTypeWithoutCollectionSymbol = GetReturnTypeWithoutCollectionSymbol(executorMethod, returnType); + var isPrimitive = conventions.IsPrimitiveType(returnTypeWithoutCollectionSymbol); writer.StartBlock($"{executorMethod.Name.ToFirstCharacterLowerCase()}: {{"); var urlTemplateValue = executorMethod.HasUrlTemplateOverride ? $"\"{executorMethod.UrlTemplateOverride}\"" : uriTemplateConstant.Name.ToFirstCharacterUpperCase(); writer.WriteLine($"uriTemplate: {urlTemplateValue},"); @@ -115,10 +117,13 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri } writer.CloseBlock("},"); } - writer.WriteLine($"adapterMethodName: \"{GetSendRequestMethodName(isVoid, isStream, executorMethod.ReturnType.IsCollection, returnTypeWithoutCollectionSymbol)}\","); - if (!isVoid) + writer.WriteLine($"adapterMethodName: \"{GetSendRequestMethodName(isVoid, isStream, executorMethod.ReturnType.IsCollection, isPrimitive, isEnum)}\","); + if (isEnum) + writer.WriteLine($"enumObject: \"{executorMethod.ReturnType.Name.ToFirstCharacterUpperCase()}\","); + else if (!isVoid) writer.WriteLine($"responseBodyFactory: {GetTypeFactory(isVoid, isStream, executorMethod, writer)},"); var sanitizedRequestBodyContentType = executorMethod.RequestBodyContentType.SanitizeDoubleQuote(); + if (!string.IsNullOrEmpty(sanitizedRequestBodyContentType)) writer.WriteLine($"requestBodyContentType: \"{sanitizedRequestBodyContentType}\","); if (executorMethod.Parameters.FirstOrDefault(static x => x.Kind is CodeParameterKind.RequestBody) is CodeParameter requestBody) @@ -186,17 +191,25 @@ currentType.TypeDefinition is CodeClass definitionClass && } throw new InvalidOperationException($"Unable to find factory method for {targetClassName}"); } - private string GetSendRequestMethodName(bool isVoid, bool isStream, bool isCollection, string returnType) + + private string GetSendRequestMethodName(bool isVoid, bool isStream, bool isCollection, bool isPrimitive, bool isEnum) { - if (isVoid) return "sendNoResponseContent"; - if (isCollection) + if (isVoid) { - if (conventions.IsPrimitiveType(returnType)) return $"sendCollectionOfPrimitive"; - return $"sendCollection"; + return "sendNoResponseContent"; + } + else if (isEnum) + { + return isCollection ? "sendCollectionOfEnum" : "sendEnum"; + } + else if (isPrimitive) + { + return isCollection ? "sendCollectionOfPrimitive" : "sendPrimitive"; + } + else + { + return isCollection ? "sendCollection" : isStream ? "sendPrimitive" : "send"; } - - if (isStream || conventions.IsPrimitiveType(returnType)) return $"sendPrimitive"; - return $"send"; } private void WriteUriTemplateConstant(CodeConstant codeElement, LanguageWriter writer) diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs index d3210509d7..2d228419e9 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs @@ -187,6 +187,108 @@ public void WritesRequestExecutorBodyForCollections() AssertExtensions.CurlyBracesAreClosed(result); } [Fact] + public void WritesRequestExecutorForEnum() + { + AddCodeEnum(); + method.Kind = CodeMethodKind.RequestExecutor; + method.HttpMethod = HttpMethod.Get; + method.ReturnType = new CodeType + { + Name = "SomeComplexTypeForRequestBody", + TypeDefinition = currentEnum, + }; + AddRequestBodyParameters(); + var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass); + var codeFile = parentClass.GetImmediateParentOfType().TryAddCodeFile("foo", constant); + codeFile.AddElements(new CodeConstant + { + Name = "UriTemplate", + Kind = CodeConstantKind.UriTemplate, + UriTemplate = "{baseurl+}/foo/bar" + }); + writer.Write(constant); + var result = tw.ToString(); + Assert.Contains("sendEnum", result); + Assert.Contains("enumObject:", result); + Assert.Contains(EnumName.ToFirstCharacterUpperCase(), result); + AssertExtensions.CurlyBracesAreClosed(result); + } + [Fact] + public void WritesRequestExecutorForEnumCollection() + { + AddCodeEnum(); + method.Kind = CodeMethodKind.RequestExecutor; + method.HttpMethod = HttpMethod.Get; + method.ReturnType = new CodeType + { + Name = "SomeComplexTypeForRequestBody", + TypeDefinition = currentEnum, + CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array, + }; + AddRequestBodyParameters(); + var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass); + var codeFile = parentClass.GetImmediateParentOfType().TryAddCodeFile("foo", constant); + codeFile.AddElements(new CodeConstant + { + Name = "UriTemplate", + Kind = CodeConstantKind.UriTemplate, + UriTemplate = "{baseurl+}/foo/bar" + }); + writer.Write(constant); + var result = tw.ToString(); + Assert.Contains("sendCollectionOfEnum", result); + Assert.Contains("enumObject:", result); + Assert.Contains(EnumName.ToFirstCharacterUpperCase(), result); + AssertExtensions.CurlyBracesAreClosed(result); + } + [Fact] + public void WritesRequestExecutorForPrimitive() + { + method.Kind = CodeMethodKind.RequestExecutor; + method.HttpMethod = HttpMethod.Get; + method.ReturnType = new CodeType + { + Name = "string", + }; + AddRequestBodyParameters(); + var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass); + var codeFile = parentClass.GetImmediateParentOfType().TryAddCodeFile("foo", constant); + codeFile.AddElements(new CodeConstant + { + Name = "UriTemplate", + Kind = CodeConstantKind.UriTemplate, + UriTemplate = "{baseurl+}/foo/bar" + }); + writer.Write(constant); + var result = tw.ToString(); + Assert.Contains("sendPrimitive", result); + AssertExtensions.CurlyBracesAreClosed(result); + } + [Fact] + public void WritesRequestExecutorForPrimitiveCollection() + { + method.Kind = CodeMethodKind.RequestExecutor; + method.HttpMethod = HttpMethod.Get; + method.ReturnType = new CodeType + { + Name = "string", + CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array, + }; + AddRequestBodyParameters(); + var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass); + var codeFile = parentClass.GetImmediateParentOfType().TryAddCodeFile("foo", constant); + codeFile.AddElements(new CodeConstant + { + Name = "UriTemplate", + Kind = CodeConstantKind.UriTemplate, + UriTemplate = "{baseurl+}/foo/bar" + }); + writer.Write(constant); + var result = tw.ToString(); + Assert.Contains("sendCollectionOfPrimitive", result); + AssertExtensions.CurlyBracesAreClosed(result); + } + [Fact] public void WritesRequestGeneratorBodyForScalar() { parentClass.Kind = CodeClassKind.RequestBuilder;