|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.OpenApi.Models; |
| 8 | + |
| 9 | +namespace Microsoft.OpenApi.Services |
| 10 | +{ |
| 11 | + public class OperationSearch : OpenApiVisitorBase |
| 12 | + { |
| 13 | + private readonly Func<OpenApiOperation, bool> _predicate; |
| 14 | + private readonly List<SearchResult> _searchResults = new(); |
| 15 | + |
| 16 | + public IList<SearchResult> SearchResults => _searchResults; |
| 17 | + |
| 18 | + public OperationSearch(Func<OpenApiOperation, bool> predicate) |
| 19 | + { |
| 20 | + _predicate = predicate ?? throw new ArgumentNullException(nameof(predicate)); |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Visits <see cref="OpenApiOperation"/>. |
| 25 | + /// </summary> |
| 26 | + /// <param name="operation">The target <see cref="OpenApiOperation"/>.</param> |
| 27 | + public override void Visit(OpenApiOperation operation) |
| 28 | + { |
| 29 | + if (_predicate(operation)) |
| 30 | + { |
| 31 | + _searchResults.Add(new SearchResult() |
| 32 | + { |
| 33 | + Operation = operation, |
| 34 | + CurrentKeys = CopyCurrentKeys(CurrentKeys) |
| 35 | + }); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Visits list of <see cref="OpenApiParameter"/>. |
| 41 | + /// </summary> |
| 42 | + /// <param name="parameters">The target list of <see cref="OpenApiParameter"/>.</param> |
| 43 | + public override void Visit(IList<OpenApiParameter> parameters) |
| 44 | + { |
| 45 | + /* The Parameter.Explode property should be true |
| 46 | + * if Parameter.Style == Form; but OData query params |
| 47 | + * as used in Microsoft Graph implement explode: false |
| 48 | + * ex: $select=id,displayName,givenName |
| 49 | + */ |
| 50 | + foreach (var parameter in parameters.Where(x => x.Style == ParameterStyle.Form)) |
| 51 | + { |
| 52 | + parameter.Explode = false; |
| 53 | + } |
| 54 | + |
| 55 | + base.Visit(parameters); |
| 56 | + } |
| 57 | + |
| 58 | + private static CurrentKeys CopyCurrentKeys(CurrentKeys currentKeys) |
| 59 | + { |
| 60 | + return new CurrentKeys |
| 61 | + { |
| 62 | + Path = currentKeys.Path, |
| 63 | + Operation = currentKeys.Operation |
| 64 | + }; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments