public void PropertyHandlerGetPropertyNullOwningType()
        {
            // Model with a single entity type
            EdmModel     model = new EdmModel();
            const string defaultNamespaceName = "Test";
            var          int32TypeRef         = EdmCoreModel.Instance.GetInt32(isNullable: false);

            // Create an entity with a complex type property.
            var singleComplexPropertyEntityType = new EdmEntityType(defaultNamespaceName, "SingleComplexPropertyEntityType");

            singleComplexPropertyEntityType.AddKeys(singleComplexPropertyEntityType.AddStructuralProperty("ID", int32TypeRef));
            model.AddElement(singleComplexPropertyEntityType);

            // Create a property handler, enter a resource set scope and update resource scope.
            PropertyCacheHandler handler = new PropertyCacheHandler();

            handler.SetCurrentResourceScopeLevel(1);
            handler.EnterResourceSetScope(singleComplexPropertyEntityType, 0);

            // Create a PropertySerializationInfo for ComplexProp.IntProp
            var info1 = handler.GetProperty("IntProp", null);

            info1.Should().NotBeNull();

            // Get a second PropertySerializationInfo for ComplexProp.IntProp; it should be the same.
            PropertySerializationInfo info2 = handler.GetProperty("IntProp", null);

            info2.Should().NotBeNull();
            info2.Should().BeSameAs(info1);
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="textWriter">The text writer to write to.</param>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        internal ODataJsonLightOutputContext(
            TextWriter textWriter,
            ODataMessageInfo messageInfo,
            ODataMessageWriterSettings messageWriterSettings)
            : base(ODataFormat.Json, messageInfo, messageWriterSettings)
        {
            Debug.Assert(!this.WritingResponse, "Expecting WritingResponse to always be false for this constructor, so no need to validate the MetadataDocumentUri on the writer settings.");
            Debug.Assert(textWriter != null, "textWriter != null");
            Debug.Assert(messageWriterSettings != null, "messageWriterSettings != null");

            this.textWriter           = textWriter;
            this.jsonWriter           = CreateJsonWriter(messageInfo.Container, textWriter, true /*isIeee754Compatible*/);
            this.metadataLevel        = new JsonMinimalMetadataLevel();
            this.propertyCacheHandler = new PropertyCacheHandler();
        }
Esempio n. 3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        public ODataJsonLightOutputContext(
            ODataMessageInfo messageInfo,
            ODataMessageWriterSettings messageWriterSettings)
            : base(ODataFormat.Json, messageInfo, messageWriterSettings)
        {
            Debug.Assert(messageInfo.MessageStream != null, "messageInfo.MessageStream != null");
            Debug.Assert(messageInfo.MediaType != null, "messageInfo.MediaType != null");

            try
            {
                this.messageOutputStream = messageInfo.MessageStream;

                Stream outputStream;
                if (this.Synchronous)
                {
                    outputStream = this.messageOutputStream;
                }
                else
                {
                    this.asynchronousOutputStream = new AsyncBufferedStream(this.messageOutputStream);
                    outputStream = this.asynchronousOutputStream;
                }

                this.textWriter = new StreamWriter(outputStream, messageInfo.Encoding);

                // COMPAT 2: JSON indentation - WCFDS indents only partially, it inserts newlines but doesn't actually insert spaces for indentation
                // in here we allow the user to specify if true indentation should be used or if the limited functionality is enough.
                this.jsonWriter = CreateJsonWriter(this.Container, this.textWriter, messageInfo.MediaType.HasIeee754CompatibleSetToTrue(), messageWriterSettings);
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the input context.
                if (ExceptionUtils.IsCatchableExceptionType(e))
                {
                    this.messageOutputStream.Dispose();
                }

                throw;
            }

            Uri  metadataDocumentUri = messageWriterSettings.MetadataDocumentUri;
            bool alwaysAddTypeAnnotationsForDerivedTypes = messageWriterSettings.AlwaysAddTypeAnnotationsForDerivedTypes;

            this.metadataLevel        = JsonLightMetadataLevel.Create(messageInfo.MediaType, metadataDocumentUri, alwaysAddTypeAnnotationsForDerivedTypes, this.Model, this.WritingResponse);
            this.propertyCacheHandler = new PropertyCacheHandler();
        }
        public void PropertyHandlerGetPropertyNameCollision()
        {
            // Model with a single entity type
            EdmModel     model = new EdmModel();
            const string defaultNamespaceName = "Test";
            var          int32TypeRef         = EdmCoreModel.Instance.GetInt32(isNullable: false);

            // Create a complext types.
            var complexType1 = new EdmComplexType(defaultNamespaceName, "ComplexType1");

            complexType1.AddStructuralProperty("Prop1", int32TypeRef);
            model.AddElement(complexType1);

            var complexType2 = new EdmComplexType(defaultNamespaceName, "ComplexType2");

            complexType1.AddStructuralProperty("Prop1", EdmCoreModel.Instance.GetString(isNullable: false));
            model.AddElement(complexType2);

            // Create an entity with a complex type property.
            var singleComplexPropertyEntityType = new EdmEntityType(defaultNamespaceName, "SingleComplexPropertyEntityType");

            singleComplexPropertyEntityType.AddKeys(singleComplexPropertyEntityType.AddStructuralProperty("ID", int32TypeRef));
            singleComplexPropertyEntityType.AddStructuralProperty("ComplexProp1", new EdmComplexTypeReference(complexType1, isNullable: true));
            singleComplexPropertyEntityType.AddStructuralProperty("ComplexProp2", new EdmComplexTypeReference(complexType2, isNullable: true));
            model.AddElement(singleComplexPropertyEntityType);

            // Create a property handler, enter a resource set scope and update resource scope.
            PropertyCacheHandler handler = new PropertyCacheHandler();

            handler.SetCurrentResourceScopeLevel(1);
            handler.EnterResourceSetScope(singleComplexPropertyEntityType, 0);

            // Create a PropertySerializationInfo for ComplexProp1.Prop1
            var info1 = handler.GetProperty("Prop1", complexType1);

            info1.Should().NotBeNull();

            // Create a PropertySerializationInfo for ComplexProp2.Prop1; they shoudl be different.
            var info2 = handler.GetProperty("Prop1", complexType2);

            info2.Should().NotBeNull();
            info2.Should().NotBeSameAs(info1);
        }