10000 Always include `additionalProperties: True` for arbitrary dictionary schemas by austinyu · Pull Request #11392 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Always include additionalProperties: True for arbitrary dictionary schemas #11392

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

Merged

Conversation

austinyu
Copy link
Contributor
@austinyu austinyu commented Feb 5, 2025

Change Summary

For following example, an extra additionalProperties: True will be added to the existing json schema.

from typing import Any
from pydantic import BaseModel


class Model(BaseModel):
    conditions: dict[str, Any]


after = Model.model_json_schema()

before = {
    "properties": {"conditions": {"title": "Conditions", "type": "object"}},
    "required": ["conditions"],
    "title": "Model",
    "type": "object",
}

assert after == {
    "properties": {
        "conditions": {
            "additionalProperties": True,
            "title": "Conditions",
            "type": "object",
        }
    },
    "required": ["conditions"],
    "title": "Model",
    "type": "object",
}

The same rule will be applied to all fields of the following model.

from typing import Any
from typing import Dict

from pydantic import BaseModel


class Model(BaseModel):
    ex3: dict
    ex1: dict[str, Any]
    ex2: dict[Any, Any]
    ex4: Dict
    ex5: Dict[str, Any]
    ex6: Dict[Any, Any]


after = Model.model_json_schema()

assert after == {
    "properties": {
        "ex3": {"additionalProperties": True, "title": "Ex3", "type": "object"},
        "ex1": {"additionalProperties": True, "title": "Ex1", "type": "object"},
        "ex2": {"additionalProperties": True, "title": "Ex2", "type": "object"},
        "ex4": {"additionalProperties": True, "title": "Ex4", "type": "object"},
        "ex5": {"additionalProperties": True, "title": "Ex5", "type": "object"},
        "ex6": {"additionalProperties": True, "title": "Ex6", "type": "object"},
    },
    "required": ["ex3", "ex1", "ex2", "ex4", "ex5", "ex6"],
    "title": "Model",
    "type": "object",
}

Related issue number

fix #11318

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @sydney-runkle

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Feb 5, 2025
Copy link
codspeed-hq bot commented Feb 5, 2025

CodSpeed Performance Report

Merging #11392 will not alter performance

Comparing austinyu:addition-properties-true-json-schema (af34cf7) with main (929e8f4)

Summary

✅ 45 untouched benchmarks

Copy link
Contributor
github-actions bot commented Feb 5, 2025

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  json_schema.py
Project Total  

This report was generated by python-coverage-comment-action

@austinyu austinyu marked this pull request as ready for review February 5, 2025 07:54
@austinyu
Copy link
Contributor Author
austinyu commented Feb 5, 2025

please review

Copy link
Member
@Viicos Viicos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM I'll merge soon if I don't see any objections

@sydney-runkle
Copy link
Contributor

Fine by me!

@Viicos Viicos merged commit bf61067 into pydantic:main Feb 7, 2025
54 checks passed
@austinyu austinyu deleted the addition-properties-true-json-schema branch February 11, 2025 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Always include additionalProperties: True for dictionary schemas
3 participants
0