public void CreateODataCollectionValue_CanSerialize_IEdmObjects() { // Arrange Mock <IEdmComplexObject> edmComplexObject = new Mock <IEdmComplexObject>(); IEdmComplexObject[] collection = new IEdmComplexObject[] { edmComplexObject.Object }; ODataSerializerContext serializerContext = new ODataSerializerContext(); IEdmComplexTypeReference elementType = new EdmComplexTypeReference(new EdmComplexType("NS", "ComplexType"), isNullable: true); edmComplexObject.Setup(s => s.GetEdmType()).Returns(elementType); IEdmCollectionTypeReference collectionType = new EdmCollectionTypeReference(new EdmCollectionType(elementType)); Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>(); Mock <ODataComplexTypeSerializer> elementSerializer = new Mock <ODataComplexTypeSerializer>(MockBehavior.Strict, serializerProvider.Object); serializerProvider.Setup(s => s.GetEdmTypeSerializer(elementType)).Returns(elementSerializer.Object); elementSerializer.Setup(s => s.CreateODataComplexValue(collection[0], elementType, serializerContext)).Returns(new ODataComplexValue()).Verifiable(); ODataCollectionSerializer serializer = new ODataCollectionSerializer(serializerProvider.Object); // Act var result = serializer.CreateODataCollectionValue(collection, elementType, serializerContext); // Assert elementSerializer.Verify(); }
public IActionResult GetCompanyByLocation([FromODataUri] string city, [FromODataUri] string street) { foreach (var company in AlternateKeysDataSource.Companies) { object value; if (company.TryGetPropertyValue("Location", out value)) { IEdmComplexObject location = value as IEdmComplexObject; if (location == null) { return(NotFound()); } if (location.TryGetPropertyValue("City", out value)) { string locCity = (string)value; if (location.TryGetPropertyValue("Street", out value)) { string locStreet = (string)value; if (locCity == city && locStreet == street) { return(Ok(company)); } } } } } return(NotFound()); }
/// <summary> /// Creates an <see cref="ODataComplexValue"/> for the object represented by <paramref name="graph"/>. /// </summary> /// <param name="graph">The value of the <see cref="ODataComplexValue"/> to be created.</param> /// <param name="complexType">The EDM complex type of the object.</param> /// <param name="writeContext">The serializer context.</param> /// <returns>The created <see cref="ODataComplexValue"/>.</returns> public virtual ODataComplexValue CreateODataComplexValue(object graph, IEdmComplexTypeReference complexType, ODataSerializerContext writeContext) { if (writeContext == null) { throw Error.ArgumentNull("writeContext"); } if (graph == null || graph is NullEdmComplexObject) { return(null); } IEdmComplexObject complexObject = graph as IEdmComplexObject ?? new TypedEdmComplexObject(graph, complexType, writeContext.Model); List <ODataProperty> propertyCollection = new List <ODataProperty>(); foreach (IEdmProperty property in complexType.ComplexDefinition().Properties()) { IEdmTypeReference propertyType = property.Type; ODataEdmTypeSerializer propertySerializer = SerializerProvider.GetEdmTypeSerializer(propertyType); if (propertySerializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, propertyType.FullName(), typeof(ODataMediaTypeFormatter).Name); } object propertyValue; if (complexObject.TryGetPropertyValue(property.Name, out propertyValue)) { propertyCollection.Add( propertySerializer.CreateProperty(propertyValue, property.Type, property.Name, writeContext)); } } // Try to add the dynamic properties if the complex type is open. if (complexType.ComplexDefinition().IsOpen) { List <ODataProperty> dynamicProperties = AppendDynamicProperties(complexObject, complexType, writeContext, propertyCollection, new string[0]); if (dynamicProperties != null) { propertyCollection.AddRange(dynamicProperties); } } string typeName = complexType.FullName(); ODataComplexValue value = new ODataComplexValue() { Properties = propertyCollection, TypeName = typeName }; AddTypeNameAnnotationAsNeeded(value, writeContext.MetadataLevel); return(value); }
public void CreateODataCollectionValue_CanSerialize_IEdmObjects() { // Arrange IEdmComplexObject[] collection = new IEdmComplexObject[] { new Mock<IEdmComplexObject>().Object }; ODataSerializerContext serializerContext = new ODataSerializerContext(); IEdmComplexTypeReference elementType = new EdmComplexTypeReference(new EdmComplexType("NS", "ComplexType"), isNullable: true); IEdmCollectionTypeReference collectionType = new EdmCollectionTypeReference(new EdmCollectionType(elementType), isNullable: false); Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); Mock<ODataComplexTypeSerializer> elementSerializer = new Mock<ODataComplexTypeSerializer>(MockBehavior.Strict, serializerProvider.Object); serializerProvider.Setup(s => s.GetEdmTypeSerializer(elementType)).Returns(elementSerializer.Object); elementSerializer.Setup(s => s.CreateODataComplexValue(collection[0], elementType, serializerContext)).Returns(new ODataComplexValue()).Verifiable(); ODataCollectionSerializer serializer = new ODataCollectionSerializer(serializerProvider.Object); // Act var result = serializer.CreateODataCollectionValue(collection, elementType, serializerContext); // Assert elementSerializer.Verify(); }
/// <summary> /// Creates an <see cref="ODataComplexValue"/> for the object represented by <paramref name="graph"/>. /// </summary> /// <param name="graph">The value of the <see cref="ODataComplexValue"/> to be created.</param> /// <param name="complexType">The EDM complex type of the object.</param> /// <param name="writeContext">The serializer context.</param> /// <returns>The created <see cref="ODataComplexValue"/>.</returns> public virtual ODataComplexValue CreateODataComplexValue(object graph, IEdmComplexTypeReference complexType, ODataSerializerContext writeContext) { if (writeContext == null) { throw Error.ArgumentNull("writeContext"); } if (graph == null || graph is NullEdmComplexObject) { return(null); } IEdmComplexObject complexObject = graph as IEdmComplexObject ?? new TypedEdmComplexObject(graph, complexType, writeContext.Model); List <IEdmProperty> settableProperties = new List <IEdmProperty>(complexType.ComplexDefinition().Properties()); if (complexObject.IsDeltaResource()) { IDelta deltaProperty = graph as IDelta; Contract.Assert(deltaProperty != null); IEnumerable <string> changedProperties = deltaProperty.GetChangedPropertyNames(); settableProperties = settableProperties.Where(p => changedProperties.Contains(p.Name)).ToList(); } List <ODataProperty> propertyCollection = new List <ODataProperty>(); foreach (IEdmProperty property in settableProperties) { IEdmTypeReference propertyType = property.Type; ODataEdmTypeSerializer propertySerializer = SerializerProvider.GetEdmTypeSerializer(propertyType); if (propertySerializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, propertyType.FullName(), typeof(ODataMediaTypeFormatter).Name); } object propertyValue; if (complexObject.TryGetPropertyValue(property.Name, out propertyValue)) { if (propertyValue != null && propertyType != null && propertyType.IsComplex()) { IEdmTypeReference actualType = writeContext.GetEdmType(propertyValue, propertyValue.GetType()); if (actualType != null && propertyType != actualType) { propertyType = actualType; } } var odataProperty = propertySerializer.CreateProperty(propertyValue, propertyType, property.Name, writeContext); if (odataProperty != null) { propertyCollection.Add(odataProperty); } } } // Try to add the dynamic properties if the complex type is open. if (complexType.ComplexDefinition().IsOpen) { List <ODataProperty> dynamicProperties = AppendDynamicProperties(complexObject, complexType, writeContext, propertyCollection, new string[0]); if (dynamicProperties != null) { propertyCollection.AddRange(dynamicProperties); } } string typeName = complexType.FullName(); ODataComplexValue value = new ODataComplexValue() { Properties = propertyCollection, TypeName = typeName }; AddTypeNameAnnotationAsNeeded(value, writeContext.MetadataLevel); return(value); }