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

NSwag generating a client method using a string for a parameter #5118

Open
hyperzrho opened this issue Mar 3, 2025 · 0 comments
Open

NSwag generating a client method using a string for a parameter #5118

hyperzrho opened this issue Mar 3, 2025 · 0 comments

Comments

@hyperzrho
Copy link

Describe the bug

If an "out" parameter is used in a C# class which in turn is used as a parameter for a controller, NSwag will generate a method whose parameter is a string instead of the actual type.

Version used

14.2.0

To Reproduce

The following class..

namespace NswagTest
{
    [Serializable]
    public class Id2
    {
        public string Name { get; set; }

        public static bool TryParse(string idStr, out object result)
        {
            result = null;
            return false;
        }
    }
}

Used in the following controller...

using Microsoft.AspNetCore.Mvc;

namespace NswagTest.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class IdController : ControllerBase
    {
        private readonly ILogger<IdController> _logger;

        public IdController(ILogger<IdController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<Id2> Get()
        {
            return
            [
                new Id2 { Name = "test" }
            ];
        }

        [HttpPost]
        public ActionResult Set([FromBody] Id2 input)
        {
            return new OkResult();
        }
    }
}

Generates a client with the following method...

set(input: string): Promise<FileResponse> {
        let url_ = this.baseUrl + "/Id";
        url_ = url_.replace(/[?&]$/, "");

        const content_ = JSON.stringify(input);

        let options_: RequestInit = {
            body: content_,
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/octet-stream"
            }
        };

        return this.http.fetch(url_, options_).then((_response: Response) => {
            return this.processSet(_response);
        });
    }

Expected behavior

The generated client would use the type Id instead of string.

Additional context

Adding a JsonSchemaType attribute on the Parameter seems to get NSwag to generate client correctly. However is this expected use case?

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

No branches or pull requests

1 participant