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

fix: replace invalid characters in $ref field with underscore. #50

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jfschneider
Copy link

used the regex from pydantic.json_schema.GenerateJSONSchema.normalize_name to replace all characters that are invalid reference names.

@jfschneider jfschneider marked this pull request as ready for review November 8, 2024 09:28
@jfschneider jfschneider changed the title fix: replace invalid characters in $ref field with underscore. fixes #44 fix: replace invalid characters in $ref field with underscore. Nov 8, 2024
@mike-oakley
Copy link
Owner

Thanks @jfschneider! Would you mind adding a comment explaining the logic too, and a link to the pydantic source? Would also be great if you could add a test or two to cover this case 🙏🏼

@jfschneider
Copy link
Author

Hi, sure, I can provide a link and an explanation. I also just noticed that there is a duplicate of the util.py in the v3_0 folder, so I probably should duplicate the fix there, too, and then put the tests in tests/util/test_utily.py and tests/v3_0/test_utily.py?

@jfschneider
Copy link
Author

OK, that was a bit premature. I had a test that works, but just for either pydantic v2 orv1 (basically copied a test in util.py and just used a generic as response), not v1, which seems to use a different method for generic models:
v2: class GenericResponse(BaseModel, Generic[DataT])
vs
v1: class GenericResponse(GenericModel, Generic[DataT])
I admit I am a bit out of my depth here, I have no idea how I can include both variants in the test, except maybe some "if PYDANTIC_V2" conditions...?

@mike-oakley
Copy link
Owner

Yep exactly that, you can use the PYDANTIC_V2 flag to constrain certain tests to pydantic V1 or v2 if that helps seperate concerns/fixtures like here

@jfschneider
Copy link
Author

Sooo, after a lot of trial and error, I managed to cobble up full tests for pydantic v1/v2. The tox run still fails, but not in my tests ("tests/util/test_pydantic_field.py:72: AssertionError"), and that also fails in a clean clone of the original repo, so I gather it has nothing to do with my changes...

I briefly thought about fixing the _construct_ref_obj function by instantiating the Pydantic GenerateJSONschema-class and calling the normalize_name-method, but decided against it as those classes are again different in Pydantic V1/V2 which would've required some further if-else and bloated code, so I just copied the regex from the normalize_name method, as I think it is rather unlikely to change often. It just replaces invalid characters for refnames with an underscore.
https://github.com/pydantic/pydantic/blob/aee6057378ccfec02126bf9c984a9b6d6b411777/pydantic/json_schema.py#L2031

@mike-oakley
Copy link
Owner

mike-oakley commented Dec 3, 2024

Ah yes, looks like the break was introduced in a recent Pydantic update (pydantic/pydantic#10692) - i'll address this separately 👍🏼

Could you add the comment with source/explanation however for the regex logic? 🙏🏼

@mike-oakley
Copy link
Owner

Test is fixed in #52 - if you rebase to main you should pick it up 👍🏼

@jfschneider
Copy link
Author

Ah yes, looks like the break was introduced in a recent Pydantic update (pydantic/pydantic#10692) - i'll address this separately 👍🏼

Could you add the comment with source/explanation however for the regex logic? 🙏🏼

Do you mean add a comment in the function itself?

@mike-oakley
Copy link
Owner

mike-oakley commented Dec 3, 2024 via email

@jfschneider
Copy link
Author

OK, added a docstring with an explanation. Tests were fine now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants