public void ShouldBeAbleToSetThePropertySerializationInfo()
 {
     ODataProperty property = new ODataProperty();
     ODataPropertySerializationInfo serializationInfo = new ODataPropertySerializationInfo();
     property.SetSerializationInfo(serializationInfo);
     property.SerializationInfo.Should().BeSameAs(serializationInfo);
 }
 public void ShouldBeAbleToClearThePropertySerializationInfo()
 {
     ODataProperty property = new ODataProperty();
     ODataPropertySerializationInfo serializationInfo = new ODataPropertySerializationInfo();
     property.SerializationInfo = serializationInfo;
     property.SetSerializationInfo(null);
     property.SerializationInfo.Should().BeNull();
 }
Esempio n. 3
0
        public static ODataEntry CreateEmployeeEntryNoMetadata(bool hasModel)
        {
            var employeeEntry = new ODataEntry()
            {
                TypeName = NameSpace + "Employee"
            };
            var employeeEntryP1 = new ODataProperty { Name = "PersonId", Value = -3 };
            var employeeEntryP2 = new ODataProperty
            {
                Name = "Name",
                Value = "ybqmssrdtjßcbhhmfxvhoxlssekuuibnmltiahdssxnpktmtorxfmeßbbujc"
            };
            var employeeEntryP3 = new ODataProperty { Name = "ManagersPersonId", Value = -465010984 };
            var employeeEntryP4 = new ODataProperty { Name = "Salary", Value = 0 };
            var employeeEntryP5 = new ODataProperty
            {
                Name = "Title",
                Value = "ミソまグたя縷ヲ弌ダゼ亜ゼをんゾ裹亜マゾダんタァハそポ縷ぁボグ黑珱ぁяポグソひゾひЯグポグボ欲を亜"
            };

            employeeEntry.Properties = new[] { employeeEntryP1, employeeEntryP2, employeeEntryP3, employeeEntryP4, employeeEntryP5 };
            if (!hasModel)
            {
                employeeEntryP1.SetSerializationInfo(new ODataPropertySerializationInfo()
                {
                    PropertyKind = ODataPropertyKind.Key
                });
            }

            return employeeEntry;
        }
Esempio n. 4
0
        public static ODataEntry CreatePersonEntryNoMetadata(bool hasModel)
        {
            var personEntry = new ODataEntry()
            {
                TypeName = NameSpace + "Person"
            };

            var personEntryP1 = new ODataProperty { Name = "PersonId", Value = -5 };
            var personEntryP2 = new ODataProperty
            {
                Name = "Name",
                Value = "xhsdckkeqzvlnprheujeycqrglfehtdocildrequohlffazfgtvmddyqsaxrojqxrsckohrakdxlrghgmzqnyruzu"
            };

            personEntry.Properties = new[] { personEntryP1, personEntryP2 };
            if (!hasModel)
            {
                personEntryP1.SetSerializationInfo(new ODataPropertySerializationInfo()
                {
                    PropertyKind = ODataPropertyKind.Key
                });
            }

            return personEntry;
        }
Esempio n. 5
0
        public static ODataEntry CreateCustomerEntryNoMetadata(bool hasModel)
        {
            var customerEntry = new ODataEntry()
            {
                TypeName = NameSpace + "Customer"
            };

            var customerEntryP1 = new ODataProperty { Name = "CustomerId", Value = -9 };
            var customerEntryP2 = new ODataProperty { Name = "Name", Value = "CustomerName" };
            var customerEntryP3 = new ODataProperty
            {
                Name = "PrimaryContactInfo",
                Value = CreatePrimaryContactODataComplexValue()
            };
            var customerEntryP4 = new ODataProperty
            {
                Name = "BackupContactInfo",
                Value = CreateBackupContactODataCollectionValue()
            };
            var customerEntryP5 = new ODataProperty
            {
                Name = "Auditing",
                Value = CreateAuditInforODataComplexValue()
            };

            customerEntry.Properties = new[]
                {
                    customerEntryP1, customerEntryP2, customerEntryP3, customerEntryP4, customerEntryP5,
                };

            if (!hasModel)
            {
                customerEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceName = "Customer", NavigationSourceEntityTypeName = NameSpace + "Customer" });
                customerEntryP1.SetSerializationInfo(new ODataPropertySerializationInfo()
                {
                    PropertyKind = ODataPropertyKind.Key
                });
            }

            return customerEntry;
        }
Esempio n. 6
0
        public static ODataEntry CreateOrderEntry2NoMetadata(bool hasModel)
        {
            var orderEntry2 = new ODataEntry()
            {
                TypeName = NameSpace + "Order"
            };

            var orderEntry2P1 = new ODataProperty { Name = "OrderId", Value = -9 };
            var orderEntry2P2 = new ODataProperty { Name = "CustomerId", Value = 78 };
            var orderEntry2P3 = new ODataProperty
            {
                Name = "Concurrency",
                Value = new ODataComplexValue()
                {
                    TypeName = NameSpace + "ConcurrencyInfo",
                    Properties = new[]
                                {
                                    new ODataProperty
                                        {
                                            Name = "Token",
                                            Value = "muunxfmcubaihvgnzoojgecdztyipapnxahnuibukrveamumfuokuvbly"
                                        },
                                    new ODataProperty
                                        {
                                            Name = "QueriedDateTime",
                                            Value = new DateTimeOffset(new DateTime(634646431705072026, System.DateTimeKind.Utc))
                                        }
                                }
                }
            };
            orderEntry2.Properties = new[] { orderEntry2P1, orderEntry2P2, orderEntry2P3 };
            if (!hasModel)
            {
                orderEntry2P1.SetSerializationInfo(new ODataPropertySerializationInfo()
                {
                    PropertyKind = ODataPropertyKind.Key
                });
            }

            return orderEntry2;
        }
        private string SerializeEntryInFullMetadataJson(
            bool? useKeyAsSegment, 
            IEdmModel edmModel, 
            IEdmEntityType entityType = null, 
            IEdmEntitySet entitySet = null)
        {
            var settings = new ODataMessageWriterSettings
            {
                AutoComputePayloadMetadataInJson = true,
                UseKeyAsSegment = useKeyAsSegment,
            };

            settings.SetServiceDocumentUri(new Uri("http://example.com/"));

            var outputStream = new MemoryStream();
            var responseMessage = new InMemoryMessage {Stream = outputStream};
            responseMessage.SetHeader("Content-Type", "application/json;odata.metadata=full");
            string output;

            using(var messageWriter = new ODataMessageWriter((IODataResponseMessage)responseMessage, settings, edmModel))
            {
                var entryWriter = messageWriter.CreateODataEntryWriter(entitySet, entityType);
                ODataProperty keyProperty = new ODataProperty() {Name = "Key", Value = "KeyValue"};

                var entry = new ODataEntry {Properties = new[] {keyProperty}, TypeName = "Namespace.Person"};

                if (edmModel == null)
                {
                    keyProperty.SetSerializationInfo(new ODataPropertySerializationInfo
                    {
                        PropertyKind = ODataPropertyKind.Key
                    });

                    entry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo
                    {
                        NavigationSourceEntityTypeName = "Namespace.Person",
                        NavigationSourceName = "People",
                        ExpectedTypeName = "Namespace.Person"
                    });
                }

                entryWriter.WriteStart(entry);
                entryWriter.WriteEnd();
                entryWriter.Flush();

                outputStream.Seek(0, SeekOrigin.Begin);
                output = new StreamReader(outputStream).ReadToEnd();
            }

            return output;
        }
        public void ShouldBeAbleToWriteFeedAndEntryResponseInJsonLightWithoutModel()
        {
            const string expectedPayload =
                "{" +
                    "\"@odata.context\":\"http://www.example.com/$metadata#Customers/NS.VIPCustomer\"," +
                    "\"value\":[" +
                        "{" +
                            "\"Name\":\"Bob\"," +
                            "\"[email protected]\":\"MostRecentOrder\"," +
                            "\"MostRecentOrder\":{\"OrderId\":101}" +
                        "}" +
                    "]" +
                "}";

            var writerSettings = new ODataMessageWriterSettings { DisableMessageStreamDisposal = true };
            writerSettings.SetContentType(ODataFormat.Json);
            writerSettings.ODataUri = new ODataUri() { ServiceRoot = new Uri("http://www.example.com") };

            MemoryStream stream = new MemoryStream();
            IODataResponseMessage responseMessage = new InMemoryMessage { StatusCode = 200, Stream = stream };

            // Write payload
            using (var messageWriter = new ODataMessageWriter(responseMessage, writerSettings))
            {
                var odataWriter = messageWriter.CreateODataFeedWriter();

                // Create customers feed with serializtion info to write odata.metadata.
                var customersFeed = new ODataFeed();
                customersFeed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceName = "Customers", NavigationSourceEntityTypeName = "NS.Customer", ExpectedTypeName = "NS.VIPCustomer" });

                // Write customers feed.
                odataWriter.WriteStart(customersFeed);

                // Write VIP customer
                {
                    // Create VIP customer, don't need to pass in serialization info since the writer knows the context from the feed scope.
                    var vipCustomer = new ODataEntry { TypeName = "NS.VIPCustomer" };

                    var customerKey = new ODataProperty { Name = "Name", Value = "Bob" };

                    // Provide serialization info at the property level to compute the edit link.
                    customerKey.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Key });
                    vipCustomer.Properties = new[] { customerKey };

                    // Write customer entry.
                    odataWriter.WriteStart(vipCustomer);

                    // Write expanded most recent order
                    {
                        // No API to set serialization info on ODataNavigationLink since what we are adding on ODataFeed and ODataEntry should be sufficient for the 5.5 release.
                        var navigationLink = new ODataNavigationLink { Name = "MostRecentOrder", IsCollection = false, Url = new Uri("MostRecentOrder", UriKind.Relative) };
                        odataWriter.WriteStart(navigationLink);

                        // Write the most recent customer.
                        {
                            var mostRecentOrder = new ODataEntry { TypeName = "NS.Order" };

                            // Add serialization info so we can computer links.
                            mostRecentOrder.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceName = "Orders", NavigationSourceEntityTypeName = "NS.Order", ExpectedTypeName = "NS.Order" });

                            var orderKey = new ODataProperty { Name = "OrderId", Value = 101 };

                            // Provide serialization info at the property level to compute the edit link.
                            orderKey.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Key });
                            mostRecentOrder.Properties = new[] { orderKey };

                            // Write order entry.
                            odataWriter.WriteStart(mostRecentOrder);
                            odataWriter.WriteEnd();
                        }

                        // End navigationLink.
                        odataWriter.WriteEnd();
                    }

                    // End customer entry.
                    odataWriter.WriteEnd();
                }

                // End customers feed.
                odataWriter.WriteEnd();
            }

            stream.Position = 0;
            string payload = (new StreamReader(stream)).ReadToEnd();
            payload.Should().Be(expectedPayload);
        }
 public void ShouldWriteOpenComplexPropertyForResponseEntryPayloadWithoutUserModel()
 {
     var complexProperty = new ODataProperty { Name = "OpenComplexProperty", Value = new ODataComplexValue { TypeName = "NS.MyDerivedEntityType", Properties = new[] { new ODataProperty { Name = "ComplexProperty", Value = new ODataComplexValue() } } } };
     complexProperty.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Open });
     var entry = new ODataEntry { TypeName = "NS.MyDerivedEntityType", Properties = new[] { complexProperty } };
     const string expectedPayload = "{\"@odata.context\":\"http://odata.org/test/$metadata#MySet/$entity\",\"@odata.type\":\"#NS.MyDerivedEntityType\",\"OpenComplexProperty\":{\"@odata.type\":\"NS.MyComplexType\",\"ComplexProperty\":{}}}";
     this.WriteNestedItemsAndValidatePayload(entitySetFullName: "MySet", derivedEntityTypeFullName: null, nestedItemToWrite: new[] { entry }, expectedPayload: expectedPayload, writingResponse: true);
 }
 public void ShouldWriteOpenPrimitivePropertyWithoutTypeNameForRequestEntryPayloadWithoutUserModel()
 {
     var property = new ODataProperty { Name = "OpenStringProperty", Value = new ODataPrimitiveValue(String.Empty) };
     property.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Open });
     var entry = new ODataEntry { TypeName = "NS.MyDerivedEntityType", Properties = new[] { property } };
     const string expectedPayload = "{\"OpenStringProperty\":\"\"}";
     this.WriteNestedItemsAndValidatePayload(entitySetFullName: "MySet", derivedEntityTypeFullName: "NS.MyDerivedEntityType", nestedItemToWrite: new[] { entry }, expectedPayload: expectedPayload, writingResponse: false, setMetadataDocumentUri: false);
 }
 public void ShouldWriteOpenPrimitivePropertyWithoutTypeNameForResponseEntryPayloadWithoutUserModel()
 {
     var property = new ODataProperty { Name = "OpenStringProperty", Value = new ODataPrimitiveValue(String.Empty + "K\uFFFF") };
     property.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Open });
     var entry = new ODataEntry { TypeName = "NS.MyDerivedEntityType", Properties = new[] { property } };
     const string expectedPayload = "{\"@odata.context\":\"http://odata.org/test/$metadata#MySet/NS.MyDerivedEntityType/$entity\",\"OpenStringProperty\":\"K\\uffff\"}";
     this.WriteNestedItemsAndValidatePayload(entitySetFullName: "MySet", derivedEntityTypeFullName: "NS.MyDerivedEntityType", nestedItemToWrite: new[] { entry }, expectedPayload: expectedPayload, writingResponse: true);
 }
 public void WritePrimitivePropertyWithByteWithoutUserModel()
 {
     var property = new ODataProperty { Name = "ByteProperty", Value = new ODataPrimitiveValue(Byte.MaxValue) };
     property.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Open });
     var entry = new ODataEntry { TypeName = "NS.MyDerivedEntityType", Properties = new[] { property } };
     const string expectedPayload = "{\"@odata.context\":\"http://odata.org/test/$metadata#MySet/$entity\",\"@odata.type\":\"#NS.MyDerivedEntityType\",\"[email protected]\":\"#Byte\",\"ByteProperty\":255}";
     this.WriteNestedItemsAndValidatePayload(entitySetFullName: "MySet", derivedEntityTypeFullName: null, nestedItemToWrite: new[] { entry }, expectedPayload: expectedPayload, writingResponse: true);
 }
Esempio n. 13
0
        /// <summary>
        /// Returns the instance of ODataProperty with the given name and property value.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyValue">Value of the property.</param>
        /// <returns>An instance of ODataProperty for the given property.</returns>
        private ODataProperty GetODataPropertyForOpenProperty(string propertyName, object propertyValue)
        {
            Debug.Assert(!string.IsNullOrEmpty(propertyName), "!string.IsNullOrEmpty(propertyName)");

            ResourceType propertyResourceType;
            if (WebUtil.IsNullValue(propertyValue))
            {
                propertyResourceType = ResourceType.PrimitiveStringResourceType;
            }
            else
            {
                propertyResourceType = WebUtil.GetResourceType(this.Provider, propertyValue);

                // A null ResourceType indicates a blacklisted type (eg, IntPtr or DateTimeOffset). So fail on it.
                if (propertyResourceType == null)
                {
                    throw new DataServiceException(500, Microsoft.OData.Service.Strings.Syndication_InvalidOpenPropertyType(propertyName));
                }
            }

            ODataProperty odataProperty = new ODataProperty
            {
                Name = propertyName,
                Value = this.GetPropertyValue(propertyName, propertyResourceType, propertyValue, true /*openProperty*/)
            };

            odataProperty.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Open });
            return odataProperty;
        }
Esempio n. 14
0
        /// <summary>Gets ODataProperty for the given <paramref name="property"/>.</summary>
        /// <param name="entityToSerialize">Entity that is currently being serialized.</param>
        /// <param name="property">ResourceProperty instance in question.</param>
        /// <returns>A instance of ODataProperty for the given <paramref name="property"/>.</returns>
        private ODataProperty GetODataPropertyForEntityProperty(EntityToSerialize entityToSerialize, ResourceProperty property)
        {
            Debug.Assert(entityToSerialize != null, "entityToSerialize != null");
            Debug.Assert(property != null && entityToSerialize.ResourceType.Properties.Contains(property), "property != null && currentResourceType.Properties.Contains(property)");

            ODataValue odataPropertyValue;
            if (property.IsOfKind(ResourcePropertyKind.Stream))
            {
                odataPropertyValue = this.GetNamedStreamPropertyValue(entityToSerialize, property);
            }
            else
            {
                object propertyValue = WebUtil.GetPropertyValue(this.Provider, entityToSerialize.Entity, entityToSerialize.ResourceType, property, null);
                odataPropertyValue = this.GetPropertyValue(property.Name, property.ResourceType, propertyValue, false /*openProperty*/);
            }

            ODataProperty odataProperty = new ODataProperty()
            {
                Name = property.Name,
                Value = odataPropertyValue
            };

            ODataPropertyKind kind = property.IsOfKind(ResourcePropertyKind.Key) ? ODataPropertyKind.Key : property.IsOfKind(ResourcePropertyKind.ETag) ? ODataPropertyKind.ETag : ODataPropertyKind.Unspecified;
            if (kind != ODataPropertyKind.Unspecified)
            {
                odataProperty.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = kind });
            }

            return odataProperty;
        }
Esempio n. 15
0
        public static ODataEntry CreateSpecialEmployeeEntryNoMetadata(bool hasModel)
        {
            var employeeEntry = new ODataEntry()
            {
                TypeName = NameSpace + "SpecialEmployee"
            };
            var employeeEntryP1 = new ODataProperty { Name = "PersonId", Value = -10 };
            var employeeEntryP2 = new ODataProperty { Name = "Name", Value = "a special employee" };
            var employeeEntryP3 = new ODataProperty { Name = "ManagersPersonId", Value = 47 };
            var employeeEntryP4 = new ODataProperty { Name = "Salary", Value = 4091 };
            var employeeEntryP5 = new ODataProperty { Name = "Title", Value = "a special title" };
            var employeeEntryP6 = new ODataProperty { Name = "CarsVIN", Value = -1911530027 };
            var employeeEntryP7 = new ODataProperty { Name = "Bonus", Value = -37730565 };
            var employeeEntryP8 = new ODataProperty { Name = "IsFullyVested", Value = false };
            employeeEntry.Properties = new[] { employeeEntryP1, employeeEntryP2, employeeEntryP3, employeeEntryP4, employeeEntryP5, employeeEntryP6, employeeEntryP7, employeeEntryP8 };
            if (!hasModel)
            {
                employeeEntryP1.SetSerializationInfo(new ODataPropertySerializationInfo()
                {
                    PropertyKind = ODataPropertyKind.Key
                });
            }

            return employeeEntry;
        }
Esempio n. 16
0
        public static ODataEntry CreateCarEntryNoMetadata(bool hasModel)
        {
            var carEntry = new ODataEntry()
            {
                TypeName = NameSpace + "Car",
            };

            // properties and named streams
            var carEntryP1 = new ODataProperty { Name = "VIN", Value = 11 };
            var carEntryP2 = new ODataProperty
            {
                Name = "Description",
                Value = "cenbviijieljtrtdslbuiqubcvhxhzenidqdnaopplvlqc"
            };

            carEntry.Properties = new[] { carEntryP1, carEntryP2 };

            carEntry.InstanceAnnotations.Add(new ODataInstanceAnnotation("carEntry.AnnotationName",
                                                                         new ODataPrimitiveValue(
                                                                             "carEntryAnnotationValue")));

            if (!hasModel)
            {
                carEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceName = "Car", NavigationSourceEntityTypeName = NameSpace + "Car" });
                carEntryP1.SetSerializationInfo(new ODataPropertySerializationInfo()
                {
                    PropertyKind = ODataPropertyKind.Key
                });
            }

            return carEntry;

        }
 public void ShouldWriteOpenPrimitiveCollectionPropertyForRequestEntryPayloadWithoutUserModel()
 {
     var property = new ODataProperty { Name = "OpenPrimitiveCollectionProperty", Value = new ODataCollectionValue { Items = new[] { "string1", "string2" } } };
     property.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Open });
     var entry = new ODataEntry { TypeName = "NS.MyDerivedEntityType", Properties = new[] { property } };
     const string expectedPayload = "{\"@odata.type\":\"#NS.MyDerivedEntityType\",\"OpenPrimitiveCollectionProperty\":[\"string1\",\"string2\"]}";
     this.WriteNestedItemsAndValidatePayload(entitySetFullName: null, derivedEntityTypeFullName: null, nestedItemToWrite: new[] { entry }, expectedPayload: expectedPayload, writingResponse: false);
 }
        private string WriteAndVerifyRequestMessage(ODataMessageWriterSettings settings, string mimeType, bool hasModel)
        {
            // create an order entry to POST
            var order = new ODataEntry()
            {
                TypeName = NameSpace + "Order"
            };

            var orderP1 = new ODataProperty { Name = "OrderId", Value = -10 };
            var orderp2 = new ODataProperty { Name = "CustomerId", Value = 8212 };
            var orderp3 = new ODataProperty { Name = "Concurrency", Value = null };
            order.Properties = new[] { orderP1, orderp2, orderp3 };
            if (!hasModel)
            {
                order.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceName = "Order", NavigationSourceEntityTypeName = NameSpace + "Order" });
                orderP1.SetSerializationInfo(new ODataPropertySerializationInfo() { PropertyKind = ODataPropertyKind.Key });
            }

            Dictionary<string, object> expectedOrderObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.OrderType, "Order(-10)", order, hasModel);

            // write the request message and read using ODL reader
            var requestMessage = new StreamRequestMessage(new MemoryStream(), new Uri(this.ServiceUri + "Order"), "POST");
            requestMessage.SetHeader("Content-Type", mimeType);
            string result = string.Empty;
            using (var messageWriter = this.CreateODataMessageWriter(requestMessage, settings, hasModel))
            {
                var odataWriter = this.CreateODataEntryWriter(messageWriter, WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType, hasModel);
                odataWriter.WriteStart(order);
                odataWriter.WriteEnd();

                Stream stream = requestMessage.GetStream();
                result = WritePayloadHelper.ReadStreamContent(stream);
                if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    var readerSetting = new ODataMessageReaderSettings() { BaseUri = this.ServiceUri };
                    ODataMessageReader messageReader = new ODataMessageReader(requestMessage, readerSetting, WritePayloadHelper.Model);
                    ODataReader reader = messageReader.CreateODataEntryReader(WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType);
                    bool verifyEntryCalled = false;
                    while (reader.Read())
                    {
                        if (reader.State == ODataReaderState.EntryEnd)
                        {
                            ODataEntry entry = reader.Item as ODataEntry;
                            Assert.AreEqual(3, entry.Properties.Count(), "entry.Properties.Count");
                            verifyEntryCalled = true;
                        }
                    }

                    Assert.AreEqual(ODataReaderState.Completed, reader.State);
                    Assert.IsTrue(verifyEntryCalled, "verifyEntryCalled");
                }
            }

            // For Json light, verify the resulting metadata is as expected
            if (mimeType != MimeTypes.ApplicationAtomXml)
            {
                JavaScriptSerializer jScriptSerializer = new JavaScriptSerializer();
                Dictionary<string, object> resultObject = jScriptSerializer.DeserializeObject(result) as Dictionary<string, object>;

                // AutoComputePayloadMetadataInJson has no effect on request message metadata
                Assert.AreEqual(this.ServiceUri + "$metadata#Order/$entity", resultObject.Single(e => e.Key == JsonLightConstants.ODataContextAnnotationName).Value);
                resultObject.Remove(JsonLightConstants.ODataContextAnnotationName);

                foreach (var pair in resultObject)
                {
                    Assert.IsFalse(pair.Key.Contains("odata.") || pair.Key.StartsWith("#"));
                }
            }

            return result;
        }
 public void ShouldWriteOpenComplexCollectionPropertyForRequestEntryPayloadWithoutUserModel()
 {
     var property = new ODataProperty { Name = "OpenComplexCollectionProperty", Value = new ODataCollectionValue { TypeName = "Collection(NS.MyComplexType)", Items = new[] { new ODataComplexValue { Properties = new[] { new ODataProperty { Name = "ComplexProperty", Value = new ODataComplexValue() } } } } } };
     property.SetSerializationInfo(new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Open });
     ODataEntry entry = new ODataEntry { TypeName = "NS.MyDerivedEntityType", Properties = new[] { property } };
     const string expectedPayload = "{\"@odata.type\":\"#NS.MyDerivedEntityType\",\"[email protected]\":\"#Collection(NS.MyComplexType)\",\"OpenComplexCollectionProperty\":[{\"ComplexProperty\":{}}]}";
     this.WriteNestedItemsAndValidatePayload(entitySetFullName: null, derivedEntityTypeFullName: null, nestedItemToWrite: new[] { entry }, expectedPayload: expectedPayload, writingResponse: false);
 }
Esempio n. 20
0
        public static ODataEntry CreateOrderEntry1NoMetadata(bool hasModel)
        {
            var orderEntry1 = new ODataEntry()
            {
                TypeName = NameSpace + "Order", 
            };

            var orderEntry1P1 = new ODataProperty { Name = "OrderId", Value = -10 };
            var orderEntry1P2 = new ODataProperty { Name = "CustomerId", Value = 8212 };
            var orderEntry1P3 = new ODataProperty { Name = "Concurrency", Value = null };
            orderEntry1.Properties = new[] { orderEntry1P1, orderEntry1P2, orderEntry1P3 };
            if (!hasModel)
            {
                orderEntry1P1.SetSerializationInfo(new ODataPropertySerializationInfo()
                {
                    PropertyKind = ODataPropertyKind.Key
                });
            }

            return orderEntry1;
        }
        private string WriteAndVerifyRequestMessage(StreamRequestMessage requestMessageWithoutModel,
                                                    ODataWriter odataWriter, bool hasModel, string mimeType)
        {
            var order = new ODataEntry()
                {
                    Id = new Uri(this.ServiceUri + "Order(-10)"),
                    TypeName = NameSpace + "Order"
                };

            var orderP1 = new ODataProperty { Name = "OrderId", Value = -10 };
            var orderp2 = new ODataProperty { Name = "CustomerId", Value = 8212 };
            var orderp3 = new ODataProperty { Name = "Concurrency", Value = null };
            order.Properties = new[] { orderP1, orderp2, orderp3 };
            if (!hasModel)
            {
                order.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceName = "Order", NavigationSourceEntityTypeName = NameSpace + "Order" });
                orderP1.SetSerializationInfo(new ODataPropertySerializationInfo() { PropertyKind = ODataPropertyKind.Key });
            }

            odataWriter.WriteStart(order);
            odataWriter.WriteEnd();

            Stream stream = requestMessageWithoutModel.GetStream();
            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                var settings = new ODataMessageReaderSettings() { BaseUri = this.ServiceUri };
                ODataMessageReader messageReader = new ODataMessageReader(requestMessageWithoutModel, settings,
                                                                          WritePayloadHelper.Model);
                ODataReader reader = messageReader.CreateODataEntryReader(WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType);
                bool verifyEntryCalled = false;
                while (reader.Read())
                {
                    if (reader.State == ODataReaderState.EntryEnd)
                    {
                        ODataEntry entry = reader.Item as ODataEntry;
                        Assert.IsTrue(entry.Id.ToString().Contains("Order(-10)"), "entry.Id");
                        Assert.AreEqual(3, entry.Properties.Count(), "entry.Properties.Count");
                        verifyEntryCalled = true;
                    }
                }

                Assert.AreEqual(ODataReaderState.Completed, reader.State);
                Assert.IsTrue(verifyEntryCalled, "verifyEntryCalled");
            }

            return WritePayloadHelper.ReadStreamContent(stream);
        }