You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OpenApiSecurityRequirement serializes as empty object when constructed programmatically with OpenApiSecuritySchemeReference #2801
When building an OpenApiDocument programmatically and adding security requirements using OpenApiSecuritySchemeReference as dictionary keys in OpenApiSecurityRequirement, the serialized output produces "security": [{}] instead of "security": [{"Bearer": []}].
The root cause is in OpenApiSecurityRequirement.SerializeInternal(), which filters entries with:
this.Where(static p =>p.Key?.Targetis not null)
When the document is constructed in memory (not parsed from a file), Target resolves via Reference.HostDocument.ResolveReferenceTo<U>(Reference, ...), which returns null because the security scheme was added to Components.SecuritySchemes as a plain OpenApiSecurityScheme, not as an IOpenApiSecurityScheme that the resolver can match. The filter silently drops the entry with no warning.
No exception or warning is produced — the entry is silently dropped.
Version
Microsoft.OpenApi 2.0.0-preview17 (and later, including transitive via Microsoft.OpenApi.OData 3.2.0)
Also affects 3.0 and 3.1 serialization — all three SerializeAsV3/V31/V32 methods share the same SerializeInternal code path.
Additional context
This is a common scenario when using Microsoft.OpenApi.OData to generate an OpenAPI document from an EDM model and then adding security definitions programmatically.
Workaround: subclass OpenApiSecurityRequirement and override SerializeAsV3/SerializeAsV31/SerializeAsV32 to write the security scheme name directly via IOpenApiWriter.
Suggested fix
The Where filter in SerializeInternal should fall back to Reference.ReferenceV3 (or Reference.Id) when Target is null, rather than silently skipping the entry. The callback already uses s.Reference.ReferenceV3 for the property name — the guard just needs to allow entries where the reference ID is available even if Target couldn't be resolved.
Have you tried with the latest versions? As you can see, I've added a unit test with pretty much the same code you've shared, and it's passing in #2802
Let me know if you have any additional comments or questions.
Activity
baywet commentedon Mar 30, 2026
Hi Jonas Lewin (@jonaslewin)
Thank you for using the SDK and for reaching out.
Have you tried with the latest versions? As you can see, I've added a unit test with pretty much the same code you've shared, and it's passing in #2802
Let me know if you have any additional comments or questions.
baywet commentedon Mar 30, 2026
Actually nevermind, the test results I had locally were outdated. The CI failed. I'll look further into this tomorrow.
baywet commentedon Mar 30, 2026
(pushed the fix)