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
Allowing overriding of model names in get_model_name_map
#6304
Comments
@aalexsmithh thanks for the suggestion! It looks like a legitimate feature request to me 👍🏻. Please, notice that Pydantic V1 won't receive new features anymore. For now, I'd suggest using a custom class attribute, i.e. I think Also, it seems like |
Hey, do you need help with this? I want to start contributing, especially with Rust code. |
@romulocollopy Sure, go ahead! |
@lig, could you take a look at #6814 and see if it is moving in the right direction? If so, I will add documentation to the change. This solution will change the In case there is a conflict in the set names, |
I think the solution to this in pydantic v2 is to override We've considered adding functionality to allow types to more directly specify what the name/fallback-choices should be, but haven't settled on a preferred implementation. Would be happy to consider a PR modifying Worst case, if you are using a framework like FastAPI and can't modify the import pydantic
from pydantic.json_schema import GenerateJsonSchema
...
class MyGenerateJsonSchema(GenerateJsonSchema):
...
pydantic.json_schema.GenerateJsonSchema = MyGenerateJsonSchema |
It's not clear to me how a user is meant to override class MyGenerateJsonSchema(GenerateJsonSchema):
def get_defs_ref(self, core_mode_ref: CoreModeRef) -> DefsRef:
my_def = ... # insert complicated string parsing logic here
full_def = super().get_defs_ref(core_mode_ref)
self._prioritized_defsref_choices[full_def].insert(0, my_def)
return full_def Putting aside how a user might customize this, it seems that a lot could be done to reduce the need to customize. By default the choices jump from |
Initial Checks
Description
I'd like to be able override the model names generated by
get_model_name_map()
.In the case of conflicts, often the verbose version (ref) of the model name is excessively verbose.
I'm using FastAPI to generate an OpenAPI spec of the app I'm building, which ultimately is being used to generate a client library for my service. It uses pydantic's
get_model_name_map
to generate model names to create the json spec. Because of a number of models have conflicting names, I end up with generated model names that are frustratingly verbose.service/clients/clientA.py
service/clients/clientB.py
service/main.py
Because of the collision on the
User
model, whenget_model_name_map()
is run, I end up with model names likeservice__clients__clientA__User
andservice__clients__clientB__User
. What I'd like to have isClientAUser
andClientBUser
.I think it would be useful to allow overrides of this long model name to allow user-defined model titles when there are collisions.
Imagine perhaps a configuration like this, where the generated names are taken directly from the model config:
service/clients/clientA.py
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeSelected Assignee: @lig
The text was updated successfully, but these errors were encountered: