This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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_model
does not invalidate None
#2719
Comments
Looking at the code, it looks like it's a bug. Good job! Let me explain why I think you're right and how this is happening: On the self.response_field = create_response_field(
name=response_name, type_=self.response_model
) Which is the line that determines the response behavior. The problem is not apparent just seeing this, so we need to follow the def create_response_field(
name: str,
type_: Type[Any],
class_validators: Optional[Dict[str, Validator]] = None,
default: Optional[Any] = None,
required: Union[bool, UndefinedType] = False,
model_config: Type[BaseConfig] = BaseConfig,
field_info: Optional[FieldInfo] = None,
alias: Optional[str] = None,
) -> ModelField:
... This is enough for us. Do you see the Just to be sure, I've changed the default value to Maybe adding a |
@Kludex if I may ask a tangentially related question, why does |
I'm not going to be able to provide the link now and I'll probably forget later, but if you go to the docs and check "/response-model" path, there's a note about this. You should search for "type hint" on that page if I remember correctly. |
This is a response I once got to that question: #653 (comment) |
Gotcha. Thanks folks! |
Okay, my server has complained a ton of errors since my yesterday's release. I suppose that's the price I'll have to pay for not pinning dependencies' versions... |
What kind of errors? I haven’t updated yet, will try in the next few days |
If I'm not mistaken... I think the problem he mentions is that changing this behavior was a breaking change. |
@JarroVGIT Something like this: pydantic.error_wrappers.ValidationError: 1 validation error for MyModel
response
none is not an allowed value (type=type_error.none.not_allowed) But if you've never sent |
Hi, I have some error regarding this change triggered by ddtrace (https://github.com/DataDog/dd-trace-py) library wrapping my fastapi application, but I think its not really, related to Datadog: Upon getting the error after returning None instead of my response_model class (which in my case was "bool"), the datadog library gets the exception object generated by fastapi with type pydantic.error_wrappers.ValidationError which makes sense, because my route returned "None" where it should return "bool". But, then when it tries to stringify the object using the built in str function, It gets an exception regarding the self.model field of the ValidationError object:
apparently, the "model" field is just a "class bool" type because of the original response_model I put in my endpoint, and it doesn't have config nor pydantic_model fields, hence the exception. any clue about that? |
Thanks for the conversation all! ☕ @AlonMorgen if you're still having problems, please create a new GitHub Discussion following the template, with a replication example, etc. That way we would be able to help you. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Example
Description
/
.null
, with HTTP code 200 OK.None
is a validRespModel
.Environment
Additional context
I'm not sure if this is a bug, or expected behavior. I tried to look for expected behavior in the docs but didn't find.
If this is expected, is there perhaps a way in the API to make FastAPI invalidate
None
whenresponse_model
is set?The text was updated successfully, but these errors were encountered: