-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for discriminator capability in response payloads #118
Comments
@darrelmiller this is one of the last issues on the 1.0.10 roadmap, could you please provide the technical details so we can implement this as part of the conversion process? |
Examples can be found here https://github.com/microsoft/kiota/pull/1014/files |
The following example demonstrates how to define a discriminator property in a JSON Schema without needing to use the discriminator keyword. The property is defined as an enum with just a single allowed value. This is effectively the same as a The The challenge for kiota is that it will need to identify that the type of object that needs to be instantiated is based on a property that has an enum constraint with a single value. We can solve with a Microsoft Graph core because we know what the discriminator is called, but to solve this generically, will require a little more creativity. openapi: 3.0.3
info:
title: Collection filtered by query parameter
version: 1.0.0
servers:
- url: https://example.org/
paths:
/sessions:
get:
parameters:
- name: location
in: query
required: false
schema:
type: string
responses:
200:
description: Ok
content:
application/json:
schema:
type: array
item:
$ref: "#/components/schemas/session"
components:
schemas:
entity:
type: object
properties:
id:
type: string
session:
allof:
- $ref: "#/components/schemas/entity"
type: object
properties:
'@OData.Type':
type: string
enum:
- session
displayName:
type: string
location:
type: string
workshop:
allof:
- $ref: "#/components/schemas/session"
type: object
properties:
'@OData.Type':
type: string
enum:
- workshop
requiredEquipment:
type: string
presentation:
allof:
- $ref: "#/components/schemas/session"
type: object
properties:
'@OData.Type':
type: string
enum:
- presentation
recorded:
type: boolean Additionally, we should check for the DerivedTypeConstraint annotation to determine if we should present the response schema as a oneOf. e.g. responses:
200:
description: Ok
content:
application/json:
schema:
type: array
item:
oneOf:
- $ref: "#/components/schemas/session"
- $ref: "#/components/schemas/workshop"
- $ref: "#/components/schemas/presentation" |
so if I understand things property because the properties OData.Type are of type enum and because the enum values match a component name, they are discriminators? Also because there is inheritance between the components. Shouldn't we wait for openapi.net to implement 3.1 so we can use the const keywork instead? Also, as you mentioned, this description doesn't describe how to parse the discriminator from the API, would some x-ms extension on the document with some kind of templating be helpful? e.g |
They implicitly work as discriminators
We want Kiota to still work for OpenAPI 3.0. Most people have not migrated to 3.1 yet. Graph could use 3.1 once we support it.
I think we can infer the discriminator from the OpenAPI description. If we needed to be explicit, we can use the OpenAPI discriminator keyword. |
Can you expand on why the example you provided doesn't include the discriminator keyword since it's part of 3.0? |
with this extension, how do you know which field is the discriminator? (i.e. Odata.type) |
Actually that's what I'm scratching my head around too. |
The |
Following on from a conversation about this topic the current proposal is to continue using the |
@darrelmiller, can you provider a complete and updated example of what you want for v3 and v2 for clarity please? |
This library only outputs V3, so I'm not sure why we care what V2 does. MyResponseType:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Lizard'
- $ref: 'https://gigantic-server.com/schemas/Monster/schema.json'
discriminator:
propertyName: petType
mapping:
dog: '#/components/schemas/Dog'
monster: 'https://gigantic-server.com/schemas/Monster/schema.json' |
So to translate that in an example closer to Microsoft Graph, it'd look like this for DirectoryObjectResponseType:
oneOf:
- $ref: '#/components/schemas/microsoft.graph.servicePrincipal'
- $ref: '#/components/schemas/microsoft.graph.user'
- $ref: '#/components/schemas/microsoft.graph.device'
- $ref: '#/components/schemas/microsoft.graph.orgContact'
discriminator:
propertyName: @odata.type
mapping:
'#microsoft.graph.servicePrincipal': '#/components/schemas/microsoft.graph.servicePrincipal'
'#microsoft.graph.user': '#/components/schemas/microsoft.graph.user'
'#microsoft.graph.device': '#/components/schemas/microsoft.graph.device'
'#microsoft.graph.orgContact': '#/components/schemas/microsoft.graph.orgContact' Correct? |
Or perhaps something like this?
|
I think you're correct for the most part except the mapping key should be the odata type value and the mapping value should be the component full key. |
This would allow representation of heterogenous collections. Details to follow,
The text was updated successfully, but these errors were encountered: