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

[REQ] Feature Request Description #5832

Open
HeshamMeneisi opened this issue Apr 5, 2020 · 0 comments
Open

[REQ] Feature Request Description #5832

HeshamMeneisi opened this issue Apr 5, 2020 · 0 comments

Comments

@HeshamMeneisi
Copy link

Is your feature request related to a problem? Please describe.

Whenever I generate a typescript-node API client, the generated code does not respect different return types for different status codes. For example, if 200 returns a user and 400 returns an error, the error gets parsed as a user and thus the error message is lost!

Describe the solution you'd like

There's a generated bit of code that looks like this:

if (error) {
    reject(error);
} else {
    body = ObjectSerializer.deserialize(body, "OrganizationMember");
    if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
        resolve({ response: response, body: body });
    } else {
        reject(new HttpError(response, body, response.statusCode));
    }
}

Ideally, it should follow the json specifications where different status codes return different types.

"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/RefreshTokenSuccessResponse"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/RefreshTokenSuccessResponse"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/RefreshTokenSuccessResponse"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"404": {
"description": "Not Found",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"500": {
"description": "Server Error",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
}

This can be a configurable mode called "strictStatusTypes" and resolve like so:

if (error) {
    reject(error);
} else if (response.statusCode === 200) 
    resolve({ response: response, body: ObjectSerializer.deserialize(body, "OrganizationMember") });
} else if (response.statusCode === 400) {
    reject(new HttpError(response, ObjectSerializer.deserialize(body, "ProblemDetails"), response.statusCode));
} else if ....

Describe alternatives you've considered

An easier solution to the issue would be to just leave the body alone when there's an error and use a raw JSON object without the parse.

if (error) {
    reject(error);
} else {
    if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
        body = ObjectSerializer.deserialize(body, "OrganizationMember");
        resolve({ response: response, body: body });
    } else {
        reject(new HttpError(response, body, response.statusCode));
    }
}

Additional context

Whenever the success status has no body (e.g. 201, 202, ...) the error body is left untouched which makes this behavior really inconsistent!

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

1 participant