Browse Source

allow multiple versions of the schema

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

67
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,28 +64,58 @@ def test_openapi_schema(client: TestClient):
} }
}, },
"components": { "components": {
"schemas": { "schemas": IsOneOf(
"PlatformRole": { # Newer Pydantic: no top-level OtherRole
"type": "string", {
"enum": ["admin", "user"], "PlatformRole": {
"title": "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": { # Older Pydantic: adds a top-level OtherRole schema
"properties": { {
"username": {"type": "string", "title": "Username"}, "OtherRole": {
"role": { "enum": [],
"anyOf": [ "title": "OtherRole",
{"$ref": "#/components/schemas/PlatformRole"}, },
{"enum": [], "title": "OtherRole"}, "PlatformRole": {
], "type": "string",
"title": "Role", "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",
}, },
} )
}, },
} }
) )

19
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(
"type": "object", # Pydantic < 2.11: no additionalProperties
"additionalProperties": True, {
"title": "Response Save Union Body Discriminator Items Post", "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",
},
)
} }
}, },
}, },

Loading…
Cancel
Save