Beispiel #1
0
    def security_objects(self):
        """Builds up the list of security options for this operation

        :returns: list of security definition associated to the operation
        """
        if self._security_objects is None:
            deref = self.swagger_spec.deref
            op_spec = deref(self.op_spec)
            spec_dict = deref(self.swagger_spec.spec_dict)
            security_spec = deref(op_spec.get('security', []))
            if len(security_spec) == 0:
                security_spec = spec_dict.get('security', [])
            security_defs_dict = spec_dict.get('securityDefinitions', {})

            self._security_objects = []
            for security_item in security_spec:
                for security_name, security_scope in security_item.items():
                    security_definition = security_defs_dict.get(security_name)
                    if security_definition is None:
                        raise SwaggerSchemaError(
                            '{security} not defined in {swagger_path}'.format(
                                swagger_path='/securityDefinitions',
                                security=security_name,
                            ))
                    self._security_objects.append(security_definition)

        return self._security_objects
Beispiel #2
0
    def pick_a_scheme(schemes):
        if not schemes:
            return origin.scheme

        if preferred_scheme:
            if preferred_scheme in schemes:
                return preferred_scheme
            raise SwaggerSchemaError(
                "Preferred scheme {0} not supported by API. Available schemes "
                "include {1}".format(preferred_scheme, schemes))

        if origin.scheme in schemes:
            return origin.scheme

        if len(schemes) == 1:
            return schemes[0]

        raise SwaggerSchemaError(
            "Origin scheme {0} not supported by API. Available schemes "
            "include {1}".format(origin.scheme, schemes))
 def __init__(self, swagger_spec, security_requirement_spec):
     self.swagger_spec = swagger_spec
     self.security_requirement_spec = swagger_spec.deref(security_requirement_spec)
     for security_definition in six.iterkeys(security_requirement_spec):
         if security_definition not in self.swagger_spec.security_definitions:
             raise SwaggerSchemaError(
                 '{security} not defined in {swagger_path}'.format(
                     swagger_path='/securityDefinitions',
                     security=security_definition,
                 ),
             )
Beispiel #4
0
 def __init__(self, swagger_spec, security_requirement_spec):
     # type: (Spec, typing.Mapping[typing.Text, typing.Mapping[typing.Text, typing.List[typing.Text]]]) -> None
     self.swagger_spec = swagger_spec
     self.security_requirement_spec = swagger_spec.deref(
         security_requirement_spec)
     for security_definition in six.iterkeys(security_requirement_spec):
         if security_definition not in self.swagger_spec.security_definitions:
             raise SwaggerSchemaError(
                 '{security} not defined in {swagger_path}'.format(
                     swagger_path='/securityDefinitions',
                     security=security_definition,
                 ), )
Beispiel #5
0
def build_params(op):
    """Builds up the list of this operation's parameters taking into account
    parameters that may be available for this operation's path component.

    :type op: :class:`bravado_core.operation.Operation`

    :returns: dict where (k,v) is (param_name, Param)
    """
    swagger_spec = op.swagger_spec
    deref = swagger_spec.deref
    op_spec = deref(op.op_spec)
    op_params_spec = deref(op_spec.get('parameters', []))
    spec_dict = deref(swagger_spec._internal_spec_dict)
    paths_spec = deref(spec_dict.get('paths', {}))
    path_spec = deref(paths_spec.get(op.path_name))
    path_params_spec = deref(path_spec.get('parameters', []))

    # Order of addition is *important* here. Since op_params are last in the
    # list, they will replace any previously defined path_params with the
    # same name when the final params dict is constructed in the loop below.
    params_spec = path_params_spec + op_params_spec

    params = AliasKeyDict()
    for param_spec in params_spec:
        param = Param(swagger_spec, op, deref(param_spec))
        sanitized_name = sanitize_name(param.name)
        params[sanitized_name] = param
        params.add_alias(param.name, sanitized_name)

    # Security parameters cannot override and been overridden by operation or path objects
    new_params = {}
    new_param_aliases = {}
    for parameter in op.security_parameters:
        param_name = sanitize_name(parameter.name)
        if param_name in params:
            raise SwaggerSchemaError(
                "'{0}' security parameter is overriding a parameter defined in operation or path object".format(
                    parameter.name,
                )
            )
        else:
            # not directly in params because different security requirements could share parameters
            new_params[param_name] = parameter
            new_param_aliases[parameter.name] = param_name

    params.update(new_params)
    for alias, name in iteritems(new_param_aliases):
        params.add_alias(alias, name)
    return params