Replies: 1 comment 1 reply
-
what you referring to is probably well this is more a pydantic question - but as far as I know there is no way - but maybe it worth asking at pydantic repo as for django-ninja you can hack around from django_ninja import NinjaAPI
import copy
class CustomNinjaAPI(NinjaAPI):
def __init__(self, *args, rename_config=None, **kwargs):
super().__init__(*args, **kwargs)
self.rename_config = rename_config or {}
def get_openapi_schema(self, *args, **kwargs):
# Call the super method to get the initial schema
schema = super().get_openapi_schema(*args, **kwargs)
# Make a copy of the schema to avoid mutating the original schema
schema = copy.deepcopy(schema)
# Apply renaming based on configuration
if "components" in schema and "$defs" in schema["components"]:
new_defs = {}
for key, value in schema["components"]["$defs"].items():
new_key = self.rename_config.get(key, key) # Rename based on config
new_defs[new_key] = value
schema["components"]["$defs"] = new_defs
# Update `$ref` references
for path, obj in self._iter_refs(schema):
if isinstance(obj, str) and obj.startswith("#/components/$defs/"):
original_key = obj.split("/")[-1]
if original_key in self.rename_config:
new_key = self.rename_config[original_key]
path[-1] = obj.replace(original_key, new_key)
return schema
def _iter_refs(self, obj, path=None):
"""Helper function to iterate over $refs in the schema."""
if path is None:
path = []
if isinstance(obj, dict):
for key, value in obj.items():
yield from self._iter_refs(value, path + [key])
elif isinstance(obj, list):
for i, item in enumerate(obj):
yield from self._iter_refs(item, path + [i])
else:
if path and path[-1] == "$ref":
yield path, obj Note: this code is generated with ChatGPT please try and let me know if that works |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm looking for a way to customize the name of the models in the generated OpenAPI specification. It only uses the model's name and I would like to use a custom one.
So far, I was only able to set a custom
title
in the specification, but not to change the model's name.Beta Was this translation helpful? Give feedback.
All reactions