Skip to content

Commit

Permalink
Add definitions_path argument to __init__ for customization
Browse files Browse the repository at this point in the history
of resulting json schema, where schemas are placed
  • Loading branch information
alexander.bezgin committed Oct 26, 2023
1 parent 7446d3d commit 7ea9dab
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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", {})
Expand Down Expand Up @@ -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

0 comments on commit 7ea9dab

Please sign in to comment.