0b01001001/spectree

[feature] easier way to override `get_model_key()`

yedpodtrzitko opened this issue · 3 comments

Hi,
the function utils.get_model_key is used for generating model IDs + their references in openapi.json. We try to use this as a source for generating matching models on a frontend (via @rtk-query/codegen-openapi ), but the model IDs are prefixed with sha1 hash (which can start with a number), and the mentioned tool cant handle that.

I overrided SpecTree._generate_spec() where I manually inject a prefix everywhere, but that's quite inelegant, and the generated model names are unpleasant.

Easy way out of this would be having get_model_key() defined in a way which could be overriden easily. Not sure how exactly that would be done, so I'm opening this issue for now to hear some feedback if I'm not missing something obvious here.

Thanks.

Thanks for your feedback.

If I understand this correctly, there are two ways to solve it:

  1. use another way to generate the hash to avoid starting with a number
  2. provide a way to easily overridden, for example, passing a function when init SpecTree

The reason why I need the hash function is that the user may define multiple Models in different Python files with the same name.

I would prefer the 1st one.

If I understand this correctly, there are two ways to solve it

yes

  1. provide a way to easily overridden, for example, passing a function when init SpecTree

that's what I was briefly looking into later on, and that would mean the (potentially custom) function get_model_key would need to be passed around in a few places, but it would give the user higher flexibility to generate the model key as they wish. I can understand the rationale for having the prefix, but it feels this scenario happens rarely (or I might be biased from scope of our projects where it does not happen... yet).

  1. use another way to generate the hash to avoid starting with a number

this seems like an easier change to make. Thinking about it, it would be enough to have the sha1 hash as a suffix instead of prefix, which would solve a few problems at the same time, even though they are kinda' minor:

  • no number at the start of the model's name (which causes the issue mentioned above)
  • generated model name start with the actual model name instead of random prefix, so it makes it eventually easier to type with autocomplete.
  1. use another way to generate the hash to avoid starting with a number

this seems like an easier change to make. Thinking about it, it would be enough to have the sha1 hash as a suffix instead of prefix, which would solve a few problems at the same time, even though they are kinda' minor:

This looks good to me. Feel free to create a pull request for this change.