-
Notifications
You must be signed in to change notification settings - Fork 253
Security requirement not serialized correctly #2300
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
Comments
@MaggieKimani1 can you take a look to see if we can reproduce it? It might have been fixed in the latest preview since preview7 was released in Feb. |
@RachitMalik12 I've managed to repro it.
Investigating why the Target in the Reference object is not being set. |
@martincostello the Target value above is not being set since the security scheme object hasn't been registered to the document's workspace, which is where reference resolution takes place. [Fact]
public void CanSerializeSecuritySchemes()
{
var document = new OpenApiDocument();
document.Components ??= new();
document.AddComponent("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("/auth-server/connect/authorize", UriKind.Relative),
TokenUrl = new Uri("/auth-server/connect/token", UriKind.Relative),
Scopes = new Dictionary<string, string>
{
{ "readAccess", "Access read operations" },
{ "writeAccess", "Access write operations" }
}
}
}
});
document.Security.Add(new OpenApiSecurityRequirement
{
{
new OpenApiSecuritySchemeReference("oauth2", document),
["readAccess", "writeAccess"]
}
});
using var writer = new StringWriter();
var jsonWriter = new OpenApiJsonWriter(writer);
document.SerializeAsV3(jsonWriter);
var json = writer.ToString();
} An alternative would be to add the line below to your test to ensure all components are registered: document.RegisterComponents(); |
Thanks for investigating @MaggieKimani1 - is this something that should be added to #2298? |
Yes I can update the upgrade guide to reflect this. Feel free to close the issue as it's not a bug. |
Describe the bug
I'm migrating Swashbuckle.AspNetCore to Microsoft.OpenApi 2.0.0-preview7 (as that's the version used by ASP.NET Core 10 preview 2), and an existing regression test we have for OAuth2 is failing due to the security definition not being deserialized correctly.
Distilling things down, this is what we expect:
However, what we get is this:
As far as I can tell, the
Target
is returningnull
(so nothing is emitted), but I don't understand why it isn't found via the reference into the host document:OpenAPI.NET/src/Microsoft.OpenApi/Models/OpenApiSecurityRequirement.cs
Line 76 in 3534a65
It might be that there's an additional code migration I need to do here for v2, but if there is I haven't been able to spot it.
OpenApi File To Reproduce
Expected behavior
The security scheme and the required scopes are serialized to the OpenAPI document.
The text was updated successfully, but these errors were encountered: