-
Notifications
You must be signed in to change notification settings - Fork 224
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
Change schema generation for PydanticObjectId #1099
base: main
Are you sure you want to change the base?
Conversation
Okay, tests raised error... I will try to fix it
|
e07cb7e
to
a2605bd
Compare
Now it should work (some tests failed but they no related to the issue) |
0f1ce78
to
7cc114f
Compare
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.
Hi @dantetemplar, thank you for the PR.
However, please note that this PR doesn't add much as the OpenAPI schema generation fix e.g. for PydanticObjectId is already merged with #1080. This PR simply adds a new type definition to the OpenAPI schema to not reference "string" everywhere but a custom defined type.
Also, please if you could change the PR title to match this description, "Add custom PydanticObjectId type to JSON schema" or something like that.
python_schema=core_schema.no_info_plain_validator_function( | ||
cls._validate | ||
), | ||
json_schema=core_schema.no_info_after_validator_function( |
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.
Previously, this was: no_info_plain_validator_function. Why was it now changed to no_info_after_validator_function?
Could someone please explain what is the difference here.
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.
Related topic:
https://docs.pydantic.dev/latest/concepts/validators/
Plain validator works as mode="before" (actually as mode="plain") while after validator as mode="after".
Dunno why it actually better and fixes the error.
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.
Use of no_info_plain_validator_function will fail the test.
============================================================================== short test summary info ===============================================================================
FAILED tests/fastapi/test_openapi_schema_generation.py::test_openapi_schema_generation - pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.PlainValidatorFunctionSchema ({'type': 'no-info', 'function': <bound method PydanticOb...
============================================================================ 1 failed, 6 passed in 2.83s =============================================================================
While with no_info_after_validator_function will pass.
def no_info_plain_validator_function(
function: NoInfoValidatorFunction,
# < no schema parameter that actually defines the schema
*,
ref: str | None = None,
json_schema_input_schema: CoreSchema | None = None,
metadata: Dict[str, Any] | None = None,
serialization: SerSchema | None = None,
) -> PlainValidatorFunctionSchema:
def no_info_after_validator_function(
function: NoInfoValidatorFunction,
schema: CoreSchema, # < and here we have it, and it will passed to the _dict_not_none helper
*,
ref: str | None = None,
json_schema_input_schema: CoreSchema | None = None,
metadata: Dict[str, Any] | None = None,
serialization: SerSchema | None = None,
) -> AfterValidatorFunctionSchema:
return _dict_not_none(
type='function-plain',
function={'type': 'no-info', 'function': function},
ref=ref,
json_schema_input_schema=json_schema_input_schema,
metadata=metadata,
serialization=serialization,
)
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.
Okay, it seems then that the Schema change breaks something.
I am not sure off the top of my head, but this needs to be tested in more detail as the _validate() method here expected a string ObjectId, which it then converts.
I would hold off from merging until this is verified how it behaves.
Unfortunately, I can't test this out as I won't have access to a PC for two weeks.
Would be good if you included that FastAPI route params PydanticObjectId test here...
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.
So, I need to include to that PR more tests?
I'm a little tired of amend & force pushing, can this be done with different commits (as mention here)?
Nope, seems issue with {id} PydanticObjectId in path parameters stays even with this fix (I have suggested pr with test). So, current schema generation is not proper. |
Sorry, but this is simply not the case as you've 'proven' in your other PR #1100 which has a test for this very case and is passing without this commit that has this supposed fix... beanie/tests/fastapi/routes.py Line 13 in 687b4a7
|
7cc114f
to
5992387
Compare
PR #1100 fails on my local setup because I've not pulled #1095 changes. Sorry for hassle. Okay, this PR tried to fix yet not reported issue (but seems to be fixed by #1095) which may had name "[BUG] pydantic.errors.PydanticInvalidForJsonSchema occured when APIRouter's has PydanticObjectId in path parameter" with beanie=1.28 and Pydantic 2.10. Although, people identify the very same issue in existing #1087: #1087 (comment) |
5992387
to
7f96679
Compare
I've tried to rebase my branch onto #1095, Let's see what happens with CI |
Interesting, GitHub Runner hits the rate limit during build stage of dockerized action: |
All tests now pass, @dantetemplar. |
Sorry, @dantetemplar, I won't be able to test this in detail yet, so can't approve. |
Fixes #1087