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

model_json_schema() not creating JSON schema when subclassed #7004

Closed
1 task done
piercsi opened this issue Aug 3, 2023 · 1 comment · Fixed by #7020
Closed
1 task done

model_json_schema() not creating JSON schema when subclassed #7004

piercsi opened this issue Aug 3, 2023 · 1 comment · Fixed by #7020
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@piercsi
Copy link

piercsi commented Aug 3, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

In the code below if BaseModel is used rather than the MyBaseModel subclass, then the results are as expected, however with the code below, the JSON schema is not produced and no error is raised.

from __future__ import annotations
from pydantic import BaseModel


class MyBaseModel(BaseModel):
    pass


class B(MyBaseModel):
    b: A


class A(MyBaseModel):
    a: str


print(B.model_json_schema())

expected result:

{'$defs': {'A': {'properties': {'a': {'title': 'A', 'type': 'string'}}, 'required': ['a'], 'title': 'A', 'type': 'object'}}, 'properties': {'b': {'$ref': '#/$defs/A'}}, 'required': ['b'], 'title': 'B', 'type': 'object'}

(or an error advising the model hasn't been initialiased)

actual result:
{'properties': {}, 'title': 'MyBaseModel', 'type': 'object'}

If MyBaseModel has any fields set then it also works ok, e.g. this works as expected:

class MyBaseModel(BaseModel):
    x: None

Appreciate this is an edge case but thought is worth noting if there's something that can be done.

Example Code

No response

Python, Pydantic & OS Version

pydantic version: 2.1.1                                                                           
        pydantic-core version: 2.4.0                                                                           
          pydantic-core build: profile=release pgo=false mimalloc=true                                         
                 install path: ...s\Python\venv\Lib\site-packages\pydantic        
               python version: 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]
                     platform: Windows-10-10.0.19044-SP0                                                       
     optional deps. installed: ['email-validator', 'typing-extensions']

Selected Assignee: @dmontagu

@piercsi piercsi added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Aug 3, 2023
@dmontagu
Copy link
Contributor

dmontagu commented Aug 7, 2023

Thanks for reporting!

I believe I have found the source of the issue, and have opened a PR to resolve it #7020.

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 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants