KEMBAR78
Ensure json-schema generator handles Literal Null types by bruno-f-cruz · Pull Request #9135 · pydantic/pydantic · GitHub
Skip to content

Conversation

@bruno-f-cruz
Copy link
Contributor

@bruno-f-cruz bruno-f-cruz commented Mar 29, 2024

Change Summary

This PR aims to complete #8944 and #8905 by also handling null types when generating a json-schema from a pydantic model.

For instance, the following model:

from pydantic import BaseModel
from typing import Literal


class Foo(BaseModel):
    bar: Literal["Bar"] = 'Bar'
    baz: Literal[None] = None
    foo: str = 'Foo'

leads to:

{'properties': {'bar': {'const': 'Bar', 'default': 'Bar', 'enum': ['Bar'], 'title': 'Bar', 'type': 'string'}, 'baz': {'const': None, 'default': None, 'enum': [None], 'title': 'Baz', 'type': 'null'}, 'foo': {'default': 'Foo', 'title': 'Foo', 'type': 'string'}}, 'title': 'Foo', 'type': 'object'}

Related issue number

fix #8905

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: @adriangb

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 29, 2024

CodSpeed Performance Report

Merging #9135 will not alter performance

Comparing bruno-f-cruz:add-literal-null-to-json-schema (65f4461) with main (8128821)

Summary

✅ 13 untouched benchmarks

@sydney-runkle sydney-runkle added the relnotes-fix Used for bugfixes. label Mar 29, 2024
@bruno-f-cruz bruno-f-cruz marked this pull request as ready for review March 29, 2024 20:25
@bruno-f-cruz
Copy link
Contributor Author

bruno-f-cruz commented Mar 29, 2024

please review @sydney-runkle Not sure why those 3 tests are failing, can you take a look? thanks!

@bruno-f-cruz bruno-f-cruz changed the title Ensure json schema generator handles Literal null types Ensure json-schema generator handles Literal Null types Mar 31, 2024
@sydney-runkle
Copy link
Contributor

@bruno-f-cruz,

Looked like flaky CI. All is well!

Copy link
Contributor

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

Waiting for final confirmation from @dmontagu before we merge.

@sydney-runkle sydney-runkle merged commit 00b4b4f into pydantic:main Apr 2, 2024
NeevCohen pushed a commit to NeevCohen/pydantic that referenced this pull request Apr 7, 2024
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.

Literal annotation does not render type to json-schema

3 participants