Skip to content

Commit da7f51a

Browse files
authored
Resolves response schemas of actions/functions that return collection (#117)
* Update response schema of action/functions that return a collection * Update Title to use entity type name for the previous segment * Update test files data appropriately
1 parent 3cce515 commit da7f51a

File tree

5 files changed

+365
-163
lines changed

5 files changed

+365
-163
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,28 @@ protected override void SetResponses(OpenApiOperation operation)
172172
}
173173
else
174174
{
175+
OpenApiSchema schema;
176+
if (EdmOperation.ReturnType.TypeKind() == EdmTypeKind.Collection)
177+
{
178+
// Get the entity type of the previous segment
179+
IEdmEntityType entityType = Path.Segments.Reverse().Skip(1).Take(1).FirstOrDefault().EntityType;
180+
schema = new OpenApiSchema
181+
{
182+
Title = $"Collection of {entityType.Name}",
183+
Type = "object",
184+
Properties = new Dictionary<string, OpenApiSchema>
185+
{
186+
{
187+
"value", Context.CreateEdmTypeSchema(EdmOperation.ReturnType)
188+
}
189+
}
190+
};
191+
}
192+
else
193+
{
194+
schema = Context.CreateEdmTypeSchema(EdmOperation.ReturnType);
195+
}
196+
175197
// function should have a return type.
176198
OpenApiResponse response = new OpenApiResponse
177199
{
@@ -182,7 +204,7 @@ protected override void SetResponses(OpenApiOperation operation)
182204
Constants.ApplicationJsonMediaType,
183205
new OpenApiMediaType
184206
{
185-
Schema = Context.CreateEdmTypeSchema(EdmOperation.ReturnType)
207+
Schema = schema
186208
}
187209
}
188210
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.json

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,9 +1169,15 @@
11691169
"200": {
11701170
"description": "Success",
11711171
"schema": {
1172-
"type": "array",
1173-
"items": {
1174-
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip"
1172+
"title": "Collection of Person",
1173+
"type": "object",
1174+
"properties": {
1175+
"value": {
1176+
"type": "array",
1177+
"items": {
1178+
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip"
1179+
}
1180+
}
11751181
}
11761182
}
11771183
},
@@ -1221,9 +1227,15 @@
12211227
"200": {
12221228
"description": "Success",
12231229
"schema": {
1224-
"type": "array",
1225-
"items": {
1226-
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
1230+
"title": "Collection of Person",
1231+
"type": "object",
1232+
"properties": {
1233+
"value": {
1234+
"type": "array",
1235+
"items": {
1236+
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
1237+
}
1238+
}
12271239
}
12281240
}
12291241
},
@@ -1636,9 +1648,15 @@
16361648
"200": {
16371649
"description": "Success",
16381650
"schema": {
1639-
"type": "array",
1640-
"items": {
1641-
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
1651+
"title": "Collection of Trip",
1652+
"type": "object",
1653+
"properties": {
1654+
"value": {
1655+
"type": "array",
1656+
"items": {
1657+
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
1658+
}
1659+
}
16421660
}
16431661
}
16441662
},
@@ -2814,9 +2832,15 @@
28142832
"200": {
28152833
"description": "Success",
28162834
"schema": {
2817-
"type": "array",
2818-
"items": {
2819-
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip"
2835+
"title": "Collection of Person",
2836+
"type": "object",
2837+
"properties": {
2838+
"value": {
2839+
"type": "array",
2840+
"items": {
2841+
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip"
2842+
}
2843+
}
28202844
}
28212845
}
28222846
},
@@ -2874,9 +2898,15 @@
28742898
"200": {
28752899
"description": "Success",
28762900
"schema": {
2877-
"type": "array",
2878-
"items": {
2879-
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
2901+
"title": "Collection of Person",
2902+
"type": "object",
2903+
"properties": {
2904+
"value": {
2905+
"type": "array",
2906+
"items": {
2907+
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
2908+
}
2909+
}
28802910
}
28812911
}
28822912
},
@@ -3353,9 +3383,15 @@
33533383
"200": {
33543384
"description": "Success",
33553385
"schema": {
3356-
"type": "array",
3357-
"items": {
3358-
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
3386+
"title": "Collection of Trip",
3387+
"type": "object",
3388+
"properties": {
3389+
"value": {
3390+
"type": "array",
3391+
"items": {
3392+
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
3393+
}
3394+
}
33593395
}
33603396
}
33613397
},
@@ -4563,9 +4599,15 @@
45634599
"200": {
45644600
"description": "Success",
45654601
"schema": {
4566-
"type": "array",
4567-
"items": {
4568-
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip"
4602+
"title": "Collection of Person",
4603+
"type": "object",
4604+
"properties": {
4605+
"value": {
4606+
"type": "array",
4607+
"items": {
4608+
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip"
4609+
}
4610+
}
45694611
}
45704612
}
45714613
},
@@ -4623,9 +4665,15 @@
46234665
"200": {
46244666
"description": "Success",
46254667
"schema": {
4626-
"type": "array",
4627-
"items": {
4628-
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
4668+
"title": "Collection of Person",
4669+
"type": "object",
4670+
"properties": {
4671+
"value": {
4672+
"type": "array",
4673+
"items": {
4674+
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
4675+
}
4676+
}
46294677
}
46304678
}
46314679
},
@@ -5102,9 +5150,15 @@
51025150
"200": {
51035151
"description": "Success",
51045152
"schema": {
5105-
"type": "array",
5106-
"items": {
5107-
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
5153+
"title": "Collection of Trip",
5154+
"type": "object",
5155+
"properties": {
5156+
"value": {
5157+
"type": "array",
5158+
"items": {
5159+
"$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person"
5160+
}
5161+
}
51085162
}
51095163
}
51105164
},

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.yaml

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,13 @@ paths:
801801
'200':
802802
description: Success
803803
schema:
804-
type: array
805-
items:
806-
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip'
804+
title: Collection of Person
805+
type: object
806+
properties:
807+
value:
808+
type: array
809+
items:
810+
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip'
807811
default:
808812
$ref: '#/responses/error'
809813
x-ms-docs-operation-type: function
@@ -836,9 +840,13 @@ paths:
836840
'200':
837841
description: Success
838842
schema:
839-
type: array
840-
items:
841-
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
843+
title: Collection of Person
844+
type: object
845+
properties:
846+
value:
847+
type: array
848+
items:
849+
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
842850
default:
843851
$ref: '#/responses/error'
844852
x-ms-docs-operation-type: action
@@ -1127,9 +1135,13 @@ paths:
11271135
'200':
11281136
description: Success
11291137
schema:
1130-
type: array
1131-
items:
1132-
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
1138+
title: Collection of Trip
1139+
type: object
1140+
properties:
1141+
value:
1142+
type: array
1143+
items:
1144+
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
11331145
default:
11341146
$ref: '#/responses/error'
11351147
x-ms-docs-operation-type: function
@@ -1952,9 +1964,13 @@ paths:
19521964
'200':
19531965
description: Success
19541966
schema:
1955-
type: array
1956-
items:
1957-
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip'
1967+
title: Collection of Person
1968+
type: object
1969+
properties:
1970+
value:
1971+
type: array
1972+
items:
1973+
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip'
19581974
default:
19591975
$ref: '#/responses/error'
19601976
x-ms-docs-operation-type: function
@@ -1993,9 +2009,13 @@ paths:
19932009
'200':
19942010
description: Success
19952011
schema:
1996-
type: array
1997-
items:
1998-
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
2012+
title: Collection of Person
2013+
type: object
2014+
properties:
2015+
value:
2016+
type: array
2017+
items:
2018+
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
19992019
default:
20002020
$ref: '#/responses/error'
20012021
x-ms-docs-operation-type: action
@@ -2332,9 +2352,13 @@ paths:
23322352
'200':
23332353
description: Success
23342354
schema:
2335-
type: array
2336-
items:
2337-
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
2355+
title: Collection of Trip
2356+
type: object
2357+
properties:
2358+
value:
2359+
type: array
2360+
items:
2361+
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
23382362
default:
23392363
$ref: '#/responses/error'
23402364
x-ms-docs-operation-type: function
@@ -3181,9 +3205,13 @@ paths:
31813205
'200':
31823206
description: Success
31833207
schema:
3184-
type: array
3185-
items:
3186-
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip'
3208+
title: Collection of Person
3209+
type: object
3210+
properties:
3211+
value:
3212+
type: array
3213+
items:
3214+
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip'
31873215
default:
31883216
$ref: '#/responses/error'
31893217
x-ms-docs-operation-type: function
@@ -3222,9 +3250,13 @@ paths:
32223250
'200':
32233251
description: Success
32243252
schema:
3225-
type: array
3226-
items:
3227-
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
3253+
title: Collection of Person
3254+
type: object
3255+
properties:
3256+
value:
3257+
type: array
3258+
items:
3259+
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
32283260
default:
32293261
$ref: '#/responses/error'
32303262
x-ms-docs-operation-type: action
@@ -3561,9 +3593,13 @@ paths:
35613593
'200':
35623594
description: Success
35633595
schema:
3564-
type: array
3565-
items:
3566-
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
3596+
title: Collection of Trip
3597+
type: object
3598+
properties:
3599+
value:
3600+
type: array
3601+
items:
3602+
$ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
35673603
default:
35683604
$ref: '#/responses/error'
35693605
x-ms-docs-operation-type: function

0 commit comments

Comments
 (0)