def __init__(self, title=..., desc=..., itype=..., size=..., *, default=..., nullable=False): keywords = {} if size is not Ellipsis: min_items, max_items = self._fetch_min_max(size) keywords['minItems'] = min_items keywords['maxItems'] = max_items if isinstance(itype, (list, tuple)): raise DefinitionError("Tuples are not supported by Untt") if itype is not Ellipsis: if itype not in builtin_json_type_mapping: raise DefinitionError(f"'{itype}' is not supported") # noqa ititle = ... if title is not Ellipsis: ititle = f'{title} item' # noqa keywords['items'] = annotation_to_untt_type( ititle, itype ).json_schema super().__init__('array', list, title=title, desc=desc, default=default, nullable=nullable, **keywords)
def test_annotation_to_untt_type(self): untt_type = annotation_to_untt_type('title', (float, 'description')) self.assertEqual(untt_type.json_schema, { 'type': 'number', 'title': 'title', 'description': 'description' })
def test_annotation_to_entity(self): class SomeOfMyEntities(Entity): pass entity_type = annotation_to_untt_type('my entity', SomeOfMyEntities) self.assertTrue(isinstance(entity_type, EntityType)) self.assertEqual(entity_type.json_schema, { '$ref': '#/definitions/SomeOfMyEntities', 'title': 'my entity' })
def test_annotation_to_enum(self): class SomeOfMyEnums(Enum): ONE = 1 TWO = 2 enum_type = annotation_to_untt_type('my enum', SomeOfMyEnums) self.assertTrue(isinstance(enum_type, EnumType)) self.assertEqual(enum_type.json_schema, { '$ref': '#/definitions/SomeOfMyEnums', 'title': 'my enum' })
def test_annotation_to_entity_nullable(self): class NullableEntity(Entity): pass entity_type = annotation_to_untt_type( 'my entity', (NullableEntity,) | Null ) self.assertTrue(isinstance(entity_type, EntityType)) self.assertTrue(entity_type.nullable) self.assertEqual(entity_type.json_schema, { 'oneOf': [ {'type': 'null'}, { '$ref': '#/definitions/NullableEntity', 'title': 'my entity' } ] })
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._untt_properties = {} if hasattr(self, '__annotations__'): for name in self.__annotations__: def_tuple = self.__annotations__[name] default = getattr(self, name, ...) if default is not Ellipsis: delattr(self, name) self._untt_properties[name] = annotation_to_untt_type( name, def_tuple, default=default ) json_schema = self._generate_json_schema() add_root_definition(self.__name__, json_schema) self.json_schema = { '$ref': f'#/definitions/{self.__name__}' # noqa }