Skip to content

Wrongly generated Axios clients when not using dto#2966

Closed
dubzzz wants to merge 1 commit intoRicoSuter:masterfrom
dubzzz:fix-axios-interface-mode
Closed

Wrongly generated Axios clients when not using dto#2966
dubzzz wants to merge 1 commit intoRicoSuter:masterfrom
dubzzz:fix-axios-interface-mode

Conversation

@dubzzz
Copy link
Copy Markdown
Contributor

@dubzzz dubzzz commented Jul 27, 2020

It seems that the Axios code generated by NSwag when we move from "typeStyle": "Class" to"typeStyle": "Interface" comes with a bug.

This Pull Request is supposed to fix the issue. Please let me know if I can add a specific test to cover this case and avoid it to come back in the future.


Here is the behaviour I observed when moving from "typeStyle": "Class" to"typeStyle": "Interface".

Actually here are the codes for process* that have been generated with "typeStyle": "Class":

    // Here we are dealing with a complex class
    // Following snippet was called by:
    /*
        return this.instance.request(options_).catch((_error: any) => {
            if (isAxiosError(_error) && _error.response) {
                return _error.response;
            } else {
                throw _error;
            }
        }).then((_response: AxiosResponse) => {
            return this.processCreateUser(_response);
        });
    */
    protected processCreateUser(response: AxiosResponse): Promise<User> {
        const status = response.status;
        let _headers: any = {};
        if (response.headers && typeof response.headers === "object") {
            for (let k in response.headers) {
                if (response.headers.hasOwnProperty(k)) {
                    _headers[k] = response.headers[k];
                }
            }
        }
        if (status === 201) {
            const _responseText = response.data;
            let result201: any = null;
            let resultData201  = _responseText;
            result201 = User.fromJS(resultData201);
            return result201;
        } else if (status !== 200 && status !== 204) {
            const _responseText = response.data;
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
        }
        return Promise.resolve<User>(<any>null);
    }

    // Here we are dealing with a simple string
    // Following snippet was called by:
    /*
        return this.instance.request(options_).catch((_error: any) => {
            if (isAxiosError(_error) && _error.response) {
                return _error.response;
            } else {
                throw _error;
            }
        }).then((_response: AxiosResponse) => {
            return this.processNewPassword(_response);
        });
    */
    protected processNewPassword(response: AxiosResponse): Promise<string> {
        const status = response.status;
        let _headers: any = {};
        if (response.headers && typeof response.headers === "object") {
            for (let k in response.headers) {
                if (response.headers.hasOwnProperty(k)) {
                    _headers[k] = response.headers[k];
                }
            }
        }
        if (status === 200) {
            const _responseText = response.data;
            let result200: any = null;
            let resultData200  = _responseText;
            result200 = resultData200 !== undefined ? resultData200 : <any>null;
            return result200;
        } else if (status !== 200 && status !== 204) {
            const _responseText = response.data;
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
        }
        return Promise.resolve<string>(<any>null);
    }

In this version of process*, response.data holds the output as a normal JS Objects not a raw string that should be parsed to extract something from it. When we toggle "typeStyle": "Interface", the code JSON.parse(response.data) crashes as we try to parse an object (or an already parsed string).

When I moved towards "typeStyle": "Interface":

    // Here we are dealing with a complex class
    // Following snippet was called by:
    /*
        return this.instance.request(options_).catch((_error: any) => {
            if (isAxiosError(_error) && _error.response) {
                return _error.response;
            } else {
                throw _error;
            }
        }).then((_response: AxiosResponse) => {
            return this.processCreateUser(_response);
        });
    */
    protected processCreateUser(response: AxiosResponse): Promise<User> {
        const status = response.status;
        let _headers: any = {};
        if (response.headers && typeof response.headers === "object") {
            for (let k in response.headers) {
                if (response.headers.hasOwnProperty(k)) {
                    _headers[k] = response.headers[k];
                }
            }
        }
        if (status === 201) {
            const _responseText = response.data;
            let result201: any = null;
            let resultData201  = _responseText;
            result201 = JSON.parse(resultData201);
            return result201;
        } else if (status !== 200 && status !== 204) {
            const _responseText = response.data;
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
        }
        return Promise.resolve<User>(<any>null);
    }

    // Here we are dealing with a simple string
    // Following snippet was called by:
    /*
        return this.instance.request(options_).catch((_error: any) => {
            if (isAxiosError(_error) && _error.response) {
                return _error.response;
            } else {
                throw _error;
            }
        }).then((_response: AxiosResponse) => {
            return this.processNewPassword(_response);
        });
    */
    protected processNewPassword(response: AxiosResponse): Promise<string> {
        const status = response.status;
        let _headers: any = {};
        if (response.headers && typeof response.headers === "object") {
            for (let k in response.headers) {
                if (response.headers.hasOwnProperty(k)) {
                    _headers[k] = response.headers[k];
                }
            }
        }
        if (status === 200) {
            const _responseText = response.data;
            let result200: any = null;
            let resultData200  = _responseText;
            result200 = JSON.parse(resultData200);
            return result200;
        } else if (status !== 200 && status !== 204) {
            const _responseText = response.data;
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
        }
        return Promise.resolve<string>(<any>null);
    }

@dubzzz
Copy link
Copy Markdown
Contributor Author

dubzzz commented Jul 28, 2020

Closing this PR for the moment, it might already be handled by #2927

@dubzzz dubzzz closed this Jul 28, 2020
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

Successfully merging this pull request may close these issues.

1 participant