Browse Source

allow multiple versions of the schema

pull/15139/head
svlandeg 4 months ago
parent
commit
7665c151e5
  1. 35
      tests/test_schema_compat_pydantic_v2.py
  2. 13
      tests/test_union_body_discriminator.py

35
tests/test_schema_compat_pydantic_v2.py

@ -1,4 +1,5 @@
import pytest import pytest
from dirty_equals import IsOneOf
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot from inline_snapshot import snapshot
@ -63,7 +64,9 @@ def test_openapi_schema(client: TestClient):
} }
}, },
"components": { "components": {
"schemas": { "schemas": IsOneOf(
# Newer Pydantic: no top-level OtherRole
{
"PlatformRole": { "PlatformRole": {
"type": "string", "type": "string",
"enum": ["admin", "user"], "enum": ["admin", "user"],
@ -84,7 +87,35 @@ def test_openapi_schema(client: TestClient):
"required": ["username", "role"], "required": ["username", "role"],
"title": "User", "title": "User",
}, },
} },
# 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",
},
},
)
}, },
} }
) )

13
tests/test_union_body_discriminator.py

@ -1,5 +1,6 @@
from typing import Annotated, Any, Literal from typing import Annotated, Any, Literal
from dirty_equals import IsOneOf
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot from inline_snapshot import snapshot
@ -88,11 +89,19 @@ def test_discriminator_pydantic_v2() -> None:
"description": "Successful Response", "description": "Successful Response",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": IsOneOf(
# Pydantic < 2.11: no additionalProperties
{
"type": "object",
"title": "Response Save Union Body Discriminator Items Post",
},
# Pydantic >= 2.11: has additionalProperties
{
"type": "object", "type": "object",
"additionalProperties": True, "additionalProperties": True,
"title": "Response Save Union Body Discriminator Items Post", "title": "Response Save Union Body Discriminator Items Post",
} },
)
} }
}, },
}, },

Loading…
Cancel
Save