| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections; |
| | | 6 | | using System.Runtime.Serialization; |
| | | 7 | | using System.Xml; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Runtime.Serialization |
| | | 10 | | { |
| | | 11 | | internal class DataContractSetEx |
| | | 12 | | { |
| | | 13 | | internal const string RemoveKeyValuePairFromWsdl = "CoreWCF.RemoveKeyValuePairFromWsdl"; |
| | 0 | 14 | | internal bool _removeKeyValuePairFromWsdl = AppContext.TryGetSwitch(RemoveKeyValuePairFromWsdl, out bool enabled |
| | | 15 | | |
| | 0 | 16 | | internal DataContractSetEx() |
| | | 17 | | { |
| | 0 | 18 | | Wrapped = FormatterServices.GetUninitializedObject(s_dataContractSetType); |
| | 0 | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | internal DataContractSetEx(DataContractSetEx dataContractSet) : this() |
| | | 22 | | { |
| | 0 | 23 | | foreach (var key in dataContractSet.Contracts.Keys) |
| | | 24 | | { |
| | 0 | 25 | | Contracts[key] = dataContractSet.Contracts[key]; |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | foreach (var key in dataContractSet.ProcessedContracts.Keys) |
| | | 29 | | { |
| | 0 | 30 | | ProcessedContracts[key] = dataContractSet.ProcessedContracts[key]; |
| | | 31 | | } |
| | 0 | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | public object Wrapped { get; } |
| | | 35 | | |
| | 0 | 36 | | public IDictionary Contracts => s_getContracts(Wrapped); |
| | | 37 | | |
| | 0 | 38 | | public IDictionary ProcessedContracts => s_getProcessedContracts(Wrapped); |
| | | 39 | | |
| | | 40 | | internal void Add(Type type) |
| | | 41 | | { |
| | 0 | 42 | | DataContractEx dataContract = DataContractEx.GetDataContract(type); |
| | 0 | 43 | | EnsureTypeNotGeneric(dataContract.UnderlyingType); |
| | 0 | 44 | | Add(dataContract); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | internal static void EnsureTypeNotGeneric(Type type) |
| | | 48 | | { |
| | 0 | 49 | | if (type.ContainsGenericParameters) |
| | 0 | 50 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR. |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | private void Add(DataContractEx dataContract) |
| | | 54 | | { |
| | 0 | 55 | | Add(dataContract.StableName, dataContract); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public void Add(XmlQualifiedName name, DataContractEx dataContract) |
| | | 59 | | { |
| | 0 | 60 | | if (dataContract.IsBuiltInDataContract) |
| | 0 | 61 | | return; |
| | 0 | 62 | | InternalAdd(name, dataContract); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | internal void InternalAdd(XmlQualifiedName name, DataContractEx dataContract) |
| | | 66 | | { |
| | | 67 | | DataContractEx dataContractInSet; |
| | 0 | 68 | | if (Contracts.Contains(name)) |
| | | 69 | | { |
| | 0 | 70 | | dataContractInSet = DataContractEx.Wrap(Contracts[name]); |
| | 0 | 71 | | if (!dataContractInSet.Equals(dataContract.WrappedDataContract)) |
| | | 72 | | { |
| | 0 | 73 | | if (dataContract.UnderlyingType == null || dataContractInSet.UnderlyingType == null) |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 75 | | else |
| | | 76 | | { |
| | 0 | 77 | | bool typeNamesEqual = (DataContractEx.GetClrTypeFullName(dataContract.UnderlyingType) == DataCon |
| | 0 | 78 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | else |
| | | 83 | | { |
| | 0 | 84 | | Contracts.Add(name, dataContract.WrappedDataContract); |
| | | 85 | | |
| | 0 | 86 | | if (dataContract is ClassDataContractEx classDataContract) |
| | | 87 | | { |
| | 0 | 88 | | AddClassDataContract(classDataContract); |
| | | 89 | | } |
| | 0 | 90 | | else if (dataContract is CollectionDataContractEx collectionDataContract) |
| | | 91 | | { |
| | 0 | 92 | | AddCollectionDataContract(collectionDataContract); |
| | | 93 | | } |
| | 0 | 94 | | else if (dataContract is XmlDataContractEx xmlDataContract) |
| | | 95 | | { |
| | 0 | 96 | | AddXmlDataContract(xmlDataContract); |
| | | 97 | | } |
| | | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | private void AddClassDataContract(ClassDataContractEx classDataContract) |
| | | 102 | | { |
| | 0 | 103 | | if (classDataContract.BaseContract != null) |
| | | 104 | | { |
| | 0 | 105 | | Add(classDataContract.BaseContract.StableName, classDataContract.BaseContract); |
| | | 106 | | } |
| | 0 | 107 | | if (!classDataContract.IsISerializable) |
| | | 108 | | { |
| | 0 | 109 | | if (classDataContract.Members != null) |
| | | 110 | | { |
| | 0 | 111 | | for (int i = 0; i < classDataContract.Members.Count; i++) |
| | | 112 | | { |
| | 0 | 113 | | DataMemberEx dataMember = classDataContract.Members[i]; |
| | 0 | 114 | | DataContractEx memberDataContract = GetMemberTypeDataContract(dataMember); |
| | | 115 | | //if (dataContractSurrogate != null && dataMember.MemberInfo != null) |
| | | 116 | | //{ |
| | | 117 | | // object customData = DataContractSurrogateCaller.GetCustomDataToExport( |
| | | 118 | | // dataContractSurrogate, |
| | | 119 | | // dataMember.MemberInfo, |
| | | 120 | | // memberDataContract.UnderlyingType); |
| | | 121 | | // if (customData != null) |
| | | 122 | | // SurrogateDataTable.Add(dataMember, customData); |
| | | 123 | | //} |
| | 0 | 124 | | Add(memberDataContract.StableName, memberDataContract); |
| | | 125 | | } |
| | | 126 | | } |
| | | 127 | | } |
| | 0 | 128 | | AddKnownDataContracts(classDataContract.KnownDataContracts); |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | private void AddCollectionDataContract(CollectionDataContractEx collectionDataContract) |
| | | 132 | | { |
| | 0 | 133 | | if (collectionDataContract.IsDictionary) |
| | | 134 | | { |
| | 0 | 135 | | ClassDataContractEx keyValueContract = collectionDataContract.ItemContract as ClassDataContractEx; |
| | 0 | 136 | | AddClassDataContract(keyValueContract); |
| | | 137 | | } |
| | | 138 | | else |
| | | 139 | | { |
| | 0 | 140 | | DataContractEx itemContract = GetItemTypeDataContract(collectionDataContract); |
| | 0 | 141 | | if (itemContract != null) |
| | 0 | 142 | | Add(itemContract.StableName, itemContract); |
| | | 143 | | } |
| | 0 | 144 | | AddKnownDataContracts(collectionDataContract.KnownDataContracts); |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | private void AddXmlDataContract(XmlDataContractEx xmlDataContract) |
| | | 148 | | { |
| | 0 | 149 | | AddKnownDataContracts(xmlDataContract.KnownDataContracts); |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | private void AddKnownDataContracts(IDictionary knownDataContracts) |
| | | 153 | | { |
| | 0 | 154 | | if (knownDataContracts != null) |
| | | 155 | | { |
| | 0 | 156 | | foreach (object knownDataContract in knownDataContracts.Values) |
| | | 157 | | { |
| | 0 | 158 | | var dataContract = DataContractEx.Wrap(knownDataContract); |
| | | 159 | | // Workaround for DataContract adding an extra schema entry for KeyValue<K,V>. See GitHub |
| | | 160 | | // issue https://github.com/dotnet/runtime/issues/67949 for details. |
| | 0 | 161 | | if (_removeKeyValuePairFromWsdl && IsKeyValuePair(dataContract)) |
| | | 162 | | continue; |
| | | 163 | | |
| | 0 | 164 | | Add(dataContract); |
| | | 165 | | } |
| | | 166 | | } |
| | 0 | 167 | | } |
| | | 168 | | |
| | | 169 | | private bool IsKeyValuePair(DataContractEx dataContract) |
| | | 170 | | { |
| | 0 | 171 | | var stableName = dataContract.StableName; |
| | 0 | 172 | | return stableName.Namespace == "http://schemas.datacontract.org/2004/07/System.Collections.Generic" |
| | 0 | 173 | | && stableName.Name.StartsWith("KeyValuePairOf"); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | internal DataContractEx GetMemberTypeDataContract(DataMemberEx dataMember) |
| | | 177 | | { |
| | 0 | 178 | | if (dataMember.MemberInfo != null) |
| | | 179 | | { |
| | 0 | 180 | | Type dataMemberType = dataMember.MemberType; |
| | 0 | 181 | | if (dataMember.IsGetOnlyCollection) |
| | | 182 | | { |
| | | 183 | | //if (dataContractSurrogate != null) |
| | | 184 | | //{ |
| | | 185 | | // Type dcType = DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, dataMembe |
| | | 186 | | // if (dcType != dataMemberType) |
| | | 187 | | // { |
| | | 188 | | // throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new |
| | | 189 | | // DataContract.GetClrTypeFullName(dataMemberType), DataContract.GetClrTypeFullName(dataM |
| | | 190 | | // } |
| | | 191 | | //} |
| | 0 | 192 | | return DataContractEx.GetGetOnlyCollectionDataContract(DataContractEx.GetId(dataMemberType.TypeHandl |
| | | 193 | | } |
| | | 194 | | else |
| | | 195 | | { |
| | 0 | 196 | | return DataContractEx.GetDataContract(dataMemberType); |
| | | 197 | | } |
| | | 198 | | } |
| | 0 | 199 | | return dataMember.MemberTypeContract; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | internal DataContractEx GetItemTypeDataContract(CollectionDataContractEx collectionContract) |
| | | 203 | | { |
| | 0 | 204 | | if (collectionContract.ItemType != null) |
| | 0 | 205 | | return DataContractEx.GetDataContract(collectionContract.ItemType); |
| | 0 | 206 | | return collectionContract.ItemContract; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | internal void FixupDataContracts() |
| | | 210 | | { |
| | | 211 | | // This fixes the Enum data contract to have an underlying type |
| | | 212 | | // and for collections of KeyValuePairAdapter to have IsValueType set to true |
| | | 213 | | // and the key and value members to be required. This is done by ensuring all |
| | | 214 | | // types get wrapped at least once. The wrapping constructor fixes up the |
| | | 215 | | // necessary data. |
| | 0 | 216 | | foreach(object contractObj in Contracts.Values) |
| | | 217 | | { |
| | 0 | 218 | | DataContractEx.Wrap(contractObj); |
| | | 219 | | } |
| | 0 | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | private static Type s_dataContractSetType = typeof(DataContractSerializer).Assembly.GetType("System.Runtime.Seri |
| | 0 | 223 | | private static Func<object, IDictionary> s_getContracts = ReflectionHelper.GetPropertyDelegate<IDictionary>(s_da |
| | 0 | 224 | | private static Func<object, IDictionary> s_getProcessedContracts = ReflectionHelper.GetPropertyDelegate<IDiction |
| | | 225 | | } |
| | | 226 | | } |