Skip to content

Commit aef4574

Browse files
korosrkodevbaywet
authored
feat: Add support for composed types in Typescript (#4602)
* 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]>
1 parent f0d4739 commit aef4574

25 files changed

+2030
-268
lines changed

it/config.json

+1-25
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@
4343
}
4444
],
4545
"Suppressions": [
46-
{
47-
"Language": "typescript",
48-
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
49-
},
5046
{
5147
"Language": "ruby",
5248
"Rationale": "https://github.com/microsoft/kiota/issues/1816"
@@ -156,10 +152,6 @@
156152
"Language": "go",
157153
"Rationale": "https://github.com/microsoft/kiota/issues/3436"
158154
},
159-
{
160-
"Language": "typescript",
161-
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
162-
},
163155
{
164156
"Language": "ruby",
165157
"Rationale": "https://github.com/microsoft/kiota/issues/2484"
@@ -170,10 +162,6 @@
170162
}
171163
],
172164
"IdempotencySuppressions": [
173-
{
174-
"Language": "typescript",
175-
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
176-
}
177165
]
178166
},
179167
"apisguru::twilio.com:api": {
@@ -248,7 +236,7 @@
248236
"Suppressions": [
249237
{
250238
"Language": "typescript",
251-
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
239+
"Rationale": "https://github.com/microsoft/kiota/issues/5256"
252240
},
253241
{
254242
"Language": "go",
@@ -280,10 +268,6 @@
280268
"Language": "python",
281269
"Rationale": "https://github.com/microsoft/kiota/issues/2842"
282270
},
283-
{
284-
"Language": "typescript",
285-
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
286-
},
287271
{
288272
"Language": "ruby",
289273
"Rationale": "https://github.com/microsoft/kiota/issues/1816"
@@ -312,20 +296,12 @@
312296
},
313297
"apisguru::twitter.com:current": {
314298
"Suppressions": [
315-
{
316-
"Language": "typescript",
317-
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
318-
},
319299
{
320300
"Language": "ruby",
321301
"Rationale": "https://github.com/microsoft/kiota/issues/1816"
322302
}
323303
],
324304
"IdempotencySuppressions": [
325-
{
326-
"Language": "typescript",
327-
"Rationale": "https://github.com/microsoft/kiota/issues/1812"
328-
}
329305
]
330306
},
331307
"apisguru::apis.guru": {

src/Kiota.Builder/CodeDOM/CodeComposedTypeBase.cs

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using static Kiota.Builder.Writers.TypeScript.TypeScriptConventionService;
56

67
namespace Kiota.Builder.CodeDOM;
78
/// <summary>
@@ -25,6 +26,13 @@ public bool ContainsType(CodeType codeType)
2526
ArgumentNullException.ThrowIfNull(codeType);
2627
return types.ContainsKey(NormalizeKey(codeType));
2728
}
29+
public void SetTypes(params CodeType[] codeTypes)
30+
{
31+
ArgumentNullException.ThrowIfNull(codeTypes);
32+
types.Clear();
33+
foreach (var codeType in codeTypes)
34+
AddType(codeType);
35+
}
2836
private readonly ConcurrentDictionary<string, CodeType> types = new(StringComparer.OrdinalIgnoreCase);
2937
public IEnumerable<CodeType> Types
3038
{
@@ -66,9 +74,18 @@ public CodeNamespace? TargetNamespace
6674
{
6775
get; set;
6876
}
77+
6978
public DeprecationInformation? Deprecation
7079
{
7180
get;
7281
set;
7382
}
83+
84+
public bool IsComposedOfPrimitives(Func<CodeType, CodeComposedTypeBase, bool> checkIfPrimitive) => Types.All(x => checkIfPrimitive(x, this));
85+
public bool IsComposedOfObjectsAndPrimitives(Func<CodeType, CodeComposedTypeBase, bool> checkIfPrimitive)
86+
{
87+
// Count the number of primitives in Types
88+
return Types.Any(x => checkIfPrimitive(x, this)) && Types.Any(x => !checkIfPrimitive(x, this));
89+
}
90+
7491
}

src/Kiota.Builder/CodeDOM/CodeFile.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public IEnumerable<T> AddElements<T>(params T[] elements) where T : CodeElement
2424
public IEnumerable<CodeUsing> AllUsingsFromChildElements => GetChildElements(true)
2525
.SelectMany(static x => x.GetChildElements(false))
2626
.OfType<ProprietableBlockDeclaration>()
27-
.SelectMany(static x => x.Usings);
27+
.SelectMany(static x => x.Usings)
28+
.Union(GetChildElements(true).Where(static x => x is CodeConstant).Cast<CodeConstant>()
29+
.SelectMany(static x => x.StartBlock.Usings));
2830
}
2931
public class CodeFileDeclaration : ProprietableBlockDeclaration
3032
{

0 commit comments

Comments
 (0)