1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
"""OpenAPI X-Model extension factories module"""
from dataclasses import make_dataclass
from pydoc import locate
from typing import Any
from typing import Dict
from typing import Iterable
from typing import Type
from jsonschema_path import SchemaPath
from openapi_core.extensions.models.types import Field
class DictFactory:
base_class = dict
def create(
self, schema: SchemaPath, fields: Iterable[Field]
) -> Type[Dict[Any, Any]]:
return self.base_class
class ModelFactory(DictFactory):
def create(
self,
schema: SchemaPath,
fields: Iterable[Field],
) -> Type[Any]:
name = schema.getkey("x-model")
if name is None:
return super().create(schema, fields)
return make_dataclass(name, fields, frozen=True)
class ModelPathFactory(ModelFactory):
def create(
self,
schema: SchemaPath,
fields: Iterable[Field],
) -> Any:
model_class_path = schema.getkey("x-model-path")
if model_class_path is None:
return super().create(schema, fields)
return locate(model_class_path)
|