strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Fix exclude_unset issue when converting strawberry type to pydantic type

Open na86421 opened this issue 2 years ago • 1 comments

Description

Fix exclude_unset issue when converting strawberry type to pydantic type

In the process of converting to pydantic type through to_pydantic_default, if Value is unset(== None) it is not put in instance_kwargs. So, make sure have the __fields_set__ value properly.

Types of Changes

  • [ ] Core
  • [x] Bugfix
  • [ ] New feature
  • [ ] Enhancement/optimization
  • [ ] Documentation

Issues Fixed or Closed by This PR

  • Fix #2163

Checklist

  • [x] My code follows the code style of this project.
  • [ ] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.
  • [x] I have read the CONTRIBUTING document.
  • [x] I have added tests to cover my changes.
  • [x] I have tested the changes and verified that they work and don't break anything (as well as I can manage).

na86421 avatar Sep 17 '22 07:09 na86421

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


Fix exclude_unset issue when converting strawberry type to pydantic type


Here's the preview release card for twitter:

Here's the tweet text:

🆕 Release (next) is out! Thanks to JunKi Yoon for the PR 👏

Get it here 👉 https://github.com/strawberry-graphql/strawberry/releases/tag/(next)

botberry avatar Sep 17 '22 07:09 botberry

Yes, it has to be considered, i don't think the new tests that i suggest will pass. Because you are only checking for none :)

On Tue, Sep 27, 2022, 8:35 PM JunKi Yoon @.***> wrote:

@.**** commented on this pull request.

In tests/experimental/pydantic/test_conversion.py https://github.com/strawberry-graphql/strawberry/pull/2180#discussion_r981182013 :

@@ -1247,3 +1247,31 @@ class Test:

 assert test.optional_list == [1, 2, 3]
 assert test.optional_str is None

+def test_exclude_unset_parameter_when_convert_strawberry_type_to_pydantic_type():

  • class User(BaseModel):
  •    id: int
    
  •    name: str
    
  •    surname: Optional[str]
    
  • @strawberry.experimental.pydantic.type(model=User, all_fields=True)
  • class UserType:
  •    pass
    
  • user = User(id=1, name="leffe")
  • user_type = UserType(id=1, name="leffe")
  • user_type_pydantic = user_type.to_pydantic()

Thank you, I understood. In fact, It is a case that I was concerned about while patching, but the existing tests all worked well, so I did't worry about that. But after seeing your comment, I think it should be considered. right?

— Reply to this email directly, view it on GitHub https://github.com/strawberry-graphql/strawberry/pull/2180#discussion_r981182013, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHI275YP4PXKUGREXHZUBXTWALSZBANCNFSM6AAAAAAQO4Q3JE . You are receiving this because you were assigned.Message ID: @.***>

thejaminator avatar Sep 27 '22 14:09 thejaminator

I took a look into this and came up with a proposal here PR https://github.com/strawberry-graphql/strawberry/pull/2631

ughyg avatar Mar 12 '23 23:03 ughyg