< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Runtime.Serialization.XsdDataContractExporterEx
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Runtime/Serialization/XsdDataContractExporterEx.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 63
Coverable lines: 63
Total lines: 144
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 26
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
.ctor(...)100%110%
GetSchemaSet()0%220%
Export(...)0%660%
GetSchemaTypeName(...)0%660%
GetSchemaType(...)0%660%
GetRootElementName(...)0%440%
GetSurrogatedType(...)100%110%
AddType(...)100%110%
Export()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Runtime/Serialization/XsdDataContractExporterEx.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.Xml;
 6using System.Xml.Schema;
 7using System.Reflection;
 8using System.Runtime.Serialization;
 9
 10namespace CoreWCF.Runtime.Serialization
 11{
 12    internal class XsdDataContractExporterEx
 13    {
 14        private XmlSchemaSet _schemas;
 15        private DataContractSetEx _dataContractSet;
 16
 017        public XsdDataContractExporterEx()
 18        {
 019        }
 20
 021        public XsdDataContractExporterEx(XmlSchemaSet schemas)
 22        {
 023            _schemas = schemas;
 024        }
 25
 26        private XmlSchemaSet GetSchemaSet()
 27        {
 028            if (_schemas == null)
 29            {
 030                _schemas = new XmlSchemaSet();
 031                _schemas.XmlResolver = null;
 32            }
 033            return _schemas;
 34        }
 35
 36        private DataContractSetEx DataContractSet
 37        {
 38            get
 39            {
 040                if (_dataContractSet == null)
 41                {
 042                    _dataContractSet = new DataContractSetEx();
 43                }
 044                return _dataContractSet;
 45            }
 46        }
 47
 48        public void Export(Type type)
 49        {
 050            if (type == null)
 051                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(type)));
 52
 053            DataContractSetEx oldValue = (_dataContractSet == null) ? null : new DataContractSetEx(_dataContractSet);
 54            try
 55            {
 056                AddType(type);
 057                Export();
 058            }
 59            catch (Exception ex)
 60            {
 061                if (Fx.IsFatal(ex))
 62                {
 063                    throw;
 64                }
 65
 066                _dataContractSet = oldValue;
 067                throw;
 68            }
 069        }
 70
 71        public XmlQualifiedName GetSchemaTypeName(Type type)
 72        {
 073            if (type == null)
 074                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(type)));
 75
 076            type = GetSurrogatedType(type);
 077            DataContractEx dataContract = DataContractEx.GetDataContract(type);
 078            DataContractSetEx.EnsureTypeNotGeneric(dataContract.UnderlyingType);
 079            XmlDataContractEx xmlDataContract = dataContract as XmlDataContractEx;
 080            if (xmlDataContract != null && xmlDataContract.IsAnonymous)
 081                return XmlQualifiedName.Empty;
 082            return dataContract.StableName;
 83        }
 84
 85        public XmlSchemaType GetSchemaType(Type type)
 86        {
 087            if (type == null)
 088                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(type)));
 89
 090            type = GetSurrogatedType(type);
 091            DataContractEx dataContract = DataContractEx.GetDataContract(type);
 092            DataContractSetEx.EnsureTypeNotGeneric(dataContract.UnderlyingType);
 093            XmlDataContractEx xmlDataContract = dataContract as XmlDataContractEx;
 094            if (xmlDataContract != null && xmlDataContract.IsAnonymous)
 095                return xmlDataContract.XsdType;
 096            return null;
 97        }
 98
 99        public XmlQualifiedName GetRootElementName(Type type)
 100        {
 0101            if (type == null)
 0102                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(type)));
 103
 0104            type = GetSurrogatedType(type);
 0105            DataContractEx dataContract = DataContractEx.GetDataContract(type);
 0106            DataContractSetEx.EnsureTypeNotGeneric(dataContract.UnderlyingType);
 0107            if (dataContract.HasRoot)
 108            {
 0109                return new XmlQualifiedName(dataContract.TopLevelElementName.Value, dataContract.TopLevelElementNamespac
 110            }
 111            else
 112            {
 0113                return null;
 114            }
 115        }
 116
 117        private Type GetSurrogatedType(Type type)
 118        {
 119            //IDataContractSurrogate dataContractSurrogate;
 120            //if (options != null && (dataContractSurrogate = Options.GetSurrogate()) != null)
 121            //    type = DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, type);
 0122            return type;
 123        }
 124
 125        private void AddType(Type type)
 126        {
 0127            DataContractSet.Add(type);
 0128        }
 129
 130        private void Export()
 131        {
 0132            DataContractSet.FixupDataContracts();
 0133            var schemaExporterType = typeof(DataContractSerializer).Assembly.GetType("System.Runtime.Serialization.Schem
 0134            var schemaExporter = Activator.CreateInstance(schemaExporterType,
 0135                                                          BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.
 0136                                                          null,
 0137                                                          new object[] { GetSchemaSet(), DataContractSet.Wrapped },
 0138                                                          null,
 0139                                                          null);
 0140            var exportMethod = schemaExporterType.GetMethod("Export", BindingFlags.Instance | BindingFlags.NonPublic, nu
 0141            exportMethod.Invoke(schemaExporter, null);
 0142        }
 143    }
 144}