Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# Code Generation doesn't validate patterns #3145

Open
MaxHaertwig opened this issue Nov 6, 2020 · 7 comments
Open

C# Code Generation doesn't validate patterns #3145

MaxHaertwig opened this issue Nov 6, 2020 · 7 comments

Comments

@MaxHaertwig
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

This feature is currently not implemented.

@davidorbelian
Copy link

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

@MortenMeisler
Copy link

MortenMeisler commented Jun 22, 2023

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants