Exemplo n.º 1
0
 def get_schema_definitions(self, data):
     definitions = {}
     schemas = lookup(data, ['components', 'schemas'], {})
     for key, value in schemas.items():
         definitions[key] = JSONSchemaCodec().decode_from_data_structure(
             value)
     return definitions
Exemplo n.º 2
0
    def get_operation(self, link, operation_id, tag=None, schema_defs=None):
        operation = {'operationId': operation_id}
        if link.title:
            operation['summary'] = link.title
        if link.description:
            operation['description'] = link.description
        if tag:
            operation['tags'] = [tag]
        if link.get_path_fields() or link.get_query_fields():
            operation['parameters'] = [
                self.get_parameter(field, schema_defs)
                for field in link.get_path_fields() + link.get_query_fields()
            ]
        if link.get_body_field():
            schema = link.get_body_field().schema
            if schema is None:
                content_info = {}
            else:
                content_info = {
                    'schema':
                    JSONSchemaCodec().encode_to_data_structure(
                        schema, schema_defs, '#/components/schemas/')
                }

            operation['requestBody'] = {
                'content': {
                    link.encoding: content_info
                }
            }
        if link.response is not None:
            operation['responses'] = {
                str(link.response.status_code): {
                    'description': '',
                    'content': {
                        link.response.encoding: {
                            'schema':
                            JSONSchemaCodec().encode_to_data_structure(
                                link.response.schema, schema_defs,
                                '#/components/schemas/')
                        }
                    }
                }
            }
        return operation
Exemplo n.º 3
0
 def get_parameter(self, field, schema_defs=None):
     parameter = {'name': field.name, 'in': field.location}
     if field.required:
         parameter['required'] = True
     if field.description:
         parameter['description'] = field.description
     if field.schema:
         parameter['schema'] = JSONSchemaCodec().encode_to_data_structure(
             field.schema, schema_defs, '#/components/schemas/')
     return parameter
Exemplo n.º 4
0
    def get_link(self, base_url, path, path_info, operation, operation_info,
                 schema_definitions):
        """
        Return a single link in the document.
        """
        name = operation_info.get('operationId')
        title = operation_info.get('summary')
        description = operation_info.get('description')

        if name is None:
            name = _simple_slugify(title)
            if not name:
                return None

        # Allow path info and operation info to override the base url.
        base_url = lookup(path_info, ['servers', 0, 'url'], default=base_url)
        base_url = lookup(operation_info, ['servers', 0, 'url'],
                          default=base_url)

        # Parameters are taken both from the path info, and from the operation.
        parameters = path_info.get('parameters', [])
        parameters += operation_info.get('parameters', [])

        fields = [
            self.get_field(parameter, schema_definitions)
            for parameter in parameters
        ]

        # TODO: Handle media type generically here...
        body_schema = lookup(
            operation_info,
            ['requestBody', 'content', 'application/json', 'schema'])

        encoding = None
        if body_schema:
            encoding = 'application/json'
            if '$ref' in body_schema:
                ref = body_schema['$ref'][len('#/components/schemas/'):]
                schema = schema_definitions.get(ref)
            else:
                schema = JSONSchemaCodec().decode_from_data_structure(
                    body_schema)
            fields += [Field(name='body', location='body', schema=schema)]

        return Link(name=name,
                    url=urljoin(base_url, path),
                    method=operation,
                    title=title,
                    description=description,
                    fields=fields,
                    encoding=encoding)
Exemplo n.º 5
0
    def get_field(self, parameter, schema_definitions):
        """
        Return a single field in a link.
        """
        name = parameter.get('name')
        location = parameter.get('in')
        description = parameter.get('description')
        required = parameter.get('required', False)
        schema = parameter.get('schema')
        example = parameter.get('example')

        if schema is not None:
            if '$ref' in schema:
                ref = schema['$ref'][len('#/components/schemas/'):]
                schema = schema_definitions.get(ref)
            else:
                schema = JSONSchemaCodec().decode_from_data_structure(schema)

        return Field(name=name,
                     location=location,
                     description=description,
                     required=required,
                     schema=schema,
                     example=example)
Exemplo n.º 6
0
def test_json_schema(schema, value, is_valid, description):
    validator = JSONSchemaCodec().decode_from_data_structure(schema)
    was_valid = validator.is_valid(value)
    assert was_valid == is_valid, description