tfranzel / drf-spectacular

Sane and flexible OpenAPI 3 schema generation for Django REST framework.
https://drf-spectacular.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.38k stars 264 forks source link

`FileField` URL should use `OpenApiTypes.URI_REF`. #584

Open ngnpope opened 3 years ago

ngnpope commented 3 years ago

Describe the bug

Django's FileField stores a relative URL, see FieldFile.url. DRF's FileField can attempt to make this absolute, see here, but this will not work when generating a schema as we have no request in the context.

Currently drf-spectacular is using the uri format:

https://github.com/tfranzel/drf-spectacular/blob/59f9749086d5bf5d359ef7f8496bd39c88267721/drf_spectacular/openapi.py#L695-L696

When attempting to validate, the uri format, according to RFC 3986, is expected to be absolute.

Expected behavior

The generated schema for FileField with use_url=True should be uri-reference.

tfranzel commented 3 years ago

I see. uri-reference is a bit more esoteric than uri, but swagger-ui does not validate either of them when used with parameters. So the change should have no regressions ui-wise. However does this really happen in practice, since the request should be present when used natively with the API (unless used manually without context)?