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

response.data is {} when it should be null #1494

Open
SavioR0 opened this issue Dec 20, 2024 · 2 comments
Open

response.data is {} when it should be null #1494

SavioR0 opened this issue Dec 20, 2024 · 2 comments
Labels
bug 🔥 Something isn't working

Comments

@SavioR0
Copy link

SavioR0 commented Dec 20, 2024

Below is the link to the minimum code to reproduce the issue:

https://github.com/SavioR0/NoneBug

When the email received by the backend is an empty string, the code should return None.

Backend

@app.get(
    "/api/v1/users/email/{user_email}",
)
def read_user_by_email(email: str) -> UserPublic | None:
    """
    Get a specific user by email.
    """
    if email != "":
        print("Not none")
        return UserPublic(name="User", age=10)
    print("None")
    return None

Output:

None

However, the frontend receives {} even when the string None is printed on the backend.

Frontend

async function remoteCall(){
    const response = await UsersService.readUserByEmail({
      query: {
        email: "",
      },
    })
    const data = response.data
    console.log(typeof data)
    console.log(data)
}

Output:

object
{}
@mrlubos
Copy link
Member

mrlubos commented Dec 20, 2024

Hi @SavioR0! Yep, aware this is an issue, I was waiting until someone brings it up. The reason is the object is used to verify if the response was successful or not to narrow down the types. We'll need to modify this to include an alternative approach so we can preserve the original response data. Is this a blocker for you or an inconvenience? Thank you for reporting!

@SavioR0
Copy link
Author

SavioR0 commented Dec 20, 2024

It's an inconvenience that I bypassed this way

const data = response.data
if (data && Object.keys(data).length > 0) {
 ...
}

@mrlubos mrlubos added the bug 🔥 Something isn't working label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🔥 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants