-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathCodeMethodWriter.cs
657 lines (639 loc) · 43.6 KB
/
CodeMethodWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
using System;
using System.Collections.Generic;
using System.Linq;
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;
namespace Kiota.Builder.Writers.CSharp;
public class CodeMethodWriter : BaseElementWriter<CodeMethod, CSharpConventionService>
{
/// <summary>
/// Flag to avoid pushing a breaking change, remove it and remove the condition that depends on it with V2
/// </summary>
private readonly bool IsRequestConfigurationGeneric;
public CodeMethodWriter(CSharpConventionService conventionService, bool isRequestConfigurationGeneric = false) : base(conventionService)
{
IsRequestConfigurationGeneric = isRequestConfigurationGeneric;
}
public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter writer)
{
ArgumentNullException.ThrowIfNull(codeElement);
if (codeElement.ReturnType == null) throw new InvalidOperationException($"{nameof(codeElement.ReturnType)} should not be null");
ArgumentNullException.ThrowIfNull(writer);
if (codeElement.Parent is not CodeClass parentClass) throw new InvalidOperationException("the parent of a method should be a class");
var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement);
var inherits = parentClass.StartBlock.Inherits != null && !parentClass.IsErrorDefinition;
var isVoid = conventions.VoidTypeName.Equals(returnType, StringComparison.OrdinalIgnoreCase);
WriteMethodDocumentation(codeElement, writer);
WriteMethodPrototype(codeElement, parentClass, writer, returnType, inherits, isVoid);
writer.IncreaseIndent();
foreach (var parameter in codeElement.Parameters.Where(static x => !x.Optional && !x.IsOfKind(CodeParameterKind.RequestAdapter, CodeParameterKind.PathParameters, CodeParameterKind.RawUrl)).OrderBy(static x => x.Name, StringComparer.OrdinalIgnoreCase))
{
var parameterName = parameter.Name.ToFirstCharacterLowerCase();
if (nameof(String).Equals(parameter.Type.Name, StringComparison.OrdinalIgnoreCase) && parameter.Type.CollectionKind == CodeTypeBase.CodeTypeCollectionKind.None)
writer.WriteLine($"if(string.IsNullOrEmpty({parameterName})) throw new ArgumentNullException(nameof({parameterName}));");
else
writer.WriteLine($"_ = {parameterName} ?? throw new ArgumentNullException(nameof({parameterName}));");
}
HandleMethodKind(codeElement, writer, inherits, parentClass, isVoid);
writer.CloseBlock();
}
protected virtual void HandleMethodKind(CodeMethod codeElement, LanguageWriter writer, bool doesInherit, CodeClass parentClass, bool isVoid)
{
ArgumentNullException.ThrowIfNull(codeElement);
ArgumentNullException.ThrowIfNull(writer);
ArgumentNullException.ThrowIfNull(parentClass);
var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement);
var returnTypeWithoutCollectionInformation = conventions.GetTypeString(codeElement.ReturnType, codeElement, false);
var requestBodyParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestBody);
var requestConfig = codeElement.Parameters.OfKind(CodeParameterKind.RequestConfiguration);
var requestParams = new RequestParams(requestBodyParam, requestConfig);
switch (codeElement.Kind)
{
case CodeMethodKind.Serializer:
WriteSerializerBody(doesInherit, codeElement, parentClass, writer);
break;
case CodeMethodKind.RequestGenerator:
WriteRequestGeneratorBody(codeElement, requestParams, parentClass, writer);
break;
case CodeMethodKind.RequestExecutor:
WriteRequestExecutorBody(codeElement, requestParams, parentClass, isVoid, returnTypeWithoutCollectionInformation, writer);
break;
case CodeMethodKind.Deserializer:
WriteDeserializerBody(doesInherit, codeElement, parentClass, writer);
break;
case CodeMethodKind.ClientConstructor:
WriteConstructorBody(parentClass, codeElement, writer);
WriteApiConstructorBody(parentClass, codeElement, writer);
break;
case CodeMethodKind.RawUrlBuilder:
WriteRawUrlBuilderBody(parentClass, codeElement, writer);
break;
case CodeMethodKind.Constructor:
case CodeMethodKind.RawUrlConstructor:
WriteConstructorBody(parentClass, codeElement, writer);
break;
case CodeMethodKind.RequestBuilderWithParameters:
WriteRequestBuilderBody(parentClass, codeElement, writer);
break;
case CodeMethodKind.Getter:
case CodeMethodKind.Setter:
throw new InvalidOperationException("getters and setters are automatically added on fields in dotnet");
case CodeMethodKind.RequestBuilderBackwardCompatibility:
throw new InvalidOperationException("RequestBuilderBackwardCompatibility is not supported as the request builders are implemented by properties.");
case CodeMethodKind.CommandBuilder:
var origParams = codeElement.OriginalMethod?.Parameters ?? codeElement.Parameters;
requestBodyParam = origParams.OfKind(CodeParameterKind.RequestBody);
requestParams = new RequestParams(requestBodyParam, null);
WriteCommandBuilderBody(codeElement, parentClass, requestParams, isVoid, returnType, writer);
break;
case CodeMethodKind.Factory:
WriteFactoryMethodBody(codeElement, parentClass, writer);
break;
case CodeMethodKind.ComposedTypeMarker:
throw new InvalidOperationException("ComposedTypeMarker is not required as interface is explicitly implemented.");
default:
writer.WriteLine("return null;");
break;
}
}
private void WriteRawUrlBuilderBody(CodeClass parentClass, CodeMethod codeElement, LanguageWriter writer)
{
var rawUrlParameter = codeElement.Parameters.OfKind(CodeParameterKind.RawUrl) ?? throw new InvalidOperationException("RawUrlBuilder method should have a RawUrl parameter");
var requestAdapterProperty = parentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) ?? throw new InvalidOperationException("RawUrlBuilder method should have a RequestAdapter property");
writer.WriteLine($"return new {parentClass.Name.ToFirstCharacterUpperCase()}({rawUrlParameter.Name.ToFirstCharacterLowerCase()}, {requestAdapterProperty.Name.ToFirstCharacterUpperCase()});");
}
private static readonly CodePropertyTypeComparer CodePropertyTypeForwardComparer = new();
private static readonly CodePropertyTypeComparer CodePropertyTypeBackwardComparer = new(true);
private void WriteFactoryMethodBodyForInheritedModel(CodeMethod codeElement, CodeClass parentClass, LanguageWriter writer)
{
writer.StartBlock($"return {DiscriminatorMappingVarName} switch {{");
foreach (var mappedType in parentClass.DiscriminatorInformation.DiscriminatorMappings)
{
writer.WriteLine($"\"{mappedType.Key}\" => new {conventions.GetTypeString(mappedType.Value.AllTypes.First(), codeElement)}(),");
}
writer.WriteLine($"_ => new {parentClass.Name.ToFirstCharacterUpperCase()}(),");
writer.CloseBlock("};");
}
private const string ResultVarName = "result";
private void WriteFactoryMethodBodyForUnionModel(CodeMethod codeElement, CodeClass parentClass, CodeParameter parseNodeParameter, LanguageWriter writer)
{
writer.WriteLine($"var {ResultVarName} = new {parentClass.Name.ToFirstCharacterUpperCase()}();");
var includeElse = false;
foreach (var property in parentClass.GetPropertiesOfKind(CodePropertyKind.Custom)
.OrderBy(static x => x, CodePropertyTypeForwardComparer)
.ThenBy(static x => x.Name))
{
if (property.Type is CodeType propertyType)
if (propertyType.TypeDefinition is CodeClass && !propertyType.IsCollection)
{
var mappedType = parentClass.DiscriminatorInformation.DiscriminatorMappings.FirstOrDefault(x => x.Value.Name.Equals(propertyType.Name, StringComparison.OrdinalIgnoreCase));
writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if(\"{mappedType.Key}\".Equals({DiscriminatorMappingVarName}, StringComparison.OrdinalIgnoreCase)) {{");
writer.IncreaseIndent();
writer.WriteLine($"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()} = new {conventions.GetTypeString(propertyType, codeElement)}();");
writer.CloseBlock();
}
else if (propertyType.TypeDefinition is CodeClass && propertyType.IsCollection || propertyType.TypeDefinition is null || propertyType.TypeDefinition is CodeEnum)
{
var typeName = conventions.GetTypeString(propertyType, codeElement, true, false);
var valueVarName = $"{property.Name.ToFirstCharacterLowerCase()}Value";
writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({parseNodeParameter.Name.ToFirstCharacterLowerCase()}.{GetDeserializationMethodName(propertyType, codeElement)} is {typeName} {valueVarName}) {{");
writer.IncreaseIndent();
writer.WriteLine($"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()} = {valueVarName};");
writer.CloseBlock();
}
if (!includeElse)
includeElse = true;
}
writer.WriteLine($"return {ResultVarName};");
}
private void WriteFactoryMethodBodyForIntersectionModel(CodeMethod codeElement, CodeClass parentClass, CodeParameter parseNodeParameter, LanguageWriter writer)
{
writer.WriteLine($"var {ResultVarName} = new {parentClass.Name.ToFirstCharacterUpperCase()}();");
var includeElse = false;
foreach (var property in parentClass.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => x.Type is not CodeType propertyType || propertyType.IsCollection || propertyType.TypeDefinition is not CodeClass)
.OrderBy(static x => x, CodePropertyTypeBackwardComparer)
.ThenBy(static x => x.Name))
{
if (property.Type is CodeType propertyType)
{
var typeName = conventions.GetTypeString(propertyType, codeElement, true, false);
var valueVarName = $"{property.Name.ToFirstCharacterLowerCase()}Value";
writer.StartBlock($"{(includeElse ? "else " : string.Empty)}if({parseNodeParameter.Name.ToFirstCharacterLowerCase()}.{GetDeserializationMethodName(propertyType, codeElement)} is {typeName} {valueVarName}) {{");
writer.WriteLine($"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()} = {valueVarName};");
writer.CloseBlock();
}
if (!includeElse)
includeElse = true;
}
var complexProperties = parentClass.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => x.Type is CodeType xType && xType.TypeDefinition is CodeClass && !xType.IsCollection)
.Select(static x => new Tuple<CodeProperty, CodeType>(x, (CodeType)x.Type))
.ToArray();
if (complexProperties.Any())
{
if (includeElse)
{
writer.WriteLine("else {");
writer.IncreaseIndent();
}
foreach (var property in complexProperties)
writer.WriteLine($"{ResultVarName}.{property.Item1.Name.ToFirstCharacterUpperCase()} = new {conventions.GetTypeString(property.Item2, codeElement)}();");
if (includeElse)
{
writer.CloseBlock();
}
}
writer.WriteLine($"return {ResultVarName};");
}
private const string DiscriminatorMappingVarName = "mappingValue";
private void WriteFactoryMethodBody(CodeMethod codeElement, CodeClass parentClass, LanguageWriter writer)
{
var parseNodeParameter = codeElement.Parameters.OfKind(CodeParameterKind.ParseNode) ?? throw new InvalidOperationException("Factory method should have a ParseNode parameter");
if (parentClass.DiscriminatorInformation.ShouldWriteParseNodeCheck && !parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType)
writer.WriteLine($"var {DiscriminatorMappingVarName} = {parseNodeParameter.Name.ToFirstCharacterLowerCase()}.GetChildNode(\"{parentClass.DiscriminatorInformation.DiscriminatorPropertyName}\")?.GetStringValue();");
if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForInheritedType)
WriteFactoryMethodBodyForInheritedModel(codeElement, parentClass, writer);
else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForUnionType)
WriteFactoryMethodBodyForUnionModel(codeElement, parentClass, parseNodeParameter, writer);
else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType)
WriteFactoryMethodBodyForIntersectionModel(codeElement, parentClass, parseNodeParameter, writer);
else
writer.WriteLine($"return new {parentClass.Name.ToFirstCharacterUpperCase()}();");
}
private void WriteRequestBuilderBody(CodeClass parentClass, CodeMethod codeElement, LanguageWriter writer)
{
var importSymbol = conventions.GetTypeString(codeElement.ReturnType, parentClass);
conventions.AddRequestBuilderBody(parentClass, importSymbol, writer, prefix: "return ", pathParameters: codeElement.Parameters.Where(static x => x.IsOfKind(CodeParameterKind.Path)));
}
private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod method, LanguageWriter writer)
{
if (parentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) is not CodeProperty requestAdapterProperty) return;
var pathParametersProperty = parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters);
var backingStoreParameter = method.Parameters.OfKind(CodeParameterKind.BackingStore);
var requestAdapterPropertyName = requestAdapterProperty.Name.ToFirstCharacterUpperCase();
WriteSerializationRegistration(method.SerializerModules, writer, "RegisterDefaultSerializer");
WriteSerializationRegistration(method.DeserializerModules, writer, "RegisterDefaultDeserializer");
if (!string.IsNullOrEmpty(method.BaseUrl))
{
writer.StartBlock($"if (string.IsNullOrEmpty({requestAdapterPropertyName}.BaseUrl)) {{");
writer.WriteLine($"{requestAdapterPropertyName}.BaseUrl = \"{method.BaseUrl}\";");
writer.CloseBlock();
if (pathParametersProperty != null)
writer.WriteLine($"{pathParametersProperty.Name.ToFirstCharacterUpperCase()}.TryAdd(\"baseurl\", {requestAdapterPropertyName}.BaseUrl);");
}
if (backingStoreParameter != null)
writer.WriteLine($"{requestAdapterPropertyName}.EnableBackingStore({backingStoreParameter.Name});");
}
private static void WriteSerializationRegistration(HashSet<string> serializationClassNames, LanguageWriter writer, string methodName)
{
if (serializationClassNames != null)
foreach (var serializationClassName in serializationClassNames)
writer.WriteLine($"ApiClientBuilder.{methodName}<{serializationClassName}>();");
}
private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMethod, LanguageWriter writer)
{
foreach (var propWithDefault in parentClass
.Properties
.Where(static x => !string.IsNullOrEmpty(x.DefaultValue) && !x.IsOfKind(CodePropertyKind.UrlTemplate, CodePropertyKind.PathParameters))
// do not apply the default value if the type is composed as the default value may not necessarily which type to use
.Where(static x => x.Type is not CodeType propType || propType.TypeDefinition is not CodeClass propertyClass || propertyClass.OriginalComposedType is null)
.OrderByDescending(static x => x.Kind)
.ThenBy(static x => x.Name))
{
var defaultValue = propWithDefault.DefaultValue;
if (propWithDefault.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition)
{
defaultValue = $"{enumDefinition.Name.ToFirstCharacterUpperCase()}.{defaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}";
}
writer.WriteLine($"{propWithDefault.Name.ToFirstCharacterUpperCase()} = {defaultValue};");
}
if (parentClass.IsOfKind(CodeClassKind.RequestBuilder) &&
parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProp &&
currentMethod.IsOfKind(CodeMethodKind.Constructor) &&
currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParam)
{
var pathParameters = currentMethod.Parameters.Where(static x => x.IsOfKind(CodeParameterKind.Path));
if (pathParameters.Any())
conventions.AddParametersAssignment(writer,
pathParametersParam.Type,
pathParametersParam.Name.ToFirstCharacterLowerCase(),
pathParametersProp.Name.ToFirstCharacterUpperCase(),
currentMethod.Parameters
.Where(static x => x.IsOfKind(CodeParameterKind.Path))
.Select(static x => (x.Type, string.IsNullOrEmpty(x.SerializationName) ? x.Name : x.SerializationName, x.Name.ToFirstCharacterLowerCase()))
.ToArray());
}
}
private string DefaultDeserializerValue => $"new Dictionary<string, Action<{conventions.ParseNodeInterfaceName}>>";
private void WriteDeserializerBody(bool shouldHide, CodeMethod codeElement, CodeClass parentClass, LanguageWriter writer)
{
if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForUnionType)
WriteDeserializerBodyForUnionModel(codeElement, parentClass, writer);
else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType)
WriteDeserializerBodyForIntersectionModel(parentClass, writer);
else
WriteDeserializerBodyForInheritedModel(shouldHide, codeElement, parentClass, writer);
}
private void WriteDeserializerBodyForUnionModel(CodeMethod method, CodeClass parentClass, LanguageWriter writer)
{
var includeElse = false;
foreach (var otherPropName in parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => !x.ExistsInBaseType)
.Where(static x => x.Type is CodeType propertyType && !propertyType.IsCollection && propertyType.TypeDefinition is CodeClass)
.OrderBy(static x => x, CodePropertyTypeForwardComparer)
.ThenBy(static x => x.Name)
.Select(static x => x.Name.ToFirstCharacterUpperCase()))
{
writer.StartBlock($"{(includeElse ? "else " : string.Empty)}if({otherPropName} != null) {{");
writer.WriteLine($"return {otherPropName}.{method.Name.ToFirstCharacterUpperCase()}();");
writer.CloseBlock();
if (!includeElse)
includeElse = true;
}
writer.WriteLine($"return {DefaultDeserializerValue}();");
}
private void WriteDeserializerBodyForIntersectionModel(CodeClass parentClass, LanguageWriter writer)
{
var complexProperties = parentClass.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => x.Type is CodeType propType && propType.TypeDefinition is CodeClass && !x.Type.IsCollection)
.ToArray();
if (complexProperties.Any())
{
var propertiesNames = complexProperties
.Select(static x => x.Name.ToFirstCharacterUpperCase())
.OrderBy(static x => x)
.ToArray();
var propertiesNamesAsConditions = propertiesNames
.Select(static x => $"{x} != null")
.Aggregate(static (x, y) => $"{x} || {y}");
writer.StartBlock($"if({propertiesNamesAsConditions}) {{");
var propertiesNamesAsArgument = propertiesNames
.Aggregate(static (x, y) => $"{x}, {y}");
writer.WriteLine($"return ParseNodeHelper.MergeDeserializersForIntersectionWrapper({propertiesNamesAsArgument});");
writer.CloseBlock();
}
writer.WriteLine($"return {DefaultDeserializerValue}();");
}
private void WriteDeserializerBodyForInheritedModel(bool shouldHide, CodeMethod codeElement, CodeClass parentClass, LanguageWriter writer)
{
var parentSerializationInfo = shouldHide ? $"(base.{codeElement.Name.ToFirstCharacterUpperCase()}())" : string.Empty;
writer.StartBlock($"return {DefaultDeserializerValue}{parentSerializationInfo} {{");
foreach (var otherProp in parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => !x.ExistsInBaseType)
.OrderBy(static x => x.Name))
{
writer.WriteLine($"{{\"{otherProp.WireName}\", n => {{ {otherProp.Name.ToFirstCharacterUpperCase()} = n.{GetDeserializationMethodName(otherProp.Type, codeElement)}; }} }},");
}
writer.CloseBlock("};");
}
private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod method)
{
var isCollection = propType.CollectionKind != CodeTypeBase.CodeTypeCollectionKind.None;
var propertyType = conventions.GetTypeString(propType, method, false);
if (propType is CodeType currentType)
{
if (isCollection)
{
var collectionMethod = propType.IsArray ? "?.ToArray()" : "?.ToList()";
if (currentType.TypeDefinition == null)
return $"GetCollectionOfPrimitiveValues<{propertyType}>(){collectionMethod}";
else if (currentType.TypeDefinition is CodeEnum)
return $"GetCollectionOfEnumValues<{propertyType.TrimEnd('?')}>(){collectionMethod}";
else
return $"GetCollectionOfObjectValues<{propertyType}>({propertyType}.CreateFromDiscriminatorValue){collectionMethod}";
}
else if (currentType.TypeDefinition is CodeEnum enumType)
return $"GetEnumValue<{enumType.Name.ToFirstCharacterUpperCase()}>()";
}
return propertyType switch
{
"byte[]" => "GetByteArrayValue()",
_ when conventions.IsPrimitiveType(propertyType) => $"Get{propertyType.TrimEnd(CSharpConventionService.NullableMarker).ToFirstCharacterUpperCase()}Value()",
_ => $"GetObjectValue<{propertyType.ToFirstCharacterUpperCase()}>({propertyType}.CreateFromDiscriminatorValue)",
};
}
protected void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass parentClass, bool isVoid, string returnTypeWithoutCollectionInformation, LanguageWriter writer)
{
ArgumentNullException.ThrowIfNull(codeElement);
ArgumentNullException.ThrowIfNull(requestParams);
ArgumentNullException.ThrowIfNull(parentClass);
ArgumentNullException.ThrowIfNull(writer);
if (codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null");
var generatorMethodName = parentClass
.Methods
.FirstOrDefault(x => x.IsOfKind(CodeMethodKind.RequestGenerator) && x.HttpMethod == codeElement.HttpMethod)
?.Name;
var parametersList = new CodeParameter?[] { requestParams.requestBody, requestParams.requestConfiguration }
.Select(static x => x?.Name).Where(static x => x != null).Aggregate(static (x, y) => $"{x}, {y}");
writer.WriteLine($"var requestInfo = {generatorMethodName}({parametersList});");
var errorMappingVarName = "default";
if (codeElement.ErrorMappings.Any())
{
errorMappingVarName = "errorMapping";
writer.WriteLine($"var {errorMappingVarName} = new Dictionary<string, ParsableFactory<IParsable>> {{");
writer.IncreaseIndent();
foreach (var errorMapping in codeElement.ErrorMappings.Where(errorMapping => errorMapping.Value.AllTypes.FirstOrDefault()?.TypeDefinition is CodeClass))
{
writer.WriteLine($"{{\"{errorMapping.Key.ToUpperInvariant()}\", {conventions.GetTypeString(errorMapping.Value, codeElement, false)}.CreateFromDiscriminatorValue}},");
}
writer.CloseBlock("};");
}
var returnTypeCodeType = codeElement.ReturnType as CodeType;
var returnTypeFactory = returnTypeCodeType?.TypeDefinition is CodeClass returnTypeClass
? $", {returnTypeWithoutCollectionInformation}.CreateFromDiscriminatorValue"
: null;
var prefix = (isVoid, codeElement.ReturnType.IsCollection) switch
{
(true, _) => string.Empty,
(_, true) => "var collectionResult = ",
(_, _) => "return ",
};
writer.WriteLine($"{prefix}await RequestAdapter.{GetSendRequestMethodName(isVoid, codeElement, codeElement.ReturnType)}(requestInfo{returnTypeFactory}, {errorMappingVarName}, cancellationToken);");
if (codeElement.ReturnType.IsCollection)
writer.WriteLine("return collectionResult?.ToList();");
}
private const string RequestInfoVarName = "requestInfo";
private const string RequestConfigVarName = "requestConfig";
private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass currentClass, LanguageWriter writer)
{
if (codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null");
var operationName = codeElement.HttpMethod.ToString();
writer.StartBlock($"var {RequestInfoVarName} = new RequestInformation {{");
writer.WriteLine($"HttpMethod = Method.{operationName?.ToUpperInvariant()},");
if (currentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty urlTemplateParamsProperty &&
currentClass.GetPropertyOfKind(CodePropertyKind.UrlTemplate) is CodeProperty urlTemplateProperty)
writer.WriteLines($"UrlTemplate = {GetPropertyCall(urlTemplateProperty, "string.Empty")},",
$"PathParameters = {GetPropertyCall(urlTemplateParamsProperty, "string.Empty")},");
writer.CloseBlock("};");
if (codeElement.AcceptedResponseTypes.Any())
writer.WriteLine($"{RequestInfoVarName}.Headers.Add(\"Accept\", \"{string.Join(", ", codeElement.AcceptedResponseTypes)}\");");
if (requestParams.requestBody != null)
{
var suffix = requestParams.requestBody.Type.IsCollection ? "Collection" : string.Empty;
if (requestParams.requestBody.Type.Name.Equals(conventions.StreamTypeName, StringComparison.OrdinalIgnoreCase))
writer.WriteLine($"{RequestInfoVarName}.SetStreamContent({requestParams.requestBody.Name});");
else if (currentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) is CodeProperty requestAdapterProperty)
if (requestParams.requestBody.Type is CodeType bodyType && (bodyType.TypeDefinition is CodeClass || bodyType.Name.Equals("MultipartBody", StringComparison.OrdinalIgnoreCase)))
writer.WriteLine($"{RequestInfoVarName}.SetContentFromParsable({requestAdapterProperty.Name.ToFirstCharacterUpperCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});");
else
writer.WriteLine($"{RequestInfoVarName}.SetContentFromScalar{suffix}({requestAdapterProperty.Name.ToFirstCharacterUpperCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});");
}
if (requestParams.requestConfiguration != null)
{
writer.StartBlock($"if ({requestParams.requestConfiguration.Name} != null) {{");
var requestConfigType = conventions.GetTypeString(requestParams.requestConfiguration.Type, codeElement, false, true, false).ToFirstCharacterUpperCase();
writer.WriteLines($"var {RequestConfigVarName} = new {requestConfigType}();",
$"{requestParams.requestConfiguration.Name}.Invoke({RequestConfigVarName});");
var queryString = requestParams.QueryParameters;
if (queryString != null || IsRequestConfigurationGeneric)
writer.WriteLine($"{RequestInfoVarName}.AddQueryParameters({RequestConfigVarName}.QueryParameters);");
writer.WriteLines($"{RequestInfoVarName}.AddRequestOptions({RequestConfigVarName}.Options);",
$"{RequestInfoVarName}.AddHeaders({RequestConfigVarName}.Headers);");
writer.CloseBlock();
}
writer.WriteLine($"return {RequestInfoVarName};");
}
private static string GetPropertyCall(CodeProperty property, string defaultValue) => property == null ? defaultValue : $"{property.Name.ToFirstCharacterUpperCase()}";
private void WriteSerializerBody(bool shouldHide, CodeMethod method, CodeClass parentClass, LanguageWriter writer)
{
if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForUnionType)
WriteSerializerBodyForUnionModel(method, parentClass, writer);
else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType)
WriteSerializerBodyForIntersectionModel(method, parentClass, writer);
else
WriteSerializerBodyForInheritedModel(shouldHide, method, parentClass, writer);
if (parentClass.GetPropertyOfKind(CodePropertyKind.AdditionalData) is CodeProperty additionalDataProperty)
writer.WriteLine($"writer.WriteAdditionalData({additionalDataProperty.Name});");
}
private void WriteSerializerBodyForInheritedModel(bool shouldHide, CodeMethod method, CodeClass parentClass, LanguageWriter writer)
{
if (shouldHide)
writer.WriteLine("base.Serialize(writer);");
foreach (var otherProp in parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => !x.ExistsInBaseType && !x.ReadOnly)
.OrderBy(static x => x.Name))
{
var serializationMethodName = GetSerializationMethodName(otherProp.Type, method);
writer.WriteLine($"writer.{serializationMethodName}(\"{otherProp.WireName}\", {otherProp.Name.ToFirstCharacterUpperCase()});");
}
}
private void WriteSerializerBodyForUnionModel(CodeMethod method, CodeClass parentClass, LanguageWriter writer)
{
var includeElse = false;
foreach (var otherProp in parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => !x.ExistsInBaseType)
.OrderBy(static x => x, CodePropertyTypeForwardComparer)
.ThenBy(static x => x.Name))
{
writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({otherProp.Name.ToFirstCharacterUpperCase()} != null) {{");
writer.IncreaseIndent();
writer.WriteLine($"writer.{GetSerializationMethodName(otherProp.Type, method)}(null, {otherProp.Name.ToFirstCharacterUpperCase()});");
writer.CloseBlock();
if (!includeElse)
includeElse = true;
}
}
private void WriteSerializerBodyForIntersectionModel(CodeMethod method, CodeClass parentClass, LanguageWriter writer)
{
var includeElse = false;
foreach (var otherProp in parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => !x.ExistsInBaseType)
.Where(static x => x.Type is not CodeType propertyType || propertyType.IsCollection || propertyType.TypeDefinition is not CodeClass)
.OrderBy(static x => x, CodePropertyTypeBackwardComparer)
.ThenBy(static x => x.Name))
{
writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({otherProp.Name.ToFirstCharacterUpperCase()} != null) {{");
writer.IncreaseIndent();
writer.WriteLine($"writer.{GetSerializationMethodName(otherProp.Type, method)}(null, {otherProp.Name.ToFirstCharacterUpperCase()});");
writer.CloseBlock();
if (!includeElse)
includeElse = true;
}
var complexProperties = parentClass.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => x.Type is CodeType propType && propType.TypeDefinition is CodeClass && !x.Type.IsCollection)
.ToArray();
if (complexProperties.Any())
{
if (includeElse)
{
writer.WriteLine("else {");
writer.IncreaseIndent();
}
var propertiesNames = complexProperties
.Select(static x => x.Name.ToFirstCharacterUpperCase())
.OrderBy(static x => x)
.Aggregate(static (x, y) => $"{x}, {y}");
writer.WriteLine($"writer.{GetSerializationMethodName(complexProperties.First().Type, method)}(null, {propertiesNames});");
if (includeElse)
{
writer.CloseBlock();
}
}
}
protected virtual void WriteCommandBuilderBody(CodeMethod codeElement, CodeClass parentClass, RequestParams requestParams, bool isVoid, string returnType, LanguageWriter writer)
{
throw new InvalidOperationException("CommandBuilder methods are not implemented in this SDK. They're currently only supported in the shell language.");
}
protected string GetSendRequestMethodName(bool isVoid, CodeElement currentElement, CodeTypeBase returnType)
{
ArgumentNullException.ThrowIfNull(returnType);
var returnTypeName = conventions.GetTypeString(returnType, currentElement, false);
var isStream = conventions.StreamTypeName.Equals(returnTypeName, StringComparison.OrdinalIgnoreCase);
var isEnum = returnType is CodeType codeType && codeType.TypeDefinition is CodeEnum;
if (isVoid) return "SendNoContentAsync";
else if (isStream || conventions.IsPrimitiveType(returnTypeName) || isEnum)
if (returnType.IsCollection)
return $"SendPrimitiveCollectionAsync<{returnTypeName}>";
else
return $"SendPrimitiveAsync<{returnTypeName}>";
else if (returnType.IsCollection) return $"SendCollectionAsync<{returnTypeName}>";
else return $"SendAsync<{returnTypeName}>";
}
private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer)
{
conventions.WriteLongDescription(code.Documentation, writer);
foreach (var paramWithDescription in code.Parameters
.Where(static x => x.Documentation.DescriptionAvailable)
.OrderBy(static x => x.Name, StringComparer.OrdinalIgnoreCase))
writer.WriteLine($"{conventions.DocCommentPrefix}<param name=\"{paramWithDescription.Name.ToFirstCharacterLowerCase()}\">{paramWithDescription.Documentation.Description.CleanupXMLString()}</param>");
conventions.WriteDeprecationAttribute(code, writer);
}
private static readonly BaseCodeParameterOrderComparer parameterOrderComparer = new();
private static string GetBaseSuffix(bool isConstructor, bool inherits, CodeClass parentClass, CodeMethod currentMethod)
{
if (isConstructor && inherits)
{
if (parentClass.IsOfKind(CodeClassKind.RequestBuilder) && parentClass.Properties.FirstOrDefaultOfKind(CodePropertyKind.UrlTemplate) is CodeProperty urlTemplateProperty &&
!string.IsNullOrEmpty(urlTemplateProperty.DefaultValue))
{
var thirdParameterName = string.Empty;
if (currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParameter)
thirdParameterName = $", {pathParametersParameter.Name}";
else if (currentMethod.Parameters.OfKind(CodeParameterKind.RawUrl) is CodeParameter rawUrlParameter)
thirdParameterName = $", {rawUrlParameter.Name}";
else if (parentClass.Properties.FirstOrDefaultOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProperty && !string.IsNullOrEmpty(pathParametersProperty.DefaultValue))
thirdParameterName = $", {pathParametersProperty.DefaultValue}";
if (currentMethod.Parameters.OfKind(CodeParameterKind.RequestAdapter) is CodeParameter requestAdapterParameter)
{
return $" : base({requestAdapterParameter.Name.ToFirstCharacterLowerCase()}, {urlTemplateProperty.DefaultValue}{thirdParameterName})";
}
else if (parentClass.StartBlock?.Inherits?.Name?.Contains("CliRequestBuilder", StringComparison.Ordinal) == true)
{
// CLI uses a different base class.
return $" : base({urlTemplateProperty.DefaultValue}{thirdParameterName})";
}
}
return " : base()";
}
return string.Empty;
}
private void WriteMethodPrototype(CodeMethod code, CodeClass parentClass, LanguageWriter writer, string returnType, bool inherits, bool isVoid)
{
var staticModifier = code.IsStatic ? "static " : string.Empty;
var hideModifier = inherits && code.IsOfKind(CodeMethodKind.Serializer, CodeMethodKind.Deserializer, CodeMethodKind.Factory) ? "new " : string.Empty;
var genericTypePrefix = isVoid ? string.Empty : "<";
var genericTypeSuffix = code.IsAsync && !isVoid ? ">" : string.Empty;
var isConstructor = code.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor, CodeMethodKind.RawUrlConstructor);
var asyncPrefix = code.IsAsync ? "async Task" + genericTypePrefix : string.Empty;
var voidCorrectedTaskReturnType = code.IsAsync && isVoid ? string.Empty : returnType;
if (code.ReturnType.IsArray && code.IsOfKind(CodeMethodKind.RequestExecutor))
voidCorrectedTaskReturnType = $"IEnumerable<{voidCorrectedTaskReturnType.StripArraySuffix()}>";
// TODO: Task type should be moved into the refiner
var completeReturnType = isConstructor ?
string.Empty :
$"{asyncPrefix}{voidCorrectedTaskReturnType}{genericTypeSuffix} ";
var baseSuffix = GetBaseSuffix(isConstructor, inherits, parentClass, code);
var parameters = string.Join(", ", code.Parameters.OrderBy(x => x, parameterOrderComparer).Select(p => conventions.GetParameterSignature(p, code)).ToList());
var methodName = isConstructor ? parentClass.Name.ToFirstCharacterUpperCase() : code.Name.ToFirstCharacterUpperCase();
var includeNullableReferenceType = code.IsOfKind(CodeMethodKind.RequestExecutor, CodeMethodKind.RequestGenerator);
if (includeNullableReferenceType)
{
var completeReturnTypeWithNullable = isConstructor || string.IsNullOrEmpty(genericTypeSuffix) ? completeReturnType : $"{completeReturnType[..^2].TrimEnd('?')}?{genericTypeSuffix} ";
var nullableParameters = string.Join(", ", code.Parameters.Order(parameterOrderComparer)
.Select(p => p.IsOfKind(CodeParameterKind.RequestConfiguration) ?
GetParameterSignatureWithNullableRefType(p, code) :
conventions.GetParameterSignature(p, code))
.ToList());
CSharpConventionService.WriteNullableOpening(writer);
writer.WriteLine($"{conventions.GetAccessModifier(code.Access)} {staticModifier}{hideModifier}{completeReturnTypeWithNullable}{methodName}({nullableParameters}){baseSuffix} {{");
CSharpConventionService.WriteNullableMiddle(writer);
}
writer.WriteLine($"{conventions.GetAccessModifier(code.Access)} {staticModifier}{hideModifier}{completeReturnType}{methodName}({parameters}){baseSuffix} {{");
if (includeNullableReferenceType)
CSharpConventionService.WriteNullableClosing(writer);
}
private string GetParameterSignatureWithNullableRefType(CodeParameter parameter, CodeElement targetElement)
{
var signatureSegments = conventions.GetParameterSignature(parameter, targetElement).Split(" ", StringSplitOptions.RemoveEmptyEntries);
return $"{signatureSegments[0]}? {string.Join(" ", signatureSegments[1..])}";
}
private string GetSerializationMethodName(CodeTypeBase propType, CodeMethod method, bool includeNullableRef = false)
{
var isCollection = propType.CollectionKind != CodeTypeBase.CodeTypeCollectionKind.None;
var propertyType = conventions.GetTypeString(propType, method, false);
if (propType is CodeType currentType)
{
if (isCollection)
if (currentType.TypeDefinition == null)
return $"WriteCollectionOfPrimitiveValues<{propertyType}>";
else if (currentType.TypeDefinition is CodeEnum)
return $"WriteCollectionOfEnumValues<{propertyType.TrimEnd('?')}>";
else
return $"WriteCollectionOfObjectValues<{propertyType}>";
else if (currentType.TypeDefinition is CodeEnum enumType)
return $"WriteEnumValue<{enumType.Name.ToFirstCharacterUpperCase()}>";
}
return propertyType switch
{
"byte[]" => "WriteByteArrayValue",
_ when conventions.IsPrimitiveType(propertyType) => $"Write{propertyType.TrimEnd(CSharpConventionService.NullableMarker).ToFirstCharacterUpperCase()}Value",
_ => $"WriteObjectValue<{propertyType.ToFirstCharacterUpperCase()}{(includeNullableRef ? "?" : string.Empty)}>",
};
}
}