Open elnigno opened 3 months ago
Hello @elnigno and thank you for reporting this. My understanding is that the record is a model property. Could you please provide a reproducer with more context? Thanks a lot!
Hi @mary-georgiou-sonarsource,
Yes, that record is used in a controller action, binding to the model with the FromBody
attribute.
[HttpPut]
[Route("{productId}")]
public async Task<IActionResult> DoSomething(
int productId,
[FromBody] MyRecord requestBody)
{
...
}
Notice this is not exact code that's triggering the rule, because I can't disclose it, but I what I am providing captures the essence of it. Thanks for looking into this!
Thanks @elnigno.
Your model is the record itself?
Yes.
Hello @elnigno! I did some tests with the following example :
[ApiController]
[Route("[controller]")]
public class AController : ControllerBase
{
[HttpPost("new")]
public string PostValue([FromBody] RecordModel model) =>$"MyString:{model.MyString}, MyInt: {model.MyInt}";
}
public record RecordModel(string MyString, int MyInt);
The rule correctly raises an issue for MyInt
and not for MyString
since the first is a value type.
However, I see that for records, our rule does not take into consideration the attributes, and this needs to be fixed.
If you agree I'll add this issue to my backlog but with a different title "Raises on record properties even if they are annotated as required".
PS: This rule has been updated in the latest analyzer version and it does not take into account the RequiredAttribute
as it has no effect on value types. See here for more info.
@mary-georgiou-sonarsource sounds good, thanks for looking into it!
Description
Rule ID: S6964
Repro steps
Expected behavior
The rule should not activate.
Actual behavior
The rule activates.
Known workarounds
-
Related information