From 06c14606a148e633bf6de823dd8cc60d4578fd31 Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Mon, 10 Jun 2024 17:29:38 +0300 Subject: [PATCH 1/5] Add support for IndexableByKey for entity sets --- .../Edm/ODataPathProvider.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs index 0a2197f38..dd48b34ca 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs @@ -245,6 +245,7 @@ private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource IEdmEntitySet entitySet = navigationSource as IEdmEntitySet; IEdmEntityType entityType = navigationSource.EntityType(); CountRestrictionsType count = null; + bool? indexableByKey = false; // for entity set, create a path with key and a $count path if (entitySet != null) @@ -258,12 +259,17 @@ private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource CreateTypeCastPaths(path, convertSettings, entityType, entitySet, true); // ~/entitySet/subType if (convertSettings.AddAlternateKeyPaths) - CreateAlternateKeyPath(path, entityType); //~/entitySet/{alternateKeyId} + CreateAlternateKeyPath(path, entityType); //~/entitySet/{alternateKeyId} - path.Push(new ODataKeySegment(entityType)); // ~/entitySet/{id} - AppendPath(path.Clone()); + indexableByKey = _model.GetBoolean(targetPath, CapabilitiesConstants.IndexableByKey) + ?? _model.GetBoolean(entitySet, CapabilitiesConstants.IndexableByKey); - CreateTypeCastPaths(path, convertSettings, entityType, entitySet, false); // ~/entitySet/{id}/subType + if (indexableByKey ?? true) + { + path.Push(new ODataKeySegment(entityType)); // ~/entitySet/{id} + AppendPath(path.Clone()); + CreateTypeCastPaths(path, convertSettings, entityType, entitySet, false); // ~/entitySet/{id}/subType + } } else if (navigationSource is IEdmSingleton singleton) { // ~/singleton/subType @@ -285,7 +291,7 @@ private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource } } - if (entitySet != null) + if (entitySet != null && (indexableByKey ?? true)) { path.Pop(); // end of entity } From 74e27f770c34e8d252ce436e2cf3ffde69f4c76b Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Mon, 10 Jun 2024 17:35:49 +0300 Subject: [PATCH 2/5] Update test --- .../Edm/ODataPathProviderTests.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs index da7b256a1..00add3ec7 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs @@ -119,6 +119,34 @@ public void GetPathsForGraphBetaModelWithDerivedTypesConstraintReturnsAllPaths() Assert.NotNull(paths); Assert.Equal(17631, paths.Count()); } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void GetEntitySetPathsWithIndexableByKeyAnnotationWorks(bool indexable) + { + // Arrange + string indexableAnnotation = @""; + indexableAnnotation = string.Format(indexableAnnotation, indexable); + IEdmModel model = GetInheritanceModel(indexableAnnotation); + ODataPathProvider provider = new(); + var settings = new OpenApiConvertSettings(); + + // Act & Assert + var paths = provider.GetPaths(model, settings); + Assert.NotNull(paths); + if (indexable) + { + Assert.Equal(3, paths.Count()); + Assert.Contains("/Customers({ID})", paths.Select(p => p.GetPathItemName())); + } + else + { + Assert.Equal(2, paths.Count()); + Assert.DoesNotContain("/Customers({ID})", paths.Select(p => p.GetPathItemName())); + } + + } [Theory] [InlineData(true, true)] From 39fba4c707de65d36692fd5526dff4fccfcce558 Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Mon, 10 Jun 2024 17:43:05 +0300 Subject: [PATCH 3/5] Update release notes --- .../Microsoft.OpenAPI.OData.Reader.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj b/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj index c9a505472..de58f91ed 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj +++ b/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj @@ -15,13 +15,13 @@ netstandard2.0 Microsoft.OpenApi.OData true - 1.6.5 + 1.6.6 This package contains the codes you need to convert OData CSDL to Open API Document of Model. © Microsoft Corporation. All rights reserved. Microsoft OpenApi OData EDM https://github.com/Microsoft/OpenAPI.NET.OData - - Use annotations with path as target to dermine whether to add an operation #535 + - Adds support for IndexableByKey restrictions annotations on entity sets #541 Microsoft.OpenApi.OData.Reader ..\..\tool\Microsoft.OpenApi.OData.snk From 752d986152fe37b6e54454a4d1b0914220f8938a Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Mon, 10 Jun 2024 17:46:40 +0300 Subject: [PATCH 4/5] Remove unnecessary whitespace --- src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs index dd48b34ca..3bde84892 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs @@ -259,7 +259,7 @@ private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource CreateTypeCastPaths(path, convertSettings, entityType, entitySet, true); // ~/entitySet/subType if (convertSettings.AddAlternateKeyPaths) - CreateAlternateKeyPath(path, entityType); //~/entitySet/{alternateKeyId} + CreateAlternateKeyPath(path, entityType); //~/entitySet/{alternateKeyId} indexableByKey = _model.GetBoolean(targetPath, CapabilitiesConstants.IndexableByKey) ?? _model.GetBoolean(entitySet, CapabilitiesConstants.IndexableByKey); From f221ef59fbc562035bf8f0c6e983e4df4e656c12 Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Mon, 10 Jun 2024 17:48:55 +0300 Subject: [PATCH 5/5] Remove whitespace --- .../Edm/ODataPathProviderTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs index 00add3ec7..2c01862f8 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs @@ -135,6 +135,7 @@ public void GetEntitySetPathsWithIndexableByKeyAnnotationWorks(bool indexable) // Act & Assert var paths = provider.GetPaths(model, settings); Assert.NotNull(paths); + if (indexable) { Assert.Equal(3, paths.Count()); @@ -145,7 +146,6 @@ public void GetEntitySetPathsWithIndexableByKeyAnnotationWorks(bool indexable) Assert.Equal(2, paths.Count()); Assert.DoesNotContain("/Customers({ID})", paths.Select(p => p.GetPathItemName())); } - } [Theory]