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

Add default in JSON Schema when const=True #4031

Closed
3 tasks done
stur86 opened this issue Apr 29, 2022 · 1 comment · Fixed by #4152
Closed
3 tasks done

Add default in JSON Schema when const=True #4031

stur86 opened this issue Apr 29, 2022 · 1 comment · Fixed by #4152
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@stur86
Copy link

stur86 commented Apr 29, 2022

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

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

python -c "import pydantic.utils; print(pydantic.utils.version_info())"
             pydantic version: 1.9.0
            pydantic compiled: True
                 install path: ...
               python version: 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
                     platform: Windows-10-10.0.22000-SP0
     optional deps. installed: ['typing-extensions']

When running this snippet of code:

from pydantic import BaseModel, Field

class MyModel(BaseModel):
    x: int = Field(default=0, const=True)

print(MyModel.schema_json(indent=2))

this is the result:

{
  "title": "MyModel",
  "type": "object",
  "properties": {
    "x": {
      "title": "X",
      "const": 0,
      "type": "integer"
    }
  }
}

I am having issues with some JSON Schema validator because of what I think is a subtle difference here - yes, we're setting x to be const=0, but we're not saying that the default is also zero, which leaves the field as not filled if one creates a default object. I don't think this is correct behaviour. I suggest deleting https://github.com/samuelcolvin/pydantic/blob/8997cc5961139dd2695761a33c06a66adbf1430a/pydantic/schema.py#L215 , which should fix the problem.

@stur86 stur86 added the bug V1 Bug related to Pydantic V1.X label Apr 29, 2022
@samuelcolvin
Copy link
Member

PR welcome, but you'll need to see if it breaks other tests and go back to where we originally added that line and see if there's a reason we did it that way.

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
Development

Successfully merging a pull request may close this issue.

2 participants