def get_type_info(spec: Spec, schema: Dict[str, Any], config: Config ) -> TypeInfo: """ Get the type of a schema within a Swagger spec. :param spec: Bravado-core spec object :param schema: Schema dict :return: A TypeInfo for the schema. """ schema = spec.deref(schema) schema_type = get_type_from_schema(spec, schema) if schema_type == "array": type_info = _get_array_type_info(spec, schema, config) elif schema_type == "object": type_info = _get_object_type_info(spec, schema, config) elif schema_type in SWAGGER_PRIMITIVE_TYPES: type_info = _get_primitive_type_info(spec, schema, config) elif schema_type == "file": type_info = TypeInfo("typing.Any") elif schema_type is None: type_info = TypeInfo("typing.Any") else: warnings.warn(f"Unknown schema type: {schema_type!r}") type_info = TypeInfo("typing.Any") if schema.get("x-nullable", False): type_info = _wrap(type_info, "typing.Optional[{}]") return type_info
def _get_unmarshaling_method(swagger_spec, object_schema, is_nullable=True): # type: (Spec, JSONDict, bool) -> UnmarshalingMethod """ Determine the method needed to unmarshal values of a defined object_schema The returned method will accept a single positional parameter that represent the value to be unmarshaled. :param swagger_spec: Spec object :param object_schema: Schema of the object type # TODO: remove is_nullable support once https://github.com/Yelp/bravado-core/issues/335 is addressed :param is_nullable: Flag set to `True` if the current schema is nullable. The flag will be set to `True` if the schema is not required or `x-nullable` attribute is set to true by the "parent" schema """ object_schema = swagger_spec.deref(object_schema) null_decorator = _handle_null_value( swagger_spec=swagger_spec, object_schema=object_schema, is_nullable=is_nullable, ) object_type = get_type_from_schema(swagger_spec, object_schema) if object_type == 'array': return null_decorator( _unmarshaling_method_array(swagger_spec, object_schema)) elif object_type == 'file': # TODO: Type file is not a valid type. It is present to support parameter unmarshaling (move to bravado_core.param) # noqa: E501 return null_decorator( _unmarshaling_method_file(swagger_spec, object_schema)) elif object_type == 'object': return null_decorator( _unmarshaling_method_object(swagger_spec, object_schema)) elif object_type in SWAGGER_PRIMITIVES: return null_decorator( _unmarshaling_method_primitive_type(swagger_spec, object_schema)) elif object_type is None: return _no_op_unmarshaling else: return partial( _unknown_type_unmarshaling, object_type, )
def _marshaling_method_primitive_type(swagger_spec, object_schema): # type: (Spec, JSONDict) -> MarshalingMethod """ Determine the marshaling method needed for a schema of a primitive type. The method will be responsible for the identification of the eventual :class:`bravado_core.formatter.SwaggerFormat` transformation to apply. :param swagger_spec: Spec object :param object_schema: Schema of the primitive type """ format_name = schema.get_format(swagger_spec, object_schema) swagger_format = swagger_spec.get_format( format_name) if format_name is not None else None if swagger_format is not None: return partial( _marshal_primitive_type, get_type_from_schema(swagger_spec, object_schema), swagger_format, ) else: return _no_op_marshaling
def _get_marshaling_method(swagger_spec, object_schema, required=False): # type: (Spec, JSONDict, bool) -> MarshalingMethod """ Determine the method needed to marshal values of a defined object_schema The returned method will accept a single positional parameter that represent the value to be marshaled. :type swagger_spec: :class:`bravado_core.spec.Spec` :type object_schema: dict """ object_schema = swagger_spec.deref(object_schema) null_decorator = _handle_null_value( swagger_spec=swagger_spec, object_schema=object_schema, is_nullable=not required, ) object_type = get_type_from_schema(swagger_spec, object_schema) if object_type == 'array': return null_decorator( _marshaling_method_array(swagger_spec, object_schema)) elif object_type == 'file': return null_decorator( _marshaling_method_file(swagger_spec, object_schema)) elif object_type == 'object': return null_decorator( _marshaling_method_object(swagger_spec, object_schema)) elif object_type in SWAGGER_PRIMITIVES: return null_decorator( _marshaling_method_primitive_type(swagger_spec, object_schema)) elif object_type is None: return _no_op_marshaling else: return partial( _unknown_type_marshaling, object_type, )