From b4f48d1f9018abaa240d1e1eaeb7d884d8930b4e Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Wed, 8 Oct 2025 08:55:28 +0100 Subject: [PATCH] Test schema in both pydantic versions --- tests/test_union_body_discriminator.py | 62 ++++++++++++++++++-------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/tests/test_union_body_discriminator.py b/tests/test_union_body_discriminator.py index f2b4e0771..aebd4cbc3 100644 --- a/tests/test_union_body_discriminator.py +++ b/tests/test_union_body_discriminator.py @@ -6,6 +6,8 @@ from inline_snapshot import snapshot from pydantic import BaseModel, Field from typing_extensions import Annotated, Literal +from .utils import needs_pydanticv1, needs_pydanticv2 + app = FastAPI() @@ -35,30 +37,54 @@ def save_union_body_discriminator( client = TestClient(app) +@needs_pydanticv1 +def test_openapi_schema_pydantic_v1() -> None: + openapi = app.openapi() + + assert openapi["paths"]["/items/"]["post"]["requestBody"]["content"] == snapshot( + { + "application/json": { + "schema": { + "anyOf": [ + {"$ref": "#/components/schemas/FirstItem"}, + {"$ref": "#/components/schemas/OtherItem"}, + ], + "discriminator": { + "propertyName": "value", + "mapping": { + "first": "#/components/schemas/FirstItem", + "other": "#/components/schemas/OtherItem", + }, + }, + "title": "Item", + } + } + } + ) + + +@needs_pydanticv2 def test_openapi_schema() -> None: openapi = app.openapi() - assert openapi["paths"]["/items/"]["post"]["requestBody"] == snapshot( + assert openapi["paths"]["/items/"]["post"]["requestBody"]["content"] == snapshot( { - "required": True, - "content": { - "application/json": { - "schema": { - "oneOf": [ - {"$ref": "#/components/schemas/FirstItem"}, - {"$ref": "#/components/schemas/OtherItem"}, - ], - "discriminator": { - "propertyName": "value", - "mapping": { - "first": "#/components/schemas/FirstItem", - "other": "#/components/schemas/OtherItem", - }, + "application/json": { + "schema": { + "oneOf": [ + {"$ref": "#/components/schemas/FirstItem"}, + {"$ref": "#/components/schemas/OtherItem"}, + ], + "discriminator": { + "propertyName": "value", + "mapping": { + "first": "#/components/schemas/FirstItem", + "other": "#/components/schemas/OtherItem", }, - "title": "Item", - } + }, + "title": "Item", } - }, + } } )