-
-
Notifications
You must be signed in to change notification settings - Fork 509
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
add support for default values with a field that is a pydantic model #3499
add support for default values with a field that is a pydantic model #3499
Conversation
for more information, see https://pre-commit.ci
Thanks for adding the Here's a preview of the changelog: Fixes a bug where pydantic models as the default value for an input did not print the proper schema. Here's the tweet text:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ppease - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 3 issues found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.
if IS_PYDANTIC_V2: | ||
import pydantic.v1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Clarify the purpose of importing pydantic.v1
under IS_PYDANTIC_V2
condition.
It seems counterintuitive to import a version-specific module conditionally based on the version check. Ensure this is intentional and document the rationale behind this decision.
CodSpeed Performance ReportMerging #3499 will not alter performanceComparing Summary
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3499 +/- ##
=======================================
Coverage 96.56% 96.56%
=======================================
Files 523 523
Lines 33485 33551 +66
Branches 5554 5566 +12
=======================================
+ Hits 32336 32400 +64
- Misses 914 916 +2
Partials 235 235 |
for more information, see https://pre-commit.ci
@@ -27,3 +29,72 @@ def filter(self, input: FilterInput) -> str: | |||
assert not result.errors | |||
assert result.data | |||
assert result.data["filter"] == "Hello nope" | |||
|
|||
|
|||
def test_input_with_nonscalar_field_default(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally thought this had issues, but as long as you specify the default factory, this works as intended. I didn't see any tests explicitly testing this so I added one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late reply, I think this looks good!
We still have to figure if we want to remove the default values from the schema or not 🤔 See #3517
But we can merge this either way 😊
@ppease is there any reason why this is draft? just double checking! |
@patrick91 This fix resolves my original issue. The only reason I made it a draft is I just wanted to make sure the fix made sense architecturally, and there wasn't a more preferred way to fix the issue. |
@ppease I think the fix is fine! 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have skipped reviewing this pull request. It looks like we've already reviewed this pull request.
@patrick91 Sounds good! Thanks for reviewing it! I went ahead and marked it as ready. |
@ppease thanks! looks like the tests all passed here, so I merged it Mypy sometimes does weird things locally with pydantic :D |
In this PR, I am adding support for having a pydantic model be a default value for a field on an input. Previously this would not generate the proper schema.
Description
My original fix was modifying code in
from_input_field
inside of the schema converter. This is the point where we are instantiating a graphql input type, and I was checking to see if the default value had a model dump method on it. This seems a little better since it is still within the pydantic specific code. The issue I'm trying to solve is that when we print the schema from withinprint_input_value
we useast_from_value
on the default value. We fall into theis_input_object_type
case and then we check if there is an object definition for the value. Ifis_input_object_type
evaluates toTrue
then we callstrawberry.asdict
. I think we'd want to do something similar with a pydantic instance. Today we fail the check and end up returningNone
which causes the default value to not be printed in the schema. What I'm doing in this MR is detecting if our default value is a pydantic instance, and if it is then I am storing the serialized value for the pydantic model as the default value.I'm curious to know if there is a better way to plug into the printing process because that feels more inline with what we are doing with dataclasses today. I could add code that I had previously into either
print_input_value
orast_from_value
, but that felt a little more hacky since we are putting pydantic specific stuff in higher level code.Types of Changes
Issues Fixed or Closed by This PR
This fixes #3285
Checklist