Skip to content

Commit

Permalink
tests for schema naming, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nj-vs-vh committed Feb 18, 2021
1 parent 7b48e75 commit 1daeed9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
9 changes: 7 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,16 @@ You can use resulting schema everywhere marshmallow.Schema can be used, e.g.
class MyOtherSchema(m.Schema):
items = f.List(f.Nested(MyUberSchema))
Building OneOfSchema with a decorator
------------------------------------

When creating "one of" schemas with many options it can become cumbersome to maintain `type_schemas` dict manually.
In this case you can leave it empty at first and register member schemas to your OneOfSchema with a decorator:
In this case you can leave it empty at first and register member schemas to your OneOfSchema with the following decorator:

.. code:: python
class MyUberSchema(OneOfSchema):
pass
type_schemas = {}
@MyUberSchema.register_one_of
Expand All @@ -143,6 +146,8 @@ By default schemas are named with class name with removed "Schema" suffix, which
class MyUberSchema(OneOfSchema):
type_schemas = {}
def schema_name(schema_class):
return schema_class._model_class.__name__
Expand Down
6 changes: 5 additions & 1 deletion marshmallow_oneofschema/one_of_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def schema_name(schema_class):
"""Key used for schema classes in type_schemas dict. The default is
schema class name stripped of the word 'Schema', if present.
"""
return schema_class.__name__.replace("Schema", "")
schema_class_name = schema_class.__name__
if schema_class_name.endswith("Schema"):
schema_class_name = schema_class_name[: -len("Schema")]
return schema_class_name

@classmethod
def register_one_of(cls, schema_class):
Expand All @@ -199,3 +202,4 @@ def register_one_of(cls, schema_class):
>>> ...
"""
cls.type_schemas[cls.schema_name(schema_class)] = schema_class
return schema_class
25 changes: 20 additions & 5 deletions tests/test_one_of_schema_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
import marshmallow.fields as f
from marshmallow_oneofschema import OneOfSchema

from .test_one_of_schema import Foo, Bar, Baz
from test_one_of_schema import Foo, Bar, Baz


class MySchemaWithDefaultNames(OneOfSchema):
pass
type_schemas = {}


class MySchemaWithCustomNames(OneOfSchema):
type_schemas = {}

counter = 0
known_types = [Foo, Bar, Baz]

def get_obj_type(self, obj):
return self.known_classes.index(obj.__class__)

def schema_name(self, schema_class):
self.counter += 1
return str(self.counter - 1)
@classmethod
def schema_name(cls, schema_class):
cls.counter += 1
return str(cls.counter - 1)


@MySchemaWithCustomNames.register_one_of
Expand Down Expand Up @@ -77,3 +80,15 @@ def test_schemas_building_with_register_one_of():
MySchemaWithCustomNames.type_schemas
== MyVerboseSchemaWithCustomNames.type_schemas
)


def test_default_schema_naming():
class SomeObjectSchema:
pass

assert OneOfSchema.schema_name(SomeObjectSchema) == "SomeObject"

class AnyOtherSchemaClass:
pass

assert OneOfSchema.schema_name(AnyOtherSchemaClass) == "AnyOtherSchemaClass"

0 comments on commit 1daeed9

Please sign in to comment.