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

Generating Json Schema for nested models that have overlapping model names. #1912

Closed
alexvoronov opened this issue Sep 8, 2020 · 1 comment · Fixed by #2170
Closed

Generating Json Schema for nested models that have overlapping model names. #1912

alexvoronov opened this issue Sep 8, 2020 · 1 comment · Fixed by #2170
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@alexvoronov
Copy link

Bug

I'm trying to generate a schema that includes several models that have nested models. The inner-most models have identical name, Params, but since they are used only inside the class, there is no name clash. However, when adding outer models to the same schema, both inner-most Params models end up in the same ref location: {'$ref': '#/definitions/Params'}, which I believe causes the error I see: KeyError: <class '__main__.A.Params'>.

I believe some fully-qualified path in '$ref' would resolve this, something like:

{'$ref': '#/definitions/A/Params'}
{'$ref': '#/definitions/B/Params'}

As a workaround, I wonder if there a way I can adjust prefix of each model independently? Or is there a way to put schema of nested classes "in-place", without creating a "$ref"?

Here is a complete minimal example:

from pydantic import BaseModel, schema


class A(BaseModel):
    class Params(BaseModel):
        a: int

    params: Params  # A.Params

class B(BaseModel):
    class Params(BaseModel):
        b: int

    params: Params  # B.Params


schema.schema([A])  # Works fine.
schema.schema([B])  # Works fine.
try:
    schema.schema([A, B])  # Two schemas together - does not work.
except KeyError as e:
    print(f"{e.__class__.__name__}: {str(e)}")
    """
    KeyError: <class '__main__.A.Params'>
    """

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.6.1
            pydantic compiled: True
               python version: 3.7.8 | packaged by conda-forge | (default, Jul 31 2020, 02:25:08)  [GCC 7.5.0]
                     platform: Linux-3.10.0-957.27.2.el7.x86_64-x86_64-with-centos-7.6.1810-Core
     optional deps. installed: ['typing-extensions']
@alexvoronov alexvoronov added the bug V1 Bug related to Pydantic V1.X label Sep 8, 2020
@jusexton
Copy link
Contributor

jusexton commented Dec 2, 2020

It doesn't appear the ref string plays any role in this KeyError. Model names coming from the models themselves are conflicting. The code currently resolves conflicts but does not consider the possibility of nested class declarations. I can raise a PR to resolve the KeyError but it is unclear how the ref property should behave in this scenario.

jusexton added a commit to jusexton/pydantic that referenced this issue Dec 2, 2020
…ctly when building map of models to create schema from (pydantic#1912)
jusexton added a commit to jusexton/pydantic that referenced this issue Dec 2, 2020
…ultiple `BaseModel` with the same names declared in separate classes (pydantic#1912)
jusexton added a commit to jusexton/pydantic that referenced this issue Dec 29, 2020
…ultiple `BaseModel` with the same names declared in separate classes (pydantic#1912)
samuelcolvin pushed a commit that referenced this issue Feb 11, 2021
…red within different classes (#2170)

* Fixed issue causing KeyError to be raised when building schema from multiple `BaseModel` with the same names declared in separate classes (#1912)

* Update test

* Removed redundant f-string

* Fixed formatting

* Fixed lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
2 participants