Skip to content

Add default in JSON Schema when const=True #4031

@stur86

Description

@stur86

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions