🐛 Fix wrong OpenApi scheme generation for custom models with computed fields in additional responses#12119
Conversation
|
Edit: I just noticed that this PR might be a duplicate PR of #11517 |
|
Hi @DavideGalilei, thank you for your interest in contributing to the project! Yes, this is a duplicate. We'll review both PRs in detail later. We currently have a high volume of PRs, we appreciate your patience as we manage the queue. 🙇♀️ |
And the PR is a duplicate of #10895 :) |
svlandeg
left a comment
There was a problem hiding this comment.
Hi @DavideGalilei, thanks for the contribution!
Your fix is correct, but the PR is indeed a duplicate of #11517 and #10895. My suggestion is to merge the earliest PR #10895 and close this one, but I'll leave the final review for Tiangolo.
Thanks again though! 🙏
|
Thanks for the interest @DavideGalilei! As @svlandeg was saying, this was handled in #10895. That is now available in FastAPI |
Previously discussed here: @computed_field not showing in OpenAPI Json #9856
📕 Description
When using computed fields in a custom Pydantic model, FastAPI incorrectly uses
mode="validation"for the additional responses model schema.This causes an issue where the response model is interpreted as an
-Inputmodel, thus excluding read-only computed fields from the documentation (note: the fields are correctly returned in the answer even if they are not shown in the documentation).👀 Example code
❌ Wrong (current) schema
Click to expand
{ "openapi": "3.1.0", "info": { "title": "FastAPI", "version": "0.1.0" }, "paths": { "/rectangle/": { "get": { "summary": "Rectangle", "operationId": "rectangle_rectangle__get", "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Rectangle-Input" } } } } } } } }, "components": { "schemas": { "Rectangle-Input": { "properties": { "width": { "type": "integer", "title": "Width" }, "height": { "type": "integer", "title": "Height" } }, "type": "object", "required": [ "width", "height" ], "title": "Rectangle" }, "Rectangle-Output": { "properties": { "width": { "type": "integer", "title": "Width" }, "height": { "type": "integer", "title": "Height" }, "area": { "type": "integer", "title": "Area", "readOnly": true } }, "type": "object", "required": [ "width", "height", "area" ], "title": "Rectangle" } } } }✅ Correct (fixed) schema:
Click to expand
{ "openapi": "3.1.0", "info": { "title": "FastAPI", "version": "0.1.0" }, "paths": { "/rectangle/": { "get": { "summary": "Rectangle", "operationId": "rectangle_rectangle__get", "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Rectangle" } } } } } } } }, "components": { "schemas": { "Rectangle": { "properties": { "width": { "type": "integer", "title": "Width" }, "height": { "type": "integer", "title": "Height" }, "area": { "type": "integer", "title": "Area", "readOnly": true } }, "type": "object", "required": [ "width", "height", "area" ], "title": "Rectangle" } } } }OpenApi schema diff (wrong ➡️ correct)
💊 Fix
Use
mode="serialization"forcreate_model_fieldfor additional responses model.🧪 Added tests!
tests/test_additional_responses_custom_model_with_computed_fields.py