Skip to content

Generate titles for model properties (using schema_json()) only if explicitly provided #1740

Description

@abunuwas

Feature Request

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

pydantic version: 1.6.1
pydantic compiled: True
install path: /Users/j/.local/share/virtualenvs/asfd-asdf-2c-XNDzz/lib/python3.7/site-packages/pydantic
python version: 3.7.5 (default, Nov  1 2019, 02:16:32)  [Clang 11.0.0 (clang-1100.0.33.8)]
platform: Darwin-18.7.0-x86_64-i386-64bit
optional deps. installed: []

Description

When generating JSON schemas out of Pydantic models, all outputs come persistently with a title keyword describing each property and each model. However, as per the JSON Schema documentation, the title keyword is not a required descriptor for a property, it's optional:

JSON Schema includes a few keywords, title, description, default, examples that aren’t strictly used for validation, but are used to describe parts of a schema.
None of these “annotation” keywords are required, but they are encouraged for good practice, and can make your schema “self-documenting”.

It should be the responsibility of the developer to include good titles for their models. Otherwise titles are not required, and a developer omitting a title may have reasons to do so. The fact that Pydantic picks up on the title keyword and generates a title version of the attribute or class name seems like an arbitrary behaviour.

Request

Would it be possible to change the implementation to make it optional to generate JSON Schemas with titles only if developer explicitly provides a title for the attribute?


The script:
test.py

from pydantic import BaseModel


class Item(BaseModel):
    attribute: str


item = Item(attribute='attribute')

print(item.schema_json())
$ python test.py | jq
{
  "title": "Item",
  "type": "object",
  "properties": {
    "attribute": {
      "title": "Attribute",
      "type": "string"
    }
  },
  "required": [
    "attribute"
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions