Skip to content
Discussion options

You must be logged in to vote

pydantic tries to convert values to destination type when it's possible.

The solution will be to use pydantic StrictInt and StrictBool:

from typing import Union
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import StrictInt, StrictBool

APP = FastAPI()
TESTER = TestClient(APP)


@APP.get('/', response_model=Union[StrictInt, StrictBool])
def root():
    return 1


def test_root():
    resp = TESTER.get("/")
    assert resp.status_code == 200
    assert resp.json() is True

More info about pydantic:
data conversion - https://pydantic-docs.helpmanual.io/usage/models/#data-conversion
strict types - https://pydantic-docs.helpmanual.io/usage/types/#strict-types

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
2 participants
Converted from issue

This discussion was converted from issue #4086 on February 27, 2023 22:11.