Skip to content

Commit 033703d

Browse files
committed
add basic tests for moving path parameters to path item
1 parent cee083e commit 033703d

File tree

3 files changed

+106
-8
lines changed

3 files changed

+106
-8
lines changed

src/Microsoft.OpenApi.OData.Reader/Operation/OperationHandler.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ public virtual OpenApiOperation CreateOperation(ODataContext context, ODataPath
4949
These need to be set before Responses, as the Parameters
5050
will be used in the Responses when creating Links.
5151
*/
52-
if (!Context.Settings.DeclarePathParametersOnPathItem)
53-
{
54-
SetParameters(operation);
55-
}
52+
SetParameters(operation);
5653

5754
// Responses
5855
SetResponses(operation);
@@ -142,12 +139,15 @@ protected virtual void SetRequestBody(OpenApiOperation operation)
142139
/// <param name="operation">The <see cref="OpenApiOperation"/>.</param>
143140
protected virtual void SetParameters(OpenApiOperation operation)
144141
{
145-
foreach (ODataKeySegment keySegment in Path.OfType<ODataKeySegment>())
142+
if (!Context.Settings.DeclarePathParametersOnPathItem)
146143
{
147-
IDictionary<string, string> mapping = ParameterMappings[keySegment];
148-
foreach (var p in Context.CreateKeyParameters(keySegment, mapping))
144+
foreach (ODataKeySegment keySegment in Path.OfType<ODataKeySegment>())
149145
{
150-
AppendParameter(operation, p);
146+
IDictionary<string, string> mapping = ParameterMappings[keySegment];
147+
foreach (var p in Context.CreateKeyParameters(keySegment, mapping))
148+
{
149+
AppendParameter(operation, p);
150+
}
151151
}
152152
}
153153

test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/EntityPathItemHandlerTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,45 @@ public void CreateEntityPathItemReturnsCorrectPathItem()
7676
pathItem.Operations.Select(o => o.Key));
7777
}
7878

79+
[Theory]
80+
[InlineData(true)]
81+
[InlineData(false)]
82+
public void CreateEntityPathItemReturnsCorrectPathItemWithPathParameters(bool declarePathParametersOnPathItem)
83+
{
84+
// Arrange
85+
IEdmModel model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: "");
86+
OpenApiConvertSettings convertSettings = new OpenApiConvertSettings
87+
{
88+
DeclarePathParametersOnPathItem = declarePathParametersOnPathItem,
89+
};
90+
ODataContext context = new ODataContext(model, convertSettings);
91+
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
92+
Assert.NotNull(entitySet); // guard
93+
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entitySet.EntityType()));
94+
95+
// Act
96+
var pathItem = _pathItemHandler.CreatePathItem(context, path);
97+
98+
// Assert
99+
Assert.NotNull(pathItem);
100+
101+
Assert.NotNull(pathItem.Operations);
102+
Assert.NotEmpty(pathItem.Operations);
103+
Assert.Equal(3, pathItem.Operations.Count);
104+
Assert.Equal(new OperationType[] { OperationType.Get, OperationType.Patch, OperationType.Delete },
105+
pathItem.Operations.Select(o => o.Key));
106+
107+
if (declarePathParametersOnPathItem)
108+
{
109+
Assert.NotEmpty(pathItem.Parameters);
110+
Assert.Equal(1, pathItem.Parameters.Count);
111+
}
112+
else
113+
{
114+
Assert.Empty(pathItem.Parameters);
115+
}
116+
}
117+
79118
[Fact]
80119
public void CreateEntityPathItemReturnsCorrectPathItemWithReferences()
81120
{

test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/NavigationPropertyPathItemHandlerTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,65 @@ public void CreateCollectionNavigationPropertyPathItemReturnsCorrectPathItem(boo
9595
Assert.NotEmpty(pathItem.Description);
9696
}
9797

98+
[Theory]
99+
[InlineData(true, true)]
100+
[InlineData(true, false)]
101+
[InlineData(false, true)]
102+
[InlineData(false, false)]
103+
public void CreateNavigationPropertyPathItemReturnsCorrectPathItemWithPathParameters(bool keySegment, bool declarePathParametersOnPathItem)
104+
{
105+
// Arrange
106+
IEdmModel model = GetEdmModel("");
107+
OpenApiConvertSettings settings = new()
108+
{
109+
DeclarePathParametersOnPathItem = declarePathParametersOnPathItem,
110+
};
111+
ODataContext context = new ODataContext(model, settings);
112+
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
113+
Assert.NotNull(entitySet); // guard
114+
IEdmEntityType entityType = entitySet.EntityType();
115+
116+
IEdmNavigationProperty property = entityType.DeclaredNavigationProperties()
117+
.FirstOrDefault(c => c.ContainsTarget == true && c.TargetMultiplicity() == EdmMultiplicity.Many);
118+
Assert.NotNull(property);
119+
120+
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(entitySet),
121+
new ODataKeySegment(entityType),
122+
new ODataNavigationPropertySegment(property));
123+
124+
if (keySegment)
125+
{
126+
path.Push(new ODataKeySegment(property.ToEntityType()));
127+
}
128+
129+
// Act
130+
var pathItem = _pathItemHandler.CreatePathItem(context, path);
131+
132+
// Assert
133+
Assert.NotNull(pathItem);
134+
135+
Assert.NotNull(pathItem.Operations);
136+
Assert.NotEmpty(pathItem.Operations);
137+
Assert.NotEmpty(pathItem.Description);
138+
139+
if (declarePathParametersOnPathItem)
140+
{
141+
Assert.NotEmpty(pathItem.Parameters);
142+
if (keySegment)
143+
{
144+
Assert.Equal(2, pathItem.Parameters.Count); // Customer ID and ContainedOrderLines Order ID
145+
}
146+
else
147+
{
148+
Assert.Equal(1, pathItem.Parameters.Count); // Customer ID
149+
}
150+
}
151+
else
152+
{
153+
Assert.Empty(pathItem.Parameters);
154+
}
155+
}
156+
98157
[Theory]
99158
[InlineData(true, new OperationType[] { OperationType.Get, OperationType.Patch, OperationType.Delete })]
100159
[InlineData(false, new OperationType[] { OperationType.Get })]

0 commit comments

Comments
 (0)