Skip to content

Map [Obsolete] attribute to deprecated in OpenAPI documents#66355

Open
fickleEfrit wants to merge 2 commits intodotnet:mainfrom
fickleEfrit:obsolete-deprecated-openapi
Open

Map [Obsolete] attribute to deprecated in OpenAPI documents#66355
fickleEfrit wants to merge 2 commits intodotnet:mainfrom
fickleEfrit:obsolete-deprecated-openapi

Conversation

@fickleEfrit
Copy link
Copy Markdown

Map [Obsolete] attribute to deprecated in OpenAPI documents

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Map ObsoleteAttribute to deprecated: true on OpenAPI operations, schemas, and schema properties.

Description

Adds built-in support for mapping ObsoleteAttribute to the OpenAPI deprecated flag on operations, schemas, and schema properties. Previously, users had to write custom IOpenApiSchemaTransformer or IOpenApiOperationTransformer implementations 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 emit deprecated: true on the OpenAPI operation.

Schemas (types)

Types decorated with [Obsolete] produce deprecated: true on the component schema.

Schema properties

  • Inline properties: [Obsolete] on a property sets deprecated: true directly on the property schema.
  • Referenced properties: [Obsolete] on a property whose type is componentized sets deprecated: true on the OpenApiSchemaReference, leaving the underlying component schema unchanged. This follows the same pattern used for [Description]x-ref-description.

Design notes

  • This follows the existing conventions for [Description]description and [Required]required, which are all automatic with no opt-out.
  • Uses inherit: false on GetCustomAttributes for type-level checks, so a derived type does not inherit deprecated from an obsolete base class.
  • The behavior is on by default. Users who need to suppress it for specific cases can use an IOpenApiSchemaTransformer or IOpenApiOperationTransformer to set Deprecated = false.

Breaking change consideration

This is a behavioral change. Existing users with [Obsolete] on types, properties, or endpoints will see deprecated: true appear 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

  • 8 new tests covering all scenarios (type, inline property, referenced property, operations via attribute, metadata, and MVC actions)
  • Full existing test suite passes (837 passed, 0 failed)

Fixes #63494

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>
@fickleEfrit fickleEfrit requested a review from a team as a code owner April 17, 2026 20:05
Copilot AI review requested due to automatic review settings April 17, 2026 20:05
@github-actions github-actions Bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Apr 17, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Apr 17, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ObsoleteAttribute is 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-deprecated annotation, 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.

Comment thread src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs Outdated
Comment thread src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
Comment thread src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
@fickleEfrit
Copy link
Copy Markdown
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>
@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc community-contribution Indicates that the PR has been added by a community member feature-openapi

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI: Marking Operations, Schemas and Properties as deprecated based on the [Obsolete] attribute

3 participants