Failed to create schema with optional fields
See original GitHub issueJust tried to use schema creation and it’s awesome, but I found, I think, bug with optional fields.
Here is my model:
from typing import Optional
from pydantic import BaseModel
class TestModel(BaseModel):
foo: Optional[int] = None
print(TestModel.schema_json(indent=2))
Output:
Traceback (most recent call last):
File "test_pydantic_schema.py", line 9, in <module>
print(TestModel.schema_json(indent=2))
File "/pydantic/pydantic/main.py", line 288, in schema_json
return json.dumps(cls.schema(by_alias=by_alias), default=pydantic_encoder, **dumps_kwargs)
File "/pydantic/pydantic/main.py", line 278, in schema
s['properties'] = {f.alias: f.schema(by_alias) for f in cls.__fields__.values()}
File "/pydantic/pydantic/main.py", line 278, in <dictcomp>
s['properties'] = {f.alias: f.schema(by_alias) for f in cls.__fields__.values()}
File "/pydantic/pydantic/fields.py", line 146, in schema
if issubclass(self.type_, Enum):
TypeError: issubclass() arg 1 must be a class
Same issue with Union
:
from typing import Union
from pydantic import BaseModel
class TestModel(BaseModel):
foo: Union[str, int]
print(TestModel.schema_json(indent=2))
I think pydantic
should cover cases like this, I don’t know what is the best way to solve issue with Optional
, but in case of Union
oneOf
or anyOf
rule can be used http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.7.
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (13 by maintainers)
Top Results From Across the Web
Failed to create schema with optional fields #213 - GitHub
Just tried to use schema creation and it's awesome, but I found, I think, bug with optional fields. Here is my model: from...
Read more >Issues with optional fields when using Avro schema
I'm trying to create a schema with optional fields. Here's my setup: $> cat mytest.avsc { "type": "record", "namespace": "com.example", ...
Read more >Is it possible to have an optional field in an Avro schema (i.e. ...
JSON file)? In my Avro schema, I have two fields: {"name": "author", "type": ["null", "string"], "default": null}, {"name": "importance", "type ...
Read more >Adding optional field to existing schema, RDBLoader failure
Hi, I am facing the following issue. I want to add an additional optional field to an existing schema. In the schema file...
Read more >Kafka-Rest Avro schema optional field error - Google Groups
I am using the kafka-rest-proxy to write out to the schema registry and kafka, but I am getting an error as below: "Avro...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
as per discussion on gitter:
type
>schema
'schema': {'type': 'int'}
which is just'schema': 'int'
, this applies both at the top level for simple types and where simple types are nested within more complex types.Overal the design is encapsulated by:
ok, after looking at #218 I propose the following way to deal with this:
type
becomes a dictionary.For “simple” fields, eg. just an int or float, type like:
For unions we’d have:
For things like
List[int]
we’d have:or
List[Union[int, str]]
we’d have:For
Dict[str, int]
we’d have:(we could add
key_mode
if the key is a union, but that’s a very rare case)The names
mode
,item_type
andshape
are not great (suggestions very welcome), but I think this is the best way to go.Questions:
type: {'item_type': 'int', ...}
or just add all these items to the main dict?properties
on sub models?Union[int, FoobarModel]
- I guess we havetype
as above, thenproperties
. What do we do withUnion[FooModel, BarModel]
- maybe this is just banned?@Gr1N @x1ah