public void ParseTypeCastOnTypeDefinitionPropertyWithDerivedTypeConstraintAnnotationWorks(bool isInLine) { string annotation = @"<Annotation Term=""Org.OData.Validation.V1.DerivedTypeConstraint"" >" + "<Collection>" + "<String>Edm.Int32</String>" + "<String>Edm.Boolean</String>" + "</Collection>" + "</Annotation>"; IEdmModel edmModel = GetTypeDefinitionEdmModel(annotation, isInLine); IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmProperty property = customer.DeclaredProperties.First(c => c.Name == "Data"); Assert.Equal(EdmTypeKind.TypeDefinition, property.Type.TypeKind()); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); // verify the positive type cast on itself var path = pathParser.ParsePath(new[] { "Customers(1)", "Data", "Edm.Int32" }); path[3].ShouldBeTypeSegment(property.Type.Definition, edmModel.FindType("Edm.Int32")); // verify the positive type cast path = pathParser.ParsePath(new[] { "Customers(1)", "Data", "Edm.Boolean" }); path[3].ShouldBeTypeSegment(property.Type.Definition, edmModel.FindType("Edm.Boolean")); // verify the negative type cast Action parsePath = () => pathParser.ParsePath(new[] { "Customers(1)", "Data", "Edm.Double" }); parsePath.Throws <ODataException>(ErrorStrings.PathParser_TypeCastOnlyAllowedInDerivedTypeConstraint("Edm.Double", "type definition", "Data")); }
public void ParseBoundOperationWithDerivedTypeConstraintAnnotationWorks(bool inLineAnnotation) { string annotation = @"<Annotation Term=""Org.OData.Validation.V1.DerivedTypeConstraint"" >" + "<Collection>" + "<String>NS.VipCustomer</String>" + "</Collection>" + "</Annotation>"; IEdmModel edmModel = GetOperationEdmModel(annotation); IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmEntityType vipCustomer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "VipCustomer"); IEdmType collectionCustomerType = new EdmCollectionType(new EdmEntityTypeReference(customer, true)); IEdmType collectionVipCustomerType = new EdmCollectionType(new EdmEntityTypeReference(vipCustomer, true)); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); // verify the positive type cast on itself: ~/Customers/NS.Customer/NS.Image() var path = pathParser.ParsePath(new[] { "Customers", "NS.Customer", "NS.Image()" }); path[1].ShouldBeTypeSegment(collectionCustomerType, collectionCustomerType); // verify the positive type cast: ~/Customers(1)/NS.VipCustomer/NS.Image() path = pathParser.ParsePath(new[] { "Customers", "NS.VipCustomer", "NS.Image()" }); path[1].ShouldBeTypeSegment(collectionVipCustomerType, collectionCustomerType); // verify the negative type cast: ~/Customers(1)/NS.NormalCustomer/NS.Image() Action parsePath = () => pathParser.ParsePath(new[] { "Customers", "NS.NormalCustomer", "NS.Image()" }); parsePath.Throws <ODataException>(ErrorStrings.PathParser_TypeCastOnlyAllowedInDerivedTypeConstraint("NS.NormalCustomer", "operation", "Image")); }
public void ParseTypeCastOnSingletonWithDerivedTypeConstraintButEmptyCollectionAnnotationWorks(bool isInLine) { string annotation = @"<Annotation Term=""Org.OData.Validation.V1.DerivedTypeConstraint"" >" + "<Collection />" + "</Annotation>"; IEdmModel edmModel = GetSingletonEdmModel(annotation, isInLine); IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmEntityType vipCustomer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "VipCustomer"); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); // verify the positive type cast on itself: ~/Me/NS.Customer var path = pathParser.ParsePath(new[] { "Me", "NS.Customer" }); path[1].ShouldBeTypeSegment(customer, customer); // verify the negative type cast: ~/Me/NS.VipCustomer Action parsePath = () => pathParser.ParsePath(new[] { "Me", "NS.VipCustomer" }); parsePath.Throws <ODataException>(ErrorStrings.PathParser_TypeCastOnlyAllowedInDerivedTypeConstraint("NS.VipCustomer", "singleton", "Me")); // verify the negative type cast: ~/Me/NS.NormalCustomer pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); parsePath = () => pathParser.ParsePath(new[] { "Me", "NS.NormalCustomer" }); parsePath.Throws <ODataException>(ErrorStrings.PathParser_TypeCastOnlyAllowedInDerivedTypeConstraint("NS.NormalCustomer", "singleton", "Me")); }
public void ParseTypeCastOnNavigationPropertyWithKeySegmentWithDerivedTypeConstraintAnnotationWorks(bool isInLine) { string annotation = @"<Annotation Term=""Org.OData.Validation.V1.DerivedTypeConstraint"" >" + "<Collection>" + "<String>NS.VipCustomer</String>" + "</Collection>" + "</Annotation>"; IEdmModel edmModel = GetNavigationPropertyEdmModel(annotation, isInLine); IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmEntityType vipCustomer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "VipCustomer"); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); // verify the positive type cast on itself var path = pathParser.ParsePath(new[] { "Customers(1)", "SubCustomers(1)", "NS.Customer" }); path[4].ShouldBeTypeSegment(customer, customer); // verify the positive type cast path = pathParser.ParsePath(new[] { "Customers(1)", "SubCustomers(1)", "NS.VipCustomer" }); path[4].ShouldBeTypeSegment(vipCustomer, customer); // verify the negative type cast Action parsePath = () => pathParser.ParsePath(new[] { "Customers(1)", "SubCustomers(1)", "NS.NormalCustomer" }); parsePath.Throws <ODataException>(ErrorStrings.PathParser_TypeCastOnlyAllowedInDerivedTypeConstraint("NS.NormalCustomer", "navigation property", "SubCustomers")); }
/// <summary> /// Binds a collection of <paramref name="segments"/> to metadata, creating a semantic ODataPath object. /// </summary> /// <param name="segments">Collection of path segments.</param> /// <param name="configuration">The configuration to use when binding the path.</param> /// <returns>A semantic <see cref="ODataPath"/> object to describe the path.</returns> internal static ODataPath BindPath(ICollection <string> segments, ODataUriParserConfiguration configuration) { ODataPathParser semanticPathParser = new ODataPathParser(configuration); var intermediateSegments = semanticPathParser.ParsePath(segments); ODataPath path = new ODataPath(intermediateSegments); return(path); }
public void ParseTypeCastOnEntitySetWithoutDerivedTypeConstraintAnnotationWorks(string typeCastName) { IEdmModel edmModel = GetEntitySetEdmModel(""); /*without Derived type constraint*/ IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmEntityType targetType = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == typeCastName); IEdmType collectionCustomerType = new EdmCollectionType(new EdmEntityTypeReference(customer, true)); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); // ~/Customers/NS.Customer var path = pathParser.ParsePath(new[] { "Customers", "NS." + typeCastName }); path[1].ShouldBeTypeSegment(collectionCustomerType, targetType); // ~/Customers(1)/NS.Customer path = pathParser.ParsePath(new[] { "Customers(1)", "NS." + typeCastName }); path[2].ShouldBeTypeSegment(customer, targetType); }
public void ParseTypeCastOnSingletonWithoutAliasSettingThrows() { IEdmModel edmModel = GetSingletonEdmModel(""); // without Derived type constraint ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); Action parsePath = () => pathParser.ParsePath(new[] { "Me", "MyAlias.VipCustomer" }); parsePath.Throws <ODataUnrecognizedPathException>(ErrorStrings.RequestUriProcessor_ResourceNotFound("MyAlias.VipCustomer")); }
public void ParseTypeCastOnEntitySetWithDerivedTypeConstraintAnnotationWorks(bool isInLine) { string annotation = @"<Annotation Term=""Org.OData.Validation.V1.DerivedTypeConstraint"" >" + "<Collection>" + "<String>NS.VipCustomer</String>" + "</Collection>" + "</Annotation>"; IEdmModel edmModel = GetEntitySetEdmModel(annotation, isInLine); IEdmEntityType vipCustomer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "VipCustomer"); IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmType collectionCustomerType = new EdmCollectionType(new EdmEntityTypeReference(customer, true)); IEdmType collectionVipCustomerType = new EdmCollectionType(new EdmEntityTypeReference(vipCustomer, true)); int index = 1; foreach (string segment in new[] { "Customers", "Customers(1)" }) { IEdmType actualType = index == 1 ? collectionCustomerType : customer; IEdmType expectVipCustomerType = index == 1 ? collectionVipCustomerType : vipCustomer; ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); // verify the positive type cast on itself: ~/Customers/NS.Customer var path = pathParser.ParsePath(new[] { segment, "NS.Customer" }); path[index].ShouldBeTypeSegment(actualType, actualType); // verify the positive type cast: ~/Customers/NS.VipCustomer path = pathParser.ParsePath(new[] { segment, "NS.VipCustomer" }); path[index].ShouldBeTypeSegment(expectVipCustomerType, actualType); // verify the negative type cast: ~/Customers/NS.NormalCustomer Action parsePath = () => pathParser.ParsePath(new[] { segment, "NS.NormalCustomer" }); parsePath.Throws <ODataException>(ErrorStrings.PathParser_TypeCastOnlyAllowedInDerivedTypeConstraint("NS.NormalCustomer", "entity set", "Customers")); index++; } }
public void ParseTypeCastOnSingletonWithoutDerivedTypeConstraintAnnotationWorks(string typeCastName) { IEdmModel edmModel = GetSingletonEdmModel(""); // without Derived type constraint IEdmEntityType customer = edmModel.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Customer"); IEdmEntityType targetType = edmModel.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == typeCastName); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); // ~/Me/NS.Customer var path = pathParser.ParsePath(new[] { "Me", "NS." + typeCastName }); path[1].ShouldBeTypeSegment(customer, targetType); }
public void CallbackReturnsBatchReferenceSegment() { ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(HardCodedTestModel.TestModel) { BatchReferenceCallback = callback }); IList <ODataPathSegment> segments = pathParser.ParsePath(new[] { "$0" }); ODataPathSegment segment = segments.ToList().Single(); segment.Identifier.Should().Be("$0"); segment.TargetEdmNavigationSource.Should().Be(HardCodedTestModel.GetPeopleSet()); segment.TargetEdmType.Should().Be(HardCodedTestModel.GetPersonType()); }
public void ParseTypeCastOnPropertyWithDerivedTypeConstraintAnnotationWorks(bool isInLine) { string annotation = @"<Annotation Term=""Org.OData.Validation.V1.DerivedTypeConstraint"" >" + "<Collection>" + "<String>NS.UsAddress</String>" + "</Collection>" + "</Annotation>"; IEdmModel edmModel = GetPropertyEdmModel(annotation, isInLine); IEdmComplexType address = edmModel.SchemaElements.OfType <IEdmComplexType>().First(c => c.Name == "Address"); IEdmComplexType usAddress = edmModel.SchemaElements.OfType <IEdmComplexType>().First(c => c.Name == "UsAddress"); IEdmType collectionAddressType = new EdmCollectionType(new EdmComplexTypeReference(address, true)); IEdmType collectionUsAddressType = new EdmCollectionType(new EdmComplexTypeReference(usAddress, true)); int index = 1; foreach (string segment in new[] { "Address", "Locations" }) { IEdmType actualType = index == 1 ? address : collectionAddressType; IEdmType expectUsAddressType = index == 1 ? usAddress : collectionUsAddressType; // verify the positive type cast on itself ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); var path = pathParser.ParsePath(new[] { "Customers(1)", segment, "NS.Address" }); path[3].ShouldBeTypeSegment(actualType, actualType); // verify the positive type cast path = pathParser.ParsePath(new[] { "Customers(1)", segment, "NS.UsAddress" }); path[3].ShouldBeTypeSegment(expectUsAddressType, actualType); // verify the negative type cast Action parsePath = () => pathParser.ParsePath(new[] { "Customers(1)", segment, "NS.CnAddress" }); parsePath.Throws <ODataException>(ErrorStrings.PathParser_TypeCastOnlyAllowedInDerivedTypeConstraint("NS.CnAddress", "property", segment)); index++; } }
public void ParseTypeCastOnOperationWithoutDerivedTypeConstraintAnnotationWorks(string typeCastName) { IEdmModel edmModel = GetOperationEdmModel(""); // without derived type constraint IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmEntityType targetType = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == typeCastName); IEdmType collectionCustomerType = new EdmCollectionType(new EdmEntityTypeReference(customer, true)); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); // verify the positive type cast on itself: ~/Customers/NS.Customer/NS.Image() var path = pathParser.ParsePath(new[] { "Customers", "NS." + typeCastName, "NS.Image()" }); path[1].ShouldBeTypeSegment(collectionCustomerType, targetType); }
public void ParseTypeCastOnNavigationPropertyWithDerivedTypeConstraintAnnotationWorks(bool isInLine) { string annotation = @"<Annotation Term=""Org.OData.Validation.V1.DerivedTypeConstraint"" >" + "<Collection>" + "<String>NS.VipCustomer</String>" + "</Collection>" + "</Annotation>"; IEdmModel edmModel = GetNavigationPropertyEdmModel(annotation, isInLine); IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmEntityType vipCustomer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "VipCustomer"); IEdmType collectionCustomerType = new EdmCollectionType(new EdmEntityTypeReference(customer, true)); int index = 1; foreach (string segment in new[] { "DirectReport", "SubCustomers" }) { IEdmType actualType = index == 1 ? customer : collectionCustomerType; // verify the positive type cast on itself ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); var path = pathParser.ParsePath(new[] { "Customers(1)", segment, "NS.Customer" }); path[3].ShouldBeTypeSegment(actualType, customer); // verify the positive type cast path = pathParser.ParsePath(new[] { "Customers(1)", segment, "NS.VipCustomer" }); path[3].ShouldBeTypeSegment(actualType, vipCustomer); // verify the negative type cast Action parsePath = () => pathParser.ParsePath(new[] { "Customers(1)", segment, "NS.NormalCustomer" }); parsePath.ShouldThrow <ODataException>().WithMessage(ErrorStrings.PathParser_TypeCastOnlyAllowedInDerivedTypeConstraint("NS.NormalCustomer", "navigation property", segment)); index++; } }
public void KeyAsSegmentWithTemplateShouldWork() { var keyAsSegmentTemplateParser = new ODataPathParser(new ODataUriParserConfiguration(HardCodedTestModel.TestModel) { EnableUriTemplateParsing = true, UrlConventions = ODataUrlConventions.KeyAsSegment }); IList <ODataPathSegment> path = keyAsSegmentTemplateParser.ParsePath(new[] { "Dogs", "{fido}", "MyPeople" }); var keySegment = path[1].As <KeySegment>(); KeyValuePair <string, object> keypair = keySegment.Keys.Single(); keypair.Key.Should().Be("ID"); keypair.Value.As <UriTemplateExpression>().ShouldBeEquivalentTo(new UriTemplateExpression() { LiteralText = "{fido}", ExpectedType = keySegment.EdmType.As <IEdmEntityType>().DeclaredKey.Single().Type }); }
public void ParseTypeCastOnSingletonWithNamespaceAndAliasWorks(string typeCastName, string namespaceOrAlias) { IEdmModel edmModel = GetSingletonEdmModel(""); // without Derived type constraint edmModel.SetNamespaceAlias("NS", "MyAlias"); IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmEntityType targetType = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == typeCastName); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); // ~/Me/NS.Customer var path = pathParser.ParsePath(new[] { "Me", namespaceOrAlias + typeCastName }); path[1].ShouldBeTypeSegment(targetType, customer); }
public void ParseTypeCastOnPropertyWithoutDerivedTypeConstraintAnnotationWorks(string propertyName, string typeCast) { IEdmModel edmModel = GetPropertyEdmModel(""); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmProperty property = customer.DeclaredProperties.First(c => c.Name == propertyName); IEdmType expectType = edmModel.FindDeclaredType(typeCast); Assert.NotNull(expectType); // ~/Customers(1)/{propertyName}/{typeCast} var path = pathParser.ParsePath(new[] { "Customers(1)", propertyName, typeCast }); path[3].ShouldBeTypeSegment(property.Type.Definition, expectType); }
public void ParseTypeCastOnNavigationPropertyWithoutDerivedTypeConstraintAnnotationWorks(string propertyName, string key, string typeCast) { IEdmModel edmModel = GetNavigationPropertyEdmModel(""); ODataPathParser pathParser = new ODataPathParser(new ODataUriParserConfiguration(edmModel)); IEdmEntityType customer = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmNavigationProperty property = customer.NavigationProperties().First(c => c.Name == propertyName); IEdmType collectionCustomerType = new EdmCollectionType(new EdmEntityTypeReference(customer, true)); IEdmType expectType = edmModel.FindDeclaredType(typeCast); Assert.NotNull(expectType); int index = key != null ? 4 : 3; IEdmType actualType = propertyName == "SubCustomers" && key == null ? collectionCustomerType : customer; // ~/Customers(1)/{propertyName}/{typeCast} var path = pathParser.ParsePath(new[] { "Customers(1)", propertyName + key, typeCast }); path[index].ShouldBeTypeSegment(actualType, expectType); }