wallneradam / esorm

Python ElasticSearch ORM based on Pydantic
https://esorm.readthedocs.io/
Mozilla Public License 2.0
30 stars 0 forks source link

Enum support keyword #20

Open poppelaars opened 1 month ago

poppelaars commented 1 month ago

Maybe a noob question.

I am trying to integrate esorm with fastapi and would like to use an Enumeration type.

Source: https://docs.pydantic.dev/2.0/usage/types/enums/

from enum import Enum, IntEnum

class FruitEnum(str, Enum):
    pear = 'pear'
    banana = 'banana'

Is there a way to specify an Enum as an elasticsearch 'keyword' inside my model class?

wallneradam commented 1 month ago

Not yet, but good idea. The closest thing now is the typing.Literal:

from typing import Literal
class MyModel(esorm.ESModel):
    fruit: Literal['banana', 'pear']

But I will implement Enum in the next release.

poppelaars commented 1 month ago

Could you take into account the support for fastapi swagger documentation? As far as I understand fastapi uses the str subclass to generate swagger documentation.

https://stackoverflow.com/questions/64160594/fastapi-enum-type-models-not-populated https://github.com/pydantic/pydantic/discussions/4967

wallneradam commented 1 month ago

FastAPI is just using Pydantic, as ESORM. So it should work out of the box. I'm using ESORM with FastAPI in one of my project, and it works very well.