drf-openapi-tester icon indicating copy to clipboard operation
drf-openapi-tester copied to clipboard

The default 'basePath' value doesn't work on Windows.

Open Axonny opened this issue 3 years ago • 3 comments

I use:

  • django
  • djangorestframework
  • drf-spectacular

I'm trying to test my openapi scheme, but It's not working. Exploring the source code, I found some problem.

If my schema does not have the basePath parameter, it takes the default value /. Then RefResolver (from prance) calls to_posix before urlparse.

In fs.py file:

def to_posix(fname):
    """
    Convert a path to posix-like format.

    :param str fname: The filename to convert to posix format.
    :return: The filename in posix-like format.
    :rtype: str
    """
    import sys

    if sys.platform == "win32":  # pragma: nocover
        import os.path

        if os.path.isabs(fname):
            fname = "/" + fname
        fname = fname.replace("\\", "/")
    return fname

If you call to_posix("/") on Windows, it will return "//". Then urlparse will not be able to parse this value. And... this happened: prance.util.url.ResolutionError: Cannot build an absolute file URL from a fragment without a reference with path!

As a temporary solution I use:

@pytest.fixture
def open_api_client(schema_tester):
    with patch("prance.util.fs.from_posix", wraps=lambda x: x):
        with patch("prance.util.fs.to_posix", wraps=lambda x: x):
            client = OpenAPIClient(schema_tester=schema_tester)
            yield client

But it can lead to other errors.

Axonny avatar Feb 01 '23 10:02 Axonny

This seems like a prance issue, no?

sondrelg avatar Feb 01 '23 12:02 sondrelg

This is probably a feature of prance that you haven't considered. 'basePath' is your parameter and its default value doesn't work on Windows. If you think prance is the problem, create a new issue!

Axonny avatar Feb 02 '23 05:02 Axonny

It's been a while since I've used this package myself, and I don't have the capacity to fix this in the short term. PRs are welcome :+1:

sondrelg avatar Feb 02 '23 09:02 sondrelg