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
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",
},
}
)
},
}
)

19
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",
},
)
}
},
},

Loading…
Cancel
Save