示例#1
0
 /// <summary>
 /// Visits a primitive value.
 /// </summary>
 /// <param name="primitiveValue">The primitive value to visit.</param>
 protected override ODataPayloadElement VisitPrimitiveValue(object primitiveValue)
 {
     if (primitiveValue == null)
     {
         return(new PrimitiveValue(null, null));
     }
     else
     {
         return(new PrimitiveValue(EntityModelUtils.GetPrimitiveEdmType(primitiveValue.GetType()).FullEdmName(), primitiveValue));
     }
 }
示例#2
0
        public void SpatialPropertyWithDisabledPrimitiveTypeConversionTest()
        {
            IEdmModel testModel = TestModels.BuildTestModel();

            var testValues = new object[]
            {
                GeographyFactory.Point(10, 20).Build(),
                GeometryFactory.Point(10, 20).Build()
            };

            IEnumerable <PayloadReaderTestDescriptor> testDescriptors =
                TestValues.PrimitiveTypes.SelectMany(primitiveTypes => testValues.Select(testValue =>
            {
                PrimitiveDataType targetType       = EntityModelUtils.GetPrimitiveEdmType(primitiveTypes);
                ODataPayloadElement payloadElement = PayloadBuilder
                                                     .Property(null, PayloadBuilder.PrimitiveValue(testValue))
                                                     .ExpectedPropertyType(targetType);
                return(new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = payloadElement,
                    PayloadEdmModel = testModel
                });
            }));

            // TODO: Task 1429690:Fix places where we've lost JsonVerbose coverage to add JsonLight
            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                new bool[] { false, true },
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations.Where(tc => false),
                (testDescriptor, disableStrictValidation, testConfig) =>
            {
                testConfig = new ReaderTestConfiguration(testConfig);
                testConfig.MessageReaderSettings.EnablePrimitiveTypeConversion = false;
                if (disableStrictValidation)
                {
                    testConfig = testConfig.CloneAndApplyBehavior(TestODataBehaviorKind.WcfDataServicesServer);
                }
                testDescriptor.RunTest(testConfig);
            });
        }
示例#3
0
        public void PrimitiveTopLevelValueWithDisabledTypeConversionTest()
        {
            IEdmModel testModel = TestModels.BuildTestModel();

            IEnumerable <ReaderContentTypeTestDescriptor> testDescriptors = primitiveValueConversionTestCases
                                                                            .SelectMany(testCase => TestValues.PrimitiveTypes
                                                                                        .SelectMany(nonNullableTargetType => new bool[] { true, false }
                                                                                                    .SelectMany(includeNullableType => new bool[] { true, false }
                                                                                                                .Select(useExpectedType =>
            {
                PrimitiveDataType targetType = EntityModelUtils.GetPrimitiveEdmType(nonNullableTargetType);
                if (includeNullableType)
                {
                    targetType = targetType.Nullable();
                }

                ODataPayloadElement resultValue;
                if (nonNullableTargetType == typeof(byte[]))
                {
                    resultValue = testCase.ConversionValues.Where(cv => cv.ClrValue.GetType() == typeof(byte[])).Single().DeepCopy();
                }
                else
                {
                    resultValue = testCase.ConversionValues.Where(cv => cv.ClrValue.GetType() == typeof(string)).Single().DeepCopy();
                }

                ODataPayloadElement payloadElement;
                if (useExpectedType)
                {
                    payloadElement = PayloadBuilder.PrimitiveValue(testCase.SourceString).ExpectedPrimitiveValueType(targetType);
                }
                else
                {
                    payloadElement = PayloadBuilder.PrimitiveValue(testCase.SourceString);
                }

                return(new ReaderContentTypeTestDescriptor(this.Settings)
                {
                    PayloadElement = payloadElement,
                    ExpectedResultPayloadElement = (testConfig) => resultValue,
                    ContentType = ComputeContentType(nonNullableTargetType),
                    ExpectedFormat = ODataFormat.RawValue,
                });
            }))));

            // add variants that use a metadata provider
            testDescriptors = testDescriptors.Concat(testDescriptors.Select(td => new ReaderContentTypeTestDescriptor(td)
            {
                PayloadEdmModel = testModel
            }));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                // restricting the set of default format configurations to limiti runtime of the tests
                this.ReaderTestConfigurationProvider.DefaultFormatConfigurations.Where(tc => tc.MessageReaderSettings.EnableMessageStreamDisposal && !tc.IsRequest),
                (testDescriptor, testConfig) =>
            {
                testConfig = new ReaderTestConfiguration(testConfig);
                testConfig.MessageReaderSettings.EnablePrimitiveTypeConversion = false;

                testDescriptor.RunTest(testConfig);
            });
        }
示例#4
0
            /// <summary>
            /// Visits a collection item.
            /// </summary>
            /// <param name="collectionValue">The collection to visit.</param>
            protected override ODataPayloadElement VisitCollectionValue(ODataCollectionValue collectionValue)
            {
                if (collectionValue == null)
                {
                    return(new PrimitiveMultiValue(null, true));
                }

                bool?isPrimitiveCollection = null;

                // Try to parse the type name and if the item type name is a primitive EDM type, then this is a primitive collection.
                string typeName = collectionValue.TypeName;

                if (typeName != null)
                {
                    string itemTypeName = EntityModelUtils.GetCollectionItemTypeName(typeName);
                    isPrimitiveCollection = itemTypeName != null && EntityModelUtils.GetPrimitiveEdmType(itemTypeName) != null;
                }

                List <object> items = new List <object>();

                foreach (object item in collectionValue.Items)
                {
                    if (!isPrimitiveCollection.HasValue)
                    {
                        ODataComplexValue complexItemValue = item as ODataComplexValue;

                        // If the first item is a complex value, then the collection is of complex kind.
                        // Note that if the first item is null, we assume primitive collection, since we can't really decide (and it's an invalid thing anyway)
                        isPrimitiveCollection = complexItemValue == null;
                    }

                    if (isPrimitiveCollection.Value)
                    {
                        items.Add((PrimitiveValue)this.Visit(item));
                    }
                    else
                    {
                        ODataComplexValue complexItemValue = item as ODataComplexValue;
                        ExceptionUtilities.Assert(
                            item == null || complexItemValue != null,
                            "The collection was determined to be of complex values but one of its items is not an ODataComplexValue.");
                        items.Add((ComplexInstance)this.Visit(complexItemValue));
                    }
                }

                if (!isPrimitiveCollection.HasValue)
                {
                    // If we could not tell until now (possible only if there was no type name and no items)
                    // assume primitive collection.
                    isPrimitiveCollection = true;
                }

                if (isPrimitiveCollection == true)
                {
                    PrimitiveMultiValue primitiveCollection = new PrimitiveMultiValue(typeName, false);
                    foreach (object item in items)
                    {
                        primitiveCollection.Add((PrimitiveValue)item);
                    }

                    this.ConvertSerializationTypeNameAnnotation(collectionValue, primitiveCollection);

                    return(primitiveCollection);
                }
                else
                {
                    ComplexMultiValue complexCollection = new ComplexMultiValue(typeName, false);
                    foreach (object item in items)
                    {
                        complexCollection.Add((ComplexInstance)item);
                    }

                    this.ConvertSerializationTypeNameAnnotation(collectionValue, complexCollection);

                    return(complexCollection);
                }
            }