diff --git a/tests/test_schema_compat_pydantic_v2.py b/tests/test_schema_compat_pydantic_v2.py index 737687f256..2aef8c0c28 100644 --- a/tests/test_schema_compat_pydantic_v2.py +++ b/tests/test_schema_compat_pydantic_v2.py @@ -1,4 +1,5 @@ import pytest +from dirty_equals import IsOneOf from fastapi import FastAPI from fastapi.testclient import TestClient from inline_snapshot import snapshot @@ -63,28 +64,58 @@ def test_openapi_schema(client: TestClient): } }, "components": { - "schemas": { - "PlatformRole": { - "type": "string", - "enum": ["admin", "user"], - "title": "PlatformRole", + "schemas": IsOneOf( + # Newer Pydantic: no top-level OtherRole + { + "PlatformRole": { + "type": "string", + "enum": ["admin", "user"], + "title": "PlatformRole", + }, + "User": { + "properties": { + "username": {"type": "string", "title": "Username"}, + "role": { + "anyOf": [ + {"$ref": "#/components/schemas/PlatformRole"}, + {"enum": [], "title": "OtherRole"}, + ], + "title": "Role", + }, + }, + "type": "object", + "required": ["username", "role"], + "title": "User", + }, }, - "User": { - "properties": { - "username": {"type": "string", "title": "Username"}, - "role": { - "anyOf": [ - {"$ref": "#/components/schemas/PlatformRole"}, - {"enum": [], "title": "OtherRole"}, - ], - "title": "Role", + # Older Pydantic: adds a top-level OtherRole schema + { + "OtherRole": { + "enum": [], + "title": "OtherRole", + }, + "PlatformRole": { + "type": "string", + "enum": ["admin", "user"], + "title": "PlatformRole", + }, + "User": { + "properties": { + "username": {"type": "string", "title": "Username"}, + "role": { + "anyOf": [ + {"$ref": "#/components/schemas/PlatformRole"}, + {"enum": [], "title": "OtherRole"}, + ], + "title": "Role", + }, }, + "type": "object", + "required": ["username", "role"], + "title": "User", }, - "type": "object", - "required": ["username", "role"], - "title": "User", }, - } + ) }, } ) diff --git a/tests/test_union_body_discriminator.py b/tests/test_union_body_discriminator.py index 1b682c7751..ae3673c6cc 100644 --- a/tests/test_union_body_discriminator.py +++ b/tests/test_union_body_discriminator.py @@ -1,5 +1,6 @@ from typing import Annotated, Any, Literal +from dirty_equals import IsOneOf from fastapi import FastAPI from fastapi.testclient import TestClient from inline_snapshot import snapshot @@ -88,11 +89,19 @@ def test_discriminator_pydantic_v2() -> None: "description": "Successful Response", "content": { "application/json": { - "schema": { - "type": "object", - "additionalProperties": True, - "title": "Response Save Union Body Discriminator Items Post", - } + "schema": IsOneOf( + # Pydantic < 2.11: no additionalProperties + { + "type": "object", + "title": "Response Save Union Body Discriminator Items Post", + }, + # Pydantic >= 2.11: has additionalProperties + { + "type": "object", + "additionalProperties": True, + "title": "Response Save Union Body Discriminator Items Post", + }, + ) } }, },