public WriterTypeNameEndToEndTests() { var model = new EdmModel(); var type = new EdmEntityType("TestModel", "TestEntity", /* baseType */ null, /* isAbstract */ false, /* isOpen */ true); var keyProperty = type.AddStructuralProperty("DeclaredInt16", EdmPrimitiveTypeKind.Int16); type.AddKeys(new[] { keyProperty }); // Note: DerivedPrimitive is declared as a Geography, but its value below will be set to GeographyPoint, which is derived from Geography. type.AddStructuralProperty("DerivedPrimitive", EdmPrimitiveTypeKind.Geography); var container = new EdmEntityContainer("TestModel", "Container"); var set = container.AddEntitySet("Set", type); model.AddElement(type); model.AddElement(container); var writerStream = new MemoryStream(); this.settings = new ODataMessageWriterSettings(); this.settings.SetServiceDocumentUri(ServiceDocumentUri); // Make the message writer and entry writer lazy so that individual tests can tweak the settings before the message writer is created. this.messageWriter = new Lazy<ODataMessageWriter>(() => new ODataMessageWriter( (IODataResponseMessage)new InMemoryMessage { Stream = writerStream }, this.settings, model)); var entryWriter = new Lazy<ODataWriter>(() => this.messageWriter.Value.CreateODataEntryWriter(set, type)); var valueWithAnnotation = new ODataPrimitiveValue(45); valueWithAnnotation.SetAnnotation(new SerializationTypeNameAnnotation { TypeName = "TypeNameFromSTNA" }); var propertiesToWrite = new List<ODataProperty> { new ODataProperty { Name = "DeclaredInt16", Value = (Int16)42 }, new ODataProperty { Name = "UndeclaredDecimal", Value = (Decimal)4.5 }, new ODataProperty { // Note: value is more derived than the declared type. Name = "DerivedPrimitive", Value = Microsoft.Spatial.GeographyPoint.Create(42, 45) }, new ODataProperty() { Name = "PropertyWithSTNA", Value = valueWithAnnotation } }; this.writerOutput = new Lazy<string>(() => { entryWriter.Value.WriteStart(new ODataEntry { Properties = propertiesToWrite }); entryWriter.Value.WriteEnd(); entryWriter.Value.Flush(); writerStream.Seek(0, SeekOrigin.Begin); return new StreamReader(writerStream).ReadToEnd(); }); }
internal static void AddTypeNameAnnotationAsNeeded(ODataPrimitiveValue primitive, ODataMetadataLevel metadataLevel) { Contract.Assert(primitive != null); object value = primitive.Value; // Don't add a type name annotation for Atom or JSON verbose. if (metadataLevel != ODataMetadataLevel.Default) { string typeName; if (ShouldSuppressTypeNameSerialization(value, metadataLevel)) { typeName = null; } else { typeName = GetTypeName(value); } primitive.SetAnnotation<SerializationTypeNameAnnotation>(new SerializationTypeNameAnnotation { TypeName = typeName }); } }
public void BuildPropertyContextUriForIntegerPropertyValueWithNonNullAnnotation() { ODataValue value = new ODataPrimitiveValue(1); value.SetAnnotation(new SerializationTypeNameAnnotation { TypeName = "FQNS.FromAnnotation" }); var contextUri = this.CreatePropertyContextUri(value); contextUri.OriginalString.Should().Be(BuildExpectedContextUri("#FQNS.FromAnnotation")); }
public void TypeNameShouldComeFromSerializationTypeNameAnnotationForPrimitiveValue() { var stna = new SerializationTypeNameAnnotation() { TypeName = "FromSTNA" }; var value = new ODataPrimitiveValue(42); value.SetAnnotation(stna); this.typeNameOracle.GetValueTypeNameForWriting(value, EdmCoreModel.Instance.GetInt32(true), EdmCoreModel.Instance.GetInt32(false), /* isOpenProperty*/ false).Should().Be("FromSTNA"); }
public void WriteTopLevelErrorWithStringInstanceAnnotationWithTypeNameAttribute() { var result = SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection <ODataInstanceAnnotation>(); var primitiveValue = new ODataPrimitiveValue("stringValue"); primitiveValue.SetAnnotation(new SerializationTypeNameAnnotation() { TypeName = "Custom.Type" }); ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.primitive", primitiveValue); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; serializer.WriteTopLevelError(error, false); }); result.Should().Contain("\"[email protected]\":\"#Custom.Type\",\"@sample.primitive\":\"stringValue\""); }
internal static void AddTypeNameAnnotationAsNeeded(ODataPrimitiveValue primitive, ODataMetadataLevel metadataLevel) { // ODataLib normally has the caller decide whether or not to serialize properties by leaving properties // null when values should not be serialized. The TypeName property is different and should always be // provided to ODataLib to enable model validation. A separate annotation is used to decide whether or not // to serialize the type name (a null value prevents serialization). // Note that this annotation should not be used for Atom or JSON verbose formats, as it will interfere with // the correct default behavior for those formats. Contract.Assert(primitive != null); object value = primitive.Value; // Only add an annotation if we want to override ODataLib's default type name serialization behavior. if (ShouldAddTypeNameAnnotation(metadataLevel)) { string typeName; // Provide the type name to serialize (or null to force it not to serialize). if (ShouldSuppressTypeNameSerialization(value, metadataLevel)) { typeName = null; } else { typeName = GetTypeName(value); } primitive.SetAnnotation <SerializationTypeNameAnnotation>(new SerializationTypeNameAnnotation { TypeName = typeName }); } }
internal static void AddTypeNameAnnotationAsNeeded(ODataPrimitiveValue primitive, IEdmPrimitiveTypeReference primitiveType, ODataMetadataLevel metadataLevel) { // ODataLib normally has the caller decide whether or not to serialize properties by leaving properties // null when values should not be serialized. The TypeName property is different and should always be // provided to ODataLib to enable model validation. A separate annotation is used to decide whether or not // to serialize the type name (a null value prevents serialization). Contract.Assert(primitive != null); object value = primitive.Value; string typeName = null; // Set null to force the type name not to serialize. // Provide the type name to serialize. if (!ShouldSuppressTypeNameSerialization(value, metadataLevel)) { typeName = primitiveType.FullName(); } primitive.SetAnnotation <SerializationTypeNameAnnotation>(new SerializationTypeNameAnnotation { TypeName = typeName }); }
internal static void AddTypeNameAnnotationAsNeeded(ODataPrimitiveValue primitive, ODataMetadataLevel metadataLevel) { // ODataLib normally has the caller decide whether or not to serialize properties by leaving properties // null when values should not be serialized. The TypeName property is different and should always be // provided to ODataLib to enable model validation. A separate annotation is used to decide whether or not // to serialize the type name (a null value prevents serialization). // Note that this annotation should not be used for Atom or JSON verbose formats, as it will interfere with // the correct default behavior for those formats. Contract.Assert(primitive != null); object value = primitive.Value; // Only add an annotation if we want to override ODataLib's default type name serialization behavior. if (ShouldAddTypeNameAnnotation(metadataLevel)) { string typeName; // Provide the type name to serialize (or null to force it not to serialize). if (ShouldSuppressTypeNameSerialization(value, metadataLevel)) { typeName = null; } else { typeName = GetTypeName(value); } primitive.SetAnnotation<SerializationTypeNameAnnotation>(new SerializationTypeNameAnnotation { TypeName = typeName }); } }
public void Initialize() { var model = new EdmModel(); var type = new EdmEntityType("TestModel", "TestEntity", /* baseType */ null, /* isAbstract */ false, /* isOpen */ true); var keyProperty = type.AddStructuralProperty("DeclaredInt16", EdmPrimitiveTypeKind.Int16); type.AddKeys(new[] { keyProperty }); // Note: DerivedPrimitive is declared as a Geography, but its value below will be set to GeographyPoint, which is derived from Geography. type.AddStructuralProperty("DerivedPrimitive", EdmPrimitiveTypeKind.Geography); var container = new EdmEntityContainer("TestModel", "Container"); var set = container.AddEntitySet("Set", type); model.AddElement(type); model.AddElement(container); var writerStream = new MemoryStream(); this.settings = new ODataMessageWriterSettings(); this.settings.SetServiceDocumentUri(ServiceDocumentUri); // Make the message writer and entry writer lazy so that individual tests can tweak the settings before the message writer is created. this.messageWriter = new Lazy <ODataMessageWriter>(() => new ODataMessageWriter( (IODataResponseMessage) new InMemoryMessage { Stream = writerStream }, this.settings, model)); var entryWriter = new Lazy <ODataWriter>(() => this.messageWriter.Value.CreateODataEntryWriter(set, type)); var valueWithAnnotation = new ODataPrimitiveValue(45); valueWithAnnotation.SetAnnotation(new SerializationTypeNameAnnotation { TypeName = "TypeNameFromSTNA" }); var propertiesToWrite = new List <ODataProperty> { new ODataProperty { Name = "DeclaredInt16", Value = (Int16)42 }, new ODataProperty { Name = "UndeclaredDecimal", Value = (Decimal)4.5 }, new ODataProperty { // Note: value is more derived than the declared type. Name = "DerivedPrimitive", Value = Microsoft.Spatial.GeographyPoint.Create(42, 45) }, new ODataProperty() { Name = "PropertyWithSTNA", Value = valueWithAnnotation } }; this.writerOutput = new Lazy <string>(() => { entryWriter.Value.WriteStart(new ODataEntry { Properties = propertiesToWrite }); entryWriter.Value.WriteEnd(); entryWriter.Value.Flush(); writerStream.Seek(0, SeekOrigin.Begin); return(new StreamReader(writerStream).ReadToEnd()); }); }
public void WriteTopLevelErrorWithStringInstanceAnnotationWithTypeNameAttribute() { var result = SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); var primitiveValue = new ODataPrimitiveValue("stringValue"); primitiveValue.SetAnnotation(new SerializationTypeNameAnnotation() { TypeName = "Custom.Type" }); ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.primitive", primitiveValue); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; serializer.WriteTopLevelError(error, false); }); result.Should().Contain("\"[email protected]\":\"#Custom.Type\",\"@sample.primitive\":\"stringValue\""); }
public void BuildPropertyContextUriForIntegerPropertyValueWithNonNullAnnotation() { ODataValue value = new ODataPrimitiveValue(1); value.SetAnnotation(new SerializationTypeNameAnnotation { TypeName = "FQNS.FromAnnotation" }); var contextUri = this.CreatePropertyContextUri(value); contextUri.OriginalString.Should().Be(BuildExpectedContextUri("#FQNS.FromAnnotation")); }
public void TypeNameShouldComeFromSerializationTypeNameAnnotationForPrimitiveValue() { var stna = new SerializationTypeNameAnnotation() {TypeName = "FromSTNA"}; var value = new ODataPrimitiveValue(42); value.SetAnnotation(stna); this.typeNameOracle.GetValueTypeNameForWriting(value, EdmCoreModel.Instance.GetInt32(true), EdmCoreModel.Instance.GetInt32(false), /* isOpenProperty*/ false).Should().Be("FromSTNA"); }