Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Version 2.5.3 to 2.6.0 error -> LookupError: Could not find a ref for #/components/schemas/xpto . Maybe you tried to call resolve_ref_schema from within a recursive model? #8689

Closed
1 task done
Tracked by #8671
MatheusVp2 opened this issue Jan 31, 2024 · 7 comments · Fixed by #8718
Labels
bug V2 Bug related to Pydantic V2

Comments

@MatheusVp2
Copy link

MatheusVp2 commented Jan 31, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

After updating from pydantic 2.5.3 > 2.6.0, updating in my project, it broke all tests, with the problem of LookupError: Could not find a ref for #/components/schemas/xpto . Maybe you tried to call resolve_ref_schema from within a recursive model?

Example Code

import typing
from enum import Enum

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from pydantic import BaseModel, Field


class AllowedValues(str, Enum):
    VAL1 = 'Val1'
    VAL2 = 'Val2'


class ModelA(BaseModel):
    # Removing the max_length from the Field definition will make the code work
    modelA_1: AllowedValues = Field(..., max_length=60)


class ModelB(ModelA):
    modelB_1: typing.List[ModelA]


class ModelC(BaseModel):
    # Removing modelC_1 or modelC_2 and keeping only one of them will make the code work
    modelC_1: ModelB
    modelC_2: ModelB


class ModelD(BaseModel):
    modelD_1: typing.List[ModelC]


app = FastAPI(version='1.0.0')


@app.post('/route1', response_model=ModelD)
async def route1():
    raise NotImplementedError


schema = get_openapi(
    title='test',
    servers=[{"url": "https://test.com", "description": "desc"}],
    description='description',
    version='1.0.0',
    routes=app.routes)
print(schema)

No response

Python, Pydantic & OS Version

pydantic version: 2.6.0
        pydantic-core version: 2.16.1
          pydantic-core build: profile=release pgo=true
                 install path: /home/myuser/.pyenv/versions/3.9.14/envs/project/lib/python3.9/site-packages/pydantic
               python version: 3.9.14 (main, Dec  9 2022, 17:08:14)  [GCC 9.4.0]
                     platform: Linux-5.15.0-92-generic-x86_64-with-glibc2.31
             related packages: typing_extensions-4.9.0 fastapi-0.109.0
                       commit: unknown
@MatheusVp2 MatheusVp2 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Jan 31, 2024
@sydney-runkle
Copy link
Member

@MatheusVp2,

Could you please provide a code snippet from which we can reproduce this error? Thanks!

@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Jan 31, 2024
@MatheusVp2
Copy link
Author

MatheusVp2 commented Jan 31, 2024

@sydney-runkle

I managed to debug the application here and found that when I call the function, get_openapi from from fastapi.openapi.utils import get_openapi, it breaks and throws the error

@Kludex
Copy link
Member

Kludex commented Jan 31, 2024

We need a minimal reproducible example that we can run. Would you mind providing that? 🙏

@MatheusVp2
Copy link
Author

@Kludex @sydney-runkle
Added the minimum code example there to simulate the error. Emphasizing that it works in version 2.5.3.

@Kludex
Copy link
Member

Kludex commented Feb 1, 2024

I can reproduce the issue on 2.6.

@dmontagu
Copy link
Contributor

dmontagu commented Feb 2, 2024

Somewhat simplified pure-pydantic reproduction of the issue:

import typing
from enum import Enum

from pydantic import BaseModel, Field


class AllowedValues(str, Enum):
    VAL1 = 'Val1'
    VAL2 = 'Val2'


class ModelA(BaseModel):
    # Removing the max_length from the Field definition will make the code work
    modelA_1: AllowedValues = Field(..., max_length=60)


class ModelB(ModelA):
    modelB_1: typing.List[ModelA]


class ModelC(BaseModel):
    modelC_1: ModelB


class Model(BaseModel):
    b: ModelB
    c: ModelC


Model.model_json_schema()

I had feared this was also introduced by #8526 but that doesn't seem to be the case.

@sydney-runkle
Copy link
Member

I've traced the source of this issue to here: b2aa36a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants