Skip to content
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

Ensure json-schema generator handles Literal Null types #9135

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions pydantic/json_schema.py
Expand Up @@ -751,6 +751,8 @@ def literal_schema(self, schema: core_schema.LiteralSchema) -> JsonSchemaValue:
result['type'] = 'boolean'
elif types == {list}:
result['type'] = 'array'
elif types == {type(None)}:
result['type'] = 'null'
return result

def enum_schema(self, schema: core_schema.EnumSchema) -> JsonSchemaValue:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_json_schema.py
Expand Up @@ -2324,7 +2324,7 @@ class Model(BaseModel):
'int_literal': {'enum': [123, 456], 'title': 'Int Literal', 'type': 'integer'},
'float_literal': {'$ref': '#/$defs/FloatEnum'},
'bool_literal': {'enum': [True, False], 'title': 'Bool Literal', 'type': 'boolean'},
'none_literal': {'const': None, 'enum': [None], 'title': 'None Literal'},
'none_literal': {'const': None, 'enum': [None], 'title': 'None Literal', 'type': 'null'},
'list_literal': {'$ref': '#/$defs/ListEnum'},
'mixed_literal': {'enum': [123, 'abc'], 'title': 'Mixed Literal'},
},
Expand Down
12 changes: 8 additions & 4 deletions tests/test_types.py
Expand Up @@ -5187,16 +5187,20 @@ class Model(BaseModel):
'title': 'Model',
'type': 'object',
'properties': {
'my_none': {'const': None, 'enum': [None], 'title': 'My None'},
'my_none_list': {'items': {'const': None, 'enum': [None]}, 'title': 'My None List', 'type': 'array'},
'my_none': {'const': None, 'enum': [None], 'title': 'My None', 'type': 'null'},
'my_none_list': {
'items': {'const': None, 'enum': [None], 'type': 'null'},
'title': 'My None List',
'type': 'array',
},
'my_none_dict': {
'additionalProperties': {'const': None, 'enum': [None]},
'additionalProperties': {'const': None, 'enum': [None], 'type': 'null'},
'title': 'My None Dict',
'type': 'object',
},
'my_json_none': {
'contentMediaType': 'application/json',
'contentSchema': {'const': None, 'enum': [None]},
'contentSchema': {'const': None, 'enum': [None], 'type': 'null'},
'title': 'My Json None',
'type': 'string',
},
Expand Down