RicoSuter/NSwag

Dynamic support broken

Open

#1,341 opened on May 23, 2018

View on GitHub
 (6 comments) (0 reactions) (0 assignees)C# (6,291 stars) (1,189 forks)batch import
help wantedtype: enhancement

Description

Related to https://github.com/RSuter/NSwag/issues/1216. The fix that removed ExpandoObject and used any instead worked for most of our cases, but surfaced another bug.

When generating TypeScript for a Web API method that accepts a dynamic, ExpandoObject, or object` parameter, the wrong code is generated.

It is generating this:

foo(bar: any | null): Observable<void> {
        let url_ = this.baseUrl + "/api/Baz/Foo?";
        if (bar === undefined)
            throw new Error("The parameter 'bar' must be defined.");
        else
            url_ += "bar=" + encodeURIComponent("" + bar) + "&"; 
...
}

Instead of this:

foo(bar: any | null): Observable<void> {
        let url_ = this.baseUrl + "/api/Baz/Foo";
        url_ = url_.replace(/[?&]$/, "");
 
        const content_ = JSON.stringify(bar);
...

In other words, its treating dynamic, ExpandoObject, or object parameters the same as it treats primitives. It instead should be calling JSON.stringify on them.

Contributor guide