mts-ai / FastAPI-JSONAPI

JSON:API for FastAPI
https://mts-ai.github.io/FastAPI-JSONAPI/
MIT License
85 stars 10 forks source link

Support Pydantic v2 #51

Open samikhailov opened 7 months ago

samikhailov commented 7 months ago

An example doesn't work with Pydantic v2.

An error occurred:

/Users/soul/Documents/projects/fastapi-json-api/venv/bin/python /Users/soul/Documents/projects/fastapi-json-api/main.py 
Traceback (most recent call last):
  File "/Users/soul/Documents/projects/fastapi-json-api/main.py", line 12, in <module>
    from fastapi_jsonapi import RoutersJSONAPI, init
  File "/Users/soul/Documents/projects/fastapi-json-api/venv/lib/python3.11/site-packages/fastapi_jsonapi/__init__.py", line 5, in <module>
    from fastapi_jsonapi.api import RoutersJSONAPI
  File "/Users/soul/Documents/projects/fastapi-json-api/venv/lib/python3.11/site-packages/fastapi_jsonapi/api.py", line 23, in <module>
    from fastapi_jsonapi.schema_builder import SchemaBuilder
  File "/Users/soul/Documents/projects/fastapi-json-api/venv/lib/python3.11/site-packages/fastapi_jsonapi/schema_builder.py", line 19, in <module>
    from pydantic.fields import FieldInfo, ModelField
ImportError: cannot import name 'ModelField' from 'pydantic.fields' (/Users/soul/Documents/projects/fastapi-json-api/venv/lib/python3.11/site-packages/pydantic/fields.py)

Dependencies:

annotated-types==0.6.0
anyio==3.7.1
click==8.1.7
fastapi==0.104.1
FastAPI-JSONAPI==2.3.2
greenlet==3.0.1
h11==0.14.0
idna==3.4
pydantic==2.5.1
pydantic_core==2.14.3
simplejson==3.19.2
sniffio==1.3.0
SQLAlchemy==2.0.23
starlette==0.27.0
typing_extensions==4.8.0
uvicorn==0.24.0.post1

Do you plan to support Pydantic v2?

mahenzon commented 6 months ago

Hey there! Sure, we plan to support FastAPI 0.100+ and Pydantic V2

DavidRomanovizc commented 5 months ago

Hi, @mahenzon, I'd like to try to take on resolving this issue. Could you provide additional information if needed, and could you assign me to this issue? Regarding the additional information, At the moment, I have a couple of questions: the current version of the library uses pydantic v1. Do you plan to add support for pydantic v2 while maintaining backward compatibility? Or is it necessary to add pydantic v2 without preserving support for pydantic v1?

mahenzon commented 5 months ago

Hi @DavidRomanovizc, great news!

Nope, backward compatibility is not planned. There'll be a new release 3.0 with FastAPI 0.100+ and Pydantic 2+

I tried to migrate using the official migration script, but there were a lot of errors, here's the branch: https://github.com/mts-ai/FastAPI-JSONAPI/tree/upgrade-fastapi-and-pydantic

Also there were some problems with annotations. For example atomic operations list has to be of length 1 or more, I could not find new annotation validator for this case.

And a strange one: schemas are being serialized with model_config: it's serialized to json with all other attributes. Probably it's because schemas generation is kinda overcomplicated. I could not find a quick solution.

We have decent tests coverage, so a lot of problems can be detected automatically.

Feel free to open new PRs, I've created a new branch for version 3.x https://github.com/mts-ai/FastAPI-JSONAPI/tree/dev-3.x

kmbhm1 commented 1 week ago

Hello there! This upgrade would be extremely valuable for a project I'm currently working on. Any updates on when it might be ready or how to contribute? Thanks!

mahenzon commented 1 week ago

@kmbhm1 hello there! Technically, Pydantic V2 support is ready, but I need to make sure it works on older Python versions, I need to update examples and all docs, and also I'd like to update some legacy stuff

Here it is: https://github.com/mts-ai/FastAPI-JSONAPI/pull/88

It'll be cool, if you can check if it works for you in this state. Feel free to report any issues