Esempio n. 1
0
    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
Esempio n. 2
0
def field_to_property(field):
    prop = swagger.__field_to_property(field)
    if hasattr(field, '__apidoc__'):
        deprecated = field.__apidoc__.get(api.DocumentationMetadata.DEPRECATED)
        has_html = field.__apidoc__.get(api.DocumentationMetadata.HAS_HTML)
        enum_cls = field.__apidoc__.get(api.DocumentationMetadata.ENUM_CLS)

        if deprecated:
            prop[CustomSwaggerAttribute.DEPRECATED] = True

        if has_html:
            prop[CustomSwaggerAttribute.HAS_HTML] = True

        if enum_cls:
            prop[CustomSwaggerAttribute.ENUM_TYPE] = ah.enum_id(enum_cls)

    return prop
Esempio n. 3
0
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