/// <summary> /// Initializes a new instance of the <see cref="DefaultODataSerializerProvider"/> class. /// </summary> public DefaultODataSerializerProvider() { _feedSerializer = new ODataFeedSerializer(this); _collectionSerializer = new ODataCollectionSerializer(this); _complexTypeSerializer = new ODataComplexTypeSerializer(this); _entityTypeSerializer = new ODataEntityTypeSerializer(this); }
public ODataCollectionSerializerTests() { _model = SerializationTestsHelpers.SimpleCustomerOrderModel(); _customerSet = _model.EntityContainer.FindEntitySet("Customers"); _edmIntType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false); _customer = new Customer() { FirstName = "Foo", LastName = "Bar", ID = 10, }; ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); _collectionType = new EdmCollectionTypeReference(new EdmCollectionType(_edmIntType)); _serializer = new ODataCollectionSerializer(serializerProvider); }
public ODataCollectionSerializerTests() { _model = SerializationTestsHelpers.SimpleCustomerOrderModel(); _customerSet = _model.EntityContainer.FindEntitySet("Customers"); _edmIntType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false); _customer = new Customer() { FirstName = "Foo", LastName = "Bar", ID = 10, }; ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); _collectionType = new EdmCollectionTypeReference(new EdmCollectionType(_edmIntType)); _serializer = new ODataCollectionSerializer(serializerProvider); }
public void AddTypeNameAnnotationAsNeeded_AddsAnnotationWithNull_InJsonLightNoMetadataMode() { // Arrange string expectedTypeName = "TypeName"; ODataCollectionValue value = new ODataCollectionValue { TypeName = expectedTypeName }; // Act ODataCollectionSerializer.AddTypeNameAnnotationAsNeeded(value, ODataMetadataLevel.NoMetadata); // Assert SerializationTypeNameAnnotation annotation = value.GetAnnotation <SerializationTypeNameAnnotation>(); Assert.NotNull(annotation); // Guard Assert.Null(annotation.TypeName); }
public void AddTypeNameAnnotationAsNeeded_AddsAnnotation_InJsonLightMetadataMode() { // Arrange string expectedTypeName = "TypeName"; ODataCollectionValue value = new ODataCollectionValue { TypeName = expectedTypeName }; // Act ODataCollectionSerializer.AddTypeNameAnnotationAsNeeded(value, ODataMetadataLevel.FullMetadata); // Assert ODataTypeAnnotation annotation = value.TypeAnnotation; Assert.NotNull(annotation); // Guard Assert.Equal(expectedTypeName, annotation.TypeName); }
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)); 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 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 void CreateODataCollectionValue_ThrowsSerializationException_TypeCannotBeSerialized() { IEnumerable enumerable = new[] { 0 }; Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); var serializer = new ODataCollectionSerializer(serializerProvider.Object); serializerProvider.Setup(s => s.GetEdmTypeSerializer(It.IsAny<IEdmTypeReference>())).Returns<IEdmTypeReference>(null); Assert.Throws<SerializationException>( () => serializer.CreateODataCollectionValue(enumerable, _edmIntType, new ODataSerializerContext { Model = _model }), "'Edm.Int32' cannot be serialized using the ODataMediaTypeFormatter."); }
public void CreateODataValue_ThrowsArgument_IfGraphIsNotEnumerable() { object nonEnumerable = new object(); Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); var serializer = new ODataCollectionSerializer(serializerProvider.Object); serializerProvider.Setup(s => s.GetEdmTypeSerializer(It.IsAny<IEdmTypeReference>())).Returns<IEdmTypeReference>(null); Assert.ThrowsArgument( () => serializer.CreateODataValue(graph: nonEnumerable, expectedType: _collectionType, writeContext: new ODataSerializerContext()), "graph", "The argument must be of type 'IEnumerable'."); }
public void CreateODataValue_ThrowsArgument_IfGraphIsNull() { // Arrange IEnumerable nullEnumerable = null; var serializerProvider = new Mock<ODataSerializerProvider>(); var serializer = new ODataCollectionSerializer(serializerProvider.Object); serializerProvider.Setup(s => s.GetEdmTypeSerializer(It.IsAny<IEdmTypeReference>())).Returns<IEdmTypeReference>(null); // Act and Assert Assert.Throws<SerializationException>( () => serializer.CreateODataValue(graph: nullEnumerable, expectedType: _collectionType, writeContext: new ODataSerializerContext()), "Null collections cannot be serialized."); }