Skip to content

JSON Schema is wrong when mode='serialization' and fields have a default #8413

@samuelcolvin

Description

@samuelcolvin

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

In the example below, bar will always have a value, so should be included in required, but it's not.

Example Code

from pydantic import BaseModel

class Foobar(BaseModel):
    foo: int
    bar: int = 1

print(Foobar.model_json_schema(mode='serialization'))
"""
{
    'properties': {
        'foo': {
            'title': 'Foo',
            'type': 'integer',
        },
        'bar': {
            'default': 1,
            'title': 'Bar',
            'type': 'integer',
        },
    },
    'required': ['foo'],
    'title': 'Foobar',
    'type': 'object',
}
"""

Python, Pydantic & OS Version

pydantic version: 2.5.2
        pydantic-core version: 2.14.5
          pydantic-core build: profile=release pgo=true
                 install path: /Users/samuel/code/fastui/env311/lib/python3.11/site-packages/pydantic
               python version: 3.11.5 (main, Aug 26 2023, 13:55:09) [Clang 14.0.3 (clang-1403.0.22.14.1)]
                     platform: macOS-14.1-arm64-arm-64bit
             related packages: typing_extensions-4.9.0 pyright-1.1.335 fastapi-0.104.1 mypy-1.7.0 email-validator-2.1.0.post1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V2Bug related to Pydantic V2v3Under consideration for V3

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions