RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

C# Code Generation doesn't validate patterns #3145

Open MaxHaertwig opened 4 years ago

MaxHaertwig commented 4 years ago

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?

jeremyVignelles commented 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 ?

MaxHaertwig commented 4 years ago

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.

MaxHaertwig commented 4 years ago

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.")
}
RicoSuter commented 4 years ago

This feature is currently not implemented.

davidorbelian commented 3 years ago

@RicoSuter Hello! Is this feature still not implemented? If so, I would be happy to try to implement it.

MortenMeisler commented 1 year ago

@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

IrinaSouth commented 11 months ago

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