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

make to_pydantic function recursive #3437

Open
chrisemke opened this issue Apr 3, 2024 · 0 comments
Open

make to_pydantic function recursive #3437

chrisemke opened this issue Apr 3, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@chrisemke
Copy link

chrisemke commented Apr 3, 2024

Today when you have a type created from a pydantic model, you can convert it back to pydantic, but not it's fields

Describe the Bug

I made this example using sqlmodel models

from pydantic import PositiveInt
from sqlmodel import (
    Field,
    SQLModel,
)
from strawberry import auto
from strawberry.experimental.pydantic import input as pydantic_input


class City(SQLModel):
    id: int | None = Field(default=None, primary_key=True)
    ibge: PositiveInt = Field(unique=True)
    name: str
    ddd: int | None = None

class AddressBase(SQLModel):
    id: int | None = Field(default=None, primary_key=True)
    zipcode: PositiveInt | None = None
    neighborhood: str | None = None
    complement: str | None = None
    # coordinates: Coordinates | None = None

@pydantic_input(model=City)
class CityInput:
    ibge: auto
    name: auto
    ddd: auto


@pydantic_input(model=AddressBase)
class AddressInput:
    zipcode: auto
    city: CityInput | None = None

address_input = AddressInput(city=CityInput(ibge=1111, name='str'))
print(address_input)
print(type(address_input.to_pydantic()))
print(address_input.to_pydantic())
print(type(address_input.city.to_pydantic()))
print(address_input.city.to_pydantic())

result:

AddressInput(city=CityInput(ibge=1111, name='str', ddd=None), zipcode=None)
<class '__main__.AddressBase'>
id=None zipcode=None neighborhood=None complement=None
<class '__main__.City'>
id=None ibge=1111 name='str' ddd=None

notice: when I print the whole address_input, it doesn't convert the field city, it's just dropped out

System Information

  • Operating system: linux
  • Strawberry version (if applicable): any

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@chrisemke chrisemke added the bug Something isn't working label Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant