RicoSuter / NSwag

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

C# $ sign type name #2442

Open BRB2000 opened 4 years ago

BRB2000 commented 4 years ago

When generating a REST client for AppCenter (https://openapi.appcenter.ms/) from Visual Studio 2019 (add connected service), code like this is generated:

    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <param name="orderBy">The name of the attribute by which to order the response by. By default, apps are in order of creation. All results are ordered in ascending order.</param>
    /// <returns>Success</returns>
    /// <exception cref="ApiException">A server side error occurred.</exception>
    public async System.Threading.Tasks.Task<System.Collections.Generic.ICollection<AppResponse>> ListAsync($orderBy? orderBy, System.Threading.CancellationToken cancellationToken)
    {
        var urlBuilder_ = new System.Text.StringBuilder();
        urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/v0.1/apps?");
        if (orderBy != null) 
        {
            urlBuilder_.Append(System.Uri.EscapeDataString("$orderBy") + "=").Append(System.Uri.EscapeDataString(ConvertToString(orderBy, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
        }
        urlBuilder_.Length--;

        var client_ = _httpClient;
        try
        {
            using (var request_ = new System.Net.Http.HttpRequestMessage())
            {
                request_.Method = new System.Net.Http.HttpMethod("GET");
                request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

                PrepareRequest(client_, request_, urlBuilder_);
                var url_ = urlBuilder_.ToString();
                request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
                PrepareRequest(client_, request_, url_);

                var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
                try
                {
                    var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
                    if (response_.Content != null && response_.Content.Headers != null)
                    {
                        foreach (var item_ in response_.Content.Headers)
                            headers_[item_.Key] = item_.Value;
                    }

                    ProcessResponse(client_, response_);

                    var status_ = ((int)response_.StatusCode).ToString();
                    if (status_ == "200") 
                    {
                        var objectResponse_ = await ReadObjectResponseAsync<System.Collections.Generic.ICollection<AppResponse>>(response_, headers_).ConfigureAwait(false);
                        return objectResponse_.Object;
                    }
                    else
                    {
                        var objectResponse_ = await ReadObjectResponseAsync<ErrorResponse>(response_, headers_).ConfigureAwait(false);
                        throw new ApiException<ErrorResponse>("Error", (int)response_.StatusCode, objectResponse_.Text, headers_, objectResponse_.Object, null);
                    }
                }
                finally
                {
                    if (response_ != null)
                        response_.Dispose();
                }
            }
        }
        finally
        {
        }
    }`

This of course is not working, because there are $ signs inserted with the type. Is there any reason for this? Am I missing something?

BRB2000 commented 4 years ago

Nobody who can shed a light on this?

RicoSuter commented 4 years ago

This is probably a bug

BRB2000 commented 4 years ago

Thank you your reply. Did you conclude that based on my sample. or did you actually try to generate a client yourself? Because at this point, im not quite sure, if this could maybe come down to two factors. One of them could be, that the client for some reason not listens to the type field in the swagger.json, but instead creates a type with the content of the name attribute. The other thing is, that it could potentially come down to the sheer size of the API specification, and how the code generation task is handled in Visual Studio. I have no evidence for the latter though.

RicoSuter commented 4 years ago

I'd expect that it does not generate $ in the parameter names: https://github.com/RicoSuter/NSwag/blob/85ae862fd6d68173a201a79e0ad06e0be2ec5de1/src/NSwag.CodeGeneration/DefaultParameterNameGenerator.cs#L32