< Summary

Class:Azure.Search.Documents.Models.SearchSuggestion`1
Assembly:Azure.Search.Documents
File(s):C:\Git\azure-sdk-for-net\sdk\search\Azure.Search.Documents\src\Models\SearchSuggestion{T}.cs
Covered lines:17
Uncovered lines:6
Coverable lines:23
Total lines:132
Line coverage:73.9% (17 of 23)
Covered branches:7
Total branches:10
Branch coverage:70% (7 of 10)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_Text()-100%100%
get_Document()-100%100%
.ctor()-100%100%
DeserializeAsync()-70%70%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Azure.Search.Documents\src\Models\SearchSuggestion{T}.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Diagnostics;
 5using System.IO;
 6using System.Text.Json;
 7using System.Threading;
 8using System.Threading.Tasks;
 9using Azure.Core;
 10#if EXPERIMENTAL_SERIALIZER
 11using Azure.Core.Serialization;
 12#endif
 13
 14#pragma warning disable SA1402 // File may only contain a single type
 15
 16namespace Azure.Search.Documents.Models
 17{
 18    // Hide the untyped SuggestResult
 19    [CodeGenModel("SuggestResult")]
 20    internal partial class SuggestResult { }
 21
 22    /// <summary>
 23    /// A result containing a document found by a suggestion query, plus
 24    /// associated metadata.
 25    /// </summary>
 26    /// <typeparam name="T">
 27    /// The .NET type that maps to the index schema. Instances of this type can
 28    /// be retrieved as documents from the index.
 29    /// </typeparam>
 30    public class SearchSuggestion<T>
 31    {
 32        /// <summary>
 33        /// The text of the suggestion result.
 34        /// </summary>
 5435        public string Text { get; internal set; }
 36
 37        /// <summary>
 38        /// The document being suggested.
 39        /// </summary>
 7640        public T Document { get; internal set; }
 41
 42        /// <summary>
 43        /// Initializes a new instance of the SearchSuggestion class.
 44        /// </summary>
 8845        internal SearchSuggestion() { }
 46
 47        #pragma warning disable CS1572 // Not all parameters will be used depending on feature flags
 48        /// <summary>
 49        /// Deserialize a SearchSuggestion and its model.
 50        /// </summary>
 51        /// <param name="element">A JSON element.</param>
 52        /// <param name="serializer">
 53        /// Optional serializer that can be used to customize the serialization
 54        /// of strongly typed models.
 55        /// </param>
 56        /// <param name="options">JSON serializer options.</param>
 57        /// <param name="async">Whether to execute sync or async.</param>
 58        /// <param name="cancellationToken">
 59        /// Optional <see cref="CancellationToken"/> to propagate notifications
 60        /// that the operation should be canceled.
 61        /// </param>
 62        /// <returns>Deserialized SearchSuggestion.</returns>
 63        internal static async Task<SearchSuggestion<T>> DeserializeAsync(
 64            JsonElement element,
 65#if EXPERIMENTAL_SERIALIZER
 66            ObjectSerializer serializer,
 67#endif
 68            JsonSerializerOptions options,
 69            bool async,
 70            CancellationToken cancellationToken)
 71        #pragma warning restore CS1572
 72        {
 73            Debug.Assert(options != null);
 74
 4475            SearchSuggestion<T> suggestion = new SearchSuggestion<T>();
 13276            foreach (JsonProperty prop in element.EnumerateObject())
 77            {
 4478                if (prop.NameEquals(Constants.SearchTextKeyJson.EncodedUtf8Bytes))
 79                {
 4480                    suggestion.Text = prop.Value.GetString();
 4481                    break; // We only have one non-model property
 82                }
 83            }
 84
 85            // Deserialize the model
 86#if EXPERIMENTAL_SERIALIZER
 4487            if (serializer != null)
 88            {
 089                using Stream stream = element.ToStream();
 090                T document = async ?
 091                    (T)await serializer.DeserializeAsync(stream, typeof(T), cancellationToken).ConfigureAwait(false) :
 092                    (T)serializer.Deserialize(stream, typeof(T), cancellationToken);
 093                suggestion.Document = document;
 094            }
 95            else
 96            {
 97#endif
 98                T document;
 4499                if (async)
 100                {
 22101                    using Stream stream = element.ToStream();
 22102                    document = await JsonSerializer.DeserializeAsync<T>(stream, options, cancellationToken).ConfigureAwa
 22103                }
 104                else
 105                {
 22106                    document = JsonSerializer.Deserialize<T>(element.GetRawText(), options);
 107                }
 44108                suggestion.Document = document;
 109#if EXPERIMENTAL_SERIALIZER
 110            }
 111#endif
 112
 44113            return suggestion;
 44114        }
 115    }
 116
 117    public static partial class SearchModelFactory
 118    {
 119        /// <summary> Initializes a new instance of SearchSuggestion. </summary>
 120        /// <typeparam name="T">
 121        /// The .NET type that maps to the index schema. Instances of this type can
 122        /// be retrieved as documents from the index.
 123        /// </typeparam>
 124        /// <param name="document">The document being suggested.</param>
 125        /// <param name="text">The text of the suggestion result.</param>
 126        /// <returns>A new SuggestResults instance for mocking.</returns>
 127        public static SearchSuggestion<T> SearchSuggestion<T>(
 128            T document,
 129            string text) =>
 130            new SearchSuggestion<T>() { Document = document, Text = text };
 131    }
 132}