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

Restore backing store feature for typescript #3543

Merged
merged 3 commits into from
Oct 23, 2023
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 @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix bug where import statements in typescript wasn't using import type notation for types that are erased at runtime. [#3190](https://github.com/microsoft/kiota/issues/3190)
- The structured content type generation parameter now supports prioritization with `q=value` syntax. [#3377](https://github.com/microsoft/kiota/issues/3377)
- Fixed bug where `Tilde` char convert to Enum member name properly in C#. [#3500](https://github.com/microsoft/kiota/issues/3500)
- Restore backing store feature for typescript. [#2613](https://github.com/microsoft/kiota/issues/2613)

## [1.7.0] - 2023-10-05

Expand Down
8 changes: 6 additions & 2 deletions src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Kiota.Builder.Refiners;
public class TypeScriptRefiner : CommonLanguageRefiner, ILanguageRefiner
{
public static readonly string BackingStoreEnabledKey = "backingStoreEnabled";
public TypeScriptRefiner(GenerationConfiguration configuration) : base(configuration) { }
public override Task Refine(CodeNamespace generatedCode, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -66,7 +67,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
CodePropertyKind.AdditionalData,
},
static (_, s) => s.ToCamelCase(UnderscoreArray),
_configuration.UsesBackingStore,
false,
false,
string.Empty,
string.Empty);
Expand Down Expand Up @@ -393,7 +394,10 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
if (currentProperty.IsOfKind(CodePropertyKind.RequestAdapter))
currentProperty.Type.Name = "RequestAdapter";
else if (currentProperty.IsOfKind(CodePropertyKind.BackingStore))
currentProperty.Type.Name = currentProperty.Type.Name[1..]; // removing the "I"
{
currentProperty.Type.Name = "boolean";
currentProperty.Name = BackingStoreEnabledKey;
}
else if (currentProperty.IsOfKind(CodePropertyKind.Options))
currentProperty.Type.Name = "RequestOption[]";
else if (currentProperty.IsOfKind(CodePropertyKind.Headers))
Expand Down
8 changes: 6 additions & 2 deletions src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;
using static Kiota.Builder.Refiners.TypeScriptRefiner;

namespace Kiota.Builder.Writers.TypeScript;

Expand Down Expand Up @@ -184,7 +185,7 @@ private void WriteDeserializerFunction(CodeFunction codeFunction, LanguageWriter
{
if (codeFunction.OriginalLocalMethod.Parameters.FirstOrDefault() is CodeParameter param && param.Type is CodeType codeType && codeType.TypeDefinition is CodeInterface codeInterface)
{
var properties = codeInterface.Properties.Where(static x => x.Kind == CodePropertyKind.Custom && !x.ExistsInBaseType);
var properties = codeInterface.Properties.Where(static x => x.IsOfKind(CodePropertyKind.Custom, CodePropertyKind.BackingStore) && !x.ExistsInBaseType);

writer.StartBlock("return {");
if (codeInterface.StartBlock.Implements.FirstOrDefault(static x => x.TypeDefinition is CodeInterface) is CodeType type && type.TypeDefinition is CodeInterface inherits)
Expand All @@ -206,7 +207,10 @@ private void WriteDeserializerFunction(CodeFunction codeFunction, LanguageWriter
{
var keyName = !string.IsNullOrWhiteSpace(otherProp.SerializationName) ? otherProp.SerializationName.ToFirstCharacterLowerCase() : otherProp.Name.ToFirstCharacterLowerCase();
var suffix = otherProp.Name.Equals(primaryErrorMappingKey, StringComparison.Ordinal) ? primaryErrorMapping : string.Empty;
writer.WriteLine($"\"{keyName}\": n => {{ {param.Name.ToFirstCharacterLowerCase()}.{otherProp.Name.ToFirstCharacterLowerCase()} = n.{GetDeserializationMethodName(otherProp.Type, codeFunction)};{suffix} }},");
if (keyName.Equals(BackingStoreEnabledKey, StringComparison.Ordinal))
writer.WriteLine($"\"{keyName}\": n => {{ {param.Name.ToFirstCharacterLowerCase()}.{otherProp.Name.ToFirstCharacterLowerCase()} = true;{suffix} }},");
else
writer.WriteLine($"\"{keyName}\": n => {{ {param.Name.ToFirstCharacterLowerCase()}.{otherProp.Name.ToFirstCharacterLowerCase()} = n.{GetDeserializationMethodName(otherProp.Type, codeFunction)};{suffix} }},");
}

writer.CloseBlock();
Expand Down