HTTP status code 200 hardcoded in generated Swagger
- Dominant language
- Python
- Stars
- 11.1k
- Forks
- 1k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 2
Description
I have an API Gateway method that returns a 202. Chalice allows for custom responses, so cool. However, the custom HTTP response code is ignored in the API Gateway auto-generated Swagger etc.
#### Example
```python
return Response(body=json.loads(foo),
status_code=202,
headers={'Content-Type': 'application/json'})
```
The generated swagger (from the API Gateway export) has the following instead of the 202 I want there (and yes, it matters).
```json
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
}
```
Looking at `_generate_precanned_responses` in [`swagger.py`](https://github.com/aws/chalice/blob/3bfbd3d6800777206198fc58d264eaee1c4f574d/chalice/deploy/swagger.py#L145) I see it's hardcoded. There are other places as well (`_add_preflight_request`, `_generate_apig_integ`).
It looks like the `Response` object is not accessible during the package/deploy phase (please let me know if I missed something). Given the custom response is specific to a route/verb combo (it is in my case but that shouldn't matter), I experimented with adding a parameter to the `@app.route`.
```python
@app.route(API_BASE_PATH + '/tasks',
methods=['POST'],
content_types=['application/json'],
custom_response_code=202)
```
I have a solution that works for me and if this is useful to the community, I can submit a PR.
I'm really interested in your thoughts on this and how you might go about dealing with it.
Thank you.
Contributor guide
Research direction
Start by reading chalice/deploy/swagger.py, especially _generate_precanned_responses, _add_preflight_request, and _generate_apig_integ, then trace how route metadata reaches the package/deploy phase. Done means an API Gateway export reflects the configured custom response status, such as 202, instead of hardcoded 200 for the affected route.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, openapi, python
- Domain
- api, cloud
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100