< Summary

Class:Microsoft.Azure.Search.Serialization.SearchContinuationTokenConverter
Assembly:Microsoft.Azure.Search.Data
File(s):C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Serialization\SearchContinuationTokenConverter.cs
Covered lines:38
Uncovered lines:1
Coverable lines:39
Total lines:108
Line coverage:97.4% (38 of 39)
Covered branches:14
Total branches:14
Branch coverage:100% (14 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
CanConvert(...)-0%100%
ReadJson(...)-100%100%
WriteJson(...)-100%100%
ParseApiVersion(...)-100%100%
get_NextLink()-100%100%
get_NextPageParameters()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Serialization\SearchContinuationTokenConverter.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for
 3// license information.
 4
 5namespace Microsoft.Azure.Search.Serialization
 6{
 7    using System;
 8    using Models;
 9    using Newtonsoft.Json;
 10
 11    internal class SearchContinuationTokenConverter : JsonConverter
 12    {
 13        // MAINTENANCE NOTE: Remember to change this when the REST API version changes.
 14        private const string TargetApiVersion = "2019-05-06";
 15
 016        public override bool CanConvert(Type objectType) => objectType == typeof(SearchContinuationToken);
 17
 18        public override object ReadJson(
 19            JsonReader reader,
 20            Type objectType,
 21            object existingValue,
 22            JsonSerializer serializer)
 23        {
 1424            Payload payload = serializer.Deserialize<Payload>(reader);
 25
 26            Uri nextLinkUri;
 27            try
 28            {
 1429                nextLinkUri = new Uri(payload.NextLink);
 1230            }
 231            catch (FormatException e)
 32            {
 233                throw new JsonSerializationException(
 234                    "Cannot deserialize continuation token. Failed to parse nextLink because it is not a valid URL.",
 235                    e);
 36            }
 37
 1238            string apiVersion = ParseApiVersion(nextLinkUri.Query);
 39
 1240            if (string.IsNullOrWhiteSpace(apiVersion))
 41            {
 642                throw new JsonSerializationException(
 643                    "Cannot deserialize continuation token because the api-version is missing.");
 44            }
 45
 646            if (apiVersion != TargetApiVersion)
 47            {
 248                string message =
 249                    "Cannot deserialize a continuation token for a different api-version. Token contains version " +
 250                    $"'{apiVersion}'; Expected version '{TargetApiVersion}'.";
 51
 252                throw new JsonSerializationException(message);
 53            }
 54
 455            return new SearchContinuationToken(payload.NextLink, payload.NextPageParameters);
 56        }
 57
 58        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 59        {
 460            var token = (SearchContinuationToken)value;
 61
 462            var payload =
 463                new Payload()
 464                {
 465                    NextLink = token.NextLink,
 466                    NextPageParameters = token.NextPageParameters
 467                };
 68
 469            serializer.Serialize(writer, payload);
 470        }
 71
 72        private static string ParseApiVersion(string query)
 73        {
 1274            if (String.IsNullOrWhiteSpace(query))
 75            {
 276                return null;
 77            }
 78
 1079            if (query[0] == '?')
 80            {
 1081                query = query.Substring(1);
 82            }
 83
 1084            string[] pairs = query.Split('&');
 85
 5286            foreach (string pair in pairs)
 87            {
 2088                string[] nameAndValue = pair.Split('=');
 89
 2090                if (nameAndValue.Length == 2 && nameAndValue[0] == "api-version")
 91                {
 892                    return nameAndValue[1];
 93                }
 94            }
 95
 296            return null;
 97        }
 98
 99        private class Payload
 100        {
 101            [JsonProperty("@odata.nextLink")]
 40102            public string NextLink { get; set; }
 103
 104            [JsonProperty("@search.nextPageParameters")]
 30105            public SearchRequest NextPageParameters { get; set; }
 106        }
 107    }
 108}