/// <summary> /// Process the current property annotation. /// </summary> /// <param name="annotatedPropertyName">The name being annotated. Can be a property or an instance annotation.</param> /// <param name="annotationName">The annotation targeting the <paramref name="annotatedPropertyName"/>.</param> /// <param name="duplicatePropertyNamesChecker">The duplicate property names checker.</param> /// <param name="readPropertyAnnotationValue">Callback to read the property annotation value.</param> private void ProcessPropertyAnnotation(string annotatedPropertyName, string annotationName, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, Func <string, object> readPropertyAnnotationValue) { // We don't currently support annotation targeting an instance annotation except for the @odata.type property annotation. if (ODataJsonLightReaderUtils.IsAnnotationProperty(annotatedPropertyName) && string.CompareOrdinal(annotationName, ODataAnnotationNames.ODataType) != 0) { throw new ODataException(OData.Core.Strings.ODataJsonLightDeserializer_OnlyODataTypeAnnotationCanTargetInstanceAnnotation(annotationName, annotatedPropertyName, ODataAnnotationNames.ODataType)); } // Read over the property name. this.JsonReader.Read(); if (ODataJsonLightReaderUtils.IsODataAnnotationName(annotationName)) { // OData annotations are read. duplicatePropertyNamesChecker.AddODataPropertyAnnotation(annotatedPropertyName, annotationName, readPropertyAnnotationValue(annotationName)); } else { if (this.ShouldSkipCustomInstanceAnnotation(annotationName) || (this is ODataJsonLightErrorDeserializer && this.MessageReaderSettings.ShouldIncludeAnnotation == null)) { // Make sure there's no duplicated instance annotation name even though we are skipping over it. duplicatePropertyNamesChecker.AddCustomPropertyAnnotation(annotatedPropertyName, annotationName); this.JsonReader.SkipValue(); } else { Debug.Assert(ReadPropertyCustomAnnotationValue != null, "readPropertyCustomAnnotationValue != null"); duplicatePropertyNamesChecker.AddCustomPropertyAnnotation(annotatedPropertyName, annotationName, ReadPropertyCustomAnnotationValue(duplicatePropertyNamesChecker, annotationName)); } } }
public void OnlyCustomAnnotationsForPropertyAddedShouldReturnNull() { DuplicatePropertyNamesChecker duplicateChecker = new DuplicatePropertyNamesChecker(false, true); duplicateChecker.AddCustomPropertyAnnotation("property", "custom.annotation"); duplicateChecker.AddCustomPropertyAnnotation("property", "custom.annotation2"); duplicateChecker.GetODataPropertyAnnotations("property").Should().BeNull(); }
public void DuplicatePropertyCustomAnnotationShouldFail() { DuplicatePropertyNamesChecker duplicateChecker = new DuplicatePropertyNamesChecker(false, true); Action action = () => duplicateChecker.AddCustomPropertyAnnotation("property", "custom.name"); action.ShouldNotThrow(); action.ShouldThrow<ODataException>().WithMessage(ErrorStrings.DuplicatePropertyNamesChecker_DuplicateAnnotationForPropertyNotAllowed("custom.name", "property")); }
public void DuplicatePropertyCustomAnnotationShouldFail() { DuplicatePropertyNamesChecker duplicateChecker = new DuplicatePropertyNamesChecker(false, true); Action action = () => duplicateChecker.AddCustomPropertyAnnotation("property", "custom.name"); action.ShouldNotThrow(); action.ShouldThrow <ODataException>().WithMessage(ErrorStrings.DuplicatePropertyNamesChecker_DuplicateAnnotationForPropertyNotAllowed("custom.name", "property")); }
public void MarkPropertyAsProcessedWithNoPropertyShouldWork() { DuplicatePropertyNamesChecker duplicateChecker = new DuplicatePropertyNamesChecker(false, true); duplicateChecker.MarkPropertyAsProcessed("property"); Action odataAction = () => duplicateChecker.AddODataPropertyAnnotation("property", JsonLightConstants.ODataAnnotationNamespacePrefix + "name", null); odataAction.ShouldThrow <ODataException>().WithMessage(ErrorStrings.DuplicatePropertyNamesChecker_PropertyAnnotationAfterTheProperty(JsonLightConstants.ODataAnnotationNamespacePrefix + "name", "property")); Action customAction = () => duplicateChecker.AddCustomPropertyAnnotation("property", "custom.name"); customAction.ShouldThrow <ODataException>().WithMessage(ErrorStrings.DuplicatePropertyNamesChecker_PropertyAnnotationAfterTheProperty("custom.name", "property")); }
/// <summary> /// Process the current property annotation. /// </summary> /// <param name="annotatedPropertyName">The name being annotated. Can be a property or an instance annotation.</param> /// <param name="annotationName">The annotation targeting the <paramref name="annotatedPropertyName"/>.</param> /// <param name="duplicatePropertyNamesChecker">The duplicate property names checker.</param> /// <param name="readPropertyAnnotationValue">Callback to read the property annotation value.</param> private void ProcessPropertyAnnotation(string annotatedPropertyName, string annotationName, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, Func <string, object> readPropertyAnnotationValue) { // We don't currently support annotation targeting an instance annotation except for the @odata.type property annotation. if (ODataJsonLightReaderUtils.IsAnnotationProperty(annotatedPropertyName) && string.CompareOrdinal(annotationName, ODataAnnotationNames.ODataType) != 0) { throw new ODataException(OData.Core.Strings.ODataJsonLightDeserializer_OnlyODataTypeAnnotationCanTargetInstanceAnnotation(annotationName, annotatedPropertyName, ODataAnnotationNames.ODataType)); } // Read over the property name. this.JsonReader.Read(); if (ODataJsonLightReaderUtils.IsODataAnnotationName(annotationName)) { // OData annotations are read. duplicatePropertyNamesChecker.AddODataPropertyAnnotation(annotatedPropertyName, annotationName, readPropertyAnnotationValue(annotationName)); } else { // All other property annotations are ignored. duplicatePropertyNamesChecker.AddCustomPropertyAnnotation(annotatedPropertyName, annotationName); this.JsonReader.SkipValue(); } }
public void MarkPropertyAsProcessedWithNoPropertyShouldWork() { DuplicatePropertyNamesChecker duplicateChecker = new DuplicatePropertyNamesChecker(false, true); duplicateChecker.MarkPropertyAsProcessed("property"); Action odataAction = () => duplicateChecker.AddODataPropertyAnnotation("property", JsonLightConstants.ODataAnnotationNamespacePrefix + "name", null); odataAction.ShouldThrow<ODataException>().WithMessage(ErrorStrings.DuplicatePropertyNamesChecker_PropertyAnnotationAfterTheProperty(JsonLightConstants.ODataAnnotationNamespacePrefix + "name", "property")); Action customAction = () => duplicateChecker.AddCustomPropertyAnnotation("property", "custom.name"); customAction.ShouldThrow<ODataException>().WithMessage(ErrorStrings.DuplicatePropertyNamesChecker_PropertyAnnotationAfterTheProperty("custom.name", "property")); }