Skip to content

Add support for different content type responses (e.g. application/octet-stream) #1073

@LevonW-IIS

Description

@LevonW-IIS

I have been creating a ninja API for my web app and have found the process very smooth, and have been enjoying the open API auto documentation, which I rely on in my front-end. I have encountered one problem in dealing with a file download endpoint.

The response should be easily specifiable under the openapi specs as

content:
  application/octet-stream:
    schema:
      type: string
      format: binary

however I've found no easy way to implement this within django-ninja.

I've had a look through the code and I think I've found where a change could be made

if model not in [None, NOT_SET]:
    # ::TODO:: test this: by_alias == True
    schema = self._create_schema_from_model(
        model, by_alias=operation.by_alias
    )[0]
    details[status]["content"] = {
        self.api.renderer.media_type: {"schema": schema}
    }

if the schema had an __ninja_override_media_type__ attribute, this could be used to provide a custom media type for a response.

If you want me to have a stab at writing a PR for this let me know

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions