public async Task ComplexTypeSerializesAsOData() { // Arrange string routeName = "OData"; IEdmModel model = GetSampleModel(); var request = RequestFactory.Create("Get", "http://localhost/property", opt => opt.AddRouteComponents(routeName, model)); request.ODataFeature().Model = model; request.ODataFeature().RoutePrefix = routeName; var payload = new ODataPayloadKind[] { ODataPayloadKind.Resource }; var formatter = ODataFormatterHelpers.GetOutputFormatter(payload); Address address = new Address { Street = "abc", City = "efg", State = "opq", ZipCode = "98029", CountryOrRegion = "Mars" }; var content = ODataFormatterHelpers.GetContent(address, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert string actual = await ODataFormatterHelpers.GetContentResult(content, request); Assert.Equal("{\"@odata.context\":\"http://localhost/OData/$metadata#Microsoft.AspNetCore.OData.Tests.Formatter.Models.Address\"," + "\"Street\":\"abc\"," + "\"City\":\"efg\"," + "\"State\":\"opq\"," + "\"ZipCode\":\"98029\"," + "\"CountryOrRegion\":\"Mars\"}", actual); }
public async Task EntityTypeSerializesAsODataEntry() { // Arrange const string routeName = "OData"; IEdmEntitySet entitySet = _model.EntityContainer.FindEntitySet("employees"); ODataPath path = new ODataPath(new EntitySetSegment(entitySet)); var request = RequestFactory.Create("Get", "http://localhost/property", opt => opt.AddModel(routeName, _model)); request.ODataFeature().Model = _model; request.ODataFeature().Path = path; request.ODataFeature().PrefixName = routeName; var payload = new ODataPayloadKind[] { ODataPayloadKind.Resource }; var formatter = ODataFormatterHelpers.GetOutputFormatter(payload); Employee employee = new Employee { EmployeeID = 8, Birthday = new System.DateTimeOffset(2020, 9, 10, 1, 2, 3, System.TimeSpan.Zero), EmployeeName = "Ssa", HomeAddress = null }; var content = ODataFormatterHelpers.GetContent(employee, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert string actual = await ODataFormatterHelpers.GetContentResult(content, request); Assert.Equal("{\"@odata.context\":\"http://localhost/OData/$metadata#employees/$entity\"," + "\"EmployeeID\":8," + "\"EmployeeName\":\"Ssa\"," + "\"BaseSalary\":0," + "\"Birthday\":\"2020-09-10T01:02:03Z\"," + "\"WorkCompanyId\":0," + "\"HomeAddress\":null" + "}", actual); }