pydantic-to-typescript icon indicating copy to clipboard operation
pydantic-to-typescript copied to clipboard

Support for both Pydantic V1 and V2

Open khalo-sa opened this issue 2 years ago • 7 comments

Hi @phillipdupuis, after switching to Pydantic V2 in one of my projects, this library stopped working. To make sure that my changes are compatible with both, Pydantic V1 and V2, I created two virtual environments with each respective Pydantic version, and this library installed. The initial goal was to keep the tests the same, but V2 requires slight adjustments to the inputs and generated outputs, which is why I created v1/v2-subdirectories for each type of test. The changes that made that necessary were:

  • Inputs:
    • Generic Models: https://docs.pydantic.dev/latest/usage/models/#generic-models
  • Outputs:
    • Optional fields becoming "type | null" in typescript interfaces. I think this is the relevant discussion regarding the generated json-schema: https://github.com/pydantic/pydantic/issues/1270

The library seems to work now with both, V1 and V2, by dynamically determining the installed Pydantic version and using the appropriate functions.

khalo-sa avatar Jul 28 '23 07:07 khalo-sa

I just added support for computed fields, and a corresponding test.

khalo-sa avatar Aug 09 '23 09:08 khalo-sa

Just looking through pydantic docs. https://docs.pydantic.dev/latest/version-policy/#pydantic-v1

Active development of V1 has already stopped, however critical bug fixes and security vulnerabilities will be fixed in V1 for one year after the release of V2 (June 30, 2024).

The complexity of supporting both versions seems excessive to me. Wouldn't this library be better just releasing a v2 with support for pydantic v2?

Any reason I'm missing for supporting both pydantic major versions?

nick-somebody avatar Oct 02 '23 05:10 nick-somebody

Any reason I'm missing for supporting both pydantic major versions?

There's a lot of Pydantic 1 using code out there that's not necessarily going to update, but could use this library. It doesn't seem to be very much extra code to support both (the majority of this PR's changes are test fixtures).

akx avatar Dec 21 '23 12:12 akx

Any reason I'm missing for supporting both pydantic major versions?

There's a lot of Pydantic 1 using code out there that's not necessarily going to update, but could use this library. It doesn't seem to be very much extra code to support both (the majority of this PR's changes are test fixtures).

@akx pydantic 1 using code can use v1 of this library right?

nick-somebody avatar Jan 04 '24 06:01 nick-somebody

What's the difference between this PR and https://github.com/phillipdupuis/pydantic-to-typescript/pull/40 or https://github.com/phillipdupuis/pydantic-to-typescript/pull/37 ?

Can they be consolidated?

voidus avatar Jan 12 '24 08:01 voidus

In my project this MR worked. I'm using pydantic 2.6.4. #40 failed. I haven't try #37

atodniAr avatar May 06 '24 09:05 atodniAr

I created a fork with this merged so the community can easily install it via pip. The code is published to PyPI as pydantic-to-typescript2.

Phillip Dupuis is still included as an author.

Repository link: Darius-Labs/pydantic-to-typescript2

seanwessmith avatar Jun 17 '24 23:06 seanwessmith