-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Bug]: SchemaFilter return properties count zero for subclasses #3021
Comments
How is Swashbuckle configured for your app? There's some specifics documented here for sub-types. There were also some changes related to subclasses, required behaviour for .NET 8 support and polymorphism since 6.6.x. Are things still not working the way you'd expect with 6.7.0? |
@martincostello , in my minimal project for reproducing the issue I only have:
The code suggested:
did not help unfortunately.
The problem still exists in 6.7.0. |
Looks like the change in behaviour was caused by #2815. The question is whether this is a bug due to something missing on that change, or correct behaviour because of that change. I'm still digging into that. |
Repro schema with 6.5.0: "components": {
"schemas": {
"CoffeeMachine": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Machine"
}
],
"properties": {
"beanType": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
},
"Machine": {
"type": "object",
"additionalProperties": false
}
}
} With 6.7.0: "components": {
"schemas": {
"CoffeeMachine": {
"allOf": [
{
"$ref": "#/components/schemas/Machine"
},
{
"type": "object",
"properties": {
"beanType": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
]
},
"Machine": {
"type": "object",
"additionalProperties": false
}
}
} |
@bkoelman You might know more about this than I do. Is this a bug because something is missing, or is this "as intended" based on the original bug fix and the issue as reported here is depending on a buggy behaviour in a previous version? |
I'm pretty sure that 6.7.0 is correct. The way I understand the spec:
Evidence for 1: https://redocly.com/docs/resources/discriminator#common-mistakes See also the full example at https://learn.microsoft.com/en-us/openapi/kiota/models#example-1---using-allof-and-discriminator, which is written by the kiota team. That same team maintains the OpenAPI.NET library that Swashbuckle uses. And they take seat in the OpenAPI standardization committee, so they're pretty likely to get it right. I can confirm that using their sample structure works with both kiota and NSwag C# client generators. Before my PR #2815, neither of them worked. So to determine the owned properties of a component schema (excluding base properties), the algorithm should be:
Hope this clarifies it. |
Thanks I'll read that properly tomorrow. The bit I'm not sure about is inheritance compared to polymorphism, as the given example for this issue appears to only be inheritance, as there isn't a type discriminator. I see how this makes sense for polymorphism, but it's inheritance that seems less clear to me. |
In that case, see the first entry at https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/. A discriminator is optional, leaving it out doesn't change the basic rules. Whether using Just as you can't combine ref with inline properties, it doesn't make sense to have to choose between schema A or B, oh and here are some more properties. How are the extra properties supposed to relate to the choice between A or B? If the intent is merging them, wrap the oneOf A/B and the inline properties in an |
Thanks for investigating this. If the new behavior is correct, could you please provide guidance on how to adjust my schema accordingly? Specifically, I need to mark all properties as required.
|
Something like this: public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
foreach (var model in schema.AllOf)
{
var additionalRequiredProps = model.Properties
.Where(x => !model.Required.Contains(x.Key))
.Select(x => x.Key);
foreach (var propKey in additionalRequiredProps)
{
model.Required.Add(propKey);
}
}
} Which outputs this for your repro: {
"components": {
"schemas": {
"CoffeeMachine": {
"allOf": [
{
"$ref": "#/components/schemas/Machine"
},
{
"required": [
"beanType"
],
"type": "object",
"properties": {
"beanType": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
]
},
"Machine": {
"type": "object",
"additionalProperties": false
}
}
}
} |
Based on @bkoelman's explanation, the behaviour in 6.6.x+ appears to be correct. That means that unfortunately your code was broken @coder925 because you depended on the bug in previous versions with how this was handled. If you update your filter similar to the example above, you should be back to getting an OpenAPI document for your use case like it was before. |
Describe the bug
From v.6.6.1 an implementation of ISchemaFilter will not return any properties for subclasses:
int propertiesCount = schema.Properties.Count; // 0
This is a regression bug. It works as expected in v6.5.0.
Full implementatoin code of SchemaFilter:
Expected behavior
Should return
1
for my example:Actual behavior
Returns
0
for my example:Steps to reproduce
Exception(s) (if any)
No response
Swashbuckle.AspNetCore version
6.6.1
.NET Version
8.0.303
Anything else?
I have a MarkAllAsRequiredFilter. I detected this issue since all properties of subclasses got optional from v6.6.1.
This was my original code:
The text was updated successfully, but these errors were encountered: