-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathOpenApiParameterGenerator.cs
More file actions
978 lines (854 loc) · 42.5 KB
/
OpenApiParameterGenerator.cs
File metadata and controls
978 lines (854 loc) · 42.5 KB
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using Microsoft.OData.Edm;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OData.Edm.Vocabularies;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
using System.Diagnostics;
using System;
using System.Text.Json.Nodes;
namespace Microsoft.OpenApi.OData.Generator
{
/// <summary>
/// Extension methods to create <see cref="OpenApiParameter"/> by Edm model.
/// </summary>
internal static class OpenApiParameterGenerator
{
/// <summary>
/// 4.6.2 Field parameters in components
/// Create a map of <see cref="OpenApiParameter"/> object.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="document">The Open API document.</param>
public static void AddParametersToDocument(this ODataContext context, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(document, nameof(document));
// It allows defining query options and headers that can be reused across operations of the service.
// The value of parameters is a map of Parameter Objects.
document.AddComponent("top", CreateTop(context.Settings.TopExample));
document.AddComponent("skip", CreateSkip());
document.AddComponent("count", CreateCount());
document.AddComponent("filter", CreateFilter());
document.AddComponent("search", CreateSearch());
}
/// <summary>
/// Create the list of <see cref="OpenApiParameter"/> for a <see cref="IEdmFunctionImport"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="functionImport">The Edm function import.</param>
/// <param name="document">The Open API document to lookup references.</param>
/// <returns>The created list of <see cref="OpenApiParameter"/>.</returns>
public static IList<IOpenApiParameter> CreateParameters(this ODataContext context, IEdmFunctionImport functionImport, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(functionImport, nameof(functionImport));
Utils.CheckArgumentNull(document, nameof(document));
return context.CreateParameters(functionImport.Function, document);
}
/// <summary>
/// Create the list of <see cref="OpenApiParameter"/> for a <see cref="IEdmFunction"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="function">The Edm function.</param>
/// <param name="document">The Open API document to lookup references.</param>
/// <param name="parameterNameMapping">The parameter name mapping.</param>
/// <returns>The created list of <see cref="OpenApiParameter"/>.</returns>
public static IList<IOpenApiParameter> CreateParameters(this ODataContext context, IEdmFunction function,
OpenApiDocument document,
IDictionary<string, string>? parameterNameMapping = null)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(function, nameof(function));
Utils.CheckArgumentNull(document, nameof(document));
var parameters = new List<IOpenApiParameter>();
int skip = function.IsBound ? 1 : 0;
foreach (IEdmOperationParameter edmParameter in function.Parameters.Skip(skip))
{
if (parameterNameMapping != null && !parameterNameMapping.ContainsKey(edmParameter.Name))
{
continue;
}
OpenApiParameter parameter;
bool isOptionalParameter = edmParameter is IEdmOptionalParameter;
if (edmParameter.Type.IsStructured() ||
edmParameter.Type.IsCollection())
{
parameter = new OpenApiParameter
{
Name = parameterNameMapping == null ? edmParameter.Name : parameterNameMapping[edmParameter.Name],
In = ParameterLocation.Path,
Required = true,
// Create schema in the Content property to indicate that the parameters are serialized as JSON
Content = new Dictionary<string, IOpenApiMediaType>
{
{
Constants.ApplicationJsonMediaType,
new OpenApiMediaType
{
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Array,
Items = new OpenApiSchema
{
Type = JsonSchemaType.String
},
// These Parameter Objects optionally can contain the field description,
// whose value is the value of the unqualified annotation Core.Description of the parameter.
Description = context.Model.GetDescriptionAnnotation(edmParameter)
}
}
}
},
// The parameter description describes the format this URL-encoded JSON object or array, and/or reference to [OData-URL].
Description = "The URL-encoded JSON " + (edmParameter.Type.IsStructured() ? "array" : "object")
};
}
else
{
// Primitive parameters use the same type mapping as described for primitive properties.
parameter = new OpenApiParameter
{
Name = parameterNameMapping == null ? edmParameter.Name : parameterNameMapping[edmParameter.Name],
In = isOptionalParameter ? ParameterLocation.Query : ParameterLocation.Path,
Required = !isOptionalParameter,
Schema = context.CreateEdmTypeSchemaForParameter(edmParameter.Type, document)
};
}
if (parameterNameMapping != null)
{
var quote = edmParameter.Type.Definition.ShouldPathParameterBeQuoted(context.Settings) ? "'" : string.Empty;
parameter.Description = isOptionalParameter
? $"Usage: {edmParameter.Name}={quote}@{parameterNameMapping[edmParameter.Name]}{quote}"
: $"Usage: {edmParameter.Name}={quote}{{{parameterNameMapping[edmParameter.Name]}}}{quote}";
}
parameters.Add(parameter);
}
return parameters;
}
/// <summary>
/// Create key parameters for the <see cref="ODataKeySegment"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="keySegment">The key segment.</param>
/// <param name="document">The Open API document to lookup references.</param>
/// <param name="parameterNameMapping">The parameter name mapping.</param>
/// <returns>The created list of <see cref="OpenApiParameter"/>.</returns>
public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext context, ODataKeySegment keySegment,
OpenApiDocument document,
IDictionary<string, string>? parameterNameMapping = null)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(keySegment, nameof(keySegment));
Utils.CheckArgumentNull(document, nameof(document));
if (keySegment.IsAlternateKey)
return CreateAlternateKeyParameters(context, keySegment, document);
IEdmEntityType entityType = keySegment.EntityType;
IList<IEdmStructuralProperty> keys = entityType.Key().ToList();
List<OpenApiParameter> parameters = new();
if (keys.Count() == 1)
{
string keyName = keys.First().Name;
// If dictionary parameterNameMapping is defined, there's no need of setting the
// keyName, we will retrieve this from the dictionary key.
if (context.Settings.PrefixEntityTypeNameBeforeKey && parameterNameMapping == null)
{
keyName = entityType.Name + "-" + keys.First().Name;
}
OpenApiParameter parameter = new OpenApiParameter
{
Name = parameterNameMapping == null ? keyName : parameterNameMapping[keyName],
In = ParameterLocation.Path,
Required = true,
Description = $"The unique identifier of {entityType.Name}",
Schema = context.CreateEdmTypeSchemaForParameter(keys[0].Type, document)
};
parameter.Extensions ??= new Dictionary<string, IOpenApiExtension>();
parameter.Extensions.Add(Constants.xMsKeyType, new JsonNodeExtension(entityType.Name));
parameters.Add(parameter);
}
else
{
// append key parameter
foreach (var keyProperty in entityType.Key())
{
OpenApiParameter parameter = new OpenApiParameter
{
Name = parameterNameMapping == null ?
keyProperty.Name :
parameterNameMapping[keyProperty.Name],// By design: not prefix with type name if enable type name prefix
In = ParameterLocation.Path,
Required = true,
Description = $"Property in multi-part unique identifier of {entityType.Name}",
Schema = context.CreateEdmTypeSchemaForParameter(keyProperty.Type, document)
};
if (keySegment.KeyMappings != null)
{
var quote = keyProperty.Type.Definition.ShouldPathParameterBeQuoted(context.Settings) ? "'" : string.Empty;
parameter.Description += $", {keyProperty.Name}={quote}{{{parameter.Name}}}{quote}";
}
parameter.Extensions ??= new Dictionary<string, IOpenApiExtension>();
parameter.Extensions.Add(Constants.xMsKeyType, new JsonNodeExtension(entityType.Name));
parameters.Add(parameter);
}
}
return parameters;
}
/// <summary>
/// Create alternate key parameters for the <see cref="ODataKeySegment"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="keySegment">The key segment.</param>
/// <param name="document">The Open API document to lookup references.</param>
/// <returns>A list of <see cref="OpenApiParameter"/> of alternate key parameters.</returns>
private static List<OpenApiParameter> CreateAlternateKeyParameters(ODataContext context, ODataKeySegment keySegment, OpenApiDocument document)
{
Debug.Assert(keySegment.Kind == ODataSegmentKind.Key);
var parameters = new List<OpenApiParameter>();
var entityType = keySegment.EntityType;
var alternateKeys = context.Model.GetAlternateKeysAnnotation(entityType);
foreach (var alternateKey in alternateKeys)
{
if (alternateKey.Count() == 1)
{
if (keySegment.Identifier.Equals(alternateKey.First().Key, StringComparison.OrdinalIgnoreCase))
{
parameters.Add(
new OpenApiParameter
{
Name = alternateKey.First().Key,
In = ParameterLocation.Path,
Description = $"Alternate key of {entityType.Name}",
Schema = context.CreateEdmTypeSchemaForParameter(alternateKey.First().Value.Type, document),
Required = true
}
);
}
}
else
{
foreach (var compositekey in alternateKey)
{
if (keySegment.Identifier.Contains(compositekey.Key))
{
parameters.Add(
new OpenApiParameter
{
Name = compositekey.Key,
In = ParameterLocation.Path,
Description = $"Property in multi-part alternate key of {entityType.Name}",
Schema = context.CreateEdmTypeSchemaForParameter(compositekey.Value.Type, document),
Required = true
}
);
}
}
}
}
return parameters;
}
/// <summary>
/// Creates the path parameters for the <see cref="ODataPath"/>
/// </summary>
/// <param name="path">The ODataPath</param>
/// <param name="context">The OData context.</param>
/// <param name="document">The Open API document to lookup references.</param>
/// <returns>The created list of <see cref="OpenApiParameter"/></returns>
public static List<IOpenApiParameter> CreatePathParameters(this ODataPath path, ODataContext context, OpenApiDocument document)
{
List<IOpenApiParameter> pathParameters = [];
var parameterMappings = path.CalculateParameterMapping(context.Settings);
foreach (ODataKeySegment keySegment in path.OfType<ODataKeySegment>())
{
IDictionary<string, string> mapping = parameterMappings[keySegment];
pathParameters.AddRange(context.CreateKeyParameters(keySegment, document, mapping));
}
foreach (ODataOperationSegment operationSegment in path.OfType<ODataOperationSegment>())
{
if (operationSegment.Operation is not IEdmFunction function)
{
continue;
}
if (operationSegment.ParameterMappings != null)
{
var parameters = context.CreateParameters(function, document, operationSegment.ParameterMappings);
foreach (var parameter in parameters)
{
pathParameters.AppendParameter(parameter);
}
}
else
{
IDictionary<string, string> mappings = parameterMappings[operationSegment];
var parameters = context.CreateParameters(function, document, mappings);
pathParameters.AddRange(parameters);
}
}
// Add the route prefix parameter v1{data}
if (context.Settings.RoutePathPrefixProvider?.Parameters != null)
{
pathParameters.AddRange(context.Settings.RoutePathPrefixProvider.Parameters);
}
return pathParameters;
}
/// <summary>
/// Adds an OpenApiParameter to an existing list of OpenApiParameters.
/// If a parameter with the same name already exists in the list, the new parameter name
/// if suffixed with an incrementing number
/// </summary>
/// <param name="parameters">The list of OpenApiParameters to be appended to</param>
/// <param name="parameter">The new OpenApiParameter to be appended</param>
public static void AppendParameter(this IList<IOpenApiParameter> parameters, IOpenApiParameter parameter)
{
HashSet<string> parametersSet = new(parameters.Select(p => p.Name).OfType<string>());
if (parameter.Name is not string parameterName &&
parameter is OpenApiParameter openApiParameter)
{
parameterName = string.Empty;
int index = 1;
while (parametersSet.Contains(parameterName))
{
parameterName += index.ToString();
index++;
}
openApiParameter.Name = parameterName;
}
parameters.Add(parameter);
}
/// <summary>
/// Create the $top parameter.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="target">The Edm annotation target.</param>
/// <param name="document">The Open API document.</param>
/// <returns>The created <see cref="OpenApiParameter"/> or null.</returns>
public static IOpenApiParameter? CreateTop(this ODataContext context, IEdmVocabularyAnnotatable target, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(target, nameof(target));
Utils.CheckArgumentNull(document, nameof(document));
bool? top = context.Model.GetBoolean(target, CapabilitiesConstants.TopSupported);
if (top == null || top.Value)
{
return new OpenApiParameterReference("top", document);
}
return null;
}
/// <summary>
/// Create the $top parameter for Edm target path.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="targetPath">The string representation of the Edm target path.</param>
/// <param name="document">The Open API document to use to build references.</param>
/// <returns></returns>
public static IOpenApiParameter? CreateTop(this ODataContext context, string targetPath, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(targetPath, nameof(targetPath));
Utils.CheckArgumentNull(document, nameof(document));
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateTop(target, document);
}
/// <summary>
/// Create the $skip parameter.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="target">The Edm annotation target.</param>
/// <param name="document">The Open API document to use to build references.</param>
/// <returns>The created <see cref="OpenApiParameter"/> or null.</returns>
public static IOpenApiParameter? CreateSkip(this ODataContext context, IEdmVocabularyAnnotatable target, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(target, nameof(target));
Utils.CheckArgumentNull(document, nameof(document));
bool? skip = context.Model.GetBoolean(target, CapabilitiesConstants.SkipSupported);
if (skip == null || skip.Value)
{
return new OpenApiParameterReference("skip", document);
}
return null;
}
/// <summary>
/// Create the $skip parameter for Edm target path.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="targetPath">The string representation of the Edm target path.</param>
/// <param name="document">The Open API document to use to build references.</param>
/// <returns></returns>
public static IOpenApiParameter? CreateSkip(this ODataContext context, string targetPath, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(targetPath, nameof(targetPath));
Utils.CheckArgumentNull(document, nameof(document));
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateSkip(target, document);
}
/// <summary>
/// Create the $search parameter.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="target">The Edm annotation target.</param>
/// <param name="document">The Open API document to use to build references.</param>
/// <returns>The created <see cref="OpenApiParameter"/> or null.</returns>
public static IOpenApiParameter? CreateSearch(this ODataContext context, IEdmVocabularyAnnotatable target, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(target, nameof(target));
Utils.CheckArgumentNull(document, nameof(document));
if (context.Model.GetRecord<SearchRestrictionsType>(target, CapabilitiesConstants.SearchRestrictions) is not {} search || search.IsSearchable)
{
return new OpenApiParameterReference("search", document);
}
return null;
}
/// <summary>
/// Create the $search parameter for Edm target path.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="targetPath">The string representation of the Edm target path.</param>
/// <param name="document">The Open API document to use to build references.</param>
/// <returns></returns>
public static IOpenApiParameter? CreateSearch(this ODataContext context, string targetPath, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(targetPath, nameof(targetPath));
Utils.CheckArgumentNull(document, nameof(document));
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateSearch(target, document);
}
/// <summary>
/// Create the $count parameter.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="target">The Edm annotation target.</param>
/// <param name="document">The Open API document to use to build references.</param>
/// <returns>The created <see cref="OpenApiParameter"/> or null.</returns>
public static IOpenApiParameter? CreateCount(this ODataContext context, IEdmVocabularyAnnotatable target, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(target, nameof(target));
Utils.CheckArgumentNull(document, nameof(document));
if (context.Model.GetRecord<CountRestrictionsType>(target, CapabilitiesConstants.CountRestrictions) is not {} count || count.IsCountable)
{
return new OpenApiParameterReference("count", document);
}
return null;
}
/// <summary>
/// Create the $count parameter for Edm target path.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="targetPath">The string representation of the Edm target path.</param>
/// <param name="document">The Open API document to use to build references.</param>
/// <returns></returns>
public static IOpenApiParameter? CreateCount(this ODataContext context, string targetPath, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(targetPath, nameof(targetPath));
Utils.CheckArgumentNull(document, nameof(document));
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateCount(target, document);
}
/// <summary>
/// Create the $filter parameter.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="target">The Edm annotation target.</param>
/// <param name="document">The Open API document to use to build references.</param>
/// <returns>The created <see cref="OpenApiParameter"/> or null.</returns>
public static IOpenApiParameter? CreateFilter(this ODataContext context, IEdmVocabularyAnnotatable target, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(target, nameof(target));
Utils.CheckArgumentNull(document, nameof(document));
if (context.Model.GetRecord<FilterRestrictionsType>(target, CapabilitiesConstants.FilterRestrictions) is not {} filter || filter.IsFilterable)
{
return new OpenApiParameterReference("filter", document);
}
return null;
}
/// <summary>
/// Create the $filter parameter for Edm target path.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="targetPath">The string representation of the Edm target path.</param>
/// <param name="document">The Open API document to use to build references.</param>
/// <returns></returns>
public static IOpenApiParameter? CreateFilter(this ODataContext context, string targetPath, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(targetPath, nameof(targetPath));
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateFilter(target, document);
}
public static OpenApiParameter? CreateOrderBy(this ODataContext context, string targetPath, IEdmEntityType entityType)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(targetPath, nameof(targetPath));
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateOrderBy(target, entityType);
}
public static OpenApiParameter? CreateOrderBy(this ODataContext context, IEdmEntitySet entitySet)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(entitySet, nameof(entitySet));
return context.CreateOrderBy(entitySet, entitySet.EntityType);
}
public static OpenApiParameter? CreateOrderBy(this ODataContext context, IEdmSingleton singleton)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(singleton, nameof(singleton));
return context.CreateOrderBy(singleton, singleton.EntityType);
}
public static OpenApiParameter? CreateOrderBy(this ODataContext context, IEdmNavigationProperty navigationProperty)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(navigationProperty, nameof(navigationProperty));
return context.CreateOrderBy(navigationProperty, navigationProperty.ToEntityType());
}
/// <summary>
/// Create $orderby parameter for the <see cref="IEdmEntitySet"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="target">The Edm annotation target.</param>
/// <param name="entityType">The Edm Entity type.</param>
/// <returns>The created <see cref="OpenApiParameter"/> or null.</returns>
public static OpenApiParameter? CreateOrderBy(this ODataContext context, IEdmVocabularyAnnotatable target, IEdmEntityType entityType)
{// patchwork to avoid breaking changes
return context.CreateOrderBy(target, entityType as IEdmStructuredType);
}
public static OpenApiParameter? CreateOrderBy(this ODataContext context, string targetPath, IEdmStructuredType structuredType)
{
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateOrderBy(target, structuredType);
}
public static OpenApiParameter? CreateOrderBy(this ODataContext context, IEdmVocabularyAnnotatable target, IEdmStructuredType structuredType)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(target, nameof(target));
Utils.CheckArgumentNull(structuredType, nameof(structuredType));
var sort = context.Model.GetRecord<SortRestrictionsType>(target, CapabilitiesConstants.SortRestrictions);
if (sort != null && !sort.IsSortable)
{
return null;
}
var orderByItems = new List<JsonNode>();
foreach (var property in structuredType.StructuralProperties())
{
if (sort != null && sort.IsNonSortableProperty(property.Name))
{
continue;
}
bool isAscOnly = sort != null && sort.IsAscendingOnlyProperty(property.Name);
bool isDescOnly = sort != null && sort.IsDescendingOnlyProperty(property.Name);
if (isAscOnly || isDescOnly)
{
if (isAscOnly)
{
orderByItems.Add(property.Name);
}
else
{
orderByItems.Add(property.Name + " desc");
}
}
else
{
orderByItems.Add(property.Name);
orderByItems.Add(property.Name + " desc");
}
}
return new OpenApiParameter
{
Name = "$orderby",
In = ParameterLocation.Query,
Description = "Order items by property values",
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Array,
UniqueItems = true,
Items = new OpenApiSchema
{
Type = JsonSchemaType.String,
Enum = context.Settings.UseStringArrayForQueryOptionsSchema ? null : orderByItems
}
},
Style = ParameterStyle.Form,
Explode = false
};
}
public static OpenApiParameter? CreateSelect(this ODataContext context, string targetPath, IEdmEntityType entityType)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(targetPath, nameof(targetPath));
if (context.Model.GetTargetPath(targetPath) is not {} target)
return null;
return context.CreateSelect(target, entityType);
}
public static OpenApiParameter? CreateSelect(this ODataContext context, IEdmEntitySet entitySet)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(entitySet, nameof(entitySet));
return context.CreateSelect(entitySet, entitySet.EntityType);
}
public static OpenApiParameter? CreateSelect(this ODataContext context, IEdmSingleton singleton)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(singleton, nameof(singleton));
return context.CreateSelect(singleton, singleton.EntityType);
}
public static OpenApiParameter? CreateSelect(this ODataContext context, IEdmNavigationProperty navigationProperty)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(navigationProperty, nameof(navigationProperty));
return context.CreateSelect(navigationProperty, navigationProperty.ToEntityType());
}
/// <summary>
/// Create $select parameter for the <see cref="IEdmVocabularyAnnotatable"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="target">The Edm target.</param>
/// <param name="entityType">The Edm entity type.</param>
/// <returns>The created <see cref="OpenApiParameter"/> or null.</returns>
public static OpenApiParameter? CreateSelect(this ODataContext context, IEdmVocabularyAnnotatable target, IEdmEntityType entityType)
{ // patchwork to avoid breaking changes
return context.CreateSelect(target, entityType as IEdmStructuredType);
}
public static OpenApiParameter? CreateSelect(this ODataContext context, string targetPath, IEdmStructuredType structuredType)
{
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateSelect(target, structuredType);
}
public static OpenApiParameter? CreateSelect(this ODataContext context, IEdmVocabularyAnnotatable target, IEdmStructuredType structuredType)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(target, nameof(target));
Utils.CheckArgumentNull(structuredType, nameof(structuredType));
var navigation = context.Model.GetRecord<NavigationRestrictionsType>(target, CapabilitiesConstants.NavigationRestrictions);
if (navigation != null && !navigation.IsNavigable)
{
return null;
}
var selectItems = new List<JsonNode>();
foreach (var property in structuredType.StructuralProperties())
{
selectItems.Add(property.Name);
}
foreach (var property in structuredType.NavigationProperties())
{
if (navigation != null && navigation.IsRestrictedProperty(property.Name))
{
continue;
}
selectItems.Add(property.Name);
}
return new OpenApiParameter
{
Name = "$select",
In = ParameterLocation.Query,
Description = "Select properties to be returned",
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Array,
UniqueItems = true,
Items = new OpenApiSchema
{
Type = JsonSchemaType.String,
Enum = context.Settings.UseStringArrayForQueryOptionsSchema ? null : selectItems
}
},
Style = ParameterStyle.Form,
Explode = false
};
}
public static OpenApiParameter? CreateExpand(this ODataContext context, string targetPath, IEdmEntityType entityType)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(targetPath, nameof(targetPath));
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateExpand(target, entityType);
}
public static OpenApiParameter? CreateExpand(this ODataContext context, IEdmEntitySet entitySet)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(entitySet, nameof(entitySet));
return context.CreateExpand(entitySet, entitySet.EntityType);
}
public static OpenApiParameter? CreateExpand(this ODataContext context, IEdmSingleton singleton)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(singleton, nameof(singleton));
return context.CreateExpand(singleton, singleton.EntityType);
}
public static OpenApiParameter? CreateExpand(this ODataContext context, IEdmNavigationProperty navigationProperty)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(navigationProperty, nameof(navigationProperty));
return context.CreateExpand(navigationProperty, navigationProperty.ToEntityType());
}
/// <summary>
/// Create $expand parameter for the <see cref="IEdmNavigationSource"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="target">The edm entity path.</param>
/// <param name="entityType">The edm entity path.</param>
/// <returns>The created <see cref="OpenApiParameter"/> or null.</returns>
public static OpenApiParameter? CreateExpand(this ODataContext context, IEdmVocabularyAnnotatable target, IEdmEntityType entityType)
{ // patchwork to avoid breaking changes
return context.CreateExpand(target, entityType as IEdmStructuredType);
}
public static OpenApiParameter? CreateExpand(this ODataContext context, string targetPath, IEdmStructuredType structuredType)
{
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
if (target == null)
return null;
return context.CreateExpand(target, structuredType);
}
public static OpenApiParameter? CreateExpand(this ODataContext context, IEdmVocabularyAnnotatable target, IEdmStructuredType structuredType)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(target, nameof(target));
Utils.CheckArgumentNull(structuredType, nameof(structuredType));
var expand = context.Model.GetRecord<ExpandRestrictionsType>(target, CapabilitiesConstants.ExpandRestrictions);
if (expand != null && !expand.IsExpandable)
{
return null;
}
List<JsonNode> expandItems = [ "*" ];
foreach (var property in structuredType.NavigationProperties())
{
if (expand != null && expand.IsNonExpandableProperty(property.Name))
{
continue;
}
expandItems.Add(property.Name);
}
return new OpenApiParameter
{
Name = "$expand",
In = ParameterLocation.Query,
Description = "Expand related entities",
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Array,
UniqueItems = true,
Items = new OpenApiSchema
{
Type = JsonSchemaType.String,
Enum = context.Settings.UseStringArrayForQueryOptionsSchema ? null : expandItems
}
},
Style = ParameterStyle.Form,
Explode = false
};
}
// #top
private static OpenApiParameter CreateTop(int topExample)
{
return new OpenApiParameter
{
Name = "$top",
In = ParameterLocation.Query,
Description = "Show only the first n items",
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Number,
Format = "int64",
Minimum = "0",
},
Example = topExample,
Style = ParameterStyle.Form,
Explode = false
};
}
// $skip
private static OpenApiParameter CreateSkip()
{
return new OpenApiParameter
{
Name = "$skip",
In = ParameterLocation.Query,
Description = "Skip the first n items",
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Number,
Format = "int64",
Minimum = "0",
},
Style = ParameterStyle.Form,
Explode = false
};
}
// $count
private static OpenApiParameter CreateCount()
{
return new OpenApiParameter
{
Name = "$count",
In = ParameterLocation.Query,
Description = "Include count of items",
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Boolean
},
Style = ParameterStyle.Form,
Explode = false
};
}
// $filter
private static OpenApiParameter CreateFilter()
{
return new OpenApiParameter
{
Name = "$filter",
In = ParameterLocation.Query,
Description = "Filter items by property values",
Schema = new OpenApiSchema
{
Type = JsonSchemaType.String
},
Style = ParameterStyle.Form,
Explode = false
};
}
// $search
private static OpenApiParameter CreateSearch()
{
return new OpenApiParameter
{
Name = "$search",
In = ParameterLocation.Query,
Description = "Search items by search phrases",
Schema = new OpenApiSchema
{
Type = JsonSchemaType.String
},
Style = ParameterStyle.Form,
Explode = false
};
}
}
}