private static void EnumMemberExpressionDemo() { Console.WriteLine("EnumMemberExpressionDemo"); var model = new EdmModel(); var personType = new EdmEntityType("TestNS", "Person"); model.AddElement(personType); var pid = personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false); personType.AddKeys(pid); personType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); var colorType = new EdmEnumType("TestNS2", "Color", true); model.AddElement(colorType); colorType.AddMember("Cyan", new EdmIntegerConstant(1)); colorType.AddMember("Blue", new EdmIntegerConstant(2)); var outColorTerm = new EdmTerm("TestNS", "OutColor", new EdmEnumTypeReference(colorType, true)); model.AddElement(outColorTerm); var exp = new EdmEnumMemberExpression( new EdmEnumMember(colorType, "Blue", new EdmIntegerConstant(2)) ); var annotation = new EdmAnnotation(personType, outColorTerm, exp); annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(annotation); ShowModel(model); var ann = model.FindVocabularyAnnotations<IEdmValueAnnotation>(personType, "TestNS.OutColor").First(); var memberExp = (IEdmEnumMemberExpression)ann.Value; foreach (var member in memberExp.EnumMembers) { Console.WriteLine(member.Name); } }
public static void SetISOCurrencyMeasuresAnnotation(this EdmModel model, IEdmProperty property, string isoCurrency) { if (model == null) throw new ArgumentNullException("model"); if (property == null) throw new ArgumentNullException("property"); var target = property; var term = ISOCurrencyTerm; var expression = new EdmStringConstant(isoCurrency); var annotation = new EdmAnnotation(target, term, expression); annotation.SetSerializationLocation(model, property.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
public static void SetScaleMeasuresAnnotation(this EdmModel model, IEdmProperty property, byte scale) { if (model == null) throw new ArgumentNullException("model"); if (property == null) throw new ArgumentNullException("property"); var target = property; var term = ScaleTerm; var expression = new EdmIntegerConstant(scale); var annotation = new EdmAnnotation(target, term, expression); annotation.SetSerializationLocation(model, property.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
public static void SetComputedAnnotation(EdmModel model, IEdmProperty target) { EdmUtil.CheckArgumentNull(model, "model"); EdmUtil.CheckArgumentNull(target, "target"); IEdmBooleanConstantExpression val = new EdmBooleanConstant(true); IEdmValueTerm term = CoreVocabularyModel.ComputedTerm; Debug.Assert(term != null, "term!=null"); EdmAnnotation annotation = new EdmAnnotation(target, term, val); annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(annotation); }
public static IEdmModel IsTypeResultFalseEvaluationModel() { var model = new EdmModel(); var booleanFlag = new EdmTerm("NS", "BooleanFlag", EdmCoreModel.Instance.GetBoolean(true)); model.AddElement(booleanFlag); var valueAnnotation = new EdmAnnotation( booleanFlag, booleanFlag, new EdmIsTypeExpression(new EdmIntegerConstant(32), EdmCoreModel.Instance.GetString(true))); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine); model.AddVocabularyAnnotation(valueAnnotation); return model; }
public static IEdmModel InvalidPropertyTypeUsingIsTypeOnInlineAnnotationModel() { var model = new EdmModel(); var bike = new EdmComplexType("NS", "Bike"); bike.AddStructuralProperty("Color", EdmCoreModel.Instance.GetString(true)); model.AddElement(bike); var car = new EdmComplexType("NS", "Car"); var carExpensive = car.AddStructuralProperty("Expensive", new EdmComplexTypeReference(bike, true)); model.AddElement(car); var carTerm = new EdmTerm("NS", "CarTerm", new EdmComplexTypeReference(car, true)); model.AddElement(carTerm); var valueAnnotation = new EdmAnnotation( car, carTerm, new EdmRecordExpression( new EdmPropertyConstructor(carExpensive.Name, new EdmIsTypeExpression(new EdmStringConstant("foo"), EdmCoreModel.Instance.GetString(true))))); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.AddVocabularyAnnotation(valueAnnotation); return model; }
public void ConstructibleVocabularyAddingValueAnnotationToNewElement() { EdmModel model = VocabularyTestModelBuilder.InlineAnnotationSimpleModel(); var vocabularyAnnotations = model.VocabularyAnnotations; Assert.AreEqual(1, vocabularyAnnotations.Count(), "Invalid vocabulary annotation count."); var container = model.FindEntityContainer("Container") as EdmEntityContainer; Assert.IsNotNull(container, "Invalid entity container name."); var carType = model.FindEntityType("DefaultNamespace.Car"); Assert.IsNotNull(carType, "Invalid entity type."); EdmEntitySet carSet = container.AddEntitySet("CarSet", carType); var person = model.FindEntityType("AnnotationNamespace.Person") as EdmEntityType; Assert.IsNotNull(person, "Invalid entity type."); EdmTerm personTerm = new EdmTerm("AnnotationNamespace", "PersonTerm", new EdmEntityTypeReference(person, true)); EdmRecordExpression recordOfPerson = new EdmRecordExpression( new EdmPropertyConstructor("Id", new EdmIntegerConstant(22)), new EdmPropertyConstructor("Name", new EdmStringConstant("Johnny"))); EdmAnnotation valueAnnotation = new EdmAnnotation( carSet, personTerm, recordOfPerson); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.AddVocabularyAnnotation(valueAnnotation); vocabularyAnnotations = model.VocabularyAnnotations; Assert.AreEqual(2, vocabularyAnnotations.Count(), "Invalid vocabulary annotation count."); List<PropertyValue> listOfNames = new List<PropertyValue> { new PropertyValue("Id", "22"), new PropertyValue("Name", "Johnny") }; var valueAnnotationFound = this.CheckForValueAnnotation(vocabularyAnnotations, EdmExpressionKind.Record, listOfNames); Assert.IsTrue(valueAnnotationFound, "Annotation can't be found."); var containerCarSet = container.EntitySets().Where(x => x.Name.Equals("CarSet")).SingleOrDefault() as EdmEntitySet; Assert.IsNotNull(containerCarSet, "Entity set did not get added to container properly."); var carSetVocabularAnnotation = containerCarSet.VocabularyAnnotations(model); Assert.AreEqual(1, carSetVocabularAnnotation.Count(), "Invalid vocabulary annotation count."); valueAnnotationFound = this.CheckForValueAnnotation(carSetVocabularAnnotation, EdmExpressionKind.Record, listOfNames); Assert.IsTrue(valueAnnotationFound, "Annotation can't be found."); }
public static void SetConformanceLevelCapabilitiesAnnotation(this EdmModel model, IEdmEntityContainer container, CapabilitiesConformanceLevelType level) { if (model == null) throw new ArgumentNullException("model"); if (container == null) throw new ArgumentNullException("container"); var target = container; var term = ConformanceLevelTerm; var name = new EdmEnumTypeReference(ConformanceLevelTypeType, false).ToStringLiteral((long)level); var expression = new EdmEnumMemberReferenceExpression(ConformanceLevelTypeType.Members.Single(m => m.Name == name)); var annotation = new EdmAnnotation(target, term, expression); annotation.SetSerializationLocation(model, container.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
private static void SetCapabilitiesAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target, IEdmValueTerm term, IEnumerable<string> values) { if (values == null) { values = new string[0]; } var expression = new EdmCollectionExpression(values.Select(function => new EdmStringConstant(function))); var annotation = new EdmAnnotation(target, term, expression); annotation.SetSerializationLocation(model, target.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
public static void SetSearchRestrictionsCapabilitiesAnnotation(this EdmModel model, IEdmEntitySet entitySet, bool searchable, CapabilitiesSearchExpressions unsupported) { if (model == null) throw new ArgumentNullException("model"); if (entitySet == null) throw new ArgumentNullException("entitySet"); var target = entitySet; var term = SearchRestrictionsTerm; var name = new EdmEnumTypeReference(SearchExpressionsType, false).ToStringLiteral((long)unsupported); var properties = new IEdmPropertyConstructor[] { new EdmPropertyConstructor("Searchable", new EdmBooleanConstant(searchable)), new EdmPropertyConstructor("UnsupportedExpressions", new EdmEnumMemberReferenceExpression(SearchExpressionsType.Members.Single(m => m.Name == name))), }; var record = new EdmRecordExpression(properties); var annotation = new EdmAnnotation(target, term, record); annotation.SetSerializationLocation(model, entitySet.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
public void ExceptionThrowForInvalidPropertyPath() { EdmModel model = new EdmModel(); EdmEntityType personType = new EdmEntityType("MyNs", "Person", null, false, false, true); personType.AddKeys(personType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); personType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(isNullable: true)); var container = new EdmEntityContainer("MyNs", "Container"); model.AddElement(personType); container.AddEntitySet("People", personType); model.AddElement(container); IEdmEntitySet peopleSet = model.FindDeclaredEntitySet("People"); IEdmPathExpression nameExpression = new EdmPropertyPathExpression("NameName"); IEdmCollectionExpression collection = new EdmCollectionExpression(new[] { nameExpression }); IEdmValueTerm term = null; foreach (var referencedModel in model.ReferencedModels) { term = referencedModel.FindDeclaredValueTerm("Org.OData.Core.V1.OptimisticConcurrencyControl"); if (term != null) { break; } } Assert.NotNull(term); EdmAnnotation valueAnnotationOnEntitySet = new EdmAnnotation(peopleSet, term, collection); valueAnnotationOnEntitySet.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.AddVocabularyAnnotation(valueAnnotationOnEntitySet); ODataEntry entry = new ODataEntry { Properties = new[] { new ODataProperty {Name = "ID", Value = 123}, new ODataProperty {Name = "Name", Value = "lucy"}, } }; Action action = () => GetWriterOutputForContentTypeAndKnobValue(entry, model, peopleSet, personType); action.ShouldThrow<ODataException>().WithMessage(ErrorStrings.EdmValueUtils_PropertyDoesntExist("MyNs.Person", "NameName")); }
private static void SetVocabularyAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target, IList<IEdmPropertyConstructor> properties, string qualifiedName) { Contract.Assert(model != null); Contract.Assert(target != null); IEdmValueTerm term = model.FindValueTerm(qualifiedName); if (term != null) { IEdmRecordExpression record = new EdmRecordExpression(properties); EdmAnnotation annotation = new EdmAnnotation(target, term, record); annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(annotation); } }
private static void SetCoreChangeTrackingAnnotation(this EdmModel model, EdmEntitySet entitySet, IEdmStructuralProperty[] filterableProperties, IEdmNavigationProperty[] expandableProperties) { IEdmModel termModel = ReadTermModel("CoreCapabilities.csdl"); IEdmValueTerm changeTracking = termModel.FindDeclaredValueTerm("Core.ChangeTracking"); var exp = new EdmRecordExpression( new EdmPropertyConstructor("Supported", new EdmBooleanConstant(true)), new EdmPropertyConstructor("FilterableProperties", new EdmCollectionExpression(filterableProperties.Select(p => new EdmPropertyPathExpression(p.Name)))), new EdmPropertyConstructor("ExpandableProperties", new EdmCollectionExpression(expandableProperties.Select(p => new EdmPropertyPathExpression(p.Name))))); EdmAnnotation annotation = new EdmAnnotation(entitySet, changeTracking, exp); annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.AddVocabularyAnnotation(annotation); }
public static EdmModel OutOfLineValueAnnotationWithAnnotationModel() { EdmModel model = new EdmModel(); EdmComplexType simpleType = new EdmComplexType("DefaultNamespace", "SimpleType"); simpleType.AddStructuralProperty("Data", EdmCoreModel.Instance.GetString(true)); model.AddElement(simpleType); EdmTerm note = new EdmTerm("DefaultNamespace", "Note", EdmCoreModel.Instance.GetString(true)); model.AddElement(note); EdmAnnotation valueAnnotation = new EdmAnnotation( simpleType, note, new EdmStringConstant("ComplexTypeNote")); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine); model.AddVocabularyAnnotation(valueAnnotation); XElement annotationElement = new XElement("{http://foo}Annotation", "1"); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(valueAnnotation, "http://foo", "Annotation", annotation); return model; }
public void TestEnumMemberReferencingExtraEnumType() { const string csdl = @"<?xml version=""1.0"" encoding=""utf-8""?> <edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx""> <edmx:DataServices> <Schema Namespace=""TestNS"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <EntityType Name=""Person""> <Key> <PropertyRef Name=""Id"" /> </Key> <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"" /> <Property Name=""Name"" Type=""Edm.String"" /> <Annotation Term=""TestNS.OutColor""> <EnumMember>TestNS2.Color/Blue TestNS2.Color/Cyan</EnumMember> </Annotation> </EntityType> <Term Name=""OutColor"" Type=""TestNS2.Color"" /> </Schema> <Schema Namespace=""TestNS2"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <EnumType Name=""Color"" IsFlags=""true""> <Member Name=""Cyan"" Value=""1"" /> <Member Name=""Blue"" Value=""2"" /> </EnumType> </Schema> </edmx:DataServices> </edmx:Edmx>"; IEdmModel model; IEnumerable<EdmError> errors; IEnumerable<EdmError> validationErrors; #region try build model var edmModel = new EdmModel(); var personType = new EdmEntityType("TestNS", "Person"); edmModel.AddElement(personType); var pid = personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false); personType.AddKeys(pid); personType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); var colorType = new EdmEnumType("TestNS2", "Color", true); edmModel.AddElement(colorType); colorType.AddMember("Cyan", new EdmIntegerConstant(1)); colorType.AddMember("Blue", new EdmIntegerConstant(2)); var outColorTerm = new EdmTerm("TestNS", "OutColor", new EdmEnumTypeReference(colorType, true)); edmModel.AddElement(outColorTerm); var exp = new EdmEnumMemberExpression( new EdmEnumMember(colorType, "Blue", new EdmIntegerConstant(2)), new EdmEnumMember(colorType, "Cyan", new EdmIntegerConstant(1)) ); var annotation = new EdmAnnotation(personType, outColorTerm, exp); annotation.SetSerializationLocation(edmModel, EdmVocabularyAnnotationSerializationLocation.Inline); edmModel.SetVocabularyAnnotation(annotation); var stream = new MemoryStream(); Assert.IsFalse(edmModel.Validate(out errors)); using (var xw = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true })) { Assert.IsTrue(EdmxWriter.TryWriteEdmx(edmModel, xw, EdmxTarget.OData, out errors)); } stream.Seek(0, SeekOrigin.Begin); using (var sr = new StreamReader(stream)) { Assert.AreEqual(csdl, sr.ReadToEnd()); } #endregion Assert.IsTrue(EdmxReader.TryParse(XmlReader.Create(new StringReader(csdl)), out model, out errors), "parsed"); Assert.IsFalse(model.Validate(out validationErrors)); TestEnumMember(model); }
public void ConstructibleVocabularyRemovingInvalidAnnotation() { var model = new FunctionalUtilities.ModelWithRemovableElements<EdmModel>(VocabularyTestModelBuilder.InlineAnnotationSimpleModel()); Assert.AreEqual(1, model.VocabularyAnnotations.Count(), "Invalid vocabulary annotation count."); var address = model.SchemaElements.Where(x => x.Name.Equals("Address")).SingleOrDefault() as EdmComplexType; var addressStreet = address.FindProperty("Street"); Assert.IsNotNull(address, "Invalid complex type property."); Assert.AreEqual(0, addressStreet.VocabularyAnnotations(model).Count(), "Invalid vocabulary annotation count."); EdmEntityType petType = new EdmEntityType("DefaultNamespace", "Pet"); EdmStructuralProperty petBreed = petType.AddStructuralProperty("Breed", EdmCoreModel.Instance.GetString(false)); model.WrappedModel.AddElement(petType); var petTerm = new EdmTerm("DefaultNamespace", "PetTerm", new EdmEntityTypeReference(petType, false)); model.WrappedModel.AddElement(petType); EdmAnnotation annotation = new EdmAnnotation( addressStreet, petTerm, new EdmRecordExpression(new EdmPropertyConstructor(petBreed.Name, new EdmStringConstant("Fluffy")))); annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.RemoveVocabularyAnnotation(annotation); Assert.AreEqual(1, model.VocabularyAnnotations.Count(), "Invalid vocabulary annotation count."); Assert.AreEqual(0, addressStreet.VocabularyAnnotations(model).Count(), "Invalid vocabulary annotation count."); }
public void ConstructibleVocabularyAddingValueAnnotationAndDeleteTargetedElement() { var model = new FunctionalUtilities.ModelWithRemovableElements<EdmModel>(VocabularyTestModelBuilder.InlineAnnotationSimpleModel()); var vocabularyAnnotations = model.VocabularyAnnotations; Assert.AreEqual(1, vocabularyAnnotations.Count(), "Invalid vocabulary annotation count."); var container = model.FindEntityContainer("Container") as EdmEntityContainer; Assert.IsNotNull(container, "Invalid entity container name."); var stringTerm = model.FindValueTerm("AnnotationNamespace.StringTerm") as EdmTerm; Assert.IsNotNull(stringTerm, "Invalid value term."); EdmAnnotation valueAnnotation = new EdmAnnotation( container, stringTerm, new EdmStringConstant("foo")); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.WrappedModel.AddVocabularyAnnotation(valueAnnotation); Assert.AreEqual(2, vocabularyAnnotations.Count(), "Invalid vocabulary annotation count."); var valueAnnotationFound = this.CheckForValueAnnotation(vocabularyAnnotations, EdmExpressionKind.StringConstant, new List<PropertyValue> { new PropertyValue("foo") }); Assert.IsTrue(valueAnnotationFound, "Annotation can't be found."); var containerVocabularyAnnotations = container.VocabularyAnnotations(model); valueAnnotationFound = this.CheckForValueAnnotation(containerVocabularyAnnotations, EdmExpressionKind.StringConstant, new List<PropertyValue> { new PropertyValue("foo") }); Assert.IsTrue(valueAnnotationFound, "Annotation can't be found."); model.RemoveElement(container); model.FindDeclaredVocabularyAnnotations(container).ToList().ForEach(a => model.RemoveVocabularyAnnotation(a)); Assert.AreEqual(1, vocabularyAnnotations.Count(), "Invalid vocabulary annotation count."); }
public static IEdmModel ValueAnnotationValidDefaultDurationConstantModel() { var model = new EdmModel(); var valueTerm = new EdmTerm("DefaultNamespace", "Note", EdmCoreModel.Instance.GetDuration(true)); model.AddElement(valueTerm); var valueAnnotation = new EdmAnnotation( valueTerm, valueTerm, new EdmDurationConstant(new TimeSpan())); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine); model.AddVocabularyAnnotation(valueAnnotation); return model; }
public static IEdmModel ValueAnnotationInvalidTypeReferenceDurationConstantModel() { var model = new EdmModel(); var valueTerm = new EdmTerm("DefaultNamespace", "Note", EdmCoreModel.Instance.GetDuration(true)); model.AddElement(valueTerm); var valueAnnotation = new EdmAnnotation( valueTerm, valueTerm, new EdmDurationConstant(EdmCoreModel.Instance.GetDateTimeOffset(false), new TimeSpan(1, 99, 99, 99, 999))); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine); model.AddVocabularyAnnotation(valueAnnotation); return model; }
private static void EdmWriteAnnotationDemo() { Console.WriteLine("EdmWriteAnnotationDemo"); var model = new EdmModel(); var mail = new EdmEntityType("ns", "Mail"); model.AddElement(mail); mail.AddKeys(mail.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); var person = new EdmEntityType("ns", "Person"); model.AddElement(person); person.AddKeys(person.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); var mails = person.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { ContainsTarget = true, Name = "Mails", TargetMultiplicity = EdmMultiplicity.Many, Target = mail, }); var ann1 = new EdmAnnotation(mails, CoreVocabularyModel.DescriptionTerm, new EdmStringConstant("test1")); ann1.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.AddVocabularyAnnotation(ann1); var container = new EdmEntityContainer("ns", "container"); model.AddElement(container); var people = container.AddEntitySet("People", person); var ann2 = new EdmAnnotation(people, CoreVocabularyModel.DescriptionTerm, new EdmStringConstant("test2")); model.AddVocabularyAnnotation(ann2); ShowModel(model); }
public static void SetNavigationRestrictionsCapabilitiesAnnotation(this EdmModel model, IEdmEntitySet entitySet, CapabilitiesNavigationType type, IEnumerable<Tuple<IEdmNavigationProperty, CapabilitiesNavigationType>> properties) { if (model == null) throw new ArgumentNullException("model"); if (entitySet == null) throw new ArgumentNullException("entitySet"); if (properties == null) { properties = new Tuple<IEdmNavigationProperty, CapabilitiesNavigationType>[0]; } var target = entitySet; var term = NavigationRestrictionsTerm; // handle type var typeLiteral = new EdmEnumTypeReference(NavigationTypeType, false).ToStringLiteral((long)type); // handle properties var propertiesExpression = properties.Select(p => { var name = new EdmEnumTypeReference(NavigationTypeType, false).ToStringLiteral((long)p.Item2); return new EdmRecordExpression(new IEdmPropertyConstructor[] { new EdmPropertyConstructor("NavigationProperty", new EdmNavigationPropertyPathExpression(p.Item1.Name)), new EdmPropertyConstructor("Navigability", new EdmEnumMemberReferenceExpression(NavigationTypeType.Members.Single(m => m.Name == name))), }); }); var record = new EdmRecordExpression(new IEdmPropertyConstructor[] { new EdmPropertyConstructor("Navigability", new EdmEnumMemberReferenceExpression(NavigationTypeType.Members.Single(m => m.Name == typeLiteral))), new EdmPropertyConstructor("RestrictedProperties", new EdmCollectionExpression(propertiesExpression)) }); var annotation = new EdmAnnotation(target, term, record); annotation.SetSerializationLocation(model, entitySet.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
public static IEdmModel IsTypeResultTrueEvaluationModel() { var model = new EdmModel(); var booleanFlag = new EdmComplexType("NS", "BooleanFlag"); var flag = booleanFlag.AddStructuralProperty("Flag", EdmCoreModel.Instance.GetBoolean(true)); model.AddElement(booleanFlag); var booleanFlagTerm = new EdmTerm("NS", "BooleanFlagTerm", new EdmComplexTypeReference(booleanFlag, true)); model.AddElement(booleanFlagTerm); var valueAnnotation = new EdmAnnotation( booleanFlag, booleanFlagTerm, new EdmRecordExpression( new EdmPropertyConstructor(flag.Name, new EdmIsTypeExpression(new EdmStringConstant("foo"), EdmCoreModel.Instance.GetString(true))))); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine); model.AddVocabularyAnnotation(valueAnnotation); return model; }
private static void SetCapabilitiesAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target, IEdmValueTerm term, bool value, IEnumerable<IEdmNavigationProperty> navigationProperties, string name1, string name2) { if (navigationProperties == null) { navigationProperties = new IEdmNavigationProperty[0]; } var properties = new IEdmPropertyConstructor[] { new EdmPropertyConstructor(name1, new EdmBooleanConstant(value)), new EdmPropertyConstructor(name2, new EdmCollectionExpression(navigationProperties.Select(p => new EdmNavigationPropertyPathExpression(p.Name)))), }; var record = new EdmRecordExpression(properties); var annotation = new EdmAnnotation(target, term, record); annotation.SetSerializationLocation(model, target.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
public static IEdmModel CastNullableToNonNullableOnInlineAnnotationModel() { var model = new EdmModel(); var address = new EdmComplexType("NS", "Address"); address.AddStructuralProperty("StreetNumber", EdmCoreModel.Instance.GetInt32(true)); address.AddStructuralProperty("StreetName", EdmCoreModel.Instance.GetString(true)); model.AddElement(address); var friend = new EdmComplexType("NS", "Friend"); friend.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(true)); friend.AddStructuralProperty("NickNames", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(true))); friend.AddStructuralProperty("Address", new EdmComplexTypeReference(address, true)); model.AddElement(friend); var friendInfo = new EdmTerm("NS", "FriendInfo", EdmCoreModel.GetCollection(new EdmComplexTypeReference(friend, true))); model.AddElement(friendInfo); var valueAnnotationCast = new EdmCastExpression(new EdmCollectionExpression(new EdmStringConstant("foo"), new EdmStringConstant("bar")), new EdmComplexTypeReference(friend, true)); var valueAnnotation = new EdmAnnotation( friendInfo, friendInfo, valueAnnotationCast); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.AddVocabularyAnnotation(valueAnnotation); return model; }
private static void SetCapabilitiesAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target, IEdmValueTerm term, bool value) { var expression = new EdmBooleanConstant(value); var annotation = new EdmAnnotation(target, term, expression); annotation.SetSerializationLocation(model, target.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
public void AnnotationValueAsEnumMemberReferenceExpressionSerializationTest() { EdmModel model = new EdmModel(); EdmEntityContainer container = new EdmEntityContainer("Ns", "Container"); var personType = new EdmEntityType("Ns", "Person"); var propertyId = personType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)); personType.AddKeys(propertyId); var structuralProperty = personType.AddStructuralProperty("Concurrency", EdmCoreModel.Instance.GetInt32(true)); model.AddElement(personType); var entitySet = container.AddEntitySet("People", personType); var permissionType = new EdmEnumType("Ns", "Permissions", true); var read = permissionType.AddMember("Read", new EdmIntegerConstant(1)); var write = permissionType.AddMember("Write", new EdmIntegerConstant(2)); var delete = permissionType.AddMember("Delete", new EdmIntegerConstant(4)); model.AddElement(permissionType); var enumTerm = new EdmTerm("Ns", "Permission", new EdmEnumTypeReference(permissionType, false)); model.AddElement(enumTerm); model.AddElement(container); EdmAnnotation personIdAnnotation = new EdmAnnotation(propertyId, enumTerm, new EdmEnumMemberReferenceExpression(read)); personIdAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(personIdAnnotation); EdmAnnotation structuralPropertyAnnotation = new EdmAnnotation(structuralProperty, enumTerm, new EdmEnumMemberExpression(read, write)); structuralPropertyAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(structuralPropertyAnnotation); EdmAnnotation entitySetAnnotation = new EdmAnnotation(entitySet, enumTerm, new EdmEnumMemberReferenceExpression(delete)); structuralPropertyAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(entitySetAnnotation); var idAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(propertyId, enumTerm).FirstOrDefault().Value as IEdmEnumMemberReferenceExpression).ReferencedEnumMember; var structuralPropertyAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(structuralProperty, enumTerm).FirstOrDefault().Value as IEdmEnumMemberExpression).EnumMembers; var entitySetAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(entitySet, enumTerm).FirstOrDefault().Value as IEdmEnumMemberReferenceExpression).ReferencedEnumMember; var enumTypeAnnotationValue = model.FindVocabularyAnnotations<IEdmValueAnnotation>(permissionType, enumTerm).FirstOrDefault(); Assert.AreEqual(read, idAnnotationValue); Assert.AreEqual(2, structuralPropertyAnnotationValue.Count()); Assert.AreEqual(delete, entitySetAnnotationValue); Assert.IsNull(enumTypeAnnotationValue); const string expected = @"<?xml version=""1.0"" encoding=""utf-16""?> <Schema Namespace=""Ns"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <EntityType Name=""Person""> <Key> <PropertyRef Name=""Id"" /> </Key> <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false""> <Annotation Term=""Ns.Permission""> <EnumMember>Ns.Permissions/Read</EnumMember> </Annotation> </Property> <Property Name=""Concurrency"" Type=""Edm.Int32""> <Annotation Term=""Ns.Permission""> <EnumMember>Ns.Permissions/Read Ns.Permissions/Write</EnumMember> </Annotation> </Property> </EntityType> <EnumType Name=""Permissions"" IsFlags=""true""> <Member Name=""Read"" Value=""1"" /> <Member Name=""Write"" Value=""2"" /> <Member Name=""Delete"" Value=""4"" /> </EnumType> <Term Name=""Permission"" Type=""Ns.Permissions"" Nullable=""false"" /> <EntityContainer Name=""Container""> <EntitySet Name=""People"" EntityType=""Ns.Person"" /> </EntityContainer> <Annotations Target=""Ns.Container/People""> <Annotation Term=""Ns.Permission""> <EnumMember>Ns.Permissions/Delete</EnumMember> </Annotation> </Annotations> </Schema>"; IEnumerable<EdmError> errors; StringWriter sw = new StringWriter(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.Encoding = System.Text.Encoding.UTF8; XmlWriter xw = XmlWriter.Create(sw, settings); model.TryWriteCsdl(xw, out errors); xw.Flush(); xw.Close(); var actual = sw.ToString(); Assert.IsTrue(!errors.Any(), "No errors"); Assert.AreEqual(expected, actual); }
public static IEdmModel CastResultTrueEvaluationModel() { var model = new EdmModel(); var address = new EdmComplexType("NS", "Address"); address.AddStructuralProperty("StreetNumber", EdmCoreModel.Instance.GetInt32(true)); address.AddStructuralProperty("StreetName", EdmCoreModel.Instance.GetString(true)); model.AddElement(address); var friend = new EdmEntityType("NS", "Friend"); var friendName = friend.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false)); friend.AddKeys(friendName); var friendAddress = friend.AddStructuralProperty("Address", new EdmComplexTypeReference(address, true)); model.AddElement(friend); var addressRecord = new EdmRecordExpression(new EdmPropertyConstructor[] { new EdmPropertyConstructor("StreetNumber", new EdmIntegerConstant(3)), new EdmPropertyConstructor("StreetName", new EdmStringConstant("에O詰 갂คำŚёæ")) }); var friendAddressCast = new EdmCastExpression(addressRecord, new EdmComplexTypeReference(address, true)); var friendTerm = new EdmTerm("NS", "FriendTerm", new EdmEntityTypeReference(friend, true)); model.AddElement(friendTerm); var valueAnnotation = new EdmAnnotation( friend, friendTerm, new EdmRecordExpression( new EdmPropertyConstructor(friendName.Name, new EdmStringConstant("foo")), new EdmPropertyConstructor(friendAddress.Name, friendAddressCast))); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine); model.AddVocabularyAnnotation(valueAnnotation); return model; }
public void TestCoreIsLanguageDependentAnnotation() { EdmModel model = new EdmModel(); EdmEntityContainer container = new EdmEntityContainer("Ns", "Container"); var personType = new EdmEntityType("Ns", "Person"); var propertyId = personType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)); personType.AddKeys(propertyId); var structuralProperty = personType.AddStructuralProperty("Concurrency", EdmCoreModel.Instance.GetInt32(true)); model.AddElement(personType); var entitySet = container.AddEntitySet("People", personType); var stringTerm = new EdmTerm("Ns", "HomeAddress", EdmCoreModel.Instance.GetString(true)); model.AddElement(stringTerm); model.AddElement(container); IEdmValueTerm term = CoreVocabularyModel.IsLanguageDependentTerm; IEdmBooleanConstantExpression trueExpression = new EdmBooleanConstant(true); IEdmBooleanConstantExpression falseExpression = new EdmBooleanConstant(false); EdmAnnotation personIdAnnotation = new EdmAnnotation(propertyId, term, trueExpression); personIdAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(personIdAnnotation); EdmAnnotation structuralPropertyAnnotation = new EdmAnnotation(structuralProperty, term, falseExpression); structuralPropertyAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(structuralPropertyAnnotation); EdmAnnotation stringTermAnnotation = new EdmAnnotation(stringTerm, term, trueExpression); stringTermAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(stringTermAnnotation); var idAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(propertyId, term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value; var structuralPropertyAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(structuralProperty, term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value; var stringTermAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(stringTerm, term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value; var entitySetAnnotation = model.FindVocabularyAnnotations<IEdmValueAnnotation>(entitySet, term).FirstOrDefault(); Assert.AreEqual(true, idAnnotationValue); Assert.AreEqual(false, structuralPropertyAnnotationValue); Assert.AreEqual(true, stringTermAnnotationValue); Assert.IsNull(entitySetAnnotation); const string expected = @"<?xml version=""1.0"" encoding=""utf-16""?> <Schema Namespace=""Ns"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <EntityType Name=""Person""> <Key> <PropertyRef Name=""Id"" /> </Key> <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false""> <Annotation Term=""Org.OData.Core.V1.IsLanguageDependent"" Bool=""true"" /> </Property> <Property Name=""Concurrency"" Type=""Edm.Int32""> <Annotation Term=""Org.OData.Core.V1.IsLanguageDependent"" Bool=""false"" /> </Property> </EntityType> <Term Name=""HomeAddress"" Type=""Edm.String""> <Annotation Term=""Org.OData.Core.V1.IsLanguageDependent"" Bool=""true"" /> </Term> <EntityContainer Name=""Container""> <EntitySet Name=""People"" EntityType=""Ns.Person"" /> </EntityContainer> </Schema>"; IEnumerable<EdmError> errors; StringWriter sw = new StringWriter(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.Encoding = System.Text.Encoding.UTF8; XmlWriter xw = XmlWriter.Create(sw, settings); model.TryWriteCsdl(xw, out errors); xw.Flush(); xw.Close(); var actual = sw.ToString(); Assert.AreEqual(expected, actual); IEdmModel parsedModel; IEnumerable<EdmError> errs; bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(expected)) }, out parsedModel, out errs); Assert.IsTrue(parsed, "parsed"); Assert.IsTrue(!errors.Any(), "No errors"); var idAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(propertyId, term).FirstOrDefault(); var propertyAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(structuralProperty, term).FirstOrDefault(); var termAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(stringTerm, term).FirstOrDefault(); entitySetAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(entitySet, term).FirstOrDefault(); Assert.AreEqual(null, propertyAnnotation); Assert.AreEqual(null, idAnnotation); Assert.AreEqual(null, termAnnotation); Assert.AreEqual(null, entitySetAnnotation); var parsedPersonType = parsedModel.FindType("Ns.Person") as IEdmEntityType; idAnnotationValue = (parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedPersonType.FindProperty("Id"), term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value; structuralPropertyAnnotationValue = (parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedPersonType.FindProperty("Concurrency"), term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value; stringTermAnnotationValue = (parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedModel.FindValueTerm("Ns.HomeAddress"), term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value; entitySetAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedModel.FindDeclaredEntitySet("People"), term).FirstOrDefault(); Assert.IsNotNull(parsedPersonType); Assert.AreEqual(false, structuralPropertyAnnotationValue); Assert.AreEqual(true, idAnnotationValue); Assert.AreEqual(true, stringTermAnnotationValue); Assert.IsNull(entitySetAnnotation); }
private static void SetComputedAnnotation(EdmModel model, IEdmProperty target) { // when 'target' is <Key> property, V4's 'Computed' also has the meaning of OData V3's 'Identity'. var val = new EdmBooleanConstant(value: true); var annotation = new EdmAnnotation(target, CoreVocabularyModel.ComputedTerm, val); annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.SetVocabularyAnnotation(annotation); }
public static IEdmModel InvalidPropertyTypeUsingIsTypeOnOutOfLineAnnotationModel() { var model = new EdmModel(); var friendName = new EdmTerm("NS", "FriendName", EdmCoreModel.Instance.GetString(true)); model.AddElement(friendName); var valueAnnotation = new EdmAnnotation( friendName, friendName, new EdmIsTypeExpression(new EdmStringConstant("foo"), EdmCoreModel.Instance.GetString(true))); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine); model.AddVocabularyAnnotation(valueAnnotation); return model; }