public void ParameterReaderShouldReadCollectionOfDerivedComplexValue() { var complexType = this.referencedModel.ComplexType("address").Property("StreetName", EdmPrimitiveTypeKind.String); var derivedComplexType = new EdmComplexType("TestModel", "derivedAddress", complexType, false); derivedComplexType.AddStructuralProperty("StreetNumber", EdmPrimitiveTypeKind.Int32, false); this.referencedModel.AddElement(derivedComplexType); this.action.AddParameter("addresses", EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexType, false))); string payload = "{\"addresses\" : [{ \"StreetName\": \"Bla\", \"StreetNumber\" : 61, \"@odata.type\":\"TestModel.derivedAddress\" }, { \"StreetName\": \"Bla2\" }]}"; var result = this.RunParameterReaderTest(payload); result.Collections.Should().OnlyContain(keyValuePair => keyValuePair.Key.Equals("addresses")); var collectioItems = result.Collections.Single().Value.Items; collectioItems.Should().HaveCount(2); collectioItems.Should().OnlyContain(item => item is ODataComplexValue); }
public void CreateODataComplexValue_Understands_IEdmComplexTypeObject() { // Arrange EdmComplexType complexEdmType = new EdmComplexType("NS", "ComplexType"); complexEdmType.AddStructuralProperty("Property", EdmPrimitiveTypeKind.Int32); IEdmComplexTypeReference edmTypeReference = new EdmComplexTypeReference(complexEdmType, isNullable: false); ODataSerializerContext context = new ODataSerializerContext(); TypedEdmComplexObject edmObject = new TypedEdmComplexObject(new { Property = 42 }, edmTypeReference, context.Model); ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(new DefaultODataSerializerProvider()); // Act ODataComplexValue result = serializer.CreateODataComplexValue(edmObject, edmTypeReference, context); // Assert Assert.Equal("Property", result.Properties.Single().Name); Assert.Equal(42, result.Properties.Single().Value); }
public static IEdmModel SimpleOpenTypeModel() { var model = new EdmModel(); // Address is an open complex type var addressType = new EdmComplexType("Default", "Address", null, false, true); addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); model.AddElement(addressType); // ZipCode is an open complex type also var zipCodeType = new EdmComplexType("Default", "ZipCode", null, false, true); zipCodeType.AddStructuralProperty("Code", EdmPrimitiveTypeKind.Int32); model.AddElement(zipCodeType); // Enum type simpleEnum EdmEnumType simpleEnum = new EdmEnumType("Default", "SimpleEnum"); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "First", new EdmEnumMemberValue(0))); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Second", new EdmEnumMemberValue(1))); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Third", new EdmEnumMemberValue(2))); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Fourth", new EdmEnumMemberValue(3))); model.AddElement(simpleEnum); // Customer is an open entity type var customerType = new EdmEntityType("Default", "Customer", null, false, true); customerType.AddKeys(customerType.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32)); customerType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); customerType.AddStructuralProperty("Address", addressType.ToEdmTypeReference(false)); model.AddElement(customerType); var container = new EdmEntityContainer("Default", "Container"); model.AddElement(container); var customers = new EdmEntitySet(container, "Customers", customerType); container.AddElement(customers); return(model); }
public void AddOperationWhileTypeHasSameNameButOneIsAction() { EdmModel model = new EdmModel(); EdmComplexType c1 = new EdmComplexType("Ambiguous", "Binding"); IEdmOperation o1 = new EdmFunction("Ambiguous", "Binding", EdmCoreModel.Instance.GetInt16(true)); IEdmOperation o2 = new EdmAction("Ambiguous", "Binding", EdmCoreModel.Instance.GetInt16(true)); IEdmOperation o3 = new EdmFunction("Ambiguous", "Binding", EdmCoreModel.Instance.GetInt16(true)); model.AddElement(o1); Assert.AreEqual(1, model.FindOperations("Ambiguous.Binding").Count(), "First function was correctly added to operation group"); model.AddElement(o2); Assert.AreEqual(2, model.FindOperations("Ambiguous.Binding").Count(), "Second function was correctly added to operation group"); model.AddElement(c1); model.AddElement(o3); Assert.AreEqual(3, model.FindOperations("Ambiguous.Binding").Count(), "Third function was correctly added to operation group"); Assert.AreEqual(c1, model.FindType("Ambiguous.Binding"), "Single item resolved"); }
public EdmLibraryExtensionsTests() { this.model = TestModel.BuildDefaultTestModel(); this.defaultContainer = (EdmEntityContainer)this.model.FindEntityContainer("Default"); this.productsSet = this.defaultContainer.FindEntitySet("Products"); this.productType = (IEdmEntityType)this.model.FindDeclaredType("TestModel.Product"); this.productTypeReference = new EdmEntityTypeReference(this.productType, false); EdmComplexType complexType = new EdmComplexType("TestModel", "MyComplexType"); this.operationWithNoOverload = new EdmFunction("TestModel", "FunctionImportWithNoOverload", EdmCoreModel.Instance.GetInt32(true)); this.operationWithNoOverload.AddParameter("p1", EdmCoreModel.Instance.GetInt32(false)); this.model.AddElement(operationWithNoOverload); this.operationImportWithNoOverload = this.defaultContainer.AddFunctionImport("FunctionImportWithNoOverload", operationWithNoOverload); this.operationWithOverloadAnd0Param = new EdmFunction("TestModel", "FunctionImportWithNoOverload", EdmCoreModel.Instance.GetInt32(true)); this.model.AddElement(operationWithOverloadAnd0Param); this.operationImportWithOverloadAnd0Param = defaultContainer.AddFunctionImport("FunctionImportWithOverload", operationWithOverloadAnd0Param); this.operationWithOverloadAnd1Param = new EdmFunction("TestModel", "FunctionImportWithNoOverload", EdmCoreModel.Instance.GetInt32(true)); this.operationWithOverloadAnd1Param.AddParameter("p1", EdmCoreModel.Instance.GetInt32(false)); this.model.AddElement(operationWithOverloadAnd1Param); this.operationImportWithOverloadAnd1Param = defaultContainer.AddFunctionImport("FunctionImportWithOverload", operationWithOverloadAnd1Param); this.operationWithOverloadAnd2Params = new EdmFunction("TestModel", "FunctionImportWithNoOverload", EdmCoreModel.Instance.GetInt32(true)); var productTypeReference = new EdmEntityTypeReference(productType, isNullable: false); this.operationWithOverloadAnd2Params.AddParameter("p1", productTypeReference); this.operationWithOverloadAnd2Params.AddParameter("p2", EdmCoreModel.Instance.GetString(true)); this.model.AddElement(operationWithOverloadAnd2Params); this.operationImportWithOverloadAnd2Params = defaultContainer.AddFunctionImport("FunctionImportWithOverload", operationWithOverloadAnd2Params); this.operationWithOverloadAnd5Params = new EdmFunction("TestModel", "FunctionImportWithNoOverload", EdmCoreModel.Instance.GetInt32(true)); this.operationWithOverloadAnd5Params.AddParameter("p1", new EdmCollectionTypeReference(new EdmCollectionType(productTypeReference))); this.operationWithOverloadAnd5Params.AddParameter("p2", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(isNullable: false)))); this.operationWithOverloadAnd5Params.AddParameter("p3", EdmCoreModel.Instance.GetString(isNullable: true)); EdmComplexTypeReference complexTypeReference = new EdmComplexTypeReference(complexType, isNullable: false); this.operationWithOverloadAnd5Params.AddParameter("p4", complexTypeReference); this.operationWithOverloadAnd5Params.AddParameter("p5", new EdmCollectionTypeReference(new EdmCollectionType(complexTypeReference))); this.model.AddElement(operationWithOverloadAnd5Params); this.operationImportWithOverloadAnd5Params = defaultContainer.AddFunctionImport("FunctionImportWithOverload", operationWithOverloadAnd5Params); }
public void WritingResourceValueWithPropertiesShouldWrite() { var complexType = new EdmComplexType("NS", "Address"); complexType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); model.AddElement(complexType); var entityType = new EdmEntityType("NS", "Customer"); entityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); entityType.AddStructuralProperty("Location", new EdmComplexTypeReference(complexType, false)); model.AddElement(entityType); settings.ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*"); var result = this.SetupSerializerAndRunTest(serializer => { var resourceValue = new ODataResourceValue { TypeName = "NS.Customer", Properties = new [] { new ODataProperty { Name = "Name", Value = "MyName" }, new ODataProperty { Name = "Location", Value = new ODataResourceValue { TypeName = "NS.Address", Properties = new [] { new ODataProperty { Name = "City", Value = "MyCity" } } } } } }; var entityTypeRef = new EdmEntityTypeReference(entityType, false); serializer.WriteResourceValue(resourceValue, entityTypeRef, false, serializer.CreateDuplicatePropertyNameChecker()); }); Assert.Equal(@"{""Name"":""MyName"",""Location"":{""City"":""MyCity""}}", result); }
static ODataJsonLightEntryAndFeedDeserializerTests() { 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, "TestEntitySet", EntityType); defaultContainer.AddElement(EntitySet); Action = new EdmAction("TestNamespace", "DoSomething", null, true, null); Action.AddParameter("p1", new EdmEntityTypeReference(EntityType, false)); tmpModel.AddElement(Action); ActionImport = defaultContainer.AddActionImport("DoSomething", Action); var serviceOperationFunction = new EdmFunction("TestNamespace", "ServiceOperation", EdmCoreModel.Instance.GetInt32(true)); defaultContainer.AddFunctionImport("ServiceOperation", serviceOperationFunction); tmpModel.AddElement(serviceOperationFunction); tmpModel.AddElement(new EdmTerm("custom", "DateTimeOffsetAnnotation", EdmPrimitiveTypeKind.DateTimeOffset)); tmpModel.AddElement(new EdmTerm("custom", "DateAnnotation", EdmPrimitiveTypeKind.Date)); tmpModel.AddElement(new EdmTerm("custom", "TimeOfDayAnnotation", EdmPrimitiveTypeKind.TimeOfDay)); EdmModel = TestUtils.WrapReferencedModelsToMainModel("TestNamespace", "DefaultContainer", tmpModel); MessageReaderSettingsReadAndValidateCustomInstanceAnnotations = new ODataMessageReaderSettings { ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*") }; MessageReaderSettingsIgnoreInstanceAnnotations = new ODataMessageReaderSettings(); }
public void ReadingPayloadOpenComplexTypeJsonLight() { EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "Person"); entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32); EdmComplexType complexType = new EdmComplexType("NS", "OpenAddress", null, false, true); complexType.AddStructuralProperty("CountryRegion", EdmPrimitiveTypeKind.String, false); EdmComplexTypeReference complexTypeRef = new EdmComplexTypeReference(complexType, true); entityType.AddStructuralProperty("Address", complexTypeRef); model.AddElement(complexType); model.AddElement(entityType); EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer_sub"); EdmEntitySet entitySet = container.AddEntitySet("People", entityType); model.AddElement(container); const string payload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#EntityNs.MyContainer.People/$entity\"," + "\"@odata.id\":\"http://mytest\"," + "\"Id\":\"0\"," + "\"Address\":{\"CountryRegion\":\"China\",\"City\":\"Shanghai\"}" + "}"; IEdmModel mainModel = TestUtils.WrapReferencedModelsToMainModel("EntityNs", "MyContainer", model); ODataEntry entry = null; this.ReadEntryPayload(mainModel, payload, entitySet, entityType, reader => { entry = entry ?? reader.Item as ODataEntry; }); Assert.IsNotNull(entry, "entry shouldn't be null"); var address = entry.Properties.FirstOrDefault(s => string.Equals(s.Name, "Address", StringComparison.OrdinalIgnoreCase)).Value as ODataComplexValue; address.Properties.FirstOrDefault(s => string.Equals(s.Name, "CountryRegion", StringComparison.OrdinalIgnoreCase)).Value.ShouldBeEquivalentTo("China", "value should be in correct type."); address.Properties.FirstOrDefault(s => string.Equals(s.Name, "City", StringComparison.OrdinalIgnoreCase)).Value.ShouldBeEquivalentTo("Shanghai", "value should be in correct type."); }
public void ReadingNullValueForDeclaredComplexProperty() { EdmModel model = new EdmModel(); EdmComplexType complexType = new EdmComplexType("NS", "Address"); complexType.AddStructuralProperty("CountriesOrRegions", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(true)))); model.AddElement(complexType); EdmEntityType entityType = new EdmEntityType("NS", "Person"); entityType.AddKeys(entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); entityType.AddStructuralProperty("Address", new EdmComplexTypeReference(complexType, true)); model.AddElement(entityType); EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer"); EdmEntitySet entitySet = container.AddEntitySet("People", entityType); model.AddElement(container); const string payload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#EntityNs.MyContainer.People/$entity\"," + "\"@odata.id\":\"http://mytest\"," + "\"Id\":0," + "\"Address\":null" + "}"; List <ODataItem> resources = new List <ODataItem>(); this.ReadEntryPayload(model, payload, entitySet, entityType, reader => { if (reader.State == ODataReaderState.ResourceEnd) { resources.Add(reader.Item); } }); Assert.Equal(2, resources.Count); Assert.Null(resources.First()); }
public void PropertyHandlerGetPropertyNameCollision() { // Model with a single entity type EdmModel model = new EdmModel(); const string defaultNamespaceName = "Test"; var int32TypeRef = EdmCoreModel.Instance.GetInt32(isNullable: false); // Create a complext types. var complexType1 = new EdmComplexType(defaultNamespaceName, "ComplexType1"); complexType1.AddStructuralProperty("Prop1", int32TypeRef); model.AddElement(complexType1); var complexType2 = new EdmComplexType(defaultNamespaceName, "ComplexType2"); complexType1.AddStructuralProperty("Prop1", EdmCoreModel.Instance.GetString(isNullable: false)); model.AddElement(complexType2); // Create an entity with a complex type property. var singleComplexPropertyEntityType = new EdmEntityType(defaultNamespaceName, "SingleComplexPropertyEntityType"); singleComplexPropertyEntityType.AddKeys(singleComplexPropertyEntityType.AddStructuralProperty("ID", int32TypeRef)); singleComplexPropertyEntityType.AddStructuralProperty("ComplexProp1", new EdmComplexTypeReference(complexType1, isNullable: true)); singleComplexPropertyEntityType.AddStructuralProperty("ComplexProp2", new EdmComplexTypeReference(complexType2, isNullable: true)); model.AddElement(singleComplexPropertyEntityType); // Create a property handler and enter a resource set scope. PropertyCacheHandler handler = new PropertyCacheHandler(); handler.EnterResourceSetScope(singleComplexPropertyEntityType, 0); // Create a PropertySerializationInfo for ComplexProp1.Prop1 var info1 = handler.GetProperty("Prop1", complexType1); info1.Should().NotBeNull(); // Create a PropertySerializationInfo for ComplexProp2.Prop1; they shoudl be different. var info2 = handler.GetProperty("Prop1", complexType2); info2.Should().NotBeNull(); info2.Should().NotBeSameAs(info1); }
public void WritingInstanceAnnotationInComplexValueShouldWrite() { var complexType = new EdmComplexType("TestNamespace", "Address"); model.AddElement(complexType); settings.ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*"); var result = this.SetupSerializerAndRunTest(serializer => { var complexValue = new ODataComplexValue { TypeName = "TestNamespace.Address", InstanceAnnotations = new Collection <ODataInstanceAnnotation> { new ODataInstanceAnnotation("Is.ReadOnly", new ODataPrimitiveValue(true)) } }; var complexTypeRef = new EdmComplexTypeReference(complexType, false); serializer.WriteComplexValue(complexValue, complexTypeRef, false, false, new DuplicatePropertyNamesChecker(false, true)); }); result.Should().Contain("\"@Is.ReadOnly\":true"); }
public void ResolvePropertyTest_ThrowsForAmbiguousPropertyName() { // Arrange EdmComplexType structuredType = new EdmComplexType("NS", "Complex"); structuredType.AddStructuralProperty("Title", EdmPrimitiveTypeKind.String); structuredType.AddStructuralProperty("tiTle", EdmPrimitiveTypeKind.Int32); structuredType.AddStructuralProperty("tiTlE", EdmPrimitiveTypeKind.Double); // Act & Assert - Positive case IEdmProperty edmProperty = structuredType.ResolveProperty("tiTlE"); Assert.NotNull(edmProperty); Assert.Equal("Edm.Double", edmProperty.Type.FullName()); // Act & Assert - Negative case Action test = () => structuredType.ResolveProperty("title"); ExceptionAssert.Throws <ODataException>(test, "Ambiguous property name 'title' found. Please use correct property name case."); }
private EdmComplexType ConstructStockComplexTypeInModel(IEdmComplexType complexType, EdmModel stockModel, Dictionary <string, EdmComplexType> stockComplexTypes) { EdmComplexType stockType; string fullName = complexType.FullName(); if (!stockComplexTypes.TryGetValue(fullName, out stockType)) { stockType = new EdmComplexType( complexType.Namespace, complexType.Name, complexType.BaseType != null ? this.ConstructStockComplexTypeInModel((IEdmComplexType)complexType.BaseType, stockModel, stockComplexTypes) : null, complexType.IsAbstract); // TODO: IsBad, Documentation stockModel.AddElement(stockType); stockComplexTypes.Add(fullName, stockType); } return(stockType); }
public void EdmPathExpressionWithInvalidComplexTypeCaseForTypeCastSegmentShouldError() { EdmEntityType entityType = new EdmEntityType("ds.s", "entityType"); EdmComplexType complexType = new EdmComplexType("ds.s", "complexType"); EdmModel model = new EdmModel(); model.AddElement(entityType); model.AddElement(complexType); EdmFunction function = new EdmFunction("ns", "GetStuff", new EdmEntityTypeReference(entityType, true), true /*isBound*/, new EdmPathExpression("bindingEntity/ds.s.complexType"), false); function.AddParameter("bindingEntity", new EdmEntityTypeReference(entityType, false)); model.AddElement(function); ValidateErrorInList( model, function, EdmErrorCode.InvalidPathTypeCastSegmentMustBeEntityType, Strings.EdmModel_Validator_Semantic_InvalidEntitySetPathTypeCastSegmentMustBeEntityType("EntitySetPath", "bindingEntity/ds.s.complexType", "ds.s.complexType")); }
public SelectModelPathTests() { // Address is an open complex type var addressType = new EdmComplexType("NS", "Address"); addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); _city = addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); EdmEntityType customerType = new EdmEntityType("NS", "Customer"); customerType.AddKeys(customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); _homeAddress = customerType.AddStructuralProperty("HomeAddress", new EdmComplexTypeReference(addressType, false)); _relatedNavProperty = addressType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "RelatedCustomers", Target = customerType, TargetMultiplicity = EdmMultiplicity.Many }); }
public void TryParseEnumMemberOfInvalidEnumMemberShouldBeFalse() { var enumType = new EdmEnumType("Ns", "Color"); enumType.AddMember("Blue", new EdmEnumMemberValue(0)); enumType.AddMember("White", new EdmEnumMemberValue(1)); var complexType = new EdmComplexType("Ns", "Address"); string enumPath = "Ns.Color/Green"; List <IEdmSchemaType> types = new List <IEdmSchemaType> { enumType, complexType }; IEnumerable <IEdmEnumMember> parsedMember; Assert.False(EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember)); // JSON string jsonEnumPath = "Green"; Assert.False(EdmEnumValueParser.TryParseJsonEnumMember(jsonEnumPath, enumType, null, out parsedMember)); }
public void TestInitialize() { this.model = new EdmModel(); this.model.AddElement(new EdmComplexType("foo", "complex")); EdmComplexType complexType = new EdmComplexType("ns", "complex"); this.model.AddElement(complexType); EdmComplexTypeReference complexTypeReference = new EdmComplexTypeReference(complexType, isNullable: false); EdmCollectionType primitiveCollectionType = new EdmCollectionType(EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Guid, isNullable: false)); EdmCollectionType complexCollectionType = new EdmCollectionType(complexTypeReference); EdmCollectionTypeReference primitiveCollectionTypeReference = new EdmCollectionTypeReference(primitiveCollectionType); EdmCollectionTypeReference complexCollectionTypeReference = new EdmCollectionTypeReference(complexCollectionType); this.model.AddElement(new EdmTerm("custom", "primitive", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Double, isNullable: false))); this.model.AddElement(new EdmTerm("custom", "complex", complexTypeReference)); this.model.AddElement(new EdmTerm("custom", "primitiveCollection", primitiveCollectionTypeReference)); this.model.AddElement(new EdmTerm("custom", "complexCollection", complexCollectionTypeReference)); this.shouldIncludeAnnotation = (annotationName) => true; }
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 EdmVocabularyAnnotation( 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 static async Task Main1(string[] args) { String path = @"C:\Users\kibandi\Desktop\SimpleExchangeOutput.txt"; using (StreamReader sr = File.OpenText(path)) { IEdmModel model = null; /*model = builder * .BuildAddressType() * .BuildCategoryType() * .BuildCustomerType() * .BuildDefaultContainer() * .BuildCustomerSet() * .GetModel();*/ Stream stream = sr.BaseStream; ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); IODataResponseMessage responseMessage = new InMemoryMessage { Stream = stream }; responseMessage.SetHeader("Content-Type", "application/json;odata.metadata=minimal;"); // ODataMessageReader reader = new ODataMessageReader((IODataResponseMessage)message, settings, GetEdmModel()); ODataMessageReader reader = new ODataMessageReader(responseMessage, settings, new EdmModel()); var oDataResourceReader = reader.CreateODataResourceReader(); var property = reader.ReadProperty(); //ODataStreamInfo odataStream = reader.Item as ODataStreamInfo; stream.Position = 0; //var asynchronousReader = reader.CreateODataAsynchronousReader(); IEdmStructuredType resource = new EdmComplexType("odata", "odata"); oDataResourceReader = reader.CreateODataResourceReader(resourceType: resource); while (oDataResourceReader.Read()) { var oItem = oDataResourceReader.Item; Console.WriteLine(oItem.ToString()); } //var responseMessage = asynchronousReader.CreateResponseMessage(); } }
public void WriteTopLevelComplexProperty() { EdmModel model = new EdmModel(); EdmComplexType complexType = AddAndGetComplexType(model); EdmEntityType entityType = AddAndGetEntityType(model); entityType.AddStructuralProperty("ComplexP", new EdmComplexTypeReference(complexType, false)); var entitySet = GetEntitySet(model, entityType); var requestUri = new Uri("http://temp.org/FakeSet('parent')/ComplexP"); var odataUri = new ODataUri { RequestUri = requestUri }; odataUri.Path = new ODataUriParser(model, new Uri("http://temp.org/"), requestUri).ParsePath(); ODataResource nestedRes = new ODataResource() { Properties = new[] { new ODataProperty { Name = "P1", Value = "cv" } } }; var actual = WriteJsonLightEntry( isRequest: false, serviceDocumentUri: new Uri("http://temp.org/"), specifySet: false, odataEntry: null, entitySet: null, resourceType: complexType, odataUri: odataUri, writeAction: (writer) => { writer.WriteStart(nestedRes); writer.WriteEnd(); }); var expected = "{\"@odata.context\":\"http://temp.org/$metadata#FakeSet('parent')/ComplexP\",\"P1\":\"cv\"}"; Assert.Equal(expected, actual); }
public void ValidateSerializationBlockingErrors() { EdmModel model = new EdmModel(); EdmComplexType complexWithBadProperty = new EdmComplexType("Foo", "Bar"); complexWithBadProperty.AddProperty(new EdmStructuralProperty(complexWithBadProperty, "baz", new EdmComplexTypeReference(new EdmComplexType("", ""), false))); model.AddElement(complexWithBadProperty); EdmEntityType baseType = new EdmEntityType("Foo", ""); IEdmStructuralProperty keyProp = new EdmStructuralProperty(baseType, "Id", EdmCoreModel.Instance.GetInt32(false)); baseType.AddProperty(keyProp); baseType.AddKeys(keyProp); EdmEntityType derivedType = new EdmEntityType("Foo", "Quip", baseType); model.AddElement(baseType); model.AddElement(derivedType); EdmNavigationProperty navProp = derivedType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "navProp", Target = derivedType, TargetMultiplicity = EdmMultiplicity.One }); EdmEntityContainer container = new EdmEntityContainer("Foo", "Container"); model.AddElement(container); container.AddElement(new EdmEntitySet(container, "badNameSet", baseType)); StringBuilder sb = new StringBuilder(); IEnumerable <EdmError> errors; bool written = model.TryWriteSchema(XmlWriter.Create(sb), out errors); var expectedErrors = new EdmLibTestErrors() { { "([. Nullable=False])", EdmErrorCode.ReferencedTypeMustHaveValidName }, { "(Foo.Quip)", EdmErrorCode.ReferencedTypeMustHaveValidName }, { "(Microsoft.OData.Edm.EdmEntitySet)", EdmErrorCode.ReferencedTypeMustHaveValidName }, }; this.CompareErrors(errors, expectedErrors); }
/// <summary> /// Create a model /// </summary> /// <param name="entityTyps"></param> /// <returns></returns> public static EdmModel CreateModel(Type[] entityTyps) { var model = new EdmModel(); foreach (var clrType in entityTyps) { EdmStructuredType edmType; if (clrType == entityTyps.Last()) { edmType = new EdmEntityType(clrType.Namespace, clrType.Name); } else { edmType = new EdmComplexType(clrType.Namespace, clrType.Name); } foreach (var pi in clrType.GetProperties()) { if (pi.PropertyType.IsValueType || pi.PropertyType.FullName == "System.String" || pi.PropertyType.IsEnum) { edmType.AddStructuralProperty( pi.Name, GetPrimitiveTypeKind(pi.PropertyType), true); } else { var propEdmType = model.FindDeclaredType(pi.PropertyType.FullName); if (propEdmType != null) { edmType.AddStructuralProperty( pi.Name, propEdmType.ToEdmTypeReference(true)); } } } model.AddElement(edmType as IEdmSchemaElement); } return(model); }
public void CreateSpatialSchemasReturnFullSpatialSchemasForModelWithEdmSpatialTypes() { // Arrange EdmModel model = new EdmModel(); EdmComplexType complex = new EdmComplexType("NS", "Complex"); complex.AddStructuralProperty("Location", EdmPrimitiveTypeKind.Geography); model.AddElement(complex); ODataContext context = new ODataContext(model); // Act var schemas = context.CreateSpatialSchemas(); // Assert Assert.NotNull(schemas); Assert.NotEmpty(schemas); Assert.Equal(new string[] { "Edm.Geography", "Edm.GeographyPoint", "Edm.GeographyLineString", "Edm.GeographyPolygon", "Edm.GeographyMultiPoint", "Edm.GeographyMultiLineString", "Edm.GeographyMultiPolygon", "Edm.GeographyCollection", "Edm.Geometry", "Edm.GeometryPoint", "Edm.GeometryLineString", "Edm.GeometryPolygon", "Edm.GeometryMultiPoint", "Edm.GeometryMultiLineString", "Edm.GeometryMultiPolygon", "Edm.GeometryCollection", "GeoJSON.position" }, schemas.Select(s => s.Key)); }
public void Property_ResourceInstance_CanBeBuiltWithSelectExpandWrapperProperties() { // Arrange EdmComplexType edmType = new EdmComplexType("NS", "Name"); var edmTypeRef = new EdmComplexTypeReference(edmType, isNullable: false); edmType.AddStructuralProperty("Property", EdmPrimitiveTypeKind.Int32); edmType.AddStructuralProperty("SubEntity1", edmTypeRef); edmType.AddStructuralProperty("SubEntity2", edmTypeRef); EdmModel model = new EdmModel(); model.AddElement(edmType); model.SetAnnotationValue <ClrTypeAnnotation>(edmType, new ClrTypeAnnotation(typeof(TestSubEntity))); Mock <IEdmComplexObject> edmObject = new Mock <IEdmComplexObject>(); object propertyValue = 42; object selectExpandWrapper = new SelectExpandWrapper <TestEntity>(); object subEntity2 = new TestSubEntity { Property = 33 }; edmObject.Setup(e => e.TryGetPropertyValue("Property", out propertyValue)).Returns(true); edmObject.Setup(e => e.TryGetPropertyValue("SubEntity1", out selectExpandWrapper)).Returns(true); edmObject.Setup(e => e.TryGetPropertyValue("SubEntity2", out subEntity2)).Returns(true); edmObject.Setup(e => e.GetEdmType()).Returns(edmTypeRef); ResourceContext entityContext = new ResourceContext { EdmModel = model, EdmObject = edmObject.Object, StructuredType = edmType }; // Act object resource = entityContext.ResourceInstance; // Assert TestSubEntity testEntity = Assert.IsType <TestSubEntity>(resource); Assert.Equal(42, testEntity.Property); Assert.Null(testEntity.SubEntity1); Assert.NotNull(testEntity.SubEntity2); Assert.Equal(33, testEntity.SubEntity2.Property); }
/// <summary> /// Initialises a new instance of the <see cref="OrderByQueryOption"/> class. /// </summary> /// <param name="rawValue">The raw request value.</param> /// <param name="model">The model.</param> internal OrderByQueryOption(string rawValue, EdmComplexType model) : base(rawValue) { if (model is null) { throw new ArgumentNullException(nameof(model)); } if (rawValue.IndexOf(',') > -1) { Properties = rawValue.Slice(',', rawValue.IndexOf('=') + 1) .Select(raw => new OrderByProperty(raw, model)) .ToArray(); } else { string property = rawValue.SubstringAfter('='); Properties = new[] { new OrderByProperty(property, model) }; } }
private static void ReferentialConstraintDemo() { Console.WriteLine("ReferentialConstraintDemo"); EdmModel model = new EdmModel(); var customer = new EdmEntityType("ns", "Customer"); model.AddElement(customer); var customerId = customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false); customer.AddKeys(customerId); var address = new EdmComplexType("ns", "Address"); model.AddElement(address); var code = address.AddStructuralProperty("gid", EdmPrimitiveTypeKind.Guid); customer.AddStructuralProperty("addr", new EdmComplexTypeReference(address, true)); var order = new EdmEntityType("ns", "Order"); model.AddElement(order); var oId = order.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false); order.AddKeys(oId); var orderCustomerId = order.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32, false); var nav = new EdmNavigationPropertyInfo() { Name = "NavCustomer", Target = customer, TargetMultiplicity = EdmMultiplicity.One, DependentProperties = new[] { orderCustomerId }, PrincipalProperties = new[] { customerId } }; order.AddUnidirectionalNavigation(nav); ShowModel(model); }
private static IEdmModel GetUntypedEdmModel() { var model = new EdmModel(); // complex type address EdmComplexType address = new EdmComplexType("NS", "Address", null, false, true); address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); model.AddElement(address); // enum type color EdmEnumType color = new EdmEnumType("NS", "Color"); color.AddMember(new EdmEnumMember(color, "Red", new EdmIntegerConstant(0))); model.AddElement(color); // entity type customer EdmEntityType customer = new EdmEntityType("NS", "UntypedSimpleOpenCustomer", null, false, true); customer.AddKeys(customer.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32)); customer.AddStructuralProperty("Color", new EdmEnumTypeReference(color, isNullable: true)); model.AddElement(customer); EdmAction action = new EdmAction( "NS", "AddColor", null, isBound: true, entitySetPathExpression: null); action.AddParameter("bindingParameter", new EdmEntityTypeReference(customer, false)); action.AddParameter("Color", new EdmEnumTypeReference(color, true)); model.AddElement(action); EdmEntityContainer container = new EdmEntityContainer("NS", "Container"); container.AddEntitySet("UntypedSimpleOpenCustomers", customer); model.AddElement(container); return(model); }
public ExpandAndSelectPathExtractingTests() { this.baseType = new EdmEntityType("FQ.NS", "Base"); this.baseType.AddKeys(this.baseType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); var baseNavigation1 = this.baseType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Navigation1", Target = this.baseType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne }); var baseNavigation2 = this.baseType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Navigation2", Target = this.baseType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne }); var addressType = new EdmComplexType("FQ.NS", "Address"); addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); addressType.AddStructuralProperty("Region", EdmPrimitiveTypeKind.String); addressType.AddStructuralProperty("NearestAirports", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(addressType, false)))); addressType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Residents", Target = this.baseType, TargetMultiplicity = EdmMultiplicity.Many }); this.baseType.AddStructuralProperty("Address", new EdmComplexTypeReference(addressType, false)); this.derivedType = new EdmEntityType("FQ.NS", "Derived", this.baseType); this.derivedType.AddStructuralProperty("Derived", EdmPrimitiveTypeKind.Int32); var derivedNavigation = this.derivedType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "DerivedNavigation", Target = this.derivedType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne }); var container = new EdmEntityContainer("FQ.NS", "Container"); this.entitySet = container.AddEntitySet("Entities", this.baseType); this.entitySet.AddNavigationTarget(baseNavigation1, this.entitySet); this.entitySet.AddNavigationTarget(baseNavigation2, this.entitySet); this.entitySet.AddNavigationTarget(derivedNavigation, this.entitySet); this.model = new EdmModel(); this.model.AddElement(this.baseType); this.model.AddElement(this.derivedType); this.model.AddElement(container); }
private IEdmModel CollectionOfComplexTypeTermModel() { var model = new EdmModel(); var complexTypeElement = new EdmComplexType("NS", "ComplexTypeElement"); complexTypeElement.AddStructuralProperty("IntegerProperty", EdmCoreModel.Instance.GetInt32(true)); complexTypeElement.AddStructuralProperty("StringProperty", EdmCoreModel.Instance.GetString(true)); model.AddElement(complexTypeElement); var collectionOfComplexTypeTerm = new EdmTerm("NS", "CollectionOfComplexTypeTerm", EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexTypeElement, true))); model.AddElement(collectionOfComplexTypeTerm); var inlineCollectionOfComplexTypeAnnotation = new EdmVocabularyAnnotation(complexTypeElement, collectionOfComplexTypeTerm, new EdmCollectionExpression( new EdmRecordExpression( new EdmPropertyConstructor("IntegerProperty", new EdmIntegerConstant(111)), new EdmPropertyConstructor("StringProperty", new EdmStringConstant("Inline String 111"))), new EdmRecordExpression( new EdmPropertyConstructor("IntegerProperty", new EdmIntegerConstant(222)), new EdmPropertyConstructor("StringProperty", new EdmStringConstant("Inline String 222"))))); inlineCollectionOfComplexTypeAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); model.AddVocabularyAnnotation(inlineCollectionOfComplexTypeAnnotation); var outlineCollectionOfComplexTypeAnnotation = new EdmVocabularyAnnotation(collectionOfComplexTypeTerm, collectionOfComplexTypeTerm, new EdmCollectionExpression( new EdmRecordExpression( new EdmPropertyConstructor("IntegerProperty", new EdmIntegerConstant(333)), new EdmPropertyConstructor("StringProperty", new EdmStringConstant("Outline String 333"))), new EdmRecordExpression( new EdmPropertyConstructor("IntegerProperty", new EdmIntegerConstant(444)), new EdmPropertyConstructor("StringProperty", new EdmStringConstant("Outline String 444"))), new EdmRecordExpression( new EdmPropertyConstructor("IntegerProperty", new EdmIntegerConstant(555)), new EdmPropertyConstructor("StringProperty", new EdmStringConstant("Outline String 555"))))); model.AddVocabularyAnnotation(outlineCollectionOfComplexTypeAnnotation); return(model); }
public void ShortQualifiedNameForCollectionOfNonPrimitiveTypeShouldBeCollectionOfFullName() { const string stringOfNamespaceName = "TestModel"; const string stringOfComplexTypeName = "MyComplexType"; var edmComplexType = new EdmComplexType(stringOfNamespaceName, stringOfComplexTypeName); var edmCollectionType = new EdmCollectionType(new EdmComplexTypeReference(edmComplexType, true)); var stringOfExpectedShortQulifiedName = String.Format("Collection({0}.{1})", stringOfNamespaceName, stringOfComplexTypeName); var stringOfObservedShortQulifiedName = edmCollectionType.ODataShortQualifiedName(); stringOfObservedShortQulifiedName.Should().Be(stringOfExpectedShortQulifiedName); const string stringEntityTypeName = "MyEntityType"; var edmEntityType = new EdmEntityType(stringOfNamespaceName, stringEntityTypeName); edmCollectionType = new EdmCollectionType(new EdmEntityTypeReference(edmEntityType, true)); stringOfExpectedShortQulifiedName = String.Format("Collection({0}.{1})", stringOfNamespaceName, stringEntityTypeName); stringOfObservedShortQulifiedName = edmCollectionType.ODataShortQualifiedName(); stringOfObservedShortQulifiedName.Should().Be(stringOfExpectedShortQulifiedName); }
protected virtual void VisitComplexType(EdmComplexType item) { VisitEdmNamedMetadataItem(item); if (item.HasDeclaredProperties) { VisitCollection(item.DeclaredProperties, VisitEdmProperty); } }
protected override void VisitComplexType(EdmComplexType item) { _schemaWriter.WriteComplexTypeElementHeader(item); base.VisitComplexType(item); _schemaWriter.WriteEndElement(); }
internal void WriteComplexTypeElementHeader(EdmComplexType complexType) { _xmlWriter.WriteStartElement(CsdlConstants.Element_ComplexType); _xmlWriter.WriteAttributeString(CsdlConstants.Attribute_Name, complexType.Name); WritePolymorphicTypeAttributes(complexType); }
protected virtual void VisitDeclaredProperties(EdmComplexType complexType, IEnumerable<EdmProperty> properties) { VisitCollection(properties, VisitEdmProperty); }