static CustomInstanceAnnotationAcceptanceTests() { Model = new EdmModel(); EntityType = new EdmEntityType("TestNamespace", "TestEntityType"); Model.AddElement(EntityType); var keyProperty = new EdmStructuralProperty(EntityType, "ID", EdmCoreModel.Instance.GetInt32(false)); EntityType.AddKeys(new IEdmStructuralProperty[] { keyProperty }); EntityType.AddProperty(keyProperty); var resourceNavigationProperty = EntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "ResourceNavigationProperty", Target = EntityType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne }); var resourceSetNavigationProperty = EntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "ResourceSetNavigationProperty", Target = EntityType, TargetMultiplicity = EdmMultiplicity.Many }); var defaultContainer = new EdmEntityContainer("TestNamespace", "DefaultContainer"); Model.AddElement(defaultContainer); EntitySet = new EdmEntitySet(defaultContainer, "TestEntitySet", EntityType); EntitySet.AddNavigationTarget(resourceNavigationProperty, EntitySet); EntitySet.AddNavigationTarget(resourceSetNavigationProperty, EntitySet); defaultContainer.AddElement(EntitySet); ComplexType = new EdmComplexType("TestNamespace", "TestComplexType"); ComplexType.AddProperty(new EdmStructuralProperty(ComplexType, "StringProperty", EdmCoreModel.Instance.GetString(false))); Model.AddElement(ComplexType); }
public void BuildKeyMappingsKeySegmentTemplate_ReturnsKeyMapping_UriTemplateExpression() { // Arrange EdmEntityType customerType = new EdmEntityType("NS", "Customer"); EdmStructuralProperty idProperty = customerType.AddStructuralProperty("customerId", EdmPrimitiveTypeKind.Int32); customerType.AddKeys(idProperty); UriTemplateExpression tempateExpression = BuildExpression("{yourId}", idProperty.Type); IDictionary <string, object> keys = new Dictionary <string, object> { { "customerId", tempateExpression } }; // Act IDictionary <string, string> mapped = KeySegmentTemplate.BuildKeyMappings(keys, customerType); // Assert Assert.NotNull(mapped); KeyValuePair <string, string> actual = Assert.Single(mapped); Assert.Equal("customerId", actual.Key); Assert.Equal("yourId", actual.Value); }
public void CreatePropertySchemaForNullableEnumPropertyReturnSchema() { // Arrange IEdmModel model = EdmModelHelper.BasicEdmModel; ODataContext context = new ODataContext(model); IEdmEnumType enumType = model.SchemaElements.OfType <IEdmEnumType>().First(e => e.Name == "Color"); EdmEntityType entitType = new EdmEntityType("NS", "Entity"); IEdmProperty property = new EdmStructuralProperty(entitType, "ColorEnumValue", new EdmEnumTypeReference(enumType, true), "yellow"); // Act var schema = context.CreatePropertySchema(property); Assert.NotNull(schema); string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); _output.WriteLine(json); // Assert Assert.Equal(@"{ ""anyOf"": [ { ""$ref"": ""#/components/schemas/DefaultNs.Color"" } ], ""default"": ""yellow"", ""nullable"": true }".ChangeLineBreaks(), json); }
public static IEdmModel Build() { var addressType = new EdmComplexType(Namespace, "address"); addressType.AddStructuralProperty("street", EdmPrimitiveTypeKind.String); addressType.AddStructuralProperty("city", EdmPrimitiveTypeKind.String); var addressTypeRef = new EdmComplexTypeReference(addressType, isNullable: false); var addressesTypeRef = EdmCoreModel.GetCollection(addressTypeRef); var nonNullableStringTypeRef = EdmCoreModel.Instance.GetString(isNullable: false); var tagsTypeRef = EdmCoreModel.GetCollection(nonNullableStringTypeRef); var docEntityType = new EdmEntityType(Namespace, DocEntityTypeName); EdmStructuralProperty key = docEntityType.AddStructuralProperty("id", EdmPrimitiveTypeKind.String); docEntityType.AddKeys(key); docEntityType.AddStructuralProperty("addresses", addressesTypeRef); docEntityType.AddStructuralProperty("tags", tagsTypeRef); var docEntitySetType = EdmCoreModel.GetCollection(new EdmEntityTypeReference(docEntityType, isNullable: true)); var container = new EdmEntityContainer("Default", "Container"); container.AddEntitySet("Docs", docEntityType); var model = new EdmModel(); model.AddElements(new IEdmSchemaElement[] { container, addressType, docEntityType }); return(model); }
public void CtorKeySegmentTemplate_SetsProperties_ForCompositeKeys() { // Arrange & Act EdmEntityType customerType = new EdmEntityType("NS", "Customer"); EdmStructuralProperty firstProperty = customerType.AddStructuralProperty("firstName", EdmPrimitiveTypeKind.String); EdmStructuralProperty lastProperty = customerType.AddStructuralProperty("lastName", EdmPrimitiveTypeKind.String); customerType.AddKeys(firstProperty, lastProperty); IDictionary <string, object> keys = new Dictionary <string, object> { { "firstName", "{key1}" }, { "lastName", "{key2}" } }; KeySegment segment = new KeySegment(keys, customerType, null); KeySegmentTemplate template = new KeySegmentTemplate(segment); // Assert Assert.Collection(template.KeyMappings, e => { Assert.Equal("firstName", e.Key); Assert.Equal("key1", e.Value); }, e => { Assert.Equal("lastName", e.Key); Assert.Equal("key2", e.Value); }); }
public void VerifyAnnotationComputedConcurrency() { var model = new EdmModel(); var entity = new EdmEntityType("NS1", "Product"); var entityId = entity.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)); entity.AddKeys(entityId); EdmStructuralProperty name1 = entity.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false)); EdmStructuralProperty timeVer = entity.AddStructuralProperty("UpdatedTime", EdmCoreModel.Instance.GetDate(false)); model.AddElement(entity); SetComputedAnnotation(model, entityId); // semantic meaning is V3's 'Identity' for Key profperty SetComputedAnnotation(model, timeVer); // semantic meaning is V3's 'Computed' for non-key profperty var entityContainer = new EdmEntityContainer("NS1", "Container"); model.AddElement(entityContainer); EdmEntitySet set1 = new EdmEntitySet(entityContainer, "Products", entity); model.SetOptimisticConcurrencyAnnotation(set1, new IEdmStructuralProperty[] { entityId, timeVer }); entityContainer.AddElement(set1); string csdlStr = GetEdmx(model, EdmxTarget.OData); Assert.AreEqual(@"<?xml version=""1.0"" encoding=""utf-16""?><edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx""><edmx:DataServices><Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""><EntityType Name=""Product""><Key><PropertyRef Name=""Id"" /></Key><Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false""><Annotation Term=""Org.OData.Core.V1.Computed"" Bool=""true"" /></Property><Property Name=""Name"" Type=""Edm.String"" Nullable=""false"" /><Property Name=""UpdatedTime"" Type=""Edm.Date"" Nullable=""false""><Annotation Term=""Org.OData.Core.V1.Computed"" Bool=""true"" /></Property></EntityType><EntityContainer Name=""Container""><EntitySet Name=""Products"" EntityType=""NS1.Product""><Annotation Term=""Org.OData.Core.V1.OptimisticConcurrency""><Collection><PropertyPath>Id</PropertyPath><PropertyPath>UpdatedTime</PropertyPath></Collection></Annotation></EntitySet></EntityContainer></Schema></edmx:DataServices></edmx:Edmx>", csdlStr); }
public void GetModel(EdmModel model, EdmEntityContainer container) { EdmEntityType product = new EdmEntityType("ns", "Product"); product.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); EdmStructuralProperty key = product.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32); product.AddKeys(key); model.AddElement(product); EdmEntitySet products = container.AddEntitySet("Products", product); EdmEntityType detailInfo = new EdmEntityType("ns", "DetailInfo"); detailInfo.AddKeys(detailInfo.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); detailInfo.AddStructuralProperty("Title", EdmPrimitiveTypeKind.String); model.AddElement(detailInfo); EdmEntitySet detailInfos = container.AddEntitySet("DetailInfos", product); EdmNavigationProperty detailInfoNavProp = product.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "DetailInfo", TargetMultiplicity = EdmMultiplicity.One, Target = detailInfo }); products.AddNavigationTarget(detailInfoNavProp, detailInfos); }
public void EdmSingletonAnnotationTests() { EdmModel model = new EdmModel(); EdmStructuralProperty customerProperty = new EdmStructuralProperty(customerType, "Name", EdmCoreModel.Instance.GetString(false)); customerType.AddProperty(customerProperty); model.AddElement(this.customerType); EdmSingleton vipCustomer = new EdmSingleton(this.entityContainer, "VIP", this.customerType); EdmTerm term = new EdmTerm(myNamespace, "SingletonAnnotation", EdmPrimitiveTypeKind.String); var annotation = new EdmVocabularyAnnotation(vipCustomer, term, new EdmStringConstant("Singleton Annotation")); model.AddVocabularyAnnotation(annotation); var singletonAnnotation = vipCustomer.VocabularyAnnotations(model).Single(); Assert.Equal(vipCustomer, singletonAnnotation.Target); Assert.Equal("SingletonAnnotation", singletonAnnotation.Term.Name); singletonAnnotation = model.FindDeclaredVocabularyAnnotations(vipCustomer).Single(); Assert.Equal(vipCustomer, singletonAnnotation.Target); Assert.Equal("SingletonAnnotation", singletonAnnotation.Term.Name); EdmTerm propertyTerm = new EdmTerm(myNamespace, "SingletonPropertyAnnotation", EdmPrimitiveTypeKind.String); var propertyAnnotation = new EdmVocabularyAnnotation(customerProperty, propertyTerm, new EdmStringConstant("Singleton Property Annotation")); model.AddVocabularyAnnotation(propertyAnnotation); var singletonPropertyAnnotation = customerProperty.VocabularyAnnotations(model).Single(); Assert.Equal(customerProperty, singletonPropertyAnnotation.Target); Assert.Equal("SingletonPropertyAnnotation", singletonPropertyAnnotation.Term.Name); }
public void ParseODataUriTemplate_ForCompositeKeys(string template) { // Arrange // function with optional parameters EdmEntityType person = new EdmEntityType("NS", "Person"); EdmStructuralProperty firstName = person.AddStructuralProperty("firstName", EdmPrimitiveTypeKind.String); EdmStructuralProperty lastName = person.AddStructuralProperty("lastName", EdmPrimitiveTypeKind.String); person.AddKeys(firstName, lastName); _edmModel.AddElement(person); EdmEntityContainer container = _edmModel.SchemaElements.OfType <EdmEntityContainer>().First(); container.AddEntitySet("People", person); IODataPathTemplateParser parser = new DefaultODataPathTemplateParser(); // Act ODataPathTemplate path = parser.Parse(_edmModel, template, null); // Assert Assert.NotNull(path); Assert.Equal(2, path.Count); KeySegmentTemplate keySegment = Assert.IsType <KeySegmentTemplate>(path[1]); Assert.Equal(2, keySegment.KeyMappings.Count); Assert.Equal(new[] { "firstName", "lastName" }, keySegment.KeyMappings.Keys); Assert.Equal(new[] { "first", "last" }, keySegment.KeyMappings.Values); }
public void NonNullableBinaryPropertyWithBothMaxLengthAndDefaultValueWorks() { // Arrange ODataContext context = new ODataContext(EdmModelHelper.BasicEdmModel); EdmEntityType entitType = new EdmEntityType("NS", "Entity"); var binaryType = new EdmBinaryTypeReference(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Binary), false, false, 44); IEdmStructuralProperty property = new EdmStructuralProperty( entitType, "BinaryValue", binaryType, "T0RhdGE"); // Act var schema = context.CreatePropertySchema(property); // Assert Assert.NotNull(schema); Assert.Equal("string", schema.Type); string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); Assert.Equal(@"{ ""maxLength"": 44, ""type"": ""string"", ""format"": ""base64url"", ""default"": ""T0RhdGE"" }".ChangeLineBreaks(), json); }
public void ReadPayloadThrowExceptionWithConflictBetweenInputformatAndIeee754CompatibleValueForInt64() { EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "MyTestEntity"); EdmStructuralProperty key = entityType.AddStructuralProperty("LongId", EdmPrimitiveTypeKind.Int64, false); entityType.AddKeys(key); entityType.AddStructuralProperty("FloatId", EdmPrimitiveTypeKind.Single, false); entityType.AddStructuralProperty("DoubleId", EdmPrimitiveTypeKind.Double, false); entityType.AddStructuralProperty("DecimalId", EdmPrimitiveTypeKind.Decimal, false); model.AddElement(entityType); EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer_sub"); EdmEntitySet entitySet = container.AddEntitySet("MyTestEntitySet", entityType); model.AddElement(container); const string payload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#EntityNs.MyContainer.MyTestEntitySet/$entity\"," + "\"@odata.id\":\"http://MyTestEntity\"," + "\"LongId\":\"12\"," + "\"FloatId\":34.98," + "\"DoubleId\":56.01," + "\"DecimalId\":\"78.62\"" + "}"; IEdmModel mainModel = TestUtils.WrapReferencedModelsToMainModel("EntityNs", "MyContainer", model); ODataEntry entry = null; Action test = () => this.ReadEntryPayload(mainModel, payload, entitySet, entityType, reader => { entry = entry ?? reader.Item as ODataEntry; }, false); test.ShouldThrow <ODataException>().WithMessage(ODataErrorStrings.ODataJsonReaderUtils_ConflictBetweenInputFormatAndParameter("Edm.Int64")); }
public void Property_ResourceInstance_HandlesModelClrNameDifferences() { // Arrange const string clrPropertyName = "Property"; const string modelPropertyName = "DifferentProperty"; EdmComplexType edmType = new EdmComplexType("NS", "Name"); EdmStructuralProperty edmProperty = edmType.AddStructuralProperty(modelPropertyName, EdmPrimitiveTypeKind.Int32); EdmModel model = new EdmModel(); model.AddElement(edmType); model.SetAnnotationValue(edmType, new ClrTypeAnnotation(typeof(TestEntity))); model.SetAnnotationValue(edmProperty, new ClrPropertyInfoAnnotation(typeof(TestEntity).GetProperty(clrPropertyName))); Mock <IEdmComplexObject> edmObject = new Mock <IEdmComplexObject>(); object propertyValue = 42; edmObject.Setup(e => e.TryGetPropertyValue(modelPropertyName, out propertyValue)).Returns(true); edmObject.Setup(e => e.GetEdmType()).Returns(new EdmComplexTypeReference(edmType, isNullable: false)); ResourceContext entityContext = new ResourceContext { EdmModel = model, EdmObject = edmObject.Object, StructuredType = edmType }; // Act object resource = entityContext.ResourceInstance; // Assert TestEntity testEntity = Assert.IsType <TestEntity>(resource); Assert.Equal(42, testEntity.Property); }
public void WriteNullEntryWithOperation() { EdmEntityType entityType = new EdmEntityType("NS", "Entity"); IEdmStructuralProperty keyProp = new EdmStructuralProperty(entityType, "Id", EdmCoreModel.Instance.GetInt32(false)); entityType.AddProperty(keyProp); entityType.AddKeys(keyProp); EdmOperation operation = new EdmFunction("NS", "Foo", EdmCoreModel.Instance.GetInt16(true)); operation.AddParameter("entry", new EdmEntityTypeReference(entityType, true)); Action <ODataJsonLightOutputContext> test = outputContext => { var parameterWriter = new ODataJsonLightParameterWriter(outputContext, operation); parameterWriter.WriteStart(); var entryWriter = parameterWriter.CreateResourceWriter("entry"); entryWriter.WriteStart((ODataResource)null); entryWriter.WriteEnd(); parameterWriter.WriteEnd(); parameterWriter.Flush(); }; WriteAndValidate(test, "{\"entry\":null}", writingResponse: false); }
private TestModel() { this.Model = new EdmModel(); this.T1 = new EdmEntityType(TestModelNameSpace, "T1"); EdmStructuralProperty p11 = this.T1.AddStructuralProperty("P11", EdmCoreModel.Instance.GetInt32(false)); this.T1.AddKeys(p11); this.Model.AddElement(this.T1); this.T2 = new EdmEntityType(TestModelNameSpace, "T2"); EdmStructuralProperty p21 = this.T2.AddStructuralProperty("P21", EdmCoreModel.Instance.GetInt32(false)); this.T2.AddKeys(p21); this.Model.AddElement(this.T2); this.functionImport = new EdmFunction(TestModelNameSpace, "Function1", new EdmEntityTypeReference(this.T1, true)); this.functionImport.AddParameter("id", EdmCoreModel.Instance.GetInt32(false)); this.Model.AddElement(this.functionImport); this.Container = new EdmEntityContainer(TestModelNameSpace, "Container"); this.Model.AddElement(this.Container); this.Container.AddEntitySet("EntitySet1", this.T1); this.Container.AddSingleton("Singleton1", this.T2); this.Container.AddFunctionImport(this.functionImport); this.EntitySet = (EdmEntitySet)this.Container.FindEntitySet("EntitySet1"); this.Singleton = (EdmSingleton)this.Container.FindSingleton("Singleton1"); }
private void CreatePrimitiveProperty(PrimitivePropertyConfiguration primitiveProperty, EdmStructuredType type, StructuralTypeConfiguration config, out IEdmProperty edmProperty) { EdmPrimitiveTypeKind typeKind = GetTypeKind(primitiveProperty.PropertyInfo.PropertyType); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( typeKind, primitiveProperty.OptionalProperty); // Set concurrency token if is entity type, and concurrency token is true EdmConcurrencyMode concurrencyMode = EdmConcurrencyMode.None; if (config.Kind == EdmTypeKind.Entity && primitiveProperty.ConcurrencyToken) { concurrencyMode = EdmConcurrencyMode.Fixed; } var primitiveProp = new EdmStructuralProperty( type, primitiveProperty.PropertyInfo.Name, primitiveTypeReference, defaultValueString: null, concurrencyMode: concurrencyMode); type.AddProperty(primitiveProp); edmProperty = primitiveProp; // Set Annotation StoreGeneratedPattern if (config.Kind == EdmTypeKind.Entity && primitiveProperty.StoreGeneratedPattern != DatabaseGeneratedOption.None) { _directValueAnnotations.Add( new StoreGeneratedPatternAnnotation(primitiveProp, primitiveProperty.StoreGeneratedPattern)); } }
private IEdmModel CreateMetadata(IEnumerable <CollectionInfo> collections = null, IEnumerable <SingletonInfo> singletons = null) { EdmModel edmModel = new EdmModel(); EdmEntityType edmEntityType = new EdmEntityType("DefaultNamespace", "EntityType"); EdmStructuralProperty edmStructuralProperty = new EdmStructuralProperty(edmEntityType, "Id", EdmCoreModel.Instance.GetInt32(false)); edmEntityType.AddKeys(edmStructuralProperty); edmEntityType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(true)); edmModel.AddElement(edmEntityType); EdmEntityContainer edmEntityContainer = new EdmEntityContainer("DefaultNamespace", "TestContainer"); edmModel.AddElement(edmEntityContainer); if (collections != null) { foreach (var collection in collections) { edmEntityContainer.AddEntitySet(collection.Url, edmEntityType); } } if (singletons != null) { foreach (var singleton in singletons) { edmEntityContainer.AddSingleton(singleton.Url, edmEntityType); } } return(edmModel); }
static ODataEntityReferenceLinkTests() { EdmModel tmpModel = new EdmModel(); EdmComplexType complexType = new EdmComplexType("TestNamespace", "TestComplexType"); complexType.AddProperty(new EdmStructuralProperty(complexType, "StringProperty", EdmCoreModel.Instance.GetString(false))); tmpModel.AddElement(complexType); EntityType = new EdmEntityType("TestNamespace", "TestEntityType"); tmpModel.AddElement(EntityType); var keyProperty = new EdmStructuralProperty(EntityType, "ID", EdmCoreModel.Instance.GetInt32(false)); EntityType.AddKeys(new IEdmStructuralProperty[] { keyProperty }); EntityType.AddProperty(keyProperty); var defaultContainer = new EdmEntityContainer("TestNamespace", "DefaultContainer_sub"); tmpModel.AddElement(defaultContainer); EntitySet = new EdmEntitySet(defaultContainer, "Customers", EntityType); defaultContainer.AddElement(EntitySet); EdmModel = TestUtils.WrapReferencedModelsToMainModel("TestNamespace", "DefaultContainer", tmpModel); MessageReaderSettingsReadAndValidateCustomInstanceAnnotations = new ODataMessageReaderSettings { ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*") }; }
public void NonNullableIntegerPropertyWithDefaultValueWorks() { // Arrange ODataContext context = new ODataContext(EdmModelHelper.BasicEdmModel); EdmEntityType entitType = new EdmEntityType("NS", "Entity"); IEdmStructuralProperty property = new EdmStructuralProperty( entitType, "IntegerValue", EdmCoreModel.Instance.GetInt32(false), "-128"); // Act var schema = context.CreatePropertySchema(property); // Assert Assert.NotNull(schema); Assert.Equal("integer", schema.Type); string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); Assert.Equal(@"{ ""maximum"": 2147483647, ""minimum"": -2147483648, ""type"": ""integer"", ""format"": ""int32"", ""default"": -128 }".ChangeLineBreaks(), json); }
public ODataComplexPropertySegmentTests() { _person = new EdmEntityType("NS", "Person"); _addressComplexType = new EdmComplexType("NS", "Address"); _addressComplexType.AddStructuralProperty("Street", EdmCoreModel.Instance.GetString(false)); _person.AddKeys(_person.AddStructuralProperty("Id", EdmCoreModel.Instance.GetString(false))); _addressProperty = _person.AddStructuralProperty("HomeAddress", new EdmComplexTypeReference(_addressComplexType, false)); }
private EdmStructuralProperty?BuildStructuralProperty(Dictionary <Type, EntityTypeInfo> entityTypes, Dictionary <Type, EdmEnumType> enumTypes, Dictionary <Type, EdmComplexType> complexTypes, PropertyInfo clrProperty) { bool isNullable = !_metadataProvider.IsRequired(clrProperty); IEdmTypeReference?typeRef = PrimitiveTypeHelper.GetPrimitiveTypeRef(clrProperty, isNullable); if (typeRef == null) { Type?underlyingType = null; if (clrProperty.PropertyType.IsEnum || (underlyingType = Nullable.GetUnderlyingType(clrProperty.PropertyType)) != null && underlyingType.IsEnum) { Type clrPropertyType = underlyingType ?? clrProperty.PropertyType; if (!enumTypes.TryGetValue(clrPropertyType, out EdmEnumType? edmEnumType)) { edmEnumType = OeEdmModelBuilder.CreateEdmEnumType(clrPropertyType); enumTypes.Add(clrPropertyType, edmEnumType); } typeRef = new EdmEnumTypeReference(edmEnumType, underlyingType != null); } else { if (complexTypes.TryGetValue(clrProperty.PropertyType, out EdmComplexType? edmComplexType)) { typeRef = new EdmComplexTypeReference(edmComplexType, clrProperty.PropertyType.IsClass); } else { FKeyInfo?fkeyInfo = FKeyInfo.Create(_metadataProvider, entityTypes, this, clrProperty); if (fkeyInfo != null) { _navigationClrProperties.Add(fkeyInfo); } return(null); } } } else { if (clrProperty.PropertyType == typeof(DateTime?) && enumTypes.ContainsKey(typeof(DateTime?))) { var edmType = enumTypes[typeof(DateTime?)]; typeRef = new EdmEnumTypeReference(edmType, true); } } EdmStructuralProperty edmProperty = clrProperty is Infrastructure.OeShadowPropertyInfo ? new OeEdmStructuralShadowProperty(EdmType, clrProperty.Name, typeRef, clrProperty) : new EdmStructuralProperty(EdmType, clrProperty.Name, typeRef); EdmType.AddProperty(edmProperty); if (_metadataProvider.IsKey(clrProperty)) { _keyProperties.Add(new KeyValuePair <PropertyInfo, EdmStructuralProperty>(clrProperty, edmProperty)); } return(edmProperty); }
public SelectedPropertiesNodeTests() { this.edmModel = new EdmModel(); this.defaultContainer = new EdmEntityContainer("TestModel", "DefaultContainer"); this.edmModel.AddElement(this.defaultContainer); this.townType = new EdmEntityType("TestModel", "Town"); this.edmModel.AddElement(townType); EdmStructuralProperty townIdProperty = townType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false)); townType.AddKeys(townIdProperty); townType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(/*isNullable*/ false)); townType.AddStructuralProperty("Size", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false)); this.cityType = new EdmEntityType("TestModel", "City", this.townType); cityType.AddStructuralProperty("Photo", EdmCoreModel.Instance.GetStream(/*isNullable*/ false)); this.edmModel.AddElement(cityType); this.districtType = new EdmEntityType("TestModel", "District"); EdmStructuralProperty districtIdProperty = districtType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false)); districtType.AddKeys(districtIdProperty); districtType.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false)); districtType.AddStructuralProperty("Thumbnail", EdmCoreModel.Instance.GetStream(/*isNullable*/ false)); this.edmModel.AddElement(districtType); cityType.AddBidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "Districts", Target = districtType, TargetMultiplicity = EdmMultiplicity.Many }, new EdmNavigationPropertyInfo { Name = "City", Target = cityType, TargetMultiplicity = EdmMultiplicity.One }); this.defaultContainer.AddEntitySet("Cities", cityType); this.defaultContainer.AddEntitySet("Districts", districtType); this.metropolisType = new EdmEntityType("TestModel", "Metropolis", this.cityType); this.metropolisType.AddStructuralProperty("MetropolisStream", EdmCoreModel.Instance.GetStream(/*isNullable*/ false)); this.edmModel.AddElement(metropolisType); metropolisType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "MetropolisNavigation", Target = districtType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne }); this.action = new EdmAction("TestModel", "Action", new EdmEntityTypeReference(this.cityType, true)); this.action.AddParameter("Param", EdmCoreModel.Instance.GetInt32(false)); this.edmModel.AddElement(action); this.actionImport = new EdmActionImport(this.defaultContainer, "Action", action); this.actionConflictingWithPropertyName = new EdmAction("TestModel", "Zip", new EdmEntityTypeReference(this.districtType, true)); this.actionConflictingWithPropertyName.AddParameter("Param", EdmCoreModel.Instance.GetInt32(false)); this.edmModel.AddElement(actionConflictingWithPropertyName); this.actionImportConflictingWithPropertyName = new EdmActionImport(this.defaultContainer, "Zip", actionConflictingWithPropertyName); this.openType = new EdmEntityType("TestModel", "OpenCity", this.cityType, false, true); }
public void WriteFeedWithMultipleEntries() { EdmEntityType entityType = new EdmEntityType("NS", "Entity"); IEdmStructuralProperty keyProp = new EdmStructuralProperty(entityType, "Id", EdmCoreModel.Instance.GetInt32(false)); entityType.AddProperty(keyProp); entityType.AddKeys(keyProp); EdmEntityType derivedType = new EdmEntityType("NS", "DerivedType", entityType); derivedType.AddProperty(new EdmStructuralProperty(derivedType, "Name", EdmCoreModel.Instance.GetString(false))); EdmOperation operation = new EdmFunction("NS", "Foo", EdmCoreModel.Instance.GetInt16(true)); operation.AddParameter("feed", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(entityType, false)))); Action <ODataJsonLightOutputContext> test = outputContext => { var entry = new ODataResource(); entry.Properties = new List <ODataProperty>() { new ODataProperty() { Name = "ID", Value = 1 } }; var entry2 = new ODataResource() { TypeName = "NS.DerivedType", }; entry2.Properties = new List <ODataProperty>() { new ODataProperty() { Name = "ID", Value = 1 }, new ODataProperty() { Name = "Name", Value = "TestName" } }; var parameterWriter = new ODataJsonLightParameterWriter(outputContext, operation: null); parameterWriter.WriteStart(); var entryWriter = parameterWriter.CreateResourceSetWriter("feed"); entryWriter.WriteStart(new ODataResourceSet()); entryWriter.WriteStart(entry); entryWriter.WriteEnd(); entryWriter.WriteStart(entry2); entryWriter.WriteEnd(); entryWriter.WriteEnd(); parameterWriter.WriteEnd(); parameterWriter.Flush(); }; WriteAndValidate(test, "{\"feed\":[{\"ID\":1},{\"@odata.type\":\"#NS.DerivedType\",\"ID\":1,\"Name\":\"TestName\"}]}", writingResponse: false); }
public void DollarMetadata_Works_WithMultipleReferentialConstraints_ForUntypedModel() { // Arrange EdmModel model = new EdmModel(); EdmEntityType customer = new EdmEntityType("DefaultNamespace", "Customer"); customer.AddKeys(customer.AddStructuralProperty("CustomerId", EdmCoreModel.Instance.GetInt32(false))); customer.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false)); model.AddElement(customer); EdmEntityType order = new EdmEntityType("DefaultNamespace", "Order"); order.AddKeys(order.AddStructuralProperty("OrderId", EdmCoreModel.Instance.GetInt32(false))); EdmStructuralProperty orderCustomerId = order.AddStructuralProperty("CustomerForeignKey", EdmCoreModel.Instance.GetInt32(true)); model.AddElement(order); customer.AddBidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "Orders", Target = order, TargetMultiplicity = EdmMultiplicity.Many }, new EdmNavigationPropertyInfo { Name = "Customer", TargetMultiplicity = EdmMultiplicity.ZeroOrOne, DependentProperties = new[] { orderCustomerId }, }); const string expect = " <ReferentialConstraint>\r\n" + " <Principal Role=\"Customer\">\r\n" + " <PropertyRef Name=\"CustomerId\" />\r\n" + " </Principal>\r\n" + " <Dependent Role=\"Orders\">\r\n" + " <PropertyRef Name=\"CustomerForeignKey\" />\r\n" + " </Dependent>\r\n" + " </ReferentialConstraint>"; HttpServer server = new HttpServer(); server.Configuration.Routes.MapODataServiceRoute("odata", "odata", model); HttpClient client = new HttpClient(server); // Act HttpResponseMessage response = client.GetAsync("http://localhost/odata/$metadata").Result; // Assert Assert.True(response.IsSuccessStatusCode); Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType); Assert.Contains(expect, response.Content.ReadAsStringAsync().Result); }
private void InitializeEdmModel() { this.edmModel = new EdmModel(); EdmEntityContainer defaultContainer = new EdmEntityContainer("TestModel", "DefaultContainer"); this.edmModel.AddElement(defaultContainer); EdmComplexType addressType = new EdmComplexType("TestModel", "Address"); addressType.AddStructuralProperty("Street", EdmCoreModel.Instance.GetString(/*isNullable*/ false)); addressType.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetString(/*isNullable*/ false)); this.cityType = new EdmEntityType("TestModel", "City"); EdmStructuralProperty cityIdProperty = cityType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false)); cityType.AddKeys(cityIdProperty); cityType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(/*isNullable*/ false)); cityType.AddStructuralProperty("Size", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false)); cityType.AddStructuralProperty("Restaurants", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(/*isNullable*/ false))); cityType.AddStructuralProperty("Address", new EdmComplexTypeReference(addressType, true)); this.edmModel.AddElement(cityType); this.capitolCityType = new EdmEntityType("TestModel", "CapitolCity", cityType); capitolCityType.AddStructuralProperty("CapitolType", EdmCoreModel.Instance.GetString(/*isNullable*/ false)); this.edmModel.AddElement(capitolCityType); EdmEntityType districtType = new EdmEntityType("TestModel", "District"); EdmStructuralProperty districtIdProperty = districtType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false)); districtType.AddKeys(districtIdProperty); districtType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(/*isNullable*/ false)); districtType.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false)); this.edmModel.AddElement(districtType); cityType.AddBidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "Districts", Target = districtType, TargetMultiplicity = EdmMultiplicity.Many }, new EdmNavigationPropertyInfo { Name = "City", Target = cityType, TargetMultiplicity = EdmMultiplicity.One }); cityType.NavigationProperties().Single(np => np.Name == "Districts"); capitolCityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "CapitolDistrict", Target = districtType, TargetMultiplicity = EdmMultiplicity.One }); capitolCityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "OutlyingDistricts", Target = districtType, TargetMultiplicity = EdmMultiplicity.Many }); this.citySet = defaultContainer.AddEntitySet("Cities", cityType); defaultContainer.AddEntitySet("Districts", districtType); this.singletonCity = defaultContainer.AddSingleton("SingletonCity", cityType); }
public void ReadNullableCollectionValueAtom() { EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "MyTestEntity"); EdmStructuralProperty key = entityType.AddStructuralProperty("LongId", EdmPrimitiveTypeKind.Int64, false); entityType.AddKeys(key); entityType.AddStructuralProperty( "NullableIntNumbers", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetInt32(true)))); model.AddElement(entityType); // Payload with an entry and an inner feed, where the inner feed has a delta link. const string payload = @" <entry xmlns=""http://www.w3.org/2005/Atom"" xmlns:d=""http://docs.oasis-open.org/odata/ns/data"" xmlns:m=""http://docs.oasis-open.org/odata/ns/metadata""> <id/> <title/> <updated>2013-01-22T01:09:20Z</updated> <author> <name/> </author> <content type=""application/xml""> <m:properties> <d:NullableIntNumbers m:type=""Collection(Int32)""> <m:element>0</m:element> <m:element m:null=""true""></m:element> <m:element>1</m:element> <m:element>2</m:element> </d:NullableIntNumbers> </m:properties> </content> </entry>"; ODataEntry entry = null; this.ReadEntryPayload(model, payload, entityType, reader => { entry = entry ?? reader.Item as ODataEntry; }); Assert.NotNull(entry); var intCollection = entry.Properties.FirstOrDefault( s => string.Equals( s.Name, "NullableIntNumbers", StringComparison.OrdinalIgnoreCase)).Value.As <ODataCollectionValue>(); var list = new List <int?>(); foreach (var val in intCollection.Items) { list.Add(val as int?); } Assert.Equal(0, list[0]); Assert.Null(list[1]); Assert.Equal(1, (int)list[2]); Assert.Equal(2, (int)list[3]); }
private static IEdmModel GetEdmModel() { EdmModel model = new EdmModel(); var complexType = new EdmComplexType("NS", "Entity"); var property = new EdmStructuralProperty(complexType, "Sample", EdmCoreModel.Instance.GetString(false)); complexType.AddProperty(property); model.AddElement(complexType); return(model); }
private void BuildProperty(Dictionary <Type, EntityTypeInfo> entityTypes, Dictionary <Type, EdmEnumType> enumTypes, Dictionary <Type, EdmComplexType> complexTypes, PropertyDescriptor clrProperty) { IEdmTypeReference typeRef = PrimitiveTypeHelper.GetPrimitiveTypeRef(clrProperty); if (typeRef == null) { EdmEnumType edmEnumType; Type underlyingType = null; if (clrProperty.PropertyType.GetTypeInfo().IsEnum || (underlyingType = Nullable.GetUnderlyingType(clrProperty.PropertyType)) != null && underlyingType.GetTypeInfo().IsEnum) { Type clrPropertyType = underlyingType ?? clrProperty.PropertyType; if (!enumTypes.TryGetValue(clrPropertyType, out edmEnumType)) { edmEnumType = CreateEdmEnumType(clrPropertyType); enumTypes.Add(clrPropertyType, edmEnumType); } typeRef = new EdmEnumTypeReference(edmEnumType, underlyingType != null); } else { EdmComplexType edmComplexType; if (complexTypes.TryGetValue(clrProperty.PropertyType, out edmComplexType)) { typeRef = new EdmComplexTypeReference(edmComplexType, clrProperty.PropertyType.GetTypeInfo().IsClass); } else { FKeyInfo fkeyInfo = FKeyInfo.Create(_metadataProvider, entityTypes, this, clrProperty); if (fkeyInfo != null) { _navigationClrProperties.Add(fkeyInfo); } return; } } } else { if (clrProperty.PropertyType == typeof(DateTime?) && enumTypes.ContainsKey(typeof(DateTime?))) //zzz { var edmType = enumTypes[typeof(DateTime?)]; typeRef = new EdmEnumTypeReference(edmType, true); } } var edmProperty = new EdmStructuralProperty(_edmType, clrProperty.Name, typeRef); _edmType.AddProperty(edmProperty); if (_metadataProvider.IsKey(clrProperty)) { _keyProperties.Add(new KeyValuePair <PropertyDescriptor, EdmStructuralProperty>(clrProperty, edmProperty)); } }
public CustomInstanceAnnotationRoundtripTests() { this.model = new EdmModel(); this.complexType = new EdmComplexType("ns", "ErrorDetails"); var property = new EdmStructuralProperty(complexType, "ErrorDetailName", EdmCoreModel.Instance.GetString(false)); this.complexType.AddProperty(property); this.model.AddElement(complexType); this.stream = new MemoryStream(); }
public ODataAtomReaderEnumIntegrationTests() { this.userModel = new EdmModel(); // enum without flags var enumType = new EdmEnumType("NS", "Color"); var red = new EdmEnumMember(enumType, "Red", new EdmIntegerConstant(1)); enumType.AddMember(red); enumType.AddMember("Green", new EdmIntegerConstant(2)); enumType.AddMember("Blue", new EdmIntegerConstant(3)); this.userModel.AddElement(enumType); // enum with flags var enumFlagsType = new EdmEnumType("NS", "ColorFlags", isFlags: true); enumFlagsType.AddMember("Red", new EdmIntegerConstant(1)); enumFlagsType.AddMember("Green", new EdmIntegerConstant(2)); enumFlagsType.AddMember("Blue", new EdmIntegerConstant(4)); this.userModel.AddElement(enumFlagsType); this.entityType = new EdmEntityType("NS", "MyEntityType", isAbstract: false, isOpen: true, baseType: null); EdmStructuralProperty floatId = new EdmStructuralProperty(this.entityType, "FloatId", EdmCoreModel.Instance.GetSingle(false)); this.entityType.AddKeys(floatId); this.entityType.AddProperty(floatId); var enumTypeReference = new EdmEnumTypeReference(enumType, true); this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "Color", enumTypeReference)); var enumFlagsTypeReference = new EdmEnumTypeReference(enumFlagsType, false); this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "ColorFlags", enumFlagsTypeReference)); // enum in complex type EdmComplexType myComplexType = new EdmComplexType("NS", "MyComplexType"); myComplexType.AddProperty(new EdmStructuralProperty(myComplexType, "MyColorFlags", enumFlagsTypeReference)); myComplexType.AddProperty(new EdmStructuralProperty(myComplexType, "Height", EdmCoreModel.Instance.GetDouble(false))); this.userModel.AddElement(myComplexType); this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "MyComplexType", new EdmComplexTypeReference(myComplexType, true))); // enum in collection type EdmCollectionType myCollectionType = new EdmCollectionType(enumFlagsTypeReference); this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "MyCollectionType", new EdmCollectionTypeReference(myCollectionType))); this.userModel.AddElement(this.entityType); var defaultContainer = new EdmEntityContainer("NS", "DefaultContainer"); this.entitySet = new EdmEntitySet(defaultContainer, "MySet", this.entityType); defaultContainer.AddEntitySet(this.entitySet.Name, this.entityType); this.userModel.AddElement(defaultContainer); }
public void Initialize() { this.model = new EdmModel(); this.complexType = new EdmComplexType("ns", "ErrorDetails"); var property = new EdmStructuralProperty(complexType, "ErrorDetailName", EdmCoreModel.Instance.GetString(false)); this.complexType.AddProperty(property); this.model.AddElement(complexType); this.stream = new MemoryStream(); }