def replace_field_type(field: ModelField): if isinstance(field.type_, type) and issubclass(field.type_, ObjectId): if field.name == 'id': field.required = True field.type_ = str elif isinstance(field.type_, type) and issubclass(field.type_, Enum): field.field_info.extra['x-enum-varnames'] = field.type_._member_names_ elif getattr(field.type_, '__origin__', None) and ObjectId in field.type_.__args__: field.type_.__args__ = tuple( map(lambda v: str if v is ObjectId else v, field.type_.__args__)) if field.sub_fields: for f in field.sub_fields: replace_field_type(f)
def create_cloned_field(field: ModelField) -> ModelField: original_type = field.type_ if is_dataclass(original_type) and hasattr(original_type, "__pydantic_model__"): original_type = original_type.__pydantic_model__ # type: ignore use_type = original_type if lenient_issubclass(original_type, BaseModel): original_type = cast(Type[BaseModel], original_type) use_type = create_model( original_type.__name__, __config__=original_type.__config__ ) for f in original_type.__fields__.values(): use_type.__fields__[f.name] = f use_type.__validators__ = original_type.__validators__ if PYDANTIC_1: new_field = ModelField( name=field.name, type_=use_type, class_validators={}, default=None, required=False, model_config=BaseConfig, field_info=FieldInfo(None), ) else: # pragma: nocover new_field = ModelField( # type: ignore name=field.name, type_=use_type, class_validators={}, default=None, required=False, model_config=BaseConfig, schema=FieldInfo(None), ) new_field.has_alias = field.has_alias new_field.alias = field.alias new_field.class_validators = field.class_validators new_field.default = field.default new_field.required = field.required new_field.model_config = field.model_config if PYDANTIC_1: new_field.field_info = field.field_info else: # pragma: nocover new_field.schema = field.schema # type: ignore new_field.allow_none = field.allow_none new_field.validate_always = field.validate_always if field.sub_fields: new_field.sub_fields = [ create_cloned_field(sub_field) for sub_field in field.sub_fields ] if field.key_field: new_field.key_field = create_cloned_field(field.key_field) new_field.validators = field.validators if PYDANTIC_1: new_field.pre_validators = field.pre_validators new_field.post_validators = field.post_validators else: # pragma: nocover new_field.whole_pre_validators = field.whole_pre_validators # type: ignore new_field.whole_post_validators = field.whole_post_validators # type: ignore new_field.parse_json = field.parse_json new_field.shape = field.shape try: new_field.populate_validators() except AttributeError: # pragma: nocover # TODO: remove when removing support for Pydantic < 1.0.0 new_field._populate_validators() # type: ignore return new_field
def get_param_field( *, param: inspect.Parameter, param_name: str, default_field_info: Type[params.Param] = params.Param, force_type: params.ParamTypes = None, ignore_default: bool = False, ) -> ModelField: default_value = Required had_schema = False if not param.default == param.empty and ignore_default is False: default_value = param.default if isinstance(default_value, FieldInfo): had_schema = True field_info = default_value default_value = field_info.default if (isinstance(field_info, params.Param) and getattr(field_info, "in_", None) is None): field_info.in_ = default_field_info.in_ if force_type: field_info.in_ = force_type # type: ignore else: field_info = default_field_info(default_value) required = default_value == Required annotation: Any = Any if not param.annotation == param.empty: annotation = param.annotation annotation = get_annotation_from_field_info(annotation, field_info, param_name) if not field_info.alias and getattr(field_info, "convert_underscores", None): alias = param.name.replace("_", "-") else: alias = field_info.alias or param.name if PYDANTIC_1: field = ModelField( name=param.name, type_=annotation, default=None if required else default_value, alias=alias, required=required, model_config=BaseConfig, class_validators={}, field_info=field_info, ) # TODO: remove when removing support for Pydantic < 1.2.0 field.required = required else: # pragma: nocover field = ModelField( # type: ignore name=param.name, type_=annotation, default=None if required else default_value, alias=alias, required=required, model_config=BaseConfig, class_validators={}, schema=field_info, ) field.required = required if not had_schema and not is_scalar_field(field=field): if PYDANTIC_1: field.field_info = params.Body(field_info.default) else: field.schema = params.Body( field_info.default) # type: ignore # pragma: nocover return field