Skip to content
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]: Behavior change with UseOneOfForPolymorphism and abstract classes from version 7.0.0 and version 7.2.0 #3201

Open
fenzad opened this issue Dec 16, 2024 · 5 comments
Labels
bug help-wanted A change up for grabs for contributions from the community

Comments

@fenzad
Copy link

fenzad commented Dec 16, 2024

Describe the bug

I notice a change in the generation of the open API in this situation:

public abstract class A {
public string? PropA { get; set; }
}

public class B: A
{
public string? PropB { get; set; }
}

public class C: B
{
public string? PropC { get; set; }
}

public class TestController : ControllerBase
{
[HttpPost]
public ActionResult Post(A model)
{
return Ok();
}
}

builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.UseOneOfForPolymorphism();
c.SelectSubTypesUsing(t => typeof(A) == t ? [typeof(C)] : []);
});

Expected behavior

With version 7.0.0, the behavior is correct.

Immagine 2024-12-16 174955

Actual behavior

With version 7.2.0, the properties of class B are missing.

Immagine 2024-12-16 174944

Steps to reproduce

I am attaching the test solution, which currently references version 7.0.0 to test the correct case. To test the incorrect case with the new version, just update the references in the csproj to 7.2.0

TestSwagger.zip

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

7.0.0 and 7.2.0

.NET Version

net9.0

Anything else?

No response

@fenzad fenzad added the bug label Dec 16, 2024
@martincostello
Copy link
Collaborator

Do you mean 7.2.0? There is no 7.0.2.

@fenzad
Copy link
Author

fenzad commented Dec 16, 2024

7.2.0

@fenzad fenzad changed the title [Bug]: Behavior change with UseOneOfForPolymorphism and abstract classes from version 7.0.0 and version 7.0.2 [Bug]: Behavior change with UseOneOfForPolymorphism and abstract classes from version 7.0.0 and version 7.2.0 Dec 16, 2024
@fenzad
Copy link
Author

fenzad commented Dec 17, 2024

The issue is resolved by specifying in SelectSubTypesUsing:
c.SelectSubTypesUsing(t => typeof(A) == t ? [typeof(C)] : typeof(B) == t ? [typeof(C)] : []);
However, this is a different behavior compared to the previous version…

@martincostello
Copy link
Collaborator

To help narrow down where the regression happened, do you get the right behaviour with 7.1.0?

@fenzad
Copy link
Author

fenzad commented Dec 17, 2024

The correct behavior (which at this point I don’t know if it was more correct before or now) is up to and including version 7.0.0. From version 7.1.0, there has been a change.

@martincostello martincostello added the help-wanted A change up for grabs for contributions from the community label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help-wanted A change up for grabs for contributions from the community
Projects
None yet
Development

No branches or pull requests

2 participants