Enable Pydantic's serialization mode for responses, add support for Pydantic's computed_field, better OpenAPI for response models, proper required attributes, better generated clients (#10011)

*  Enable Pydantic's serialization mode for responses

*  Update tests with new Pydantic v2 serialization mode

*  Add a test for Pydantic v2's computed_field
This commit is contained in:
Sebastián Ramírez
2023-08-04 22:47:07 +02:00
committed by GitHub
parent d943e02232
commit 19a2c3bb54
31 changed files with 1446 additions and 256 deletions

View File

@@ -1,4 +1,4 @@
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from docs_src.response_model.tutorial003 import app
@@ -70,7 +70,11 @@ def test_openapi_schema():
"schemas": {
"UserOut": {
"title": "UserOut",
"required": ["username", "email"],
"required": IsOneOf(
["username", "email", "full_name"],
# TODO: remove when deprecating Pydantic v1
["username", "email"],
),
"type": "object",
"properties": {
"username": {"title": "Username", "type": "string"},

View File

@@ -1,4 +1,4 @@
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from docs_src.response_model.tutorial003_01 import app
@@ -70,7 +70,11 @@ def test_openapi_schema():
"schemas": {
"BaseUser": {
"title": "BaseUser",
"required": ["username", "email"],
"required": IsOneOf(
["username", "email", "full_name"],
# TODO: remove when deprecating Pydantic v1
["username", "email"],
),
"type": "object",
"properties": {
"username": {"title": "Username", "type": "string"},

View File

@@ -1,5 +1,5 @@
import pytest
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from ...utils import needs_py310
@@ -79,7 +79,11 @@ def test_openapi_schema(client: TestClient):
"schemas": {
"BaseUser": {
"title": "BaseUser",
"required": ["username", "email"],
"required": IsOneOf(
["username", "email", "full_name"],
# TODO: remove when deprecating Pydantic v1
["username", "email"],
),
"type": "object",
"properties": {
"username": {"title": "Username", "type": "string"},

View File

@@ -1,5 +1,5 @@
import pytest
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from ...utils import needs_py310
@@ -79,7 +79,11 @@ def test_openapi_schema(client: TestClient):
"schemas": {
"UserOut": {
"title": "UserOut",
"required": ["username", "email"],
"required": IsOneOf(
["username", "email", "full_name"],
# TODO: remove when deprecating Pydantic v1
["username", "email"],
),
"type": "object",
"properties": {
"username": {"title": "Username", "type": "string"},

View File

@@ -1,5 +1,5 @@
import pytest
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from docs_src.response_model.tutorial004 import app
@@ -79,7 +79,11 @@ def test_openapi_schema():
"schemas": {
"Item": {
"title": "Item",
"required": ["name", "price"],
"required": IsOneOf(
["name", "description", "price", "tax", "tags"],
# TODO: remove when deprecating Pydantic v1
["name", "price"],
),
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},

View File

@@ -1,5 +1,5 @@
import pytest
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from ...utils import needs_py310
@@ -87,7 +87,11 @@ def test_openapi_schema(client: TestClient):
"schemas": {
"Item": {
"title": "Item",
"required": ["name", "price"],
"required": IsOneOf(
["name", "description", "price", "tax", "tags"],
# TODO: remove when deprecating Pydantic v1
["name", "price"],
),
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},

View File

@@ -1,5 +1,5 @@
import pytest
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from ...utils import needs_py39
@@ -87,7 +87,11 @@ def test_openapi_schema(client: TestClient):
"schemas": {
"Item": {
"title": "Item",
"required": ["name", "price"],
"required": IsOneOf(
["name", "description", "price", "tax", "tags"],
# TODO: remove when deprecating Pydantic v1
["name", "price"],
),
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},

View File

@@ -1,4 +1,4 @@
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from docs_src.response_model.tutorial005 import app
@@ -102,7 +102,11 @@ def test_openapi_schema():
"schemas": {
"Item": {
"title": "Item",
"required": ["name", "price"],
"required": IsOneOf(
["name", "description", "price", "tax"],
# TODO: remove when deprecating Pydantic v1
["name", "price"],
),
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},

View File

@@ -1,5 +1,5 @@
import pytest
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from ...utils import needs_py310
@@ -112,7 +112,11 @@ def test_openapi_schema(client: TestClient):
"schemas": {
"Item": {
"title": "Item",
"required": ["name", "price"],
"required": IsOneOf(
["name", "description", "price", "tax"],
# TODO: remove when deprecating Pydantic v1
["name", "price"],
),
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},

View File

@@ -1,4 +1,4 @@
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from docs_src.response_model.tutorial006 import app
@@ -102,7 +102,11 @@ def test_openapi_schema():
"schemas": {
"Item": {
"title": "Item",
"required": ["name", "price"],
"required": IsOneOf(
["name", "description", "price", "tax"],
# TODO: remove when deprecating Pydantic v1
["name", "price"],
),
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},

View File

@@ -1,5 +1,5 @@
import pytest
from dirty_equals import IsDict
from dirty_equals import IsDict, IsOneOf
from fastapi.testclient import TestClient
from ...utils import needs_py310
@@ -112,7 +112,11 @@ def test_openapi_schema(client: TestClient):
"schemas": {
"Item": {
"title": "Item",
"required": ["name", "price"],
"required": IsOneOf(
["name", "description", "price", "tax"],
# TODO: remove when deprecating Pydantic v1
["name", "price"],
),
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},