示例#1
0
def test_get_operation_with_serializer():
    view = View()
    route = get_route(Controller.post_with_serializer)
    View.post.route = route
    reference_resolver = openapi.ReferenceResolver('definitions',
                                                   'parameters',
                                                   force_init=True)
    auto_schema = SwaggerAutoSchema(view, 'path', route.http_method,
                                    reference_resolver, 'request', {})
    operation = auto_schema.get_operation(['test_app', 'post'])
    schema_ref = openapi.SchemaRef(
        resolver=reference_resolver,
        schema_name='User',
    )
    parameters = [
        openapi.Parameter(
            name='data',
            in_=openapi.IN_BODY,
            required=True,
            schema=schema_ref,
        ),
    ]
    responses = openapi.Responses({
        '200':
        openapi.Response(
            description='',
            schema=openapi.Schema(
                type=openapi.TYPE_OBJECT,
                properties={
                    'name': {
                        'type': 'string',
                    },
                },
            ),
        ),
    })

    assert operation == openapi.Operation(
        operation_id='Controller.post_with_serializer',
        responses=responses,
        consumes=['application/json'],
        produces=['application/json'],
        tags=['test_app'],
        parameters=parameters,
    )
    del View.post.route
示例#2
0
    def get_schema(self, request=None, public=False):
        schema_urls = utils.get_swagger_urls()
        config_aggregator = {
            'info': {
                'title': 'API Gateway',
                'description': '',
                'version': '1.0'
            },
            'apis':
            schema_urls,
            'produces': [
                'application/json', 'application/x-www-form-urlencoded',
                'multipart/form-data'
            ],
            'consumes': [
                'application/json', 'application/x-www-form-urlencoded',
                'multipart/form-data'
            ],
        }
        sw_aggregator = SwaggerAggregator(config_aggregator)
        swagger_spec = sw_aggregator.generate_swagger()

        endpoints = self.get_endpoints(request)
        components = openapi.ReferenceResolver(openapi.SCHEMA_DEFINITIONS)
        paths, prefix = self.get_paths(endpoints, components, request, public)
        paths.update(swagger_spec['paths'])
        components['definitions'].update(swagger_spec['definitions'])

        url = self.url
        if url is None and request is not None:
            url = request.build_absolute_uri()

        return openapi.Swagger(info=self.info,
                               paths=paths,
                               consumes=swagger_spec['consumes'],
                               produces=swagger_spec['produces'],
                               security_definitions=swagger_spec.get(
                                   'security_definitions', None),
                               security=swagger_spec.get('security', None),
                               _url=url,
                               _version=self.version,
                               _prefix=prefix,
                               **dict(components))
示例#3
0
def test_get_operation_without_route():
    view = View()
    reference_resolver = openapi.ReferenceResolver('definitions',
                                                   'parameters',
                                                   force_init=True)
    auto_schema = SwaggerAutoSchema(view, 'path', 'get', reference_resolver,
                                    'request', {})
    operation = auto_schema.get_operation(['test_app', 'post'])
    parameters = []
    responses = openapi.Responses({
        '200': openapi.Response(description=''),
    })

    assert operation == openapi.Operation(
        description='',
        operation_id='test_app_post',
        responses=responses,
        consumes=['application/json'],
        produces=['application/json'],
        tags=['test_app'],
        parameters=parameters,
    )