Add support to use serialisation as mode for response models#41
Merged
Conversation
Reviewer's GuideAdd support for generating response schemas in serialization mode so Pydantic v2 computed/read-only fields appear correctly, and ensure request/params schemas still reflect only input fields. Flow diagram for using serialization mode in response ModelField generationflowchart LR
EndpointHandler --> RouterOperation
RouterOperation -->|create_field mode=validation| RequestModelField
RouterOperation -->|create_field mode=serialization| ReplyModelField
RouterOperation -->|create_field mode=serialization| PublishParamsField
subgraph Utils
create_field
end
RouterOperation --> create_field
create_field -->|PYDANTIC_V2 and mode set| ModelField
create_field -->|Pydantic v1 no mode| ModelField
ModelField --> AsyncapiSchemaGeneration
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/asyncapi/test_generation.py" line_range="416" />
<code_context>
assert schema["channels"]["natsapi.development.pub"]["publish"]["summary"] == "Pub"
+
+
+def test_computed_field_in_result_should_appear_in_response_schema():
+
+ class User(BaseModel):
</code_context>
<issue_to_address>
**issue (testing):** Guard this test so it only runs under Pydantic v2 where `@computed_field` is available and the `mode` parameter is supported.
Because this test relies on v2-only features, it will fail or be meaningless under Pydantic v1. Please either skip it when running with v1 (e.g., via a version check and `pytest.mark.skipif`) or move it into a v2-only test module so the test suite remains valid across both supported versions.
</issue_to_address>
### Comment 2
<location path="tests/asyncapi/test_generation.py" line_range="443" />
<code_context>
+ def get_user(app, params: CreateUserParams):
+ return {}
+
+ @router.publish("users.CREATED")
+ def user_created(app, user: User):
+ return {}
</code_context>
<issue_to_address>
**suggestion (testing):** Add an assertion that the `users.CREATED` publish payload schema also exposes `full_name` as readOnly.
This test only verifies the shared `User` schema and request model. It doesn’t confirm that the `users.CREATED` channel’s publish message actually uses that schema (and therefore exposes `full_name` as readOnly). Please add an assertion on `schema["channels"]["natsapi.development.v1.users.CREATED"]["publish"]["message"]["payload"]` (or the correct path) to ensure the publish payload uses the serialization view including `full_name`.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
b4aeff7 to
a2ca3bb
Compare
a2ca3bb to
da041b2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Specify mode when generating
ModelField. This adds support for calculated / read-only properties defined in Pydantic (see: Fields | Pydantic Docs).Summary by Sourcery
Support generation of response schemas using Pydantic serialization mode so computed/read-only fields are included correctly.
New Features:
Enhancements:
Tests: