Skip to content

Commit

Permalink
Issue OData#400 Support Dynamic/Additional properties of a non-open type
Browse files Browse the repository at this point in the history
  • Loading branch information
VikingsFan committed Dec 8, 2015
1 parent b0c3133 commit 496c079
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ private ODataJsonLightReaderNavigationLinkInfo ReadUndeclaredProperty(IODataJson
}

// Property with value can only be ignored if we're asked to do so.
if (!this.MessageReaderSettings.IgnoreUndeclaredValueProperties)
if (!this.ReadingResponse && !this.MessageReaderSettings.IgnoreUndeclaredValueProperties)
{
throw new ODataException(ODataErrorStrings.ValidationUtils_PropertyDoesNotExistOnType(propertyName, entryState.EntityType.FullTypeName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,16 @@ private void WriteProperty(
this.InstanceAnnotationWriter.WriteInstanceAnnotations(property.InstanceAnnotations, propertyName);
}
}

IEdmProperty edmProperty = WriterValidationUtils.ValidatePropertyDefined(
propertyName,
owningType,
!this.bypassValidation);
IEdmProperty edmProperty = null;
if (this.WritingResponse)
{
edmProperty = owningType == null ? null : owningType.FindProperty(propertyName);
}
else
{
edmProperty = WriterValidationUtils.ValidatePropertyDefined(propertyName, owningType,
!this.bypassValidation);
}
IEdmTypeReference propertyTypeReference = edmProperty == null ? null : edmProperty.Type;

ODataValue value = property.ODataValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public PayloadReaderTestDescriptor ToTestDescriptor(PayloadReaderTestDescriptor.
expectedException = ODataExpectedExceptions.ODataException("ValidationUtils_PropertyDoesNotExistOnType", "UndeclaredProperty", "TestModel.CityType");
}

if (this.IsValue && !undeclaredPropertyBehaviorKinds.HasFlag(ODataUndeclaredPropertyBehaviorKinds.IgnoreUndeclaredValueProperty))
if (this.IsLink && this.IsValue && !undeclaredPropertyBehaviorKinds.HasFlag(ODataUndeclaredPropertyBehaviorKinds.IgnoreUndeclaredValueProperty))
{
expectedException = ODataExpectedExceptions.ODataException("ValidationUtils_PropertyDoesNotExistOnType", "UndeclaredProperty", "TestModel.CityType");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public void UndeclaredClosedPropertyTest()
: ODataExpectedExceptions.ODataException("ValidationUtils_PropertyDoesNotExistOnType", propertyName, "TestModel.ClosedEntityType")
: ODataExpectedExceptions.ODataException("ValidationUtils_PropertyDoesNotExistOnType", propertyName, "TestModel.ClosedEntityType"),
},
SkipTestConfiguration = tc => testCase is NamedStreamInstance && tc.IsRequest
SkipTestConfiguration = tc => testCase is NamedStreamInstance && tc.IsRequest || !tc.IsRequest
};
});

Expand Down Expand Up @@ -1345,7 +1345,7 @@ public void PropertyValueDepthLimitTest()
PayloadBuilder.ExpandedNavigationProperty("UndeclaredProperty", PayloadBuilder.EntitySet(), null, "http://odata.org/associationLink"),
};

[TestMethod, TestCategory("Reader.Entries"), Variation(Description = "Verifies that UndeclaredPropertyBehaviorKinds setting behaves correctly.")]
[TestMethod, TestCategory("Reader.Entries"), Variation(Description = "Verifies that UndeclaredProperty can be readed in response")]
public void UndeclaredPropertyJsonLightTest()
{
this.CombinatorialEngineProvider.RunCombinations(
Expand All @@ -1362,7 +1362,6 @@ public void UndeclaredPropertyJsonLightTest()
{
testConfiguration = new ReaderTestConfiguration(testConfiguration);
testConfiguration.MessageReaderSettings.UndeclaredPropertyBehaviorKinds = undeclaredPropertyBehaviorKinds;

testDescriptor.RunTest(testConfiguration);
});
});
Expand Down Expand Up @@ -1393,19 +1392,15 @@ private IEnumerable<PayloadReaderTestDescriptor> CreateUndeclaredPropertyTestDes
PayloadElement = inEntity.DeepCopy().Property(undeclaredProperty.DeepCopy()),
ExpectedResultPayloadElement = tc => inEntity.DeepCopy().Property(undeclaredProperty.DeepCopy()),
PayloadEdmModel = model,
ExpectedException = undeclaredPropertyBehaviorKinds.HasFlag(ODataUndeclaredPropertyBehaviorKinds.IgnoreUndeclaredValueProperty)
? null
: ODataExpectedExceptions.ODataException("ValidationUtils_PropertyDoesNotExistOnType", "UndeclaredProperty", "TestModel.OfficeType")
ExpectedException = null
},
// In MLE entry
new PayloadReaderTestDescriptor(settings)
{
PayloadElement = PayloadBuilder.Entity("TestModel.CityWithMapType").PrimitiveProperty("Id", 1).AsMediaLinkEntry().Property(undeclaredProperty.DeepCopy()),
ExpectedResultPayloadElement = tc => PayloadBuilder.Entity("TestModel.CityWithMapType").PrimitiveProperty("Id", 1).AsMediaLinkEntry().Property(undeclaredProperty.DeepCopy()),
PayloadEdmModel = model,
ExpectedException = undeclaredPropertyBehaviorKinds.HasFlag(ODataUndeclaredPropertyBehaviorKinds.IgnoreUndeclaredValueProperty)
? null
: ODataExpectedExceptions.ODataException("ValidationUtils_PropertyDoesNotExistOnType", "UndeclaredProperty", "TestModel.CityWithMapType")
ExpectedException = null
},
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ public void ReadingNestedInlinecountTest()
#endregion Inlinecount Tests

[TestMethod]
public void ShouldWriteAdditionalPropertyWhenFullValidationDisabled()
public void ShouldAlwaysWriteAdditionalPropertyWhenWriteResponse()
{
var entry = new ODataEntry
{
Expand All @@ -928,30 +928,30 @@ 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,
Model,
EntitySet,
EntityType,
enableFullValidation: true);
action.ShouldThrow<ODataException>().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,
Model,
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2129,23 +2129,6 @@ private IEnumerable<PayloadWriterTestDescriptor<ODataPayloadElement>> 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<ODataPayloadElement>(
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,6 @@ public void IgnoreMissingPropertiesTestJsonLight()
supplier.Concurrency.Should().Be(1);
}

[TestMethod]
public void IgnoreMissingPropertiesTestJsonLightShouldThrow()
{
// Ignore missing properties
var context = CreateTransportLayerContext(JsonLightExtraPropertyPayload, "4.0");
context.IgnoreMissingProperties = false;
Action test = ()=> context.Execute<SimpleNorthwind.Supplier>(new Uri("/Suppliers", UriKind.Relative)).SingleOrDefault();
test.ShouldThrow<InvalidOperationException>().WithMessage("The property 'ExtraProperty' does not exist on type 'ODataDemo.Supplier'. Make sure to only use property names that are defined by the type.");
}

private const string JsonLightUnknownNavigationLink = @"{
""@odata.context"":""http://localhost:9000/Service.svc/$metadata#Suppliers"",
""value"":
Expand Down

0 comments on commit 496c079

Please sign in to comment.