Skip to content

Commit 38c7c04

Browse files
authored
Adds support for IndexableByKey restrictions annotations on entity sets (#543)
* Add support for IndexableByKey for entity sets * Update test * Update release notes * Remove unnecessary whitespace * Remove whitespace
1 parent c565c5a commit 38c7c04

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource
245245
IEdmEntitySet entitySet = navigationSource as IEdmEntitySet;
246246
IEdmEntityType entityType = navigationSource.EntityType();
247247
CountRestrictionsType count = null;
248+
bool? indexableByKey = false;
248249

249250
// for entity set, create a path with key and a $count path
250251
if (entitySet != null)
@@ -260,10 +261,15 @@ private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource
260261
if (convertSettings.AddAlternateKeyPaths)
261262
CreateAlternateKeyPath(path, entityType); //~/entitySet/{alternateKeyId}
262263

263-
path.Push(new ODataKeySegment(entityType)); // ~/entitySet/{id}
264-
AppendPath(path.Clone());
264+
indexableByKey = _model.GetBoolean(targetPath, CapabilitiesConstants.IndexableByKey)
265+
?? _model.GetBoolean(entitySet, CapabilitiesConstants.IndexableByKey);
265266

266-
CreateTypeCastPaths(path, convertSettings, entityType, entitySet, false); // ~/entitySet/{id}/subType
267+
if (indexableByKey ?? true)
268+
{
269+
path.Push(new ODataKeySegment(entityType)); // ~/entitySet/{id}
270+
AppendPath(path.Clone());
271+
CreateTypeCastPaths(path, convertSettings, entityType, entitySet, false); // ~/entitySet/{id}/subType
272+
}
267273
}
268274
else if (navigationSource is IEdmSingleton singleton)
269275
{ // ~/singleton/subType
@@ -285,7 +291,7 @@ private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource
285291
}
286292
}
287293

288-
if (entitySet != null)
294+
if (entitySet != null && (indexableByKey ?? true))
289295
{
290296
path.Pop(); // end of entity
291297
}

src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1616
<PackageId>Microsoft.OpenApi.OData</PackageId>
1717
<SignAssembly>true</SignAssembly>
18-
<Version>1.6.5</Version>
18+
<Version>1.6.6</Version>
1919
<Description>This package contains the codes you need to convert OData CSDL to Open API Document of Model.</Description>
2020
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
2121
<PackageTags>Microsoft OpenApi OData EDM</PackageTags>
2222
<RepositoryUrl>https://github.com/Microsoft/OpenAPI.NET.OData</RepositoryUrl>
2323
<PackageReleaseNotes>
24-
- Use annotations with path as target to dermine whether to add an operation #535
24+
- Adds support for IndexableByKey restrictions annotations on entity sets #541
2525
</PackageReleaseNotes>
2626
<AssemblyName>Microsoft.OpenApi.OData.Reader</AssemblyName>
2727
<AssemblyOriginatorKeyFile>..\..\tool\Microsoft.OpenApi.OData.snk</AssemblyOriginatorKeyFile>

test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@ public void GetPathsForGraphBetaModelWithDerivedTypesConstraintReturnsAllPaths()
119119
Assert.NotNull(paths);
120120
Assert.Equal(17631, paths.Count());
121121
}
122+
123+
[Theory]
124+
[InlineData(true)]
125+
[InlineData(false)]
126+
public void GetEntitySetPathsWithIndexableByKeyAnnotationWorks(bool indexable)
127+
{
128+
// Arrange
129+
string indexableAnnotation = @"<Annotation Term=""Org.OData.Capabilities.V1.IndexableByKey"" Bool=""{0}"" />";
130+
indexableAnnotation = string.Format(indexableAnnotation, indexable);
131+
IEdmModel model = GetInheritanceModel(indexableAnnotation);
132+
ODataPathProvider provider = new();
133+
var settings = new OpenApiConvertSettings();
134+
135+
// Act & Assert
136+
var paths = provider.GetPaths(model, settings);
137+
Assert.NotNull(paths);
138+
139+
if (indexable)
140+
{
141+
Assert.Equal(3, paths.Count());
142+
Assert.Contains("/Customers({ID})", paths.Select(p => p.GetPathItemName()));
143+
}
144+
else
145+
{
146+
Assert.Equal(2, paths.Count());
147+
Assert.DoesNotContain("/Customers({ID})", paths.Select(p => p.GetPathItemName()));
148+
}
149+
}
122150

123151
[Theory]
124152
[InlineData(true, true)]

0 commit comments

Comments
 (0)