- adds a setting to globally disable dollar count path items

Signed-off-by: Vincent Biret <vibiret@microsoft.com>
This commit is contained in:
Vincent Biret 2021-11-23 10:28:01 -05:00
parent f87c2b4951
commit 4093249a19
No known key found for this signature in database
GPG key ID: 32426322EDFFB7E3
5 changed files with 46 additions and 14 deletions

View file

@ -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 <see cref="IEdmNavigationSource"/>.
/// </summary>
/// <param name="navigationSource">The navigation source.</param>
private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource)
/// <param name="convertSettings">The settings for the current conversion.</param>
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
/// </summary>
/// <param name="navigationProperty">The navigation property.</param>
/// <param name="currentPath">The current OData path.</param>
private void RetrieveNavigationPropertyPaths(IEdmNavigationProperty navigationProperty, ODataPath currentPath)
/// <param name="convertSettings">The settings for the current conversion.</param>
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.
/// </summary>
/// <param name="currentPath">The current OData path.</param>
private void CreateCountPath(ODataPath currentPath) {
/// <param name="convertSettings">The settings for the current conversion.</param>
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);

View file

@ -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;
}

View file

@ -183,6 +183,11 @@ namespace Microsoft.OpenApi.OData
/// </summary>
public IODataPathProvider PathProvider { get; set; }
/// <summary>
/// Gets/sets a value indicating whether or not add OData $count segments in the description for collections.
/// </summary>
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;

View file

@ -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

View file

@ -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()
{