From 938b611879c0df238e75d66322341b76542436c3 Mon Sep 17 00:00:00 2001 From: Scott Kearney <=> Date: Thu, 8 Aug 2024 15:55:12 -0400 Subject: [PATCH 1/2] test: Check whether the primary key property can be read on a resolved entity from an abstract manifest. Currently fails due to incompatible types. --- .../Utilities/TraitToPropertyMapTests.cs | 27 +++++++++++++++++++ .../Input/Account.cdm.json | 20 ++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 objectModel/TestData/Utilities/TraitToPropertyMap/TestFetchAbstractPrimaryKey/Input/Account.cdm.json diff --git a/objectModel/CSharp/Microsoft.CommonDataModel.ObjectModel.Tests/Utilities/TraitToPropertyMapTests.cs b/objectModel/CSharp/Microsoft.CommonDataModel.ObjectModel.Tests/Utilities/TraitToPropertyMapTests.cs index 595a2c1713..4d9e005faa 100644 --- a/objectModel/CSharp/Microsoft.CommonDataModel.ObjectModel.Tests/Utilities/TraitToPropertyMapTests.cs +++ b/objectModel/CSharp/Microsoft.CommonDataModel.ObjectModel.Tests/Utilities/TraitToPropertyMapTests.cs @@ -102,6 +102,33 @@ public async Task TestFetchPrimaryKey() } } + /// + /// Test fetching primary key of a resolved entity from an abstract manifest. + /// + [TestMethod] + public async Task TestFetchAbstractPrimaryKey() + { + var corpus = TestHelper.GetLocalCorpus(testsSubpath, nameof(TestFetchAbstractPrimaryKey)); + var doc = await corpus.FetchObjectAsync("Account.cdm.json"); + + if (doc == null) + { + Assert.Fail($"Unable to load acccount.cdm.json. Please inspect error log for additional details."); + } + + var entity = (CdmEntityDefinition)doc.Definitions[0]; + var resolvedEntity = await entity.CreateResolvedEntityAsync("ResolvedAccount"); + try + { + var pk = resolvedEntity.PrimaryKey; + Assert.IsNotNull(pk); + } + catch (Exception e) + { + Assert.Fail($"Exception occur while reading primary key for entity account. {e.Message}"); + } + } + /// /// Test setting and getting of data format /// diff --git a/objectModel/TestData/Utilities/TraitToPropertyMap/TestFetchAbstractPrimaryKey/Input/Account.cdm.json b/objectModel/TestData/Utilities/TraitToPropertyMap/TestFetchAbstractPrimaryKey/Input/Account.cdm.json new file mode 100644 index 0000000000..62503bceb6 --- /dev/null +++ b/objectModel/TestData/Utilities/TraitToPropertyMap/TestFetchAbstractPrimaryKey/Input/Account.cdm.json @@ -0,0 +1,20 @@ +{ + "jsonSchemaSemanticVersion": "1.0.0", + "imports": [ + { + "corpusPath": "cdm:/foundations.cdm.json" + } + ], + "definitions": [ + { + "entityName": "Account", + "hasAttributes": [ + { + "name": "accountId", + "purpose": "identifiedBy", + "dataType": "entityId" + } + ] + } + ] +} \ No newline at end of file From f59e4d3e453f971a138fac7a02421abc0c6de6ac Mon Sep 17 00:00:00 2001 From: Scott Kearney <=> Date: Thu, 8 Aug 2024 15:58:19 -0400 Subject: [PATCH 2/2] fix: Cast to a base-level compatible type to get the name. The attRef returned can be cast to the base CdmObject type in order to just retrieve the object definition name. --- .../Utilities/TraitToPropertyMap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/objectModel/CSharp/Microsoft.CommonDataModel.ObjectModel/Utilities/TraitToPropertyMap.cs b/objectModel/CSharp/Microsoft.CommonDataModel.ObjectModel/Utilities/TraitToPropertyMap.cs index 0590aaacf7..4bed42aabc 100644 --- a/objectModel/CSharp/Microsoft.CommonDataModel.ObjectModel/Utilities/TraitToPropertyMap.cs +++ b/objectModel/CSharp/Microsoft.CommonDataModel.ObjectModel/Utilities/TraitToPropertyMap.cs @@ -203,7 +203,7 @@ internal dynamic FetchPropertyValue(string propertyName, bool onlyFromProperty = { return attRef; } - return ((CdmTypeAttributeDefinition)attRef).FetchObjectDefinitionName(); + return ((CdmObject)attRef).FetchObjectDefinitionName(); } break; case "defaultValue":