Skip to content

Commit

Permalink
Ensure json-schema generator handles Literal Null types (#9135)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-f-cruz committed Apr 2, 2024
1 parent fc0cb91 commit 00b4b4f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
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

0 comments on commit 00b4b4f

Please sign in to comment.