You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
OpenAPI document generation fails when a request body class contains a nullable property with [MaxLength(...)].
Here is reproducible code:
usingSystem.ComponentModel.DataAnnotations;usingMicrosoft.AspNetCore.Mvc;varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddControllers();builder.Services.AddOpenApi();varapp=builder.Build();app.MapControllers();app.MapOpenApi();awaitapp.RunAsync();[ApiController][Route("api/[controller]")]publicclassMyController:ControllerBase{publicclassMyRequestModel{// Both types fail with the same error[MaxLength(10)]publicIList<string>?Items{get;set;}// public string[]? Items { get; set; }}[HttpPost]publicIActionResultPost([FromBody]MyRequestModelrequest){returnOk(request);}}
Please let me know if I'm doing something wrong, thanks!
Expected Behavior
The document generator should be able to handle a nullable array/collection with a MaxLength(N) validation attribute.
The resulting OpenAPI document should list the property as a nullable array, with maxItems as N
Steps To Reproduce
See bug description
Exceptions (if any)
See bug description for full trace
System.InvalidOperationException: The node must be of type 'JsonValue'.
at System.Text.Json.Nodes.JsonNode.GetValue[T]()
at Microsoft.AspNetCore.OpenApi.JsonNodeSchemaExtensions.ApplyValidationAttributes(JsonNode schema, IEnumerable`1 validationAttributes)
.NET Version
.NET 9
Anything else?
No response
The text was updated successfully, but these errors were encountered:
@JarrodOsborne Thanks for filing this issue and taking the time to investigate! Your analysis on the root cause of the issue is correct. Would you be interested in opening a PR to resolve this issue by updating the check in the ApplyValidationAttributes call?
Is there an existing issue for this?
Describe the bug
OpenAPI document generation fails when a request body class contains a nullable property with [MaxLength(...)].
Here is reproducible code:
Calling
http://localhost:5000/openapi/v1.json
results in:I have done some debugging and the fault happens here in
JsonNodeSchemaExtensions.cs:101
The schema comes through as:
But fails because it's a
JsonArray
and not aJsonValue
Please let me know if I'm doing something wrong, thanks!
Expected Behavior
The document generator should be able to handle a nullable array/collection with a
MaxLength(N)
validation attribute.The resulting OpenAPI document should list the property as a nullable array, with maxItems as
N
Steps To Reproduce
See bug description
Exceptions (if any)
See bug description for full trace
.NET Version
.NET 9
Anything else?
No response
The text was updated successfully, but these errors were encountered: