def __init__(self, *args, **kwargs): self.validator = kwargs.pop('validator', None) self.metadata = kwargs.pop('metadata', {}) super(Argument, self).__init__(*args, **kwargs) self.choices = list(self.choices) # Use a list instead of tuple for easier management self.metadata[api.DocumentationMetadata.DEPRECATED] = kwargs.pop('deprecated', False) if eu.is_enum_type(self.type): enum_id = ah.enum_id(self.type) self.help = "{}: {}".format('Dynamic Enum' if eu.is_dynamic_enum(self.type) else 'Enum', enum_id) # Set the choices if the argument type is an Enum and explicit choices were not provided. # Note: Choices are validated after type conversion occurs. if not self.choices and not eu.is_dynamic_enum(self.type): self.choices = self.type.keys() if self.choices: # Store the external Enum identifier strings for Swagger documentation self.metadata[api.DocumentationMetadata.ENUM_CHOICES] = ah.enum_choices_for_keys(self.type, *self.choices) # None is a valid choice if the argument isn't required and it appears in the request if not self.required and not self.store_missing: self.choices.append(None) elif self.type is UnicodeHTML: self.metadata[api.DocumentationMetadata.HAS_HTML] = True elif self.type is JSONObject: self.metadata[api.DocumentationMetadata.HAS_JSON] = True
def convert(self, value, op): if eu.is_enum_type(self.type): # Fetch the `EnumItem` key for this `EnumItem` identifier converted_value = ah.enum_item_key(self.type, value) if value else None elif self.type is bool: # Convert truthy or falsey string values to a bool converted_value = vv.Boolean().to_python(value) else: # Otherwise, perform the default type conversion converted_value = super(Argument, self).convert(value, op) if isinstance(converted_value, (str, unicode)): if converted_value.strip() == '': converted_value = None # Raise an error if a required argument has no value post-conversion if self.required and converted_value is None: if isinstance(self.location, (str, unicode)): error_msg = u"Missing required parameter {0} in {1}".format( self.name, reqparse._friendly_location.get(self.location, self.location)) else: friendly_locations = [reqparse._friendly_location.get(loc, loc) for loc in self.location] error_msg = u"Missing required parameter {0} in {1}".format( self.name, ' or '.join(friendly_locations)) raise ValueError(error_msg) if hasattr(self.validator, '__call__'): self.validator(converted_value) return converted_value
def parser_to_params(parser): params = swagger.__parser_to_params(parser) for arg in parser.args: if eu.is_enum_type(arg.type) and arg.name in params: params[arg.name][CustomSwaggerAttribute.ENUM_TYPE] = ah.enum_id(arg.type) # Only include the enum choices in the Swagger param if there are values, # we want the documentation to show a string input box otherwise. if arg.metadata.get(api.DocumentationMetadata.ENUM_CHOICES): # Replace the enum choices with those defined on the argument params[arg.name]['enum'] = arg.metadata[api.DocumentationMetadata.ENUM_CHOICES] if arg.metadata.get(api.DocumentationMetadata.DEPRECATED): params[arg.name][CustomSwaggerAttribute.DEPRECATED] = True if arg.metadata.get(api.DocumentationMetadata.HAS_HTML): params[arg.name][CustomSwaggerAttribute.HAS_HTML] = True elif arg.metadata.get(api.DocumentationMetadata.HAS_JSON): params[arg.name][CustomSwaggerAttribute.HAS_JSON] = True return params