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

give more context to custom schemas #165

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def _get_python_type(self, field):
def _get_schema_for_field(self, obj, field):
"""Get schema and validators for field."""
if hasattr(field, "_jsonschema_type_mapping"):
schema = field._jsonschema_type_mapping()
schema = field._jsonschema_type_mapping(self, obj)
Copy link
Owner

Choose a reason for hiding this comment

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

Hmm, the problem with this is that it would break backward compatibility with custom type support as currently documented:

https://github.com/fuhrysteve/marshmallow-jsonschema#custom-type-support

Is there a way we can achieve what your trying to do without breaking backward compatibility?

Copy link
Author

Choose a reason for hiding this comment

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

hmm yeah, i think a part of the difficulty here is actually the kind of ad-hoc nature of jsonschema_type_mapping, where there's no explicit definition of an interface so we can't just update some base definition like

def _jsonschema_type_mapping(self, json_schema = None, obj = None):
  ...

to make this easily work for everyone.

I think similar to how you're using reflection to check if that _jsonschema_type_mapping even exists, I can use inspect to count the number of arguments and only pass the context in if the argument count supports it. This would preserve existing behavior but does make this interface a bit more loosey than it already is.

I think I'm ok with that. You could argue a proper solution requires a revamp of how this functionality is supported anyhow, so the best we can do atm is just to bolt more on to what was bolted on.

I'll make an update, and also be sure to update the README this time.

elif "_jsonschema_type_mapping" in field.metadata:
schema = field.metadata["_jsonschema_type_mapping"]
else:
Expand Down
25 changes: 24 additions & 1 deletion tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ class UserSchema(Schema):

def test_unknown_typed_field():
class Colour(fields.Field):
def _jsonschema_type_mapping(self):
def _jsonschema_type_mapping(self, json_schema, obj):
return {"type": "string"}

def _serialize(self, value, attr, obj):
Expand Down Expand Up @@ -744,3 +744,26 @@ class TestSchema(Schema):
assert (
len(data["definitions"]["TestSchema"]["properties"]["union_prop"]["anyOf"]) == 3
)

def test_recursive_custom_field():
class NoOpWrapper(fields.Field):
def __init__(self, field):
self.field = field
super().__init__()

def _jsonschema_type_mapping(self, json_schema, obj):
field_schema = json_schema._get_schema_for_field(obj, self.field)
return field_schema

class ContrivedSchema(Schema):
recursive = NoOpWrapper(
fields.Nested("ContrivedSchema"),
)

schema = ContrivedSchema()
dumped = validate_and_dump(schema)

assert dumped["definitions"]["ContrivedSchema"]["properties"]["recursive"] == {
"type": "object",
"$ref": "#/definitions/ContrivedSchema"
}