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

Handle non-json native enum values #7056

Merged
merged 2 commits into from Aug 16, 2023
Merged

Handle non-json native enum values #7056

merged 2 commits into from Aug 16, 2023

Conversation

adriangb
Copy link
Member

@adriangb adriangb commented Aug 9, 2023

Fixes #7045

This isn't perfect, e.g. ideally if the value is always a two tuple of ints it would be reflected in the generated schema. But this is better than invalid json.

Selected Reviewer: @dmontagu

@adriangb
Copy link
Member Author

adriangb commented Aug 9, 2023

please review

@cloudflare-pages
Copy link

cloudflare-pages bot commented Aug 9, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: c8e068d
Status: ✅  Deploy successful!
Preview URL: https://b57b7799.pydantic-docs2.pages.dev
Branch Preview URL: https://enum-value-jsonifyu.pydantic-docs2.pages.dev

View logs

@Viicos
Copy link
Contributor

Viicos commented Aug 9, 2023

Thanks 🙏 this will raise an exception if the type can't be serialized using to_jsonable_python:

from enum import Enum

from pydantic_core import to_jsonable_python

class Test: pass

class A(Enum):
    b = Test()

to_jsonable_python(A.b.value)
#> pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <__main__.Test object at 0x7fcad9baf070>

But I guess it is expected?

@adriangb
Copy link
Member Author

adriangb commented Aug 9, 2023

I guess so! Better than generating invalid json and having something downstream crash right?

ta = TypeAdapter(MyEnum)

# insert_assert(ta.json_schema())
assert ta.json_schema() == {'enum': [[1, 2], [2, 3]], 'title': 'MyEnum', 'type': 'array'}
Copy link
Contributor

@Viicos Viicos Aug 9, 2023

Choose a reason for hiding this comment

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

This would still fail to parse with JSON data matching this schema:

TypeAdapter(Direction).validate_json('[0,1]')
#> Input should be (0, 1),(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0) or (-1, 1) [type=enum, input_value=[0, 1], input_type=list]

But serializing complex enums by their value isn't a good idea anyway, as I mentioned in #7045

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup not much we can do about that

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would work with an appropriate validator, perhaps annoying that you have to write it, but I think given how rare this is it's not unreasonable, and I think this change to the JSON schema generation is an improvement either way.

@Viicos
Copy link
Contributor

Viicos commented Aug 9, 2023

I guess so! Better than generating invalid json and having something downstream crash right?

True! Just wanted to be sure pydantic "allowed" exceptions to be raised in this model_json_schema method

@dmontagu
Copy link
Contributor

I think it should work to use the WithJsonSchema annotation if you want/need to explicitly specify something, for what it's worth.

@dmontagu dmontagu enabled auto-merge (squash) August 16, 2023 20:10
@dmontagu dmontagu merged commit 0d4b77b into main Aug 16, 2023
47 checks passed
@dmontagu dmontagu deleted the enum-value-jsonifyu branch August 16, 2023 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong JSON Schema generation with sequence like enums
3 participants