< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.SingleBodyParameterMessageFormatter
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/SingleBodyParameterMessageFormatter.cs
Line coverage
67%
Covered lines: 65
Uncovered lines: 32
Coverable lines: 97
Total lines: 268
Line coverage: 67%
Branch coverage
48%
Covered branches: 25
Total branches: 52
Branch coverage: 48%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%2288.88%
CreateXmlAndJsonDispatchFormatter(...)50%2290%
DeserializeRequest(...)50%2275%
SerializeReply(...)50%4471.42%
CreateDispatchFormatter(...)100%44100%
SuppressReplyEntityBody(...)33.33%6645.45%
AttachMessageProperties(...)100%110%
ValidateMessageFormatProperty(...)100%110%
GetTypeForSerializer(...)12.5%8828.57%
CreateXmlFormatter(...)25%4442.85%
CreateJsonFormatter(...)50%2275%
CreateBodyWriter(...)50%4466.66%
ReadObject(...)50%8877.77%
DeserializeRequest(...)100%110%
SerializeReply(...)100%11100%
.ctor(...)75%4483.33%
OnWriteBodyContents(...)100%22100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/SingleBodyParameterMessageFormatter.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Runtime.Serialization;
 7using System.Xml;
 8using CoreWCF.Channels;
 9using CoreWCF.Collections.Generic;
 10using CoreWCF.Description;
 11using CoreWCF.Web;
 12
 13namespace CoreWCF.Dispatcher
 14{
 15    internal abstract class SingleBodyParameterMessageFormatter : IDispatchMessageFormatter
 16    {
 17        private readonly bool _isRequestFormatter;
 18        private readonly string _serializerType;
 19
 27220        protected SingleBodyParameterMessageFormatter(OperationDescription operation, bool isRequestFormatter, string se
 21        {
 27222            if (operation == null)
 23            {
 024                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operation));
 25            }
 26
 27227            ContractName = operation.DeclaringContract.Name;
 27228            ContractNs = operation.DeclaringContract.Namespace;
 27229            OperationName = operation.Name;
 27230            _isRequestFormatter = isRequestFormatter;
 27231            _serializerType = serializerType;
 27232        }
 33
 034        protected string ContractName { get; }
 35
 036        protected string ContractNs { get; }
 37
 038        protected string OperationName { get; }
 39
 40        public static IDispatchMessageFormatter CreateXmlAndJsonDispatchFormatter(OperationDescription operation, Type t
 41        {
 16042            IDispatchMessageFormatter xmlFormatter = CreateDispatchFormatter(operation, type, isRequestFormatter, false,
 16043            if (!WebHttpBehavior.SupportsJsonFormat(operation))
 44            {
 045                return xmlFormatter;
 46            }
 47
 16048            IDispatchMessageFormatter jsonFormatter = CreateDispatchFormatter(operation, type, isRequestFormatter, true,
 16049            Dictionary<WebContentFormat, IDispatchMessageFormatter> map = new Dictionary<WebContentFormat, IDispatchMess
 16050            {
 16051                { WebContentFormat.Xml, xmlFormatter },
 16052                { WebContentFormat.Json, jsonFormatter }
 16053            };
 54
 16055            return new DemultiplexingDispatchMessageFormatter(map, xmlFormatter);
 56        }
 57
 58        public void DeserializeRequest(Message message, object[] parameters)
 59        {
 560            if (!_isRequestFormatter)
 61            {
 062                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.For
 63            }
 64
 565            parameters[0] = ReadObject(message);
 566        }
 67
 68        public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
 69        {
 2670            if (_isRequestFormatter)
 71            {
 072                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.For
 73            }
 74
 2675            Message message = Message.CreateMessage(messageVersion, (string)null, CreateBodyWriter(result));
 2676            if (result == null)
 77            {
 078                SuppressReplyEntityBody(message);
 79            }
 80
 2681            AttachMessageProperties(message, false);
 82
 2683            return message;
 84        }
 85
 86        internal static IDispatchMessageFormatter CreateDispatchFormatter(OperationDescription operation, Type type, boo
 87        {
 64088            if (type == null)
 89            {
 36890                return new NullMessageFormatter();
 91            }
 27292            else if (useJson)
 93            {
 13694                return CreateJsonFormatter(operation, type, isRequestFormatter);
 95            }
 96            else
 97            {
 13698                return CreateXmlFormatter(operation, type, isRequestFormatter, xmlSerializerManager);
 99            }
 100        }
 101
 102        internal static void SuppressReplyEntityBody(Message message)
 103        {
 4104            WebOperationContext currentContext = WebOperationContext.Current;
 4105            if (currentContext != null)
 106            {
 4107                OutgoingWebResponseContext responseContext = currentContext.OutgoingResponse;
 4108                if (responseContext != null)
 109                {
 4110                    responseContext.SuppressEntityBody = true;
 111                }
 112            }
 113            else
 114            {
 0115                message.Properties.TryGetValue(HttpResponseMessageProperty.Name, out object untypedProp);
 0116                if (!(untypedProp is HttpResponseMessageProperty prop))
 117                {
 0118                    prop = new HttpResponseMessageProperty();
 0119                    message.Properties[HttpResponseMessageProperty.Name] = prop;
 120                }
 0121                prop.SuppressEntityBody = true;
 122            }
 0123        }
 124
 125        protected virtual void AttachMessageProperties(Message message, bool isRequest)
 126        {
 0127        }
 128
 129        protected abstract XmlObjectSerializer[] GetInputSerializers();
 130
 131        protected abstract XmlObjectSerializer GetOutputSerializer(Type type);
 132
 133        protected virtual void ValidateMessageFormatProperty(Message message)
 134        {
 0135        }
 136
 137        protected Type GetTypeForSerializer(Type type, Type parameterType, IList<Type> knownTypes)
 138        {
 26139            if (type == parameterType)
 140            {
 26141                return type;
 142            }
 0143            else if (knownTypes != null)
 144            {
 0145                for (int i = 0; i < knownTypes.Count; ++i)
 146                {
 0147                    if (type == knownTypes[i])
 148                    {
 0149                        return type;
 150                    }
 151                }
 152            }
 153
 0154            return parameterType;
 155        }
 156
 157        public static SingleBodyParameterMessageFormatter CreateXmlFormatter(OperationDescription operation, Type type, 
 158        {
 136159            DataContractSerializerOperationBehavior dcsob = ((KeyedByTypeCollection<IOperationBehavior>)operation.Operat
 136160            if (dcsob != null)
 161            {
 136162                return new SingleBodyParameterDataContractMessageFormatter(operation, type, isRequestFormatter, false, d
 163            }
 164
 0165            XmlSerializerOperationBehavior xsob = ((KeyedByTypeCollection<IOperationBehavior>)operation.OperationBehavio
 0166            if (xsob != null)
 167            {
 0168                return new SingleBodyParameterXmlSerializerMessageFormatter(operation, type, isRequestFormatter, xsob, x
 169            }
 170
 0171            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.OnlyDataCon
 172        }
 173
 174        public static SingleBodyParameterMessageFormatter CreateJsonFormatter(OperationDescription operation, Type type,
 175        {
 136176            DataContractSerializerOperationBehavior dcsob = ((KeyedByTypeCollection<IOperationBehavior>)operation.Operat
 136177            if (dcsob == null)
 178            {
 0179                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Jso
 180            }
 181
 136182            return new SingleBodyParameterDataContractMessageFormatter(operation, type, isRequestFormatter, true, dcsob)
 183        }
 184
 185        private BodyWriter CreateBodyWriter(object body)
 186        {
 187            XmlObjectSerializer serializer;
 26188            if (body != null)
 189            {
 26190                serializer = GetOutputSerializer(body.GetType());
 26191                if (serializer == null)
 192                {
 0193                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Can
 194                }
 195            }
 196            else
 197            {
 0198                serializer = null;
 199            }
 200
 26201            return new SingleParameterBodyWriter(body, serializer);
 202        }
 203
 204        protected virtual object ReadObject(Message message)
 205        {
 5206            if (HttpStreamFormatter.IsEmptyMessage(message))
 207            {
 0208                return null;
 209            }
 210
 5211            XmlObjectSerializer[] inputSerializers = GetInputSerializers();
 5212            XmlDictionaryReader reader = message.GetReaderAtBodyContents();
 5213            if (inputSerializers != null)
 214            {
 10215                for (int i = 0; i < inputSerializers.Length; ++i)
 216                {
 5217                    if (inputSerializers[i].IsStartObject(reader))
 218                    {
 5219                        return inputSerializers[i].ReadObject(reader, false);
 220                    }
 221                }
 222            }
 223
 0224            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.Format(SR.CannotDese
 225        }
 226
 227        internal class NullMessageFormatter : IDispatchMessageFormatter
 228        {
 229            public void DeserializeRequest(Message message, object[] parameters)
 230            {
 0231            }
 232
 233            public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
 234            {
 4235                Message reply = Message.CreateMessage(messageVersion, null);
 4236                SuppressReplyEntityBody(reply);
 237
 4238                return reply;
 239            }
 240        }
 241
 242        internal class SingleParameterBodyWriter : BodyWriter
 243        {
 244            private readonly object _body;
 245            private readonly XmlObjectSerializer _serializer;
 246
 247            public SingleParameterBodyWriter(object body, XmlObjectSerializer serializer)
 26248                : base(false)
 249            {
 26250                if (body != null && serializer == null)
 251                {
 0252                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer));
 253                }
 254
 26255                _body = body;
 26256                _serializer = serializer;
 26257            }
 258
 259            protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 260            {
 26261                if (_body != null)
 262                {
 26263                    _serializer.WriteObject(writer, _body);
 264                }
 26265            }
 266        }
 267    }
 268}