From 4093249a191df5efe0e2b0b89836cc58d3de6b5c Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 23 Nov 2021 10:28:01 -0500 Subject: [PATCH] - adds a setting to globally disable dollar count path items Signed-off-by: Vincent Biret --- .../Edm/ODataPathProvider.cs | 23 +++++++++++-------- .../Generator/OpenApiSchemaGenerator.cs | 9 ++++---- .../OpenApiConvertSettings.cs | 8 ++++++- .../PublicAPI.Unshipped.txt | 2 ++ .../Edm/ODataPathProviderTests.cs | 18 +++++++++++++++ 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs index 2885827..1068d5c 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs @@ -57,7 +57,7 @@ namespace Microsoft.OpenApi.OData.Edm { if (CanFilter(entitySet)) { - RetrieveNavigationSourcePaths(entitySet); + RetrieveNavigationSourcePaths(entitySet, settings); } } @@ -66,7 +66,7 @@ namespace Microsoft.OpenApi.OData.Edm { if (CanFilter(singleton)) { - RetrieveNavigationSourcePaths(singleton); + RetrieveNavigationSourcePaths(singleton, settings); } } @@ -169,7 +169,8 @@ namespace Microsoft.OpenApi.OData.Edm /// Retrieve the paths for . /// /// The navigation source. - private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource) + /// The settings for the current conversion. + private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource, OpenApiConvertSettings convertSettings) { Debug.Assert(navigationSource != null); @@ -183,7 +184,7 @@ namespace Microsoft.OpenApi.OData.Edm // for entity set, create a path with key and a $count path if (entitySet != null) { - CreateCountPath(path); + CreateCountPath(path, convertSettings); path.Push(new ODataKeySegment(entityType)); AppendPath(path.Clone()); @@ -197,7 +198,7 @@ namespace Microsoft.OpenApi.OData.Edm { if (CanFilter(np)) { - RetrieveNavigationPropertyPaths(np, path); + RetrieveNavigationPropertyPaths(np, path, convertSettings); } } @@ -252,7 +253,8 @@ namespace Microsoft.OpenApi.OData.Edm /// /// The navigation property. /// The current OData path. - private void RetrieveNavigationPropertyPaths(IEdmNavigationProperty navigationProperty, ODataPath currentPath) + /// The settings for the current conversion. + private void RetrieveNavigationPropertyPaths(IEdmNavigationProperty navigationProperty, ODataPath currentPath, OpenApiConvertSettings convertSettings) { Debug.Assert(navigationProperty != null); Debug.Assert(currentPath != null); @@ -282,7 +284,7 @@ namespace Microsoft.OpenApi.OData.Edm if (targetsMany) { // ~/entityset/{key}/collection-valued-Nav/$count - CreateCountPath(currentPath); + CreateCountPath(currentPath, convertSettings); } if (!navigationProperty.ContainsTarget) @@ -321,7 +323,7 @@ namespace Microsoft.OpenApi.OData.Edm { if (CanFilter(subNavProperty)) { - RetrieveNavigationPropertyPaths(subNavProperty, currentPath); + RetrieveNavigationPropertyPaths(subNavProperty, currentPath, convertSettings); } } } @@ -373,8 +375,11 @@ namespace Microsoft.OpenApi.OData.Edm /// Create $count paths. /// /// The current OData path. - private void CreateCountPath(ODataPath currentPath) { + /// The settings for the current conversion. + private void CreateCountPath(ODataPath currentPath, OpenApiConvertSettings convertSettings) { if(currentPath == null) throw new ArgumentNullException(nameof(currentPath)); + if(convertSettings == null) throw new ArgumentNullException(nameof(convertSettings)); + if(!convertSettings.IncludeDollarCountPathSegments) return; var countPath = currentPath.Clone(); countPath.Push(ODataDollarCountSegment.Instance); AppendPath(countPath); diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs index 158bfd3..3cbb9a9 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs @@ -67,10 +67,11 @@ namespace Microsoft.OpenApi.OData.Generator schemas[schema.Key] = schema.Value; } - schemas[Constants.DollarCountSchemaName] = new OpenApiSchema { - Type = "integer", - Format = "int32" - }; + if(context.Settings.IncludeDollarCountPathSegments) + schemas[Constants.DollarCountSchemaName] = new OpenApiSchema { + Type = "integer", + Format = "int32" + }; return schemas; } diff --git a/src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs b/src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs index 5fdc97d..72bff80 100644 --- a/src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs +++ b/src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs @@ -183,6 +183,11 @@ namespace Microsoft.OpenApi.OData /// public IODataPathProvider PathProvider { get; set; } + /// + /// Gets/sets a value indicating whether or not add OData $count segments in the description for collections. + /// + public bool IncludeDollarCountPathSegments { get; set; } = true; + internal OpenApiConvertSettings Clone() { var newSettings = new OpenApiConvertSettings @@ -212,7 +217,8 @@ namespace Microsoft.OpenApi.OData RequireDerivedTypesConstraintForBoundOperations = this.RequireDerivedTypesConstraintForBoundOperations, ShowSchemaExamples = this.ShowSchemaExamples, ShowRootPath = this.ShowRootPath, - PathProvider = this.PathProvider + PathProvider = this.PathProvider, + IncludeDollarCountPathSegments = this.IncludeDollarCountPathSegments, }; return newSettings; diff --git a/src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt b/src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt index 3aa5997..ed58c36 100644 --- a/src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt +++ b/src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt @@ -128,6 +128,8 @@ Microsoft.OpenApi.OData.OpenApiConvertSettings.PathPrefix.get -> string Microsoft.OpenApi.OData.OpenApiConvertSettings.PathPrefix.set -> void Microsoft.OpenApi.OData.OpenApiConvertSettings.PathProvider.get -> Microsoft.OpenApi.OData.Edm.IODataPathProvider Microsoft.OpenApi.OData.OpenApiConvertSettings.PathProvider.set -> void +Microsoft.OpenApi.OData.OpenApiConvertSettings.IncludeDollarCountPathSegments.get -> bool +Microsoft.OpenApi.OData.OpenApiConvertSettings.IncludeDollarCountPathSegments.set -> void Microsoft.OpenApi.OData.OpenApiConvertSettings.PrefixEntityTypeNameBeforeKey.get -> bool Microsoft.OpenApi.OData.OpenApiConvertSettings.PrefixEntityTypeNameBeforeKey.set -> void Microsoft.OpenApi.OData.OpenApiConvertSettings.RequireDerivedTypesConstraintForBoundOperations.get -> bool diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs index a0dc92e..24ddffc 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs @@ -86,6 +86,24 @@ namespace Microsoft.OpenApi.OData.Edm.Tests Assert.Equal(4, paths.Count()); } + [Fact] + public void GetPathsDoesntReturnPathsForCountWhenDisabled() + { + // Arrange + IEdmModel model = GetInheritanceModel(string.Empty); + ODataPathProvider provider = new ODataPathProvider(); + var settings = new OpenApiConvertSettings { + IncludeDollarCountPathSegments = false, + }; + + // Act + var paths = provider.GetPaths(model, settings); + + // Assert + Assert.NotNull(paths); + Assert.Equal(3, paths.Count()); + } + [Fact] public void GetPathsForInheritanceModelWithDerivedTypesConstraintNoAnnotationReturnsFewer() {