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 #1643 : Fully qualified model name + Unique ShortID generation for DTO Model to avoid collision issues. #1649
Conversation
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 @v3ss0n - thanks for taking this one on!
When we were talking in discord, I was only referring to the msgspec backend type, however there is also the pydantic backend, so perhaps we can create a utility method on AbstractDTOBackend
that accepts unique_name
as an argument and returns that with the short uid appended to it. We can then call that method from within create_transfer_model_type
on either backend implementation.
Also, tests please:)
Thanks again!
Thanks , i am happy to help. i saw other back-end uses it , i was about to ask tha Should i write test to see if the method is generating unique ids? |
@pytest.mark.parametrize("backend_type", [MsgspecDTOBackend, PydanticDTOBackend])
def test_backend_model_name_uniqueness(backend_type: type[AbstractDTOBackend], backend_context: BackendContext) -> None:
backend = backend_type(backend_context)
unique_names: set = set()
fd: FieldDefinitionsType = FieldDefinition(
name="a",
default=Empty,
parsed_type=ParsedType(int),
default_factory=None,
dto_field=None,
unique_model_name="some_module.SomeModel",
)
for i in range(100):
model_class = backend.create_transfer_model_type("some_module.SomeModel", fd)
model_name = model_class.unique_name
assert model_name not in unique_names
unique_names.add(model_name) I am trying like this , i don't know how to get unique_name back from the transfer_model . |
does |
sync with main , resolved conflict at poetry.lock , its ready for review. |
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.
Almost there, just need to get that test passing.
Co-authored-by: Peter Schutt <peter.github@proton.me>
Just tested this on the example app - the first 9 characters of each of those random strings is identical across all of the models which I think is due to the first part of uuid1 being a timestamp. This is why you needed to increase the size of the random string in 8e025cb - because the first chars of a timestamp are too slow-moving to be reliably different. I think we'd be better of just using something like Sorry I didn't pick this up earlier! |
Why not just use |
Perfect. |
I was thinking that too , with uuid1 looks like we need whole of it. it is base off on timestamp so the first chars are not much moving within a few minutes. |
|
Probably better not to. Try |
thanks , this look better , going to raise to 8 chars to increase randomness. In [30]: secrets.token_hex(8)
Out[30]: '342e6bed9da5e8a5' |
changed to : secrets.token_hex(8) , tested by incresing loop to 100k , passed locally. |
@all-contributors add @v3ss0n for code! |
I've put up a pull request to add @v3ss0n! 🎉 |
Thanks a lot!! |
Pull Request Checklist
Description
Bug fix for #1643 - where it cause duplicated DTOModel names collision. Attempted to fixed by adding a 12 char unique id to fully qualified name of model.
This PR adds two new test cases to ensure that the generated model names and schema names are unique
Close Issue(s)
Fixes #1643