-
Notifications
You must be signed in to change notification settings - Fork 214
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
Polymorphic responses #2745
Comments
Hi @papegaaij |
Hi @baywet It could be that the example I provided is wrong or not in the format expected by Kiota. OpenAPI is rather free and there are many ways to write the same spec. If so, could you please explain how to change to spec? The documentation only contains an example about the inheritance, not with a polymorphic response. |
It might be because your user and group schemas don't have any properties of themselves and are getting trimmed (unless you simplified them for this discussion). Case and point /groups/id/members returns a collection of directory objects (wrap in a response class in that case, but similar to yours) https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/10eaf4b4bc7f0dc535b41938402bedb60d95cc7f/src/Microsoft.Graph/Generated/Models/DirectoryObjectCollectionResponse.cs#L17 |
@baywet It seems only those types are generated that are explicitly referenced from responses. If I change the example OpenAPI by adding a second path and have that path return the |
The trimming logic is supposed to include directly derived types of types that are referenced specifically for this downcast/discriminator scenario. |
No, the outcome is the same with 1.1.3. I've also added a property to both I decided to take a different approach and trim down the MSGraph openapi.yaml and I think I may have found the cause. In the example on https://learn.microsoft.com/nl-nl/openapi/kiota/models the base class is
It turns out that our own OpenAPI has the exact same issue, with the base type consisting of a single |
@baywet I don't really understand what you mean. The original form only had a single property, the discriminator. I don't see any part besides the allOf I could remove workout breaking the example. This is the original definition:
|
I mean this "microsoft.graph.directoryObject": {
"allOf": [
{
"title": "directoryObject",
"required": ["@odata.type"],
"type": "object",
"properties": {
"@odata.type": {
"type": "string",
"default": "#microsoft.graph.directoryObject"
}
}
}
]
}, |
I've tried several different setups, but as soon as the base type has an In the same category, I've also spotted an issue with the subtypes: these must have an It seems Kiota is quite strict in the format of the OpenAPI file, while the OpenAPI specification allows for a lot of variation. Maybe Kiota could benefit from a normalization step in the generation process, where these variations are all converted into a single canonical format? |
base type being an all of and derailing inheritance: if you could come up with a unit test as found in KiotaBuilderTests it'd be super helpful to investigate this issue. x > 2 entries: this should get flattened into a single class (with all properties) after implementing #2438 (version 1.2) We did a lot of thinking and adjustments on the types projection. The gist being JSON schema (what OpenAPI uses to describe the payloads) is a validation schema and not an IDL. People use AllOf to materialize inheritance by convention, there are a couple of different conventions going on, but it's not what JSON schema is designed for. While there are ongoing efforts to define an IDL vocab (extension) for JSON schema, we're still a long way out from broad adoption. For this reason we've had to make judgement calls. |
@baywet as you can see, I've created a PR with 2 new tests. Please note that these were my first lines of C# ever, so it could be that I did not do things entirely correct. |
The API we develop uses quite some inheritance in its models. The approach Kiota takes with
allOf
suits us quite well, but we are having problems defining paths with polymorphic responses. Our API is a bit too large to share here, but I've created a similar situation using the inheritance example from the documentation. This example API has one path that returns adirectoryObject
, which can be either auser
or agroup
.If the response references the
directoryObject
, the resulting code only contains thisdirectoryObject
and no code is generated for theuser
andgroup
. In this situation the entire typing is lost. If I change the type of the response to aoneOf
, the resulting code does contain model classes fordirectoryObject
,user
andgroup
. However, the result type of the request method will be a union type or wrapper. Although this is technically correct, it is not very convenient. The OpenAPI specification needs to enumerate all possible types and the user of the API has to check the returned type on every call.I'm looking for a way to have the request method use
directoryObject
as return type, but use the actual subtype (user
orgroup
) as value.The example OpenAPI specification is:
The text was updated successfully, but these errors were encountered: