Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining a file upload endpoint #476

Closed
tiholic opened this issue Aug 2, 2021 · 3 comments
Closed

Defining a file upload endpoint #476

tiholic opened this issue Aug 2, 2021 · 3 comments
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@tiholic
Copy link

tiholic commented Aug 2, 2021

I'm trying to define an endpoint which is a pure file upload API and doesn't depend on a model

swagger ref: https://swagger.io/docs/specification/describing-request-body/file-upload/#multipart

Tried setting this, but seems like I'm missing something.

@extend_schema(
  request={
    'multipart/form-data': { 'schema': ... }  # similar to what's in the above link
  }
)
@tfranzel
Copy link
Owner

tfranzel commented Aug 3, 2021

hi!

i skipped this on purpose initially, because it's only well defined when used with a MIME type listing. basic annotation (e.g. bytes) has been possible though:

@extend_schema(
request={
'application/json': dict,
'application/pdf': bytes,
'text/html': OpenApiTypes.STR
},
responses=None
)

i added this now as it is in fact a missing feature. however, it will only work for @extend_schema(request={MIME: {SCHEMA}}) and NOT for @extend_schema(request={SCHEMA}). turns out this also creates parity again with responses=, so it's the right thing to do.

@tfranzel tfranzel added enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Aug 3, 2021
@tiholic
Copy link
Author

tiholic commented Aug 3, 2021

Wonderful! Thanks. It works very well.

Here's how I use it for defining file uploads using multipart:

extend_schema(
        operation_id='upload_file',
        request={
            'multipart/form-data': {
                'type': 'object',
                'properties': {
                    'file': {
                        'type': 'string',
                        'format': 'binary'
                        }
                    }
                }
            },
        )

I believe these 2 doc strings can be updated:
https://github.com/tfranzel/drf-spectacular/blob/master/drf_spectacular/utils.py#L230
https://github.com/tfranzel/drf-spectacular/blob/master/drf_spectacular/utils.py#L217

tfranzel added a commit that referenced this issue Aug 3, 2021
@tfranzel
Copy link
Owner

tfranzel commented Aug 3, 2021

awesome!

good point. updated the doc strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants