Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<PackageId>Microsoft.OpenApi.OData</PackageId>
<SignAssembly>true</SignAssembly>
<Version>1.2.0-preview8</Version>
<Version>1.2.0-preview9</Version>
<Description>This package contains the codes you need to convert OData CSDL to Open API Document of Model.</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>Microsoft OpenApi OData EDM</PackageTags>
Expand All @@ -30,6 +30,7 @@
- Expands navigation properties of derived types only if declaring navigation property is a containment #269
- Adds custom parameters to $count and ODataTypeCast paths' Get operations #207
- Adds support for configuring the default value of derived types' @odata.type property #304
- Adds OData query parameters to $count endpoints #313
</PackageReleaseNotes>
<AssemblyName>Microsoft.OpenApi.OData.Reader</AssemblyName>
<AssemblyOriginatorKeyFile>..\..\tool\Microsoft.OpenApi.OData.snk</AssemblyOriginatorKeyFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Generator;
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;

namespace Microsoft.OpenApi.OData.Operation
Expand All @@ -27,6 +28,8 @@ internal class DollarCountGetOperationHandler : OperationHandler
/// </summary>
internal ODataSegment LastSecondSegment { get; set; }
private const int SecondLastSegmentIndex = 2;
private IEdmVocabularyAnnotatable annotatable;

/// <inheritdoc/>
protected override void Initialize(ODataContext context, ODataPath path)
{
Expand All @@ -36,6 +39,15 @@ protected override void Initialize(ODataContext context, ODataPath path)
int count = path.Segments.Count;
if(count >= SecondLastSegmentIndex)
LastSecondSegment = path.Segments.ElementAt(count - SecondLastSegmentIndex);

if (LastSecondSegment is ODataNavigationSourceSegment sourceSegment)
{
annotatable = sourceSegment.NavigationSource as IEdmEntitySet;
}
else if (LastSecondSegment is ODataNavigationPropertySegment navigationPropertySegment)
{
annotatable = navigationPropertySegment.NavigationProperty;
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -75,19 +87,33 @@ protected override void SetResponses(OpenApiOperation operation)
base.SetResponses(operation);
}

protected override void AppendCustomParameters(OpenApiOperation operation)
/// <inheritdoc/>
protected override void SetParameters(OpenApiOperation operation)
{
IEdmVocabularyAnnotatable annotatable = null;
if (LastSecondSegment is ODataNavigationSourceSegment sourceSegment)
base.SetParameters(operation);

if (annotatable == null)
{
annotatable = sourceSegment.NavigationSource as IEdmEntitySet;
annotatable ??= sourceSegment.NavigationSource as IEdmSingleton;
return;
}
else if (LastSecondSegment is ODataNavigationPropertySegment navigationPropertySegment)

OpenApiParameter parameter;

parameter = Context.CreateSearch(annotatable);
if (parameter != null)
{
annotatable = navigationPropertySegment.NavigationProperty;
operation.Parameters.Add(parameter);
}

parameter = Context.CreateFilter(annotatable);
if (parameter != null)
{
operation.Parameters.Add(parameter);
}
}

protected override void AppendCustomParameters(OpenApiOperation operation)
{
if (annotatable == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,10 @@ public void CreateDollarCountGetOperationForNavigationPropertyReturnsCorrectOper
Assert.Equal("Get the number of the resource", operation.Summary);

Assert.NotNull(operation.Parameters);
Assert.Equal(2, operation.Parameters.Count);
Assert.Collection(operation.Parameters,
item =>
{
Assert.Equal("UserName", item.Name);
Assert.Equal(ParameterLocation.Path, item.In);
},
item =>
{
Assert.Equal("ConsistencyLevel", item.Name);
Assert.Equal(ParameterLocation.Header, item.In);
});

Assert.Equal(4, operation.Parameters.Count);
Assert.Equal(new[] { "UserName", "ConsistencyLevel", "search", "filter"},
operation.Parameters.Select(x => x.Name ?? x.Reference.Id).ToList());

Assert.Null(operation.RequestBody);

Assert.Equal(2, operation.Responses.Count);
Expand Down Expand Up @@ -105,18 +96,12 @@ public void CreateDollarCountGetOperationForNavigationSourceReturnsCorrectOperat
// Assert
Assert.NotNull(operation);
Assert.Equal("Get the number of the resource", operation.Summary);

Assert.NotNull(operation.Parameters);
Assert.Equal(1, operation.Parameters.Count);
Assert.Collection(operation.Parameters,
item =>
{
Assert.Equal("ConsistencyLevel", item.Name);
Assert.Equal(ParameterLocation.Header, item.In);
});
Assert.Equal(3, operation.Parameters.Count);
Assert.Equal(new[] { "ConsistencyLevel", "search", "filter" },
operation.Parameters.Select(x => x.Name ?? x.Reference.Id).ToList());

Assert.Null(operation.RequestBody);

Assert.Equal(2, operation.Responses.Count);
var statusCode = useHTTPStatusCodeClass2XX ? "2XX" : "200";
Assert.Equal(new string[] { statusCode, "default" }, operation.Responses.Select(e => e.Key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@
"get": {
"summary": "Get the number of the resource",
"operationId": "Get.Count.City-8728",
"parameters": [
{
"$ref": "#/parameters/search"
},
{
"$ref": "#/parameters/filter"
}
],
"responses": {
"200": {
"$ref": "#/responses/ODataCountResponse"
Expand Down Expand Up @@ -503,6 +511,14 @@
"get": {
"summary": "Get the number of the resource",
"operationId": "Get.Count.CountryOrRegion-daf5",
"parameters": [
{
"$ref": "#/parameters/search"
},
{
"$ref": "#/parameters/filter"
}
],
"responses": {
"200": {
"$ref": "#/responses/ODataCountResponse"
Expand Down Expand Up @@ -852,6 +868,14 @@
"get": {
"summary": "Get the number of the resource",
"operationId": "Get.Count.People-dd8d",
"parameters": [
{
"$ref": "#/parameters/search"
},
{
"$ref": "#/parameters/filter"
}
],
"responses": {
"200": {
"$ref": "#/responses/ODataCountResponse"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ paths:
get:
summary: Get the number of the resource
operationId: Get.Count.City-8728
parameters:
- $ref: '#/parameters/search'
- $ref: '#/parameters/filter'
responses:
'200':
$ref: '#/responses/ODataCountResponse'
Expand Down Expand Up @@ -331,6 +334,9 @@ paths:
get:
summary: Get the number of the resource
operationId: Get.Count.CountryOrRegion-daf5
parameters:
- $ref: '#/parameters/search'
- $ref: '#/parameters/filter'
responses:
'200':
$ref: '#/responses/ODataCountResponse'
Expand Down Expand Up @@ -566,6 +572,9 @@ paths:
get:
summary: Get the number of the resource
operationId: Get.Count.People-dd8d
parameters:
- $ref: '#/parameters/search'
- $ref: '#/parameters/filter'
responses:
'200':
$ref: '#/responses/ODataCountResponse'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@
"get": {
"summary": "Get the number of the resource",
"operationId": "Get.Count.City-8728",
"parameters": [
{
"$ref": "#/components/parameters/search"
},
{
"$ref": "#/components/parameters/filter"
}
],
"responses": {
"200": {
"$ref": "#/components/responses/ODataCountResponse"
Expand Down Expand Up @@ -567,6 +575,14 @@
"get": {
"summary": "Get the number of the resource",
"operationId": "Get.Count.CountryOrRegion-daf5",
"parameters": [
{
"$ref": "#/components/parameters/search"
},
{
"$ref": "#/components/parameters/filter"
}
],
"responses": {
"200": {
"$ref": "#/components/responses/ODataCountResponse"
Expand Down Expand Up @@ -955,6 +971,14 @@
"get": {
"summary": "Get the number of the resource",
"operationId": "Get.Count.People-dd8d",
"parameters": [
{
"$ref": "#/components/parameters/search"
},
{
"$ref": "#/components/parameters/filter"
}
],
"responses": {
"200": {
"$ref": "#/components/responses/ODataCountResponse"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ paths:
get:
summary: Get the number of the resource
operationId: Get.Count.City-8728
parameters:
- $ref: '#/components/parameters/search'
- $ref: '#/components/parameters/filter'
responses:
'200':
$ref: '#/components/responses/ODataCountResponse'
Expand Down Expand Up @@ -373,6 +376,9 @@ paths:
get:
summary: Get the number of the resource
operationId: Get.Count.CountryOrRegion-daf5
parameters:
- $ref: '#/components/parameters/search'
- $ref: '#/components/parameters/filter'
responses:
'200':
$ref: '#/components/responses/ODataCountResponse'
Expand Down Expand Up @@ -635,6 +641,9 @@ paths:
get:
summary: Get the number of the resource
operationId: Get.Count.People-dd8d
parameters:
- $ref: '#/components/parameters/search'
- $ref: '#/components/parameters/filter'
responses:
'200':
$ref: '#/components/responses/ODataCountResponse'
Expand Down
Loading