public void WriteNullEntryWithOperation()
        {
            EdmEntityType          entityType = new EdmEntityType("NS", "Entity");
            IEdmStructuralProperty keyProp    = new EdmStructuralProperty(entityType, "Id", EdmCoreModel.Instance.GetInt32(false));

            entityType.AddProperty(keyProp);
            entityType.AddKeys(keyProp);

            EdmOperation operation = new EdmFunction("NS", "Foo", EdmCoreModel.Instance.GetInt16(true));

            operation.AddParameter("entry", new EdmEntityTypeReference(entityType, true));

            Action <ODataJsonLightOutputContext> test = outputContext =>
            {
                var parameterWriter = new ODataJsonLightParameterWriter(outputContext, operation);
                parameterWriter.WriteStart();

                var entryWriter = parameterWriter.CreateResourceWriter("entry");
                entryWriter.WriteStart((ODataResource)null);
                entryWriter.WriteEnd();

                parameterWriter.WriteEnd();
                parameterWriter.Flush();
            };

            WriteAndValidate(test, "{\"entry\":null}", writingResponse: false);
        }
        public void ShouldWriteParameterPayloadInRequestWithoutModelAndWithoutFunctionImport()
        {
            Action <ODataJsonLightOutputContext> test = outputContext =>
            {
                var parameterWriter = new ODataJsonLightParameterWriter(outputContext, operation: null);
                parameterWriter.WriteStart();
                parameterWriter.WriteValue("primitive", Guid.Empty);
                var resourceWriter = parameterWriter.CreateResourceWriter("complex");
                resourceWriter.WriteStart(new ODataResource()
                {
                    Properties = new[] { new ODataProperty {
                                             Name = "prop1", Value = 1
                                         } }
                });
                resourceWriter.WriteEnd();
                var collectionWriter = parameterWriter.CreateCollectionWriter("collection");
                collectionWriter.WriteStart(new ODataCollectionStart());
                collectionWriter.WriteItem("item1");
                collectionWriter.WriteEnd();
                parameterWriter.WriteEnd();
                parameterWriter.Flush();
            };

            WriteAndValidate(test, "{\"primitive\":\"00000000-0000-0000-0000-000000000000\",\"complex\":{\"prop1\":1},\"collection\":[\"item1\"]}", writingResponse: false);
        }
        public void WriteEntryAndComplex()
        {
            Action <ODataJsonLightOutputContext> test = outputContext =>
            {
                var entry = new ODataResource();

                var complex = new ODataResource()
                {
                    Properties = new List <ODataProperty>()
                    {
                        new ODataProperty()
                        {
                            Name = "Name", Value = "ComplexName"
                        }
                    }
                };
                entry.Properties = new List <ODataProperty>()
                {
                    new ODataProperty()
                    {
                        Name = "ID", Value = 1
                    }
                };
                var nestedComplexInfo = new ODataNestedResourceInfo()
                {
                    Name = "complexProperty", IsCollection = false
                };
                var parameterWriter = new ODataJsonLightParameterWriter(outputContext, operation: null);
                parameterWriter.WriteStart();
                var entryWriter = parameterWriter.CreateResourceWriter("entry");
                entryWriter.WriteStart(entry);
                entryWriter.WriteStart(nestedComplexInfo);
                entryWriter.WriteStart(complex);
                entryWriter.WriteEnd();
                entryWriter.WriteEnd();
                entryWriter.WriteEnd();

                var complexWriter = parameterWriter.CreateResourceWriter("complex");
                complexWriter.WriteStart(complex);
                complexWriter.WriteEnd();
                parameterWriter.WriteEnd();
                parameterWriter.Flush();
            };

            WriteAndValidate(test, "{\"entry\":{\"ID\":1,\"complexProperty\":{\"Name\":\"ComplexName\"}},\"complex\":{\"Name\":\"ComplexName\"}}", writingResponse: false);
        }
        public void WriteDerivedEntryWithOperation()
        {
            EdmEntityType          entityType = new EdmEntityType("NS", "Entity");
            IEdmStructuralProperty keyProp    = new EdmStructuralProperty(entityType, "Id", EdmCoreModel.Instance.GetInt32(false));

            entityType.AddProperty(keyProp);
            entityType.AddKeys(keyProp);
            EdmEntityType derivedType = new EdmEntityType("NS", "DerivedType", entityType);

            derivedType.AddProperty(new EdmStructuralProperty(derivedType, "Name", EdmCoreModel.Instance.GetString(false)));

            EdmOperation operation = new EdmFunction("NS", "Foo", EdmCoreModel.Instance.GetInt16(true));

            operation.AddParameter("entry", new EdmEntityTypeReference(entityType, true));

            Action <ODataJsonLightOutputContext> test = outputContext =>
            {
                var entry = new ODataResource()
                {
                    TypeName = "NS.DerivedType",
                };
                entry.Properties = new List <ODataProperty>()
                {
                    new ODataProperty()
                    {
                        Name = "ID", Value = 1
                    },
                    new ODataProperty()
                    {
                        Name = "Name", Value = "TestName"
                    }
                };

                var parameterWriter = new ODataJsonLightParameterWriter(outputContext, operation);
                parameterWriter.WriteStart();

                var entryWriter = parameterWriter.CreateResourceWriter("entry");
                entryWriter.WriteStart(entry);
                entryWriter.WriteEnd();


                parameterWriter.WriteEnd();
                parameterWriter.Flush();
            };

            WriteAndValidate(test, "{\"entry\":{\"@odata.type\":\"#NS.DerivedType\",\"ID\":1,\"Name\":\"TestName\"}}", writingResponse: false);
        }
        public void WriteEntryWithoutOperation()
        {
            Action <ODataJsonLightOutputContext> test = outputContext =>
            {
                var entry = new ODataResource();
                entry.Properties = new List <ODataProperty>()
                {
                    new ODataProperty()
                    {
                        Name = "ID", Value = 1
                    }
                };

                var parameterWriter = new ODataJsonLightParameterWriter(outputContext, operation: null);
                parameterWriter.WriteStart();
                var entryWriter = parameterWriter.CreateResourceWriter("entry");
                entryWriter.WriteStart(entry);
                entryWriter.WriteEnd();
                parameterWriter.WriteEnd();
                parameterWriter.Flush();
            };

            WriteAndValidate(test, "{\"entry\":{\"ID\":1}}", writingResponse: false);
        }