question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatic status code and response type detection

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI, Response

app = FastAPI()


@app.get("/{option}")
def read_root(option: int):
    if option == 1:
        return Response(status_code=200)
    if option == 2:
        return Response(status_code=201)
    return Response(status_code=204)

Description

  • Open de browser and go to localhost:8000/docs
  • Docs do not show correct response status codes:

Uploading image.png…

Wanted Solution

I would like that FastAPI could automatically detect Response status code and response type

Wanted Code

# Using the same code above but it produces correct documented endpoint

Alternatives

No response

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.78.0

Python Version

3.10.4

Additional Context

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
Kludexcommented, Aug 4, 2022

I can tell you how to implement this, but it’s not trivial… 🤷‍♂️

I can tell you for sure that this will never be merged on FastAPI, because it involves doing static type analysis, which is out of scope. Also, it’s a lot of work, and it can be an external package.

How you can implement this:

  1. You need to traverse the AST (or CST if using libcst) of the whole source code, and map all calls that are being made in the code. You need to do this because from an endpoint function you can have function calls (or any other structure that performs calls, methods, class constructor, etc) that return the Response you want… and that can be recursive as you can have a function calling another function and only then having a return Response
  2. After that, you need to make sure that you need to traverse the AST starting from the endpoint functions. You need to follow two paths: dependencies and function body. Any response returned on those needs to be stored… When you find a call, you go to your map from step 1, and traverse that node as well…
  3. At the end of step 2 you’re going to have the list of Responses from each endpoint function. Maybe some dynamic programing on functions and possible responses can speed up a bit if the source code is huge (may not be an issue now).

In any case… I’m going to work on this idea at some point, mainly for HTTPExceptions tho. I already have this: https://github.com/Kludex/fastapi-responses, but it doesn’t follow the instructions above, it currently implements a very naive approach.

1reaction
JarroVGITcommented, Aug 3, 2022

It’s highly unlikely this will ever come to be, as it is pretty hard to know what kind of output types an arbitrary function can have.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rest API Response Codes And Types Of Rest Requests
In this tutorial, we will learn about different REST response codes, types of REST Requests, and some best practices to be followed.
Read more >
Response Status Code - FastAPI
300 and above are for "Redirection". Responses with these status codes may or may not have a body, except for 304 , "Not...
Read more >
HTTP/1.1: Status Code Definitions
This class of status code indicates that the client's request was successfully received, understood, and accepted. 10.2.1 200 OK. The request has succeeded....
Read more >
What Is Endpoint Detection and Response? | EDR Security
Automated response. Pre-configured rules in an EDR solution can recognize when incoming data indicates a known type of security breach and triggers an...
Read more >
The 6 Types of HTTP Status Codes Explained - DYNO Mapper
This status response indicates that the entity making the request has asked the server to change protocols and the server has acquiesce to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found