Open ygoe opened 1 year ago
Can you write the simplest possible project that demonstrates these errors and post a link to a public GitHub repo?
While creating the repro I noticed that another fix for a missing ASP.NET Core feature might cause the problem. I get the impression that one shouldn't take "RESTful APIs" and "OpenAPI" too serious. There are too many critical bugs in all parts, in ASP and third-party tools. Meanwhile I have to deliver a somehow working API so I cannot investigate these details any further, but will revisit them later.
Alright, Swashbuckle and Swagger UI (that I use to test the API and its responses) are both so broken that it's close to impossible to give accurate descriptions of what works and what doesn't. After very close inspection of what it really does in the Firefox network monitor, I know that I was sending URL query parameters in some places where I typed and expected a JSON request body. Obviously, any JSON naming conventions are not applied to URL query parameters. So the part with the wrong request property name is incorrect.
Also, in any case the validation property keys are correctly converted to camelCase if it's the framework's own validation that generates them before the action method is even invoked. Any model errors I later add with AddModelError
are never converted. I'll probably need to patch that in myself somehow if that's out of scope for that configuration. But this is low prio for me because it only generates slightly wrong details of an error message, it's not critical for API compatibility.
https://learn.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-7.0#format-problemdetails-and-validationproblemdetails-responses
The text says that validation result field names are included in the JSON unchanged. I can confirm that. My invalid property name with a PascalCase name appears as-is in the JSON response.
The text also suggests how to change that and convert [these names?] to camelCase. I cannot confirm that this works. It has no effect here. Not for validation results generated from the framework (from property attributes like
MaxLength
), and not from validation errors that I added myself through code (withModelState.AddModelError(nameof(obj.Property), "Error")
). Even when I assign a name to the property with theJsonPropertyName
attribute with changed casing, the JSON response has the name in PascalCase. Only if I also change the letters of the name in theJsonPropertyName
attribute, it is displayed as specified. Even worse, I still have to specify the property name with the original name from the C# class (but still in camelCase) in the HTTP request or it won't be mapped. So model binding completely ignores that attribute and validation results ignore it if only casing has changed.These are probably several bugs in .NET, but it all starts with the documentation. What is described here is not included in the product.
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.