1+ from typing import Any
2+
13from marshmallow import fields
24from marshmallow import INCLUDE
35from marshmallow import Schema
46from marshmallow import validate
57from marshmallow import ValidationError
8+ from marshmallow .fields import Boolean
9+ from marshmallow .fields import Field
10+ from marshmallow .fields import Nested
611
712from .custom_fields import AllOf
813from .custom_fields import AnyOf
@@ -20,6 +25,10 @@ def _validate(self, value):
2025 raise ValidationError ('Invalid value' )
2126
2227
28+ class HashmapField (fields .Field ):
29+ pass
30+
31+
2332FIELDS_VIA_TYPES = {
2433 'boolean' : fields .Boolean ,
2534 'number' : fields .Float ,
@@ -47,12 +56,16 @@ class Meta:
4756
4857
4958def _make_object_field (schema : dict , as_nested : bool = True ) -> fields .Nested | type :
50- if schema .get ('properties' ) is None and schema ['additionalProperties' ].get ('oneOf' ):
51- return HashmapSchema
5259
5360 fields_obj = {}
5461 for field_name , field_schema in schema ['properties' ].items ():
55- if field_schema ['type' ] == 'object' :
62+ if (
63+ field_schema .get ('additionalProperties' )
64+ and isinstance (field_schema ['additionalProperties' ], dict )
65+ and field_schema ['additionalProperties' ].get ('oneOf' )
66+ ):
67+ field = HashmapField ()
68+ elif field_schema ['type' ] == 'object' :
5669 field = make_marshmallow_schema (field_schema , as_nested = True )
5770 else :
5871 field = make_marshmallow_schema (field_schema )
@@ -107,7 +120,9 @@ def _make_field_validators(schema: dict) -> list[validate.Validator]:
107120 return validators
108121
109122
110- def make_marshmallow_schema (schema : dict , as_nested : bool = False ) -> fields .Field :
123+ def make_marshmallow_schema (
124+ schema : dict , as_nested : bool = False
125+ ) -> type [HashmapSchema ] | Field | Nested | type | Boolean | Any :
111126 if 'nullable' in schema and schema .get ('type' , ...) is ...:
112127 field = FIELDS_VIA_TYPES ['boolean' ]()
113128 elif 'allOf' in schema :
@@ -118,6 +133,12 @@ def make_marshmallow_schema(schema: dict, as_nested: bool = False) -> fields.Fie
118133 field = _make_multiple_field (schema ['oneOf' ], 'oneOf' )
119134 elif schema .get ('format' ):
120135 field = FIELDS_VIA_FORMATS [schema ['format' ]]()
136+ elif (
137+ schema .get ('properties' ) is None
138+ and schema ['type' ] == 'object'
139+ and schema ['additionalProperties' ].get ('oneOf' )
140+ ):
141+ field = HashmapSchema
121142 elif schema ['type' ] == 'object' :
122143 field = _make_object_field (schema , as_nested = as_nested )
123144 elif schema ['type' ] == 'array' :
0 commit comments