Repository navigation
Avoid materializing all custom attributes during OpenAPI schema generation - #70171
Conversation
…ation Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
The frozen base/head evidence, existing feedback, and independent guide review were unavailable, preventing a complete review and deduplication of findings.
No open issues 🎉
What changed in this PR
Reduces reflection and allocation overhead during ASP.NET Core OpenAPI document generation while preserving attribute precedence and schema annotation order.
Changes:
- Materializes only matching type-level description attributes and uses
IsDefinedfor obsolete and required checks. - Consolidates property-attribute discovery into one pass, applying validation only when validation attributes are present.
- Adds coverage for derived description attributes on types and properties.
| File | Description |
|---|---|
| src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.Annotations.cs | Tests derived description attributes on types and properties. |
| src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs | Narrows type-attribute reflection and consolidates property-attribute discovery. |
| src/OpenApi/src/Services/OpenApiDocumentService.cs | Uses IsDefined to detect required parameters without materializing attributes. |
Review effort: Balanced
|
/review |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Merge assessmentVerdict: Merge candidate after a fresh
Reasons to merge
Reasons not to merge yet
What would change this verdict: A fresh CI failure or Verify snapshot diff after rebasing onto current main would make this a Hold. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Avoid materializing all custom attributes during OpenAPI schema generation
Avoid materializing all custom attributes in OpenAPI schema generation
Description
OpenAPI schemas are rebuilt every time a document is generated, and
TransformSchemaNoderuns for every schema node, including every property of every type. For each node it:underlyingType.GetCustomAttributes(inherit: false). That instantiates every attribute on the type, including the[Serializable],[TypeForwardedFrom]and similar attributes onstring,DateTime,intand so on. It then scanned the array withOfType<DescriptionAttribute>().LastOrDefault()andOfType<ObsoleteAttribute>().Any().OfType<T>()LINQ iterators.This change:
GetCustomAttributes(typeof(DescriptionAttribute), inherit: false)(last one wins, as before) andIsDefined(typeof(ObsoleteAttribute), inherit: false). Only matching attributes are materialized. Derived attribute types still match.GetCustomAttributes(inherit: false)once, but finds the lastDescriptionAttribute, the lastDefaultValueAttribute, anyObsoleteAttributeand anyValidationAttributein a singleforeachpass.ApplyValidationAttributesis only called (with the sameOfType<ValidationAttribute>()sequence) when a validation attribute exists. With none it was a no-op.OpenApiDocumentService.IsRequired: usesParameterInfo.IsDefined(typeof(RequiredAttribute), inherit: true)instead of materializing all parameter attributes for.Any(attr => attr is RequiredAttribute).PropertyAsParameterInfooverridesIsDefinedconsistently withGetCustomAttributes.The order of operations on the schema is unchanged: validation, default value, then description/deprecated. Generated documents are byte-identical. No public API or behavior changes.
Results
GenerationBenchmarks.GenerateDocument(existing benchmark), local BenchmarkDotNet, 4 A/B rounds against main (round 4 ran in reverse order). Each cell shows mean ± error. Rounds 2–3 were noisy because of contention on the shared VM.In the quiet rounds (1 and 4) generation is about 10–13% faster, and allocations drop by 11.8% in every round. This is independent of, and additive with, #70168.
Tests
SchemaDescriptions_HandlesDerivedDescriptionAttributes, which checks that a subclass ofDescriptionAttributeon a type and on a property is still honored.Microsoft.AspNetCore.OpenApi.Tests: 1034 passed, 5 skipped. This includes the snapshot (Verify) integration tests that compare full generated documents, plus the existing Description, Obsolete, DefaultValue and validation attribute tests.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)