From 73fa6ba5cd5817839917354a4a1b8f73238b9e1a Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Wed, 15 Sep 2021 15:43:40 +0300 Subject: [PATCH 1/3] Update response schema of action/functions that return a collection --- .../Operation/EdmOperationOperationHandler.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs index 3d446d82..dc8f7692 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs @@ -172,6 +172,26 @@ protected override void SetResponses(OpenApiOperation operation) } else { + OpenApiSchema schema; + if (EdmOperation.ReturnType.TypeKind() == EdmTypeKind.Collection) + { + schema = new OpenApiSchema + { + Title = $"Collection of {EdmOperation.Name}", + Type = "object", + Properties = new Dictionary + { + { + "value", Context.CreateEdmTypeSchema(EdmOperation.ReturnType) + } + }, + }; + } + else + { + schema = Context.CreateEdmTypeSchema(EdmOperation.ReturnType); + } + // function should have a return type. OpenApiResponse response = new OpenApiResponse { @@ -182,7 +202,7 @@ protected override void SetResponses(OpenApiOperation operation) Constants.ApplicationJsonMediaType, new OpenApiMediaType { - Schema = Context.CreateEdmTypeSchema(EdmOperation.ReturnType) + Schema = schema } } } From 762ef1dc73fe31cebcdc214d17923262f58bab3d Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Wed, 15 Sep 2021 17:00:31 +0300 Subject: [PATCH 2/3] Update Title to use entity type name for the previous segment --- .../Operation/EdmOperationOperationHandler.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs index dc8f7692..1e981091 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs @@ -175,16 +175,18 @@ protected override void SetResponses(OpenApiOperation operation) OpenApiSchema schema; if (EdmOperation.ReturnType.TypeKind() == EdmTypeKind.Collection) { + // Get the entity type of the previous segment + IEdmEntityType entityType = Path.Segments.Reverse().Skip(1).Take(1).FirstOrDefault().EntityType; schema = new OpenApiSchema { - Title = $"Collection of {EdmOperation.Name}", + Title = $"Collection of {entityType.Name}", Type = "object", Properties = new Dictionary { { "value", Context.CreateEdmTypeSchema(EdmOperation.ReturnType) } - }, + } }; } else From af780a6fb8c90765669b6a40ba5ea748e76f8778 Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Wed, 15 Sep 2021 17:10:20 +0300 Subject: [PATCH 3/3] Update test files data appropriately --- .../Resources/TripService.OpenApi.V2.json | 108 ++++++++--- .../Resources/TripService.OpenApi.V2.yaml | 90 ++++++--- .../Resources/TripService.OpenApi.json | 180 ++++++++++++------ .../Resources/TripService.OpenApi.yaml | 126 +++++++----- 4 files changed, 342 insertions(+), 162 deletions(-) diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.json index 71f40acd..8ad1c253 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.json @@ -1169,9 +1169,15 @@ "200": { "description": "Success", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + } + } } } }, @@ -1221,9 +1227,15 @@ "200": { "description": "Success", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + } } } }, @@ -1636,9 +1648,15 @@ "200": { "description": "Success", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Trip", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + } } } }, @@ -2814,9 +2832,15 @@ "200": { "description": "Success", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + } + } } } }, @@ -2874,9 +2898,15 @@ "200": { "description": "Success", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + } } } }, @@ -3353,9 +3383,15 @@ "200": { "description": "Success", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Trip", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + } } } }, @@ -4563,9 +4599,15 @@ "200": { "description": "Success", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + } + } } } }, @@ -4623,9 +4665,15 @@ "200": { "description": "Success", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + } } } }, @@ -5102,9 +5150,15 @@ "200": { "description": "Success", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Trip", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "$ref": "#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + } } } }, diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.yaml index 531ce05e..dbe4998d 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.yaml @@ -801,9 +801,13 @@ paths: '200': description: Success schema: - type: array - items: - $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' + title: Collection of Person + type: object + properties: + value: + type: array + items: + $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' default: $ref: '#/responses/error' x-ms-docs-operation-type: function @@ -836,9 +840,13 @@ paths: '200': description: Success schema: - type: array - items: - $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + title: Collection of Person + type: object + properties: + value: + type: array + items: + $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' default: $ref: '#/responses/error' x-ms-docs-operation-type: action @@ -1127,9 +1135,13 @@ paths: '200': description: Success schema: - type: array - items: - $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + title: Collection of Trip + type: object + properties: + value: + type: array + items: + $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' default: $ref: '#/responses/error' x-ms-docs-operation-type: function @@ -1952,9 +1964,13 @@ paths: '200': description: Success schema: - type: array - items: - $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' + title: Collection of Person + type: object + properties: + value: + type: array + items: + $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' default: $ref: '#/responses/error' x-ms-docs-operation-type: function @@ -1993,9 +2009,13 @@ paths: '200': description: Success schema: - type: array - items: - $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + title: Collection of Person + type: object + properties: + value: + type: array + items: + $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' default: $ref: '#/responses/error' x-ms-docs-operation-type: action @@ -2332,9 +2352,13 @@ paths: '200': description: Success schema: - type: array - items: - $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + title: Collection of Trip + type: object + properties: + value: + type: array + items: + $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' default: $ref: '#/responses/error' x-ms-docs-operation-type: function @@ -3181,9 +3205,13 @@ paths: '200': description: Success schema: - type: array - items: - $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' + title: Collection of Person + type: object + properties: + value: + type: array + items: + $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' default: $ref: '#/responses/error' x-ms-docs-operation-type: function @@ -3222,9 +3250,13 @@ paths: '200': description: Success schema: - type: array - items: - $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + title: Collection of Person + type: object + properties: + value: + type: array + items: + $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' default: $ref: '#/responses/error' x-ms-docs-operation-type: action @@ -3561,9 +3593,13 @@ paths: '200': description: Success schema: - type: array - items: - $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + title: Collection of Trip + type: object + properties: + value: + type: array + items: + $ref: '#/definitions/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' default: $ref: '#/responses/error' x-ms-docs-operation-type: function diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.json index 30639236..5d092910 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.json @@ -1331,14 +1331,20 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "anyOf": [ - { - "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + } + ], + "nullable": true } - ], - "nullable": true + } } } } @@ -1386,14 +1392,20 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "anyOf": [ - { - "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + ], + "nullable": true } - ], - "nullable": true + } } } } @@ -1841,14 +1853,20 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "anyOf": [ - { - "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Trip", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + ], + "nullable": true } - ], - "nullable": true + } } } } @@ -3162,14 +3180,20 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "anyOf": [ - { - "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + } + ], + "nullable": true } - ], - "nullable": true + } } } } @@ -3229,14 +3253,20 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "anyOf": [ - { - "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + ], + "nullable": true } - ], - "nullable": true + } } } } @@ -3768,14 +3798,20 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "anyOf": [ - { - "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Trip", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + ], + "nullable": true } - ], - "nullable": true + } } } } @@ -5129,14 +5165,20 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "anyOf": [ - { - "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip" + } + ], + "nullable": true } - ], - "nullable": true + } } } } @@ -5196,14 +5238,20 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "anyOf": [ - { - "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Person", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + ], + "nullable": true } - ], - "nullable": true + } } } } @@ -5735,14 +5783,20 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "anyOf": [ - { - "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + "title": "Collection of Trip", + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person" + } + ], + "nullable": true } - ], - "nullable": true + } } } } diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.yaml index 57c53be5..5c441d1b 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.yaml @@ -894,11 +894,15 @@ paths: content: application/json: schema: - type: array - items: - anyOf: - - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' - nullable: true + title: Collection of Person + type: object + properties: + value: + type: array + items: + anyOf: + - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' + nullable: true default: $ref: '#/components/responses/error' x-ms-docs-operation-type: function @@ -929,11 +933,15 @@ paths: content: application/json: schema: - type: array - items: - anyOf: - - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' - nullable: true + title: Collection of Person + type: object + properties: + value: + type: array + items: + anyOf: + - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + nullable: true default: $ref: '#/components/responses/error' x-ms-docs-operation-type: action @@ -1243,11 +1251,15 @@ paths: content: application/json: schema: - type: array - items: - anyOf: - - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' - nullable: true + title: Collection of Trip + type: object + properties: + value: + type: array + items: + anyOf: + - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + nullable: true default: $ref: '#/components/responses/error' x-ms-docs-operation-type: function @@ -2152,11 +2164,15 @@ paths: content: application/json: schema: - type: array - items: - anyOf: - - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' - nullable: true + title: Collection of Person + type: object + properties: + value: + type: array + items: + anyOf: + - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' + nullable: true default: $ref: '#/components/responses/error' x-ms-docs-operation-type: function @@ -2195,11 +2211,15 @@ paths: content: application/json: schema: - type: array - items: - anyOf: - - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' - nullable: true + title: Collection of Person + type: object + properties: + value: + type: array + items: + anyOf: + - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + nullable: true default: $ref: '#/components/responses/error' x-ms-docs-operation-type: action @@ -2567,11 +2587,15 @@ paths: content: application/json: schema: - type: array - items: - anyOf: - - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' - nullable: true + title: Collection of Trip + type: object + properties: + value: + type: array + items: + anyOf: + - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + nullable: true default: $ref: '#/components/responses/error' x-ms-docs-operation-type: function @@ -3504,11 +3528,15 @@ paths: content: application/json: schema: - type: array - items: - anyOf: - - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' - nullable: true + title: Collection of Person + type: object + properties: + value: + type: array + items: + anyOf: + - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Trip' + nullable: true default: $ref: '#/components/responses/error' x-ms-docs-operation-type: function @@ -3547,11 +3575,15 @@ paths: content: application/json: schema: - type: array - items: - anyOf: - - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' - nullable: true + title: Collection of Person + type: object + properties: + value: + type: array + items: + anyOf: + - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + nullable: true default: $ref: '#/components/responses/error' x-ms-docs-operation-type: action @@ -3919,11 +3951,15 @@ paths: content: application/json: schema: - type: array - items: - anyOf: - - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' - nullable: true + title: Collection of Trip + type: object + properties: + value: + type: array + items: + anyOf: + - $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person' + nullable: true default: $ref: '#/components/responses/error' x-ms-docs-operation-type: function