From 27ded6a0d00e6dd25c237c1875fd089f7fb293a3 Mon Sep 17 00:00:00 2001 From: Fan Ouyang Date: Tue, 8 Dec 2015 16:14:19 +0800 Subject: [PATCH] Issue#400 Support Dynamic/Additional properties of a non-open type --- .../ODataJsonLightPropertySerializer.cs | 7 +-- .../JsonLight/FullPayloadValidateTests.cs | 58 +++++++++---------- .../Writer/WriterPropertyTests.cs | 17 ------ 3 files changed, 31 insertions(+), 51 deletions(-) diff --git a/src/Microsoft.OData.Core/JsonLight/ODataJsonLightPropertySerializer.cs b/src/Microsoft.OData.Core/JsonLight/ODataJsonLightPropertySerializer.cs index 885eb3a545..06b427322b 100644 --- a/src/Microsoft.OData.Core/JsonLight/ODataJsonLightPropertySerializer.cs +++ b/src/Microsoft.OData.Core/JsonLight/ODataJsonLightPropertySerializer.cs @@ -186,11 +186,8 @@ private void WriteProperty( this.InstanceAnnotationWriter.WriteInstanceAnnotations(property.InstanceAnnotations, propertyName); } } - - IEdmProperty edmProperty = WriterValidationUtils.ValidatePropertyDefined( - propertyName, - owningType, - !this.bypassValidation); + IEdmProperty edmProperty = WriterValidationUtils.ValidatePropertyDefined(propertyName, owningType, + !(this.bypassValidation || this.WritingResponse)); IEdmTypeReference propertyTypeReference = edmProperty == null ? null : edmProperty.Type; ODataValue value = property.ODataValue; diff --git a/test/FunctionalTests/Microsoft.OData.Core.Tests/ScenarioTests/Writer/JsonLight/FullPayloadValidateTests.cs b/test/FunctionalTests/Microsoft.OData.Core.Tests/ScenarioTests/Writer/JsonLight/FullPayloadValidateTests.cs index e0b7d83cef..f35c0042f3 100644 --- a/test/FunctionalTests/Microsoft.OData.Core.Tests/ScenarioTests/Writer/JsonLight/FullPayloadValidateTests.cs +++ b/test/FunctionalTests/Microsoft.OData.Core.Tests/ScenarioTests/Writer/JsonLight/FullPayloadValidateTests.cs @@ -347,11 +347,11 @@ public void WritingFeedExpandWithNonCollectionContainedElement() { ODataItem[] itemsToWrite = new ODataItem[] { - new ODataFeed(), + new ODataFeed(), this.entryWithOnlyData1, this.containedNavLink, this.entryWithOnlyData2, - this.containedNavLink, + this.containedNavLink, this.entryWithOnlyData3 }; @@ -386,9 +386,9 @@ public void WritingEntryExpandWithMixedCollectionAndNonCollectionContainedElemen { this.entryWithOnlyData1, this.containedCollectionNavLink, - new ODataFeed(), + new ODataFeed(), this.entryWithOnlyData2, - this.containedNavLink, + this.containedNavLink, this.entryWithOnlyData3, }; @@ -422,9 +422,9 @@ public void WritingEntryExpandWithExpandCollectionNavPropAndNonCollectionContain { this.entryWithOnlyData1, this.expandedCollectionNavLink, - new ODataFeed(), + new ODataFeed(), this.entryWithOnlyData2, - this.containedNavLink, + this.containedNavLink, this.entryWithOnlyData3 }; @@ -458,8 +458,8 @@ public void WritingEntryExpandWithCollectionContainedElementAndExpandNavProp() ODataItem[] itemsToWrite = new ODataItem[] { this.entryWithOnlyData1, - this.containedCollectionNavLink, - new ODataFeed(), + this.containedCollectionNavLink, + new ODataFeed(), this.entryWithOnlyData2, this.expandedNavLink, this.entryWithOnlyData3 @@ -537,11 +537,11 @@ public void WritingFeedExpandWithNonCollectionContainedElementWithTypeCast() { ODataItem[] itemsToWrite = new ODataItem[] { - new ODataFeed(), + new ODataFeed(), this.entryWithOnlyData1, this.containedNavLink, this.entryWithOnlyData2, - this.containedNavLink, + this.containedNavLink, this.entryWithOnlyData3 }; @@ -576,9 +576,9 @@ public void WritingEntryExpandWithMixedCollectionAndNonCollectionContainedElemen { this.entryWithOnlyData1, this.containedCollectionNavLink, - new ODataFeed(), + new ODataFeed(), this.entryWithOnlyData2, - this.containedNavLink, + this.containedNavLink, this.entryWithOnlyData3, }; @@ -662,10 +662,10 @@ public void WritingEntryExpandWithMixedCollectionAndNonCollectionContainedElemen this.entryWithOnlyData2, new ODataNavigationLinkEnd(), this.containedCollectionNavLink, - new ODataFeed(), + new ODataFeed(), this.entryWithOnlyData2, - new ODataNavigationLinkEnd(), - this.containedNavLink, + new ODataNavigationLinkEnd(), + this.containedNavLink, this.entryWithOnlyData3, }; @@ -780,7 +780,7 @@ public void WritingTopLevelInlinecountTest() ODataItem[] itemsToWrite = new ODataItem[] { - feed, + feed, this.entryWithOnlyData1, }; @@ -806,7 +806,7 @@ public void WritingNestedInlinecountTest() ODataItem[] itemsToWrite = new ODataItem[] { - new ODataFeed(), + new ODataFeed(), this.entryWithOnlyData1, this.containedCollectionNavLink, feed @@ -907,7 +907,7 @@ public void ReadingNestedInlinecountTest() #endregion Inlinecount Tests [Fact] - public void ShouldWriteAdditionalPropertyWhenFullValidationDisabled() + public void ShouldAlwaysWriteAdditionalPropertyWhenWriteResponse() { var entry = new ODataEntry { @@ -921,7 +921,13 @@ public void ShouldWriteAdditionalPropertyWhenFullValidationDisabled() ODataItem[] itemsToWrite = { entry }; - Action action = () => this.GetWriterOutputForContentTypeAndKnobValue( + string expectedPayload = + "{\"" + + "@odata.context\":\"http://example.org/odata.svc/$metadata#EntitySet/$entity\"," + + "\"ID\":102,\"Name\":\"Bob\",\"Prop1\":\"Var1\"" + + "}"; + + string result = this.GetWriterOutputForContentTypeAndKnobValue( "application/json;odata.metadata=minimal", true, itemsToWrite, @@ -929,10 +935,9 @@ public void ShouldWriteAdditionalPropertyWhenFullValidationDisabled() EntitySet, EntityType, enableFullValidation: true); - action.ShouldThrow().WithMessage( - Strings.ValidationUtils_PropertyDoesNotExistOnType("Prop1", "Namespace.EntityType")); + result.Should().Be(expectedPayload); - string result = this.GetWriterOutputForContentTypeAndKnobValue( + result = this.GetWriterOutputForContentTypeAndKnobValue( "application/json;odata.metadata=minimal", true, itemsToWrite, @@ -940,11 +945,6 @@ public void ShouldWriteAdditionalPropertyWhenFullValidationDisabled() EntitySet, EntityType, enableFullValidation: false); - string expectedPayload = - "{\"" + - "@odata.context\":\"http://example.org/odata.svc/$metadata#EntitySet/$entity\"," + - "\"ID\":102,\"Name\":\"Bob\",\"Prop1\":\"Var1\"" + - "}"; result.Should().Be(expectedPayload); } @@ -964,8 +964,8 @@ public void ShouldWriteNestedContextUrlIfCanNotBeInferred() ODataItem[] itemsToWrite = new ODataItem[] { this.entryWithOnlyData1, - this.containedCollectionNavLink, - new ODataFeed(), + this.containedCollectionNavLink, + new ODataFeed(), entryWithOnlyData2WithSerializationInfo, }; diff --git a/test/FunctionalTests/Tests/DataOData/Tests/OData.Writer.Tests/Writer/WriterPropertyTests.cs b/test/FunctionalTests/Tests/DataOData/Tests/OData.Writer.Tests/Writer/WriterPropertyTests.cs index 7fcee3fe5b..4db60ddafa 100644 --- a/test/FunctionalTests/Tests/DataOData/Tests/OData.Writer.Tests/Writer/WriterPropertyTests.cs +++ b/test/FunctionalTests/Tests/DataOData/Tests/OData.Writer.Tests/Writer/WriterPropertyTests.cs @@ -2129,23 +2129,6 @@ private IEnumerable> GenerateCo ) )); } - - var propertyInComplexValue2 = duplicateProperty.Value.Properties.First(); - propertyInComplexValue2.Name = String.Format(CultureInfo.InvariantCulture, "{0}{1}", propertyInComplexValue2.Name, Guid.NewGuid().ToString().Substring(0, 5)); - - testCases.Add(new PayloadWriterTestDescriptor( - this.Settings, - duplicateProperty, - new PayloadWriterTestDescriptor.WriterTestExpectedResultCallback( - (config) => - { - return new WriterTestExpectedResults(this.Settings.ExpectedResultSettings) - { - ExpectedException2 = ODataExpectedExceptions.ODataException("ValidationUtils_PropertyDoesNotExistOnType", propertyInComplexValue2.Name, duplicateProperty.Value.FullTypeName), - }; - } - ) - )); } } return testCases;