Skip to content

Analyzer to detect greater than 1 [FromBody] usage #45382

@mitchdenny

Description

@mitchdenny

Background and Motivation

When wiring up an endpoint using minimal API, the following code compiles, but results in a runtime error:

app.MapGet("/", ([FromBody]MyType t1, [FromBody] MyType t2) => {});

Currently the runtime error is:

InvalidOperationException: Failure to infer one or more parameters.
Below is the list of parameters that we found:

Parameter | Source
---------------------------------------------------------------------------------
t1 | Body (Attribute)
t2 | UNKNOWN


Did you mean to register the "UNKNOWN" parameters as a Service?

It would be better to analyze the parameters of the handler delegate during compilation and raise and error in this particular case.

Proposed Analyzer

Analyzer Behavior and Message

When the analyzer encounters a MapXXX endpoint builder method with a handler delegate with more than one [FromBody] attribute it raises an error.

Category

  • Design
  • Documentation
  • Globalization
  • Interoperability
  • Maintainability
  • Naming
  • Performance
  • Reliability
  • Security
  • Style
  • Usage

Severity Level

  • Error
  • Warning
  • Info
  • Hidden

Usage Scenarios

In the example below both t1 and t2 parameters would be highlighted.

app.MapGet("/", ([FromBody]MyType t1, [FromBody] MyType t2) => {});

Risks

I don't think there will be any risks with this as currently doing this results in a runtime error.

Metadata

Metadata

Assignees

Labels

analyzerIndicates an issue which is related to analyzer experienceapi-approvedAPI was approved in API review, it can be implementedold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions