Skip to content

Commit 41a38f2

Browse files
gavinbarronCopilot
andcommitted
fix: restore orderby fallback for unresolved navigation targets
Fall back to navigation-property sort metadata only when the annotation target cannot be resolved. Preserve explicit path-level sorting prohibitions. Fixes #952 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 2124246 commit 41a38f2

2 files changed

Lines changed: 103 additions & 4 deletions

File tree

‎src/Microsoft.OpenApi.OData.Reader/Operation/NavigationPropertyGetOperationHandler.cs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,12 @@ protected override void SetParameters(OpenApiOperation operation)
178178
AddParameterIfExists(parametersToAdd, Context.CreateFilter, Context.CreateFilter);
179179
AddParameterIfExists(parametersToAdd, Context.CreateCount, Context.CreateCount);
180180

181-
var orderByParameter = (string.IsNullOrEmpty(TargetPath), NavigationProperty) switch
181+
// Fall back only for unresolved targets, not for explicit sorting restrictions.
182+
var orderByParameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.Model.GetTargetPath(TargetPath), NavigationProperty) switch
182183
{
183-
(false, not null) when NavigationProperty.ToEntityType() is {} entityType =>
184-
Context.CreateOrderBy(TargetPath!, entityType),
185-
(true, not null) => Context.CreateOrderBy(NavigationProperty),
184+
({} target, not null) when NavigationProperty.ToEntityType() is {} entityType =>
185+
Context.CreateOrderBy(target, entityType),
186+
(null, not null) => Context.CreateOrderBy(NavigationProperty),
186187
_ => null,
187188
};
188189
if (orderByParameter != null)

‎test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/NavigationPropertyGetOperationHandlerTests.cs‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Threading.Tasks;
9+
using System.Xml.Linq;
910
using Microsoft.OData.Edm;
11+
using Microsoft.OData.Edm.Csdl;
1012
using Microsoft.OpenApi.OData.Edm;
1113
using Microsoft.OpenApi.OData.Generator;
1214
using Microsoft.OpenApi.OData.PathItem.Tests;
@@ -145,6 +147,102 @@ public void CreateNavigationGetOperationViaComposableFunctionReturnsCorrectOpera
145147
Assert.Contains(operation.Parameters, x => x.Name == "path");
146148
}
147149

150+
[Theory]
151+
[InlineData(true)]
152+
[InlineData(false)]
153+
public void CreateNavigationGetOperationWithUnresolvedFunctionTargetUsesPropertySortRestrictions(bool sortable)
154+
{
155+
VerifyNavigationSortRestrictions(viaFunction: true, propertySortable: sortable, pathSortable: null);
156+
}
157+
158+
[Theory]
159+
[InlineData(true, null)]
160+
[InlineData(true, false)]
161+
[InlineData(false, true)]
162+
public void CreateNavigationGetOperationWithResolvedTargetUsesPathSortRestrictions(bool propertySortable, bool? pathSortable)
163+
{
164+
VerifyNavigationSortRestrictions(viaFunction: false, propertySortable, pathSortable);
165+
}
166+
167+
private void VerifyNavigationSortRestrictions(bool viaFunction, bool propertySortable, bool? pathSortable)
168+
{
169+
string pathAnnotation = pathSortable.HasValue ? $@"
170+
<Annotations Target=""microsoft.graph.GraphService/tables/columns"">
171+
<Annotation Term=""Org.OData.Capabilities.V1.SortRestrictions"">
172+
<Record>
173+
<PropertyValue Property=""Sortable"" Bool=""{pathSortable.Value.ToString().ToLowerInvariant()}"" />
174+
</Record>
175+
</Annotation>
176+
</Annotations>" : "";
177+
string csdl = $@"
178+
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
179+
<edmx:DataServices>
180+
<Schema Namespace=""microsoft.graph"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
181+
<EntityType Name=""workbookTable"">
182+
<Key><PropertyRef Name=""id"" /></Key>
183+
<Property Name=""id"" Type=""Edm.String"" Nullable=""false"" />
184+
<NavigationProperty Name=""columns"" Type=""Collection(microsoft.graph.workbookTableColumn)"" ContainsTarget=""true"">
185+
<Annotation Term=""Org.OData.Capabilities.V1.SortRestrictions"">
186+
<Record>
187+
<PropertyValue Property=""Sortable"" Bool=""{propertySortable.ToString().ToLowerInvariant()}"" />
188+
<PropertyValue Property=""NonSortableProperties"">
189+
<Collection><PropertyPath>id</PropertyPath></Collection>
190+
</PropertyValue>
191+
</Record>
192+
</Annotation>
193+
</NavigationProperty>
194+
</EntityType>
195+
<EntityType Name=""workbookTableColumn"">
196+
<Key><PropertyRef Name=""id"" /></Key>
197+
<Property Name=""id"" Type=""Edm.String"" Nullable=""false"" />
198+
<Property Name=""name"" Type=""Edm.String"" />
199+
</EntityType>
200+
<Function Name=""itemAt"" IsBound=""true"" IsComposable=""true"">
201+
<Parameter Name=""bindingParameter"" Type=""Collection(microsoft.graph.workbookTable)"" />
202+
<Parameter Name=""index"" Type=""Edm.Int32"" Nullable=""false"" />
203+
<ReturnType Type=""microsoft.graph.workbookTable"" />
204+
</Function>
205+
<EntityContainer Name=""GraphService"">
206+
<EntitySet Name=""tables"" EntityType=""microsoft.graph.workbookTable"" />
207+
</EntityContainer>
208+
{pathAnnotation}
209+
</Schema>
210+
</edmx:DataServices>
211+
</edmx:Edmx>";
212+
IEdmModel model = CsdlReader.Parse(XElement.Parse(csdl).CreateReader());
213+
IEdmEntitySet tables = model.EntityContainer.FindEntitySet("tables");
214+
IEdmNavigationProperty columns = tables.EntityType.DeclaredNavigationProperties().Single();
215+
var path = new ODataPath(new ODataNavigationSourceSegment(tables));
216+
if (viaFunction)
217+
{
218+
path.Push(new ODataOperationSegment(model.SchemaElements.OfType<IEdmFunction>().Single()));
219+
}
220+
else
221+
{
222+
path.Push(new ODataKeySegment(tables.EntityType));
223+
}
224+
path.Push(new ODataNavigationPropertySegment(columns));
225+
226+
var target = model.GetTargetPath(path.GetTargetPath(model));
227+
Assert.Equal(viaFunction, target is null);
228+
229+
var settings = new OpenApiConvertSettings { UseStringArrayForQueryOptionsSchema = false };
230+
var operation = _operationHandler.CreateOperation(new ODataContext(model, settings), path);
231+
232+
if (viaFunction ? propertySortable : pathSortable ?? true)
233+
{
234+
var orderBy = Assert.Single(operation.Parameters, parameter => parameter.Name == "$orderby");
235+
var expectedProperties = viaFunction
236+
? new[] { "name", "name desc" }
237+
: new[] { "id", "id desc", "name", "name desc" };
238+
Assert.Equal(expectedProperties, orderBy.Schema.Items.Enum.Select(value => value.GetValue<string>()));
239+
}
240+
else
241+
{
242+
Assert.DoesNotContain(operation.Parameters, parameter => parameter.Name == "$orderby");
243+
}
244+
}
245+
148246
[Fact]
149247
public void CreateNavigationGetOperationViaOverloadedComposableFunctionReturnsCorrectOperation()
150248
{

0 commit comments

Comments
 (0)