Map [Obsolete] attribute to deprecated in OpenAPI documents#66355
Open
fickleEfrit wants to merge 2 commits intodotnet:mainfrom
Open
Map [Obsolete] attribute to deprecated in OpenAPI documents#66355fickleEfrit wants to merge 2 commits intodotnet:mainfrom
fickleEfrit wants to merge 2 commits intodotnet:mainfrom
Conversation
Add built-in support for mapping ObsoleteAttribute to the OpenAPI deprecated flag on operations, schemas, and schema properties. - Operations: endpoints with [Obsolete] on the method or added via WithMetadata(new ObsoleteAttribute()) now emit deprecated: true - Schemas: types decorated with [Obsolete] produce deprecated: true on the component schema - Properties: [Obsolete] on properties sets deprecated: true on the property schema (inline) or on the schema reference (componentized) Addresses dotnet#63494 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds first-class support in the OpenAPI document generator for mapping .NET [Obsolete] metadata to the OpenAPI deprecated: true flag across operations and JSON schemas, reducing the need for custom transformers.
Changes:
- Mark OpenAPI operations as deprecated when
ObsoleteAttributeis present in endpoint metadata. - Mark component schemas and inline property schemas as deprecated when the underlying type/property is
[Obsolete]. - Support referenced-property deprecation via a new
x-ref-deprecatedannotation, plus corresponding parsing and tests.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs | Maps [Obsolete] on types/properties to schema-level deprecated or to a ref-specific annotation for componentized properties. |
| src/OpenApi/src/Services/OpenApiDocumentService.cs | Sets OpenApiOperation.Deprecated based on ObsoleteAttribute in endpoint metadata. |
| src/OpenApi/src/Services/OpenApiConstants.cs | Adds x-ref-deprecated constant for ref-only schema deprecation. |
| src/OpenApi/src/Schemas/OpenApiSchemaKeywords.cs | Adds the deprecated JSON schema keyword constant. |
| src/OpenApi/src/Schemas/OpenApiJsonSchema.Helpers.cs | Parses deprecated and x-ref-deprecated when reading schema JSON into OpenApiSchema. |
| src/OpenApi/src/Extensions/OpenApiDocumentExtensions.cs | Propagates x-ref-deprecated metadata onto OpenApiSchemaReference.Deprecated. |
| src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.Annotations.cs | Adds tests for type/property deprecation (inline + referenced). |
| src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Operations.cs | Adds tests for operation deprecation via attribute, metadata, and MVC action attribute. |
Author
|
@dotnet-policy-service agree company="Microsoft" |
Avoid calling GetCustomAttributes(inherit: false) twice on the same type by caching the result in a local variable. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Map
[Obsolete]attribute todeprecatedin OpenAPI documentsMap
ObsoleteAttributetodeprecated: trueon OpenAPI operations, schemas, and schema properties.Description
Adds built-in support for mapping
ObsoleteAttributeto the OpenAPIdeprecatedflag on operations, schemas, and schema properties. Previously, users had to write customIOpenApiSchemaTransformerorIOpenApiOperationTransformerimplementations to achieve this.Operations
Endpoints with
[Obsolete]on the handler method (minimal APIs) or action method (MVC controllers), or added via.WithMetadata(new ObsoleteAttribute()), now automatically emitdeprecated: trueon the OpenAPI operation.Schemas (types)
Types decorated with
[Obsolete]producedeprecated: trueon the component schema.Schema properties
[Obsolete]on a property setsdeprecated: truedirectly on the property schema.[Obsolete]on a property whose type is componentized setsdeprecated: trueon theOpenApiSchemaReference, leaving the underlying component schema unchanged. This follows the same pattern used for[Description]→x-ref-description.Design notes
[Description]→descriptionand[Required]→required, which are all automatic with no opt-out.inherit: falseonGetCustomAttributesfor type-level checks, so a derived type does not inheritdeprecatedfrom an obsolete base class.IOpenApiSchemaTransformerorIOpenApiOperationTransformerto setDeprecated = false.Breaking change consideration
This is a behavioral change. Existing users with
[Obsolete]on types, properties, or endpoints will seedeprecated: trueappear in their generated OpenAPI documents after upgrading. This could affect downstream codegen tools. However, we believe this is the correct default — if something is marked obsolete in .NET, it should be reflected as deprecated in the API contract.If the team prefers an opt-in/opt-out mechanism (e.g., a property on
OpenApiOptions), that can be added.Tests
Fixes #63494