Skip to content

Commit a5acb89

Browse files
committed
fix: security scheme references serialization
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent a6291d1 commit a5acb89

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Microsoft.OpenApi/Models/OpenApiSecurityRequirement.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ private void SerializeInternal(IOpenApiWriter writer, Action<IOpenApiWriter, Ope
7979

8080
writer.WriteStartObject();
8181

82-
// Reaching this point means the reference to a specific OpenApiSecurityScheme fails.
83-
// We are not able to serialize this SecurityScheme/Scopes key value pair since we do not know what
84-
// string to output.
85-
86-
foreach (var securitySchemeAndScopesValuePair in this.Where(static p => p.Key?.Target is not null))
82+
foreach (var securitySchemeAndScopesValuePair in this.Where(static p => CanSerializeSecurityScheme(p.Key)))
8783
{
8884
var securityScheme = securitySchemeAndScopesValuePair.Key;
8985
var scopes = securitySchemeAndScopesValuePair.Value;
@@ -103,6 +99,25 @@ private void SerializeInternal(IOpenApiWriter writer, Action<IOpenApiWriter, Ope
10399
writer.WriteEndObject();
104100
}
105101

102+
private static bool CanSerializeSecurityScheme(OpenApiSecuritySchemeReference? securityScheme)
103+
{
104+
if (securityScheme is null)
105+
{
106+
return false;
107+
}
108+
109+
if (securityScheme.Target is not null)
110+
{
111+
return true;
112+
}
113+
114+
var schemeId = securityScheme.Reference?.Id;
115+
var securitySchemes = securityScheme.Reference?.HostDocument?.Components?.SecuritySchemes;
116+
return !string.IsNullOrEmpty(schemeId)
117+
&& securitySchemes is not null
118+
&& securitySchemes.ContainsKey(schemeId!);
119+
}
120+
106121
/// <summary>
107122
/// Serialize <see cref="OpenApiSecurityRequirement"/> to Open Api v2.0
108123
/// </summary>

0 commit comments

Comments
 (0)