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

Old HTTPValidationError and ValidationError OpenAPI entry #10787

Open
1 task done
Kludex opened this issue Dec 16, 2023 · 3 comments · May be fixed by #10802
Open
1 task done

Old HTTPValidationError and ValidationError OpenAPI entry #10787

Kludex opened this issue Dec 16, 2023 · 3 comments · May be fixed by #10802
Labels
bug Something isn't working

Comments

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Dec 16, 2023

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

Those lines need to be updated:

validation_error_definition = {
"title": "ValidationError",
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
"required": ["loc", "msg", "type"],
}
validation_error_response_definition = {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": REF_PREFIX + "ValidationError"},
}
},
}

They reflect how it was before 0.100.0, but with Pydantic V2, that's no longer true.

@Kludex
Copy link
Sponsor Collaborator Author

Kludex commented Dec 16, 2023

It would be cool to have an API to modify the RequestValidationError OpenAPI entry, and not only the exception_handler.

@Kludex Kludex added the bug Something isn't working label Dec 16, 2023
@iudeen
Copy link
Contributor

iudeen commented Dec 18, 2023

validation_error_definition = {
    "title": "ValidationError",
    "type": "object",
    "properties": {
        "loc": {
            "title": "Location",
            "type": "array",
            "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
        },
        "input": {"title": "Input", "type": "any"},
        "url": {"title": "URL", "type": "string"},
        "msg": {"title": "Message", "type": "string"},
        "type": {"title": "Error Type", "type": "string"},
    },
    "required": ["loc", "msg", "type"],
}

This is what we need right? But this change would require changing a lot of tests.

iudeen added a commit to iudeen/fastapi that referenced this issue Dec 18, 2023
@iudeen iudeen linked a pull request Dec 18, 2023 that will close this issue
2 tasks
@DeepTanthetaa
Copy link

DeepTanthetaa commented Dec 21, 2023

from fastapi import FastAPI

app = FastAPI()

Your existing OpenAPI definitions

validation_error_definition = {...}
validation_error_response_definition = {...}

Add the existing definitions to the app

app.openapi_schema["components"]["schemas"]["ValidationError"] = validation_error_definition
app.openapi_schema["components"]["schemas"]["HTTPValidationError"] = validation_error_response_definition

Define the new definition for HTTPValidationError (modify as needed)

new_http_validation_error_definition = {
"title": "ModifiedHTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
}

Update the OpenAPI entry

update_openapi_entry(app, new_http_validation_error_definition)

Your FastAPI routes and other configurations go here

using this code stay happy 👍

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

Successfully merging a pull request may close this issue.

3 participants