Open MaxHaertwig opened 4 years ago
What code generation are you talking about? Controller code generation or client-side validation?
For controllers generation, I think that DataAnnotations are enough to validate such entries? doesn't that work? Could you send an example ?
I'm referring to client-side code generation. I'd expect the client to validate URL parameters before sending the request. Even if the validation happens on the server, we can avoid this unnecessary network roundtrip.
Example:
/api/test/{version}:
get:
parameters:
name: version
in: path
schema:
type: string
pattern: "^\\d+(\\.\\d+)?(\\.\\d+)?$"
responses:
"200":
description: Success
content:
text/plain:
schema:
type: string
generates the following client code:
public async System.Threading.Tasks.Task<string> TestAsync(string version, System.Threading.CancellationToken cancellationToken)
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/test/{version}");
urlBuilder_.Replace("{version}", System.Uri.EscapeDataString(ConvertToString(version, System.Globalization.CultureInfo.InvariantCulture)));
var client_ = _httpClient;
var disposeClient_ = false;
try
{
using (var request_ = new System.Net.Http.HttpRequestMessage())
...
I would expect to see a few lines like:
if (!Regex.IsMatch(version, @"^\d+(\.\d+)?(\.\d+)?$"))
{
throw new ApiException($"Query parameter 'version' doesn't match the specified pattern.")
}
This feature is currently not implemented.
@RicoSuter Hello! Is this feature still not implemented? If so, I would be happy to try to implement it.
@RicoSuter @davidorbelian any updates on this? We're making wrappers at the moment to do simple validation on the client-side
Example:
/// <summary>
/// Gets or sets the value of ids
/// </summary>
[Newtonsoft.Json.JsonProperty("Ids", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
[System.ComponentModel.DataAnnotations.MinLength(1)]
public System.Collections.Generic.ICollection<string> Ids { get; set; } = new
System.Collections.ObjectModel.Collection<string>();
The generated csharp client does not create any validation here. Or is there any options to enforce this?
Thanks
Hi, is there a plan to prioritise this? None of the System.ComponentModel.DataAnnotations
that get generated seem to be respected when the generated client code is executed. Thanks
Hi everyone,
I noticed that the C# code generation doesn't validate patterns (on the client side). If I annotate a parameter of type string with a pattern (e.g. "^v[1-9]$"), I expect the Api method to throw an ApiException when the supplied string doesn't match the pattern.
I'd be happy to open a PR. Is that something this project could use?