Skip to content

Add generic ConsumesAttribute<T> - #69017

Draft
campersau wants to merge 1 commit into
dotnet:mainfrom
campersau:consumes-attribute-of-t
Draft

Add generic ConsumesAttribute<T>#69017
campersau wants to merge 1 commit into
dotnet:mainfrom
campersau:consumes-attribute-of-t

Conversation

@campersau

Copy link
Copy Markdown
Contributor
  • 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.

Add a generic ConsumesAttribute

Description

Adds ConsumesAttribute<T>, a generic variant of ConsumesAttribute that lets the request type be specified as a type parameter instead of through the Type requestType constructor overload, mirroring the existing ProducesResponseTypeAttribute<T> pattern:

[Consumes<Todo>("application/x-www-form-urlencoded")]
async Task<IResult> AddTodo(Todo todo, TodoDb db)

ConsumesAttribute<T> inherits from ConsumesAttribute and forwards to the existing ConsumesAttribute(Type requestType, string contentType, params string[] otherContentTypes) constructor with typeof(T). It automatically picks up the base class's [AttributeUsage(Class | Method)] as well as IAcceptsMetadata/IConsumesActionConstraint behavior, so no additional wiring is needed for either MVC controllers or minimal API endpoints.

No new tests were added. Instead, existing tests that already exercised ConsumesAttribute with an explicit request type were updated to use the new Consumes<T> syntax, which confirms the generic form behaves identically to the non-generic one:

  • EndpointMetadataApiDescriptionProviderTest
  • OpenApiDocumentServiceTests.RequestBody
  • OpenApiGeneratorTests

Fixes #67701

Adds a generic ConsumesAttribute<T>, mirroring the existing
ProducesResponseTypeAttribute<T> pattern, so the request type can be
specified with compile-time type safety instead of via
[Consumes(typeof(T), ...)].

Existing tests that exercised ConsumesAttribute with an explicit
request type (EndpointMetadataApiDescriptionProviderTest,
OpenApiDocumentServiceTests, OpenApiGeneratorTests) are updated to use
the new Consumes<T> syntax, confirming the generic form behaves the
same as the non-generic one.

Fixes dotnet#67701

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G6qhxdfagHUri2p6icypbT
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 3, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @campersau. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@Youssef1313 Youssef1313 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the contribution @campersau.

We will need to review the API proposal first before moving forward. So, marking request changes until the API is reviewed/approved.

@gfoidl gfoidl added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Sep 3, 2026
@gfoidl

gfoidl commented Sep 3, 2026

Copy link
Copy Markdown
Member

marking request changes

@Youssef1313 can you make this PR a draft also? (I don't have rights for this).

@campersau
campersau marked this pull request as draft September 3, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generic version of ConsumesAttribute

3 participants