From 7ea9dab2ce131ab1cb607c9c76adefbc07a7a629 Mon Sep 17 00:00:00 2001 From: "alexander.bezgin" Date: Thu, 26 Oct 2023 11:23:58 +0700 Subject: [PATCH] Add definitions_path argument to __init__ for customization of resulting json schema, where schemas are placed --- marshmallow_jsonschema/base.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/marshmallow_jsonschema/base.py b/marshmallow_jsonschema/base.py index 1096363..80ca55e 100644 --- a/marshmallow_jsonschema/base.py +++ b/marshmallow_jsonschema/base.py @@ -149,6 +149,7 @@ def __init__(self, *args, **kwargs) -> None: self._nested_schema_classes: typing.Dict[str, typing.Dict[str, typing.Any]] = {} self.nested = kwargs.pop("nested", False) self.props_ordered = kwargs.pop("props_ordered", False) + self.definitions_path = kwargs.pop("definitions_path", "definitions") setattr(self.opts, "ordered", self.props_ordered) super().__init__(*args, **kwargs) @@ -326,7 +327,7 @@ def _from_nested_schema(self, obj, field): self._nested_schema_classes.update(wrapped_nested._nested_schema_classes) # and the schema is just a reference to the def - schema = {"type": "object", "$ref": "#/definitions/{}".format(name)} + schema = {"type": "object", "$ref": "#/{}/{}".format(self.definitions_path, name)} # NOTE: doubled up to maintain backwards compatibility metadata = field.metadata.get("metadata", {}) @@ -367,7 +368,7 @@ def wrap(self, data, **_) -> typing.Dict[str, typing.Any]: self._nested_schema_classes[name] = data root = { "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": self._nested_schema_classes, - "$ref": "#/definitions/{name}".format(name=name), + self.definitions_path: self._nested_schema_classes, + "$ref": "#/{path}/{name}".format(path=self.definitions_path, name=name), } return root