Skip to content

Automatic validation with ApiController does not work as expected #41505

Closed
@liborpansky

Description

@liborpansky

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Automatic validation for non-nullable properties does not work as expected for all inheritance hierarchy. There are some properties that are not being validated for null value.

Expected Behavior

Validation works correctly and returns 400 bad request API response.

Steps To Reproduce

Create empty .NET API project with nullable enabled.

Create a test structure with 4 levels and one generic property:

public class OuterWeatherForecast : MiddleWeatherForecast<InternalWithInheritance>
{
    public string OuterRequired { get; set; } = null!;
}

public class MiddleWeatherForecast<T> : WeatherForecast<T>
{
    public string MiddleRequired { get; set; } = null!;
}

public class WeatherForecast<T> : BaseForecast
{
    public string TestRequired { get; set; } = null!;
    public T Inner { get; set; }
}

public class BaseForecast
{
    public string BaseRequired { get; set; } = null!;
}

public class Internal
{
    public string InnerRequired { get; set; } = null!;
}

public class InternalWithInheritance : Internal
{
    public string InternalWithInhRequired { get; set; } = null!;
}

Create a simple POST API endpoint within a controller with [ApiController] attribute.

[HttpPost(Name = "UpdateWeatherForecast")]
public void Update(OuterWeatherForecast model)
{
}

POST without TestRequired or MiddleRequired properties does not fire validation process and 200 OK is being returned. For the rest of the properties, validation works as expected with a standard validation result "FieldName": ["The FieldName field is required." ]

Exceptions (if any)

No response

.NET Version

6.0.202

Anything else?

No response

Metadata

Metadata

Assignees

Labels

✔️ Resolution: FixedThe bug or enhancement requested in this issue has been checked-in!old-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

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions