4 changed files with 9 additions and 92 deletions
@ -1,51 +0,0 @@ |
|||||
from typing import List |
|
||||
|
|
||||
from fastapi import FastAPI |
|
||||
from pydantic import BaseModel |
|
||||
|
|
||||
app = FastAPI() |
|
||||
|
|
||||
|
|
||||
class RecursiveItem(BaseModel): |
|
||||
sub_items: List["RecursiveItem"] = [] |
|
||||
name: str |
|
||||
|
|
||||
|
|
||||
RecursiveItem.model_rebuild() |
|
||||
|
|
||||
|
|
||||
class RecursiveSubitemInSubmodel(BaseModel): |
|
||||
sub_items2: List["RecursiveItemViaSubmodel"] = [] |
|
||||
name: str |
|
||||
|
|
||||
|
|
||||
class RecursiveItemViaSubmodel(BaseModel): |
|
||||
sub_items1: List[RecursiveSubitemInSubmodel] = [] |
|
||||
name: str |
|
||||
|
|
||||
|
|
||||
RecursiveSubitemInSubmodel.model_rebuild() |
|
||||
RecursiveItemViaSubmodel.model_rebuild() |
|
||||
|
|
||||
|
|
||||
@app.get("/items/recursive", response_model=RecursiveItem) |
|
||||
def get_recursive(): |
|
||||
return {"name": "item", "sub_items": [{"name": "subitem", "sub_items": []}]} |
|
||||
|
|
||||
|
|
||||
@app.get("/items/recursive-submodel", response_model=RecursiveItemViaSubmodel) |
|
||||
def get_recursive_submodel(): |
|
||||
return { |
|
||||
"name": "item", |
|
||||
"sub_items1": [ |
|
||||
{ |
|
||||
"name": "subitem", |
|
||||
"sub_items2": [ |
|
||||
{ |
|
||||
"name": "subsubitem", |
|
||||
"sub_items1": [{"name": "subsubsubitem", "sub_items2": []}], |
|
||||
} |
|
||||
], |
|
||||
} |
|
||||
], |
|
||||
} |
|
@ -1,12 +1,9 @@ |
|||||
from fastapi.testclient import TestClient |
from fastapi.testclient import TestClient |
||||
|
|
||||
from ..utils import needs_pydanticv1 |
from .app import app |
||||
|
|
||||
|
|
||||
@needs_pydanticv1 |
|
||||
def test_recursive(): |
def test_recursive(): |
||||
from .app_pv1 import app |
|
||||
|
|
||||
client = TestClient(app) |
client = TestClient(app) |
||||
response = client.get("/items/recursive") |
response = client.get("/items/recursive") |
||||
assert response.status_code == 200, response.text |
assert response.status_code == 200, response.text |
@ -1,33 +0,0 @@ |
|||||
from fastapi.testclient import TestClient |
|
||||
|
|
||||
from ..utils import needs_pydanticv2 |
|
||||
|
|
||||
|
|
||||
@needs_pydanticv2 |
|
||||
def test_recursive(): |
|
||||
from .app_pv2 import app |
|
||||
|
|
||||
client = TestClient(app) |
|
||||
response = client.get("/items/recursive") |
|
||||
assert response.status_code == 200, response.text |
|
||||
assert response.json() == { |
|
||||
"sub_items": [{"name": "subitem", "sub_items": []}], |
|
||||
"name": "item", |
|
||||
} |
|
||||
|
|
||||
response = client.get("/items/recursive-submodel") |
|
||||
assert response.status_code == 200, response.text |
|
||||
assert response.json() == { |
|
||||
"name": "item", |
|
||||
"sub_items1": [ |
|
||||
{ |
|
||||
"name": "subitem", |
|
||||
"sub_items2": [ |
|
||||
{ |
|
||||
"name": "subsubitem", |
|
||||
"sub_items1": [{"name": "subsubsubitem", "sub_items2": []}], |
|
||||
} |
|
||||
], |
|
||||
} |
|
||||
], |
|
||||
} |
|
Loading…
Reference in new issue