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

Refactor the get_model_key util function #302

Merged
merged 1 commit into from
Apr 22, 2023

Conversation

AndreasBBS
Copy link
Contributor

@AndreasBBS AndreasBBS commented Apr 21, 2023

Following the discussion in #300 this refactor moves the function get_model_key() from utils.py to BasePlugin as a staticmethod.

This will allow custom plugin implementations to override the way the model key is generated as described by @kemingy here.

The following example describes how to create a custom flask plugin that does not append the module hash to the model name:

class YourFlaskPlugin(FlaskPlugin):
    def get_model_key(model: ModelType) -> str:
        return f"{model.__name__}"

spec = SpecTree(backend=YourFlaskPlugin)

EDIT:

After lots of attempts to refactor through the staticmethod on the Base Plugin and continuously running into cyclical imports I decided to change course in the implementation. I decided to go with the good old Strategy pattern. Here's the new usage guide:

spec = SpecTree(naming_strategy=lambda model: f"{model.__name__}")

@AndreasBBS AndreasBBS force-pushed the refactor/get_model_key branch 8 times, most recently from 88e9c3c to 4c18861 Compare April 21, 2023 19:47
spectree/response.py Fixed Show fixed Hide fixed
spectree/utils.py Fixed Show fixed Hide fixed
@AndreasBBS AndreasBBS force-pushed the refactor/get_model_key branch 2 times, most recently from 88ff6d0 to 6925086 Compare April 21, 2023 20:34
The import statements in the _types.py file were too long and difficult to read. Line breaks were added to improve readability.

🔀 refactor(response.py): add naming_strategy parameter to generate_spec method
The generate_spec method in the Response class now accepts a naming_strategy parameter. This allows the user to specify a custom naming strategy for the model names in the response spec.

🔀 refactor(spec.py): add naming_strategy parameter to __init__ and parse_model methods
The __init__ and parse_model methods in the SpecTree class now accept a naming_strategy parameter. This allows the user to specify a custom naming strategy for the model names in the OpenAPI spec.

🔀 refactor(utils.py): add naming_strategy parameter to get_model_schema and parse_resp functions
The get_model_schema and parse_resp functions in the utils.py file now accept a naming_strategy parameter. This allows the user to specify a
@AndreasBBS
Copy link
Contributor Author

AndreasBBS commented Apr 21, 2023

After lots of attempts to refactor through the staticmethod on the Base Plugin and continuously running into cyclical imports I decided to change course in the implementation.
I decided to go with the good old Strategy pattern. Here's the new usage guide:

spec = SpecTree(naming_strategy=lambda model: f"{model.__name__}")

Copy link
Member

@kemingy kemingy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! 🎉

@kemingy kemingy added this pull request to the merge queue Apr 22, 2023
@kemingy kemingy linked an issue Apr 22, 2023 that may be closed by this pull request
@kemingy kemingy removed this pull request from the merge queue due to a manual request Apr 22, 2023
@kemingy kemingy merged commit d41dea0 into 0b01001001:master Apr 22, 2023
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.

Key appended to Schema Component
2 participants