private IEnumerable <ODataProperty> CreatePropertyBag(object graph, ODataSerializerContext writeContext) { IEnumerable <IEdmStructuralProperty> selectProperties; if (writeContext.CurrentProjectionNode != null && writeContext.CurrentProjectionNode.Selects.Any()) { selectProperties = _edmEntityTypeReference .StructuralProperties() .Where(p => writeContext.CurrentProjectionNode.Selects.Any(node => node.Name == p.Name)); } else { selectProperties = _edmEntityTypeReference.StructuralProperties(); } List <ODataProperty> properties = new List <ODataProperty>(); foreach (IEdmStructuralProperty property in selectProperties) { ODataSerializer serializer = SerializerProvider.GetEdmTypeSerializer(property.Type); if (serializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, property.Type.FullName(), typeof(ODataMediaTypeFormatter).Name); } object propertyValue = graph.GetType().GetProperty(property.Name).GetValue(graph, index: null); properties.Add(serializer.CreateProperty(propertyValue, property.Name, writeContext)); } return(properties); }
public void ReturnsNullForNonPrimitiveTypes(Type type) { ODataSerializerProvider provider = new RawValueSerializerProvider(); ODataSerializer result = provider.GetODataPayloadSerializer(null, type); Assert.Null(result); }
public void GetODataSerializer_ReturnsSameSerializer_ForSameType() { ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(_edmModel); ODataSerializer firstCallSerializer = serializerProvider.GetODataPayloadSerializer(typeof(ODataEntityDeserializerTests.Supplier)); ODataSerializer secondCallSerializer = serializerProvider.GetODataPayloadSerializer(typeof(ODataEntityDeserializerTests.Supplier)); Assert.Same(firstCallSerializer, secondCallSerializer); }
public void ReturnsAnODataRawValueSerializerForPrimitiveTypes(Type type) { ODataSerializerProvider provider = new RawValueSerializerProvider(); ODataSerializer result = provider.GetODataPayloadSerializer(null, type); Assert.NotNull(result); Assert.IsType <ODataRawValueSerializer>(result); }
public void GetODataSerializer_Returns_ExpectedSerializerType(Type payloadType, Type expectedSerializerType) { ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataSerializer serializer = serializerProvider.GetODataPayloadSerializer(_edmModel, payloadType); Assert.NotNull(serializer); Assert.IsType(expectedSerializerType, serializer); }
public void GetODataSerializer_ODataError() { ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(_edmModel); ODataSerializer serializer = serializerProvider.GetODataPayloadSerializer(typeof(ODataError)); Assert.NotNull(serializer); Assert.Equal(typeof(ODataErrorSerializer), serializer.GetType()); Assert.Equal(ODataPayloadKind.Error, serializer.ODataPayloadKind); }
private void WriteFeed(object graph, ODataWriter writer, ODataSerializerContext writeContext) { ODataSerializer entrySerializer = SerializerProvider.GetEdmTypeSerializer(_edmCollectionType.ElementType()); if (entrySerializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, _edmCollectionType.ElementType(), typeof(ODataMediaTypeFormatter).Name); } Contract.Assert(entrySerializer.ODataPayloadKind == ODataPayloadKind.Entry); IEnumerable enumerable = graph as IEnumerable; // Data to serialize if (enumerable != null) { ODataFeed feed = new ODataFeed(); if (writeContext.EntitySet != null) { IEntitySetLinkBuilder linkBuilder = SerializerProvider.EdmModel.GetEntitySetLinkBuilder(writeContext.EntitySet); Uri feedSelfLink = linkBuilder.BuildFeedSelfLink(new FeedContext(writeContext.EntitySet, writeContext.UrlHelper, graph)); if (feedSelfLink != null) { feed.SetAnnotation(new AtomFeedMetadata() { SelfLink = new AtomLinkMetadata() { Relation = SelfLinkRelation, Href = feedSelfLink } }); } } // TODO: Bug 467590: remove the hardcoded feed id. Get support for it from the model builder ? feed.Id = FeedNamespace + _edmCollectionType.FullName(); // If we have more OData format specific information apply it now. ODataResult odataFeedAnnotations = graph as ODataResult; if (odataFeedAnnotations != null) { feed.Count = odataFeedAnnotations.Count; feed.NextPageLink = odataFeedAnnotations.NextPageLink; } writer.WriteStart(feed); foreach (object entry in enumerable) { entrySerializer.WriteObjectInline(entry, writer, writeContext); } writer.WriteEnd(); } }
private void WriteNavigationLinks(EntityInstanceContext context, ODataWriter writer, ODataSerializerWriteContext writeContext) { foreach (IEdmNavigationProperty navProperty in _edmEntityTypeReference.NavigationProperties()) { IEdmTypeReference propertyType = navProperty.Type; object propertyValue = context.EntityInstance.GetType().GetProperty(navProperty.Name).GetValue(context.EntityInstance, index: null); if (writeContext.EntitySet != null) { IEdmEntitySet currentEntitySet = writeContext.EntitySet.FindNavigationTarget(navProperty); IEntitySetLinkBuilder linkBuilder = SerializerProvider.EdmModel.GetEntitySetLinkBuilder(writeContext.EntitySet); ODataNavigationLink navigationLink = new ODataNavigationLink { IsCollection = propertyType.IsCollection(), Name = navProperty.Name, Url = linkBuilder.BuildNavigationLink(context, navProperty) }; ODataQueryProjectionNode expandNode = null; if (writeContext.CurrentProjectionNode != null) { expandNode = writeContext.CurrentProjectionNode.Expands.FirstOrDefault(p => p.Name == navProperty.Name); } if (expandNode != null && propertyValue != null) { writer.WriteStart(navigationLink); ODataSerializer serializer = SerializerProvider.GetEdmTypeSerializer(propertyType); if (serializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, navProperty.Type.FullName(), typeof(ODataMediaTypeFormatter).Name); } ODataSerializerWriteContext childWriteContext = new ODataSerializerWriteContext(writeContext.ResponseContext); childWriteContext.UrlHelper = writeContext.UrlHelper; childWriteContext.EntitySet = currentEntitySet; childWriteContext.RootProjectionNode = writeContext.RootProjectionNode; childWriteContext.CurrentProjectionNode = expandNode; serializer.WriteObjectInline(propertyValue, writer, childWriteContext); writer.WriteEnd(); } else if (!writeContext.IsRequest) { // delayed links cannot be written on requests writer.WriteStart(navigationLink); writer.WriteEnd(); } } } }
/// <summary> /// Sets the ODataSerializer for the given edmType in the serializer cache. /// </summary> /// <param name="edmType"></param> /// <param name="serializer"></param> public void SetEdmTypeSerializer(IEdmTypeReference edmType, ODataSerializer serializer) { if (edmType == null) { throw Error.ArgumentNull("edmType"); } if (serializer == null) { throw Error.ArgumentNull("serializer"); } _serializerCache.AddOrUpdate(edmType, serializer, (t, s) => serializer); }
public override ODataProperty CreateProperty(object graph, string elementName, ODataSerializerWriteContext writeContext) { if (String.IsNullOrWhiteSpace(elementName)) { throw Error.ArgumentNullOrEmpty("elementName"); } if (writeContext == null) { throw Error.ArgumentNull("writeContext"); } List <ODataProperty> propertyCollection = null; if (graph != null) { propertyCollection = new List <ODataProperty>(); foreach (IEdmProperty property in _edmComplexType.ComplexDefinition().Properties()) { IEdmTypeReference propertyType = property.Type; ODataSerializer propertySerializer = SerializerProvider.GetEdmTypeSerializer(propertyType); if (propertySerializer == null) { throw Error.NotSupported("Type {0} is not a serializable type", propertyType.FullName()); } // TODO 453795: [OData]Cleanup reflection code in the ODataFormatter. object propertyValue = graph.GetType().GetProperty(property.Name).GetValue(graph, index: null); propertyCollection.Add(propertySerializer.CreateProperty(propertyValue, property.Name, writeContext)); } } if (propertyCollection != null) { return(new ODataProperty() { Name = elementName, Value = new ODataComplexValue() { Properties = propertyCollection, TypeName = _edmComplexType.FullName() } }); } else { return(null); } }
/// <summary> /// Sets the ODataSerializer for the given edmType in the serializer cache. /// </summary> /// <param name="edmType"></param> /// <param name="serializer"></param> public void SetEdmTypeSerializer(IEdmTypeReference edmType, ODataSerializer serializer) { if (edmType == null) { throw Error.ArgumentNull("edmType"); } if (serializer == null) { throw Error.ArgumentNull("serializer"); } _serializerCache.AddOrUpdate(edmType, serializer, (t, s) => serializer); }
/// <inheritdoc/> public override ODataProperty CreateProperty(object graph, string elementName, ODataSerializerContext writeContext) { if (String.IsNullOrWhiteSpace(elementName)) { throw Error.ArgumentNullOrEmpty("elementName"); } if (writeContext == null) { throw Error.ArgumentNull("writeContext"); } IEnumerable enumerable = graph as IEnumerable; if (enumerable == null) { return(new ODataProperty() { Name = elementName, Value = null }); } else { ArrayList valueCollection = new ArrayList(); IEdmTypeReference itemType = _edmCollectionType.ElementType(); ODataSerializer itemSerializer = SerializerProvider.GetEdmTypeSerializer(itemType); if (itemSerializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, itemType.FullName(), typeof(ODataMediaTypeFormatter).Name); } foreach (object item in enumerable) { valueCollection.Add(itemSerializer.CreateProperty(item, ODataFormatterConstants.Element, writeContext).Value); } // ODataCollectionValue is only a V3 property, arrays inside Complex Types or Entity types are only supported in V3 // if a V1 or V2 Client requests a type that has a collection within it ODataLIb will throw. // Also, note that TypeName is an optional property for ODataCollectionValue return(new ODataProperty() { Name = elementName, Value = new ODataCollectionValue { Items = valueCollection } }); } }
private void WriteFeed(object graph, ODataWriter writer, ODataSerializerContext writeContext) { IEnumerable enumerable = graph as IEnumerable; // Data to serialize if (enumerable != null) { ODataFeed feed = new ODataFeed(); if (writeContext.EntitySet != null) { IEntitySetLinkBuilder linkBuilder = SerializerProvider.EdmModel.GetEntitySetLinkBuilder(writeContext.EntitySet); FeedContext feedContext = new FeedContext { EntitySet = writeContext.EntitySet, UrlHelper = writeContext.UrlHelper, PathHandler = writeContext.PathHandler, FeedInstance = graph }; Uri feedSelfLink = linkBuilder.BuildFeedSelfLink(feedContext); if (feedSelfLink != null) { feed.SetAnnotation(new AtomFeedMetadata() { SelfLink = new AtomLinkMetadata() { Relation = SelfLinkRelation, Href = feedSelfLink } }); } } // TODO: Bug 467590: remove the hardcoded feed id. Get support for it from the model builder ? feed.Id = FeedNamespace + _edmCollectionType.FullName(); // If we have more OData format specific information apply it now. ODataResult odataFeedAnnotations = graph as ODataResult; if (odataFeedAnnotations != null) { feed.Count = odataFeedAnnotations.Count; feed.NextPageLink = odataFeedAnnotations.NextPageLink; } else { object nextPageLinkPropertyValue; HttpRequestMessage request = writeContext.Request; if (request != null && request.Properties.TryGetValue(ODataQueryOptions.NextPageLinkPropertyKey, out nextPageLinkPropertyValue)) { Uri nextPageLink = nextPageLinkPropertyValue as Uri; if (nextPageLink != null) { feed.NextPageLink = nextPageLink; } } } writer.WriteStart(feed); foreach (object entry in enumerable) { if (entry == null) { throw Error.NotSupported(SRResources.NullElementInCollection); } ODataSerializer entrySerializer = SerializerProvider.GetODataPayloadSerializer(entry.GetType()); if (entrySerializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, entry.GetType(), typeof(ODataMediaTypeFormatter).Name); } Contract.Assert(entrySerializer.ODataPayloadKind == ODataPayloadKind.Entry); entrySerializer.WriteObjectInline(entry, writer, writeContext); } writer.WriteEnd(); } }
public FakeODataSerializerProvider(ODataSerializer serializer) { _serializer = serializer; }