|
6 | 6 | using System.Collections.Generic; |
7 | 7 | using System.Linq; |
8 | 8 | using System.Threading.Tasks; |
| 9 | +using System.Xml.Linq; |
9 | 10 | using Microsoft.OData.Edm; |
| 11 | +using Microsoft.OData.Edm.Csdl; |
10 | 12 | using Microsoft.OpenApi.OData.Edm; |
11 | 13 | using Microsoft.OpenApi.OData.Generator; |
12 | 14 | using Microsoft.OpenApi.OData.PathItem.Tests; |
@@ -145,6 +147,102 @@ public void CreateNavigationGetOperationViaComposableFunctionReturnsCorrectOpera |
145 | 147 | Assert.Contains(operation.Parameters, x => x.Name == "path"); |
146 | 148 | } |
147 | 149 |
|
| 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 | + |
148 | 246 | [Fact] |
149 | 247 | public void CreateNavigationGetOperationViaOverloadedComposableFunctionReturnsCorrectOperation() |
150 | 248 | { |
|
0 commit comments