def process_api_declaration(self, resources, resource, context): required_fields = [u'swaggerVersion', u'basePath', u'apis'] validate_required_fields(resource, required_fields, context) if not resource[u'swaggerVersion'] in SWAGGER_VERSIONS: raise SwaggerError( u"Unsupported Swagger version %s" % resource[u'swaggerVersion'], context) # Check model name and id consistency if u'models' in resource: for (model_name, model) in resource[u'models'].items(): if model.get('id') and model_name != model[u'id']: raise SwaggerError(u"Model id doesn't match name", context)
def validate_type_or_ref(json, model_ids, allowed_types, allowed_refs, context): """Validates that either type OR ref is present in the json :param json: dict to check whether type or ref is present :param model_ids: list of allowed $ref ids (all models) :param allowed_types: list of all kind of types allowed :param allowed_refs: list of all kind of refs allowed :param context: only used for Request Operation and Paramter """ if json.get(u'type') in swagger_type.CONTAINER_TYPES: validate_required_fields(json, [u'items'], context) # OVerride allowed_refs to add model_ids if empty allowed_refs = model_ids.get('model_ids') return validate_type_or_ref(json[u'items'], model_ids, allowed_types, allowed_refs, context) if json.get(u'type') not in allowed_types and \ json.get(u'$ref') not in allowed_refs: # Show more detailed error with context, if present if context: raise SwaggerError( "%s not in allowed types: %s" % (json.get(u'type'), allowed_types), context) else: raise TypeError("%s not in allowed types: %s" % (json.get(u'type') or json.get(u'$ref'), allowed_types + allowed_refs))
def process_api_declaration(self, resources, resource, context): required_fields = ['basePath', 'resourcePath', 'apis', 'models'] validate_required_fields(resource, required_fields, context) # Check model name and id consistency for (model_name, model) in resource['models'].items(): if model_name != model['id']: raise SwaggerError("Model id doesn't match name", context)
def process_resource_listing(self, resources, context): required_fields = ['basePath', 'apis', 'swaggerVersion'] validate_required_fields(resources, required_fields, context) if not resources['swaggerVersion'] in SWAGGER_VERSIONS: raise SwaggerError( "Unsupported Swagger version %s" % resources.swaggerVersion, context)
def validate_required_fields(json, required_fields, context): """Checks a JSON object for a set of required fields. If any required field is missing, a SwaggerError is raised. :param json: JSON object to check. :param required_fields: List of required fields. :param context: Current context in the API. """ missing_fields = [f for f in required_fields if not f in json] if missing_fields: raise SwaggerError("Missing fields: %s" % ', '.join(missing_fields), context)
def process_parameter(self, resources, resource, api, operation, parameter, context): required_fields = ['name', 'paramType'] validate_required_fields(parameter, required_fields, context) if parameter['paramType'] == 'path': # special handling for path parameters parameter['required'] = True parameter['dataType'] = 'string' else: # dataType is required for non-path parameters validate_required_fields(parameter, ['dataType'], context) if 'allowedValues' in parameter: raise SwaggerError( "Field 'allowedValues' invalid; use 'allowableValues'", context)
def get_swagger_type(json_): """Converts swagger type from json to swagger internal type Example: .. code-block:: python { ... "type": "array", "items": { "type": "integer", "format": "int64" } ... } Returns: .. code-block:: python "array:integer:int64" :param json_: dict containing type and rest of the data :type json_: dict :rtype: str or unicode """ type_ = json_.get('type') format_ = json_.get('format') ref = json_.get('$ref') if format_ and type_: return type_ + COLON + format_ elif type_ == 'array': return type_ + COLON + get_swagger_type(json_["items"]) elif ref: return ref elif type_: return type_ else: raise SwaggerError("No proper type could be found from %s" % json_)
def process_resource_listing_api(self, resources, listing_api, context): validate_required_fields(listing_api, ['path', 'description'], context) if not listing_api['path'].startswith("/"): raise SwaggerError("Path must start with /", context)
def process_resource_listing_api(self, resources, listing_api, context): # removing 'description' as it is recommended but not required validate_required_fields(listing_api, [u'path'], context) if not listing_api[u'path'].startswith(u"/"): raise SwaggerError(u"Path must start with /", context)