diff --git a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs index 3d446d82..1e981091 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs @@ -172,6 +172,28 @@ protected override void SetResponses(OpenApiOperation operation) } else { + 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 {entityType.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 +204,7 @@ protected override void SetResponses(OpenApiOperation operation) Constants.ApplicationJsonMediaType, new OpenApiMediaType { - Schema = Context.CreateEdmTypeSchema(EdmOperation.ReturnType) + Schema = schema } } } 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