Skip to content

Commit

Permalink
feat: Add support for composed types in Typescript (#4602)
Browse files Browse the repository at this point in the history
* initial commit for composed types implementation for typescript

* use inline declaration of composed types instead of wrapper types

* serialize primitive union types

* finished adding support for composed types comprised of union of primitives

* use more meaningful naming & semantics

* refactor large methods

* unit test for TypescriptRefiner

* ad dopen api test file

* unit tests for Typescript refiner

* test coverage - cover all cases

* minor cleanup refactor for TypeScript/CodeFunctionWriter.cs

* access the conventions service from static context

* move the method to get factory name to the conventions service

* refactor composed type to use specific writers

* increase coverage

* increase code coverage

* handle union of objects in typescript

* remove unused code

* add serialization for union objects

* fix failing test and improve coverage

* add pets union test yml file

* remove unused parameter

* retrieve serialization method correctly

* rectify the serilaization function for CodeUnion types

* **allow primitive intersection types as per existing yml test file

* typescript: add support for intersection type objects

* fix primitive intersection bug

* refactor the factory method for primitive composed type values

* removed unused import statement

* inline composed types

* remove unused param

* fix sonar warnings

* format code

* address pr comments

* adress pr comments

* address pr comments

* address pr comments

* address pr coments

* format code

* address pr comments

* increase test coverage

* increase test coverage

* address pr comments

* address pr comments

* address pr comments

* address pr comments

* remove reference to external urls on tests

* simplify open api sample test files

* address pr comments

* address pr comments, format code

* address pr comments

* fix failing test

* address pr comments

* format code

* apply pr suggestion to other places for consistency

* enable supressed typescript tests

* allow generation to constinue when the discriminator property is missing

* fix failing it tests

* fix it test compilation error 'parseNode' is possibly 'undefined' for apisguru::twitter.com:current

* fix failing test

* fix failing it test

* ignore null default values

* ignore 'null' default values

* omit null from default values while serializing

* exclude apisguru::stripe.com integration test from running since it contains intersection type between objects and primitive values which isn't supported in typescript

* address pr comments

* format code

* fix sonar warnings

* search for factory function using namespace only

* use a more meaningful method name

* appply pr suggestion

* refactor code remove new enum values

* format code

* remove unused parameters

* fix compilation issue

* remove unused deserializer

* simplify statement

* apply pr review suggestions

* remove unused parameter

* use | symbol for code intersection in typescript

* move composed-type utility method to extensions

* remove CodeComposedTypeBase extension class

* use Nullish Coalescing Operator ?? over logical or ||

* handle edge cases for composed types - Union of objects and primitive types without discriminator property

* format code

* remove unused code in CodeMethod.cs

* delete unused method

* import all associated deserializers for composed types

* remove collection symbol for composed type param in serailizer and deserializer functions

* handle endge cases where composed type is a mix of objects and/or array of objects and/or primitive values

* fix casing

* add serialization functions to obsolete class definitions to prevent typescript refiner from throwing exceptions

* use correct serialization method for composed type collection

* improve code coverage

* Refactor composed types

* fix: null check on composed types

* fix: type casting

* Fix serialization

* Fix serialization with differenterent types

* Fixes import collition for types

* Fix import types

* Fix import

* Fix unit tests

* minor refactr

* Find correct function

* fix: formatting

* fix: unit tests

* fix: tests

* format code

* rafactor: reduce code complexity

* fix: String as value

* feat: add unit tests

* code cleanup

* sonar cleanup

* fix: compilation errors from main branch

* Update src/Kiota.Builder/CodeDOM/CodeComposedTypeBase.cs

* fix: typo

---------

Co-authored-by: rkodev <[email protected]>
Co-authored-by: Vincent Biret <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2024
1 parent f0d4739 commit aef4574
Show file tree
Hide file tree
Showing 25 changed files with 2,030 additions and 268 deletions.
26 changes: 1 addition & 25 deletions it/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
}
],
"Suppressions": [
{
"Language": "typescript",
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
},
{
"Language": "ruby",
"Rationale": "https://github.com/microsoft/kiota/issues/1816"
Expand Down Expand Up @@ -156,10 +152,6 @@
"Language": "go",
"Rationale": "https://github.com/microsoft/kiota/issues/3436"
},
{
"Language": "typescript",
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
},
{
"Language": "ruby",
"Rationale": "https://github.com/microsoft/kiota/issues/2484"
Expand All @@ -170,10 +162,6 @@
}
],
"IdempotencySuppressions": [
{
"Language": "typescript",
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
}
]
},
"apisguru::twilio.com:api": {
Expand Down Expand Up @@ -248,7 +236,7 @@
"Suppressions": [
{
"Language": "typescript",
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
"Rationale": "https://github.com/microsoft/kiota/issues/5256"
},
{
"Language": "go",
Expand Down Expand Up @@ -280,10 +268,6 @@
"Language": "python",
"Rationale": "https://github.com/microsoft/kiota/issues/2842"
},
{
"Language": "typescript",
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
},
{
"Language": "ruby",
"Rationale": "https://github.com/microsoft/kiota/issues/1816"
Expand Down Expand Up @@ -312,20 +296,12 @@
},
"apisguru::twitter.com:current": {
"Suppressions": [
{
"Language": "typescript",
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
},
{
"Language": "ruby",
"Rationale": "https://github.com/microsoft/kiota/issues/1816"
}
],
"IdempotencySuppressions": [
{
"Language": "typescript",
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
}
]
},
"apisguru::apis.guru": {
Expand Down
17 changes: 17 additions & 0 deletions src/Kiota.Builder/CodeDOM/CodeComposedTypeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using static Kiota.Builder.Writers.TypeScript.TypeScriptConventionService;

namespace Kiota.Builder.CodeDOM;
/// <summary>
Expand All @@ -25,6 +26,13 @@ public bool ContainsType(CodeType codeType)
ArgumentNullException.ThrowIfNull(codeType);
return types.ContainsKey(NormalizeKey(codeType));
}
public void SetTypes(params CodeType[] codeTypes)
{
ArgumentNullException.ThrowIfNull(codeTypes);
types.Clear();
foreach (var codeType in codeTypes)
AddType(codeType);
}
private readonly ConcurrentDictionary<string, CodeType> types = new(StringComparer.OrdinalIgnoreCase);
public IEnumerable<CodeType> Types
{
Expand Down Expand Up @@ -66,9 +74,18 @@ public CodeNamespace? TargetNamespace
{
get; set;
}

public DeprecationInformation? Deprecation
{
get;
set;
}

public bool IsComposedOfPrimitives(Func<CodeType, CodeComposedTypeBase, bool> checkIfPrimitive) => Types.All(x => checkIfPrimitive(x, this));
public bool IsComposedOfObjectsAndPrimitives(Func<CodeType, CodeComposedTypeBase, bool> checkIfPrimitive)
{
// Count the number of primitives in Types
return Types.Any(x => checkIfPrimitive(x, this)) && Types.Any(x => !checkIfPrimitive(x, this));
}

}
4 changes: 3 additions & 1 deletion src/Kiota.Builder/CodeDOM/CodeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public IEnumerable<T> AddElements<T>(params T[] elements) where T : CodeElement
public IEnumerable<CodeUsing> AllUsingsFromChildElements => GetChildElements(true)
.SelectMany(static x => x.GetChildElements(false))
.OfType<ProprietableBlockDeclaration>()
.SelectMany(static x => x.Usings);
.SelectMany(static x => x.Usings)
.Union(GetChildElements(true).Where(static x => x is CodeConstant).Cast<CodeConstant>()
.SelectMany(static x => x.StartBlock.Usings));
}
public class CodeFileDeclaration : ProprietableBlockDeclaration
{
Expand Down
Loading

0 comments on commit aef4574

Please sign in to comment.