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

How to provide examples for in multipart/form-data? #967

Closed
kiran-4444 opened this issue Mar 24, 2023 · 4 comments
Closed

How to provide examples for in multipart/form-data? #967

kiran-4444 opened this issue Mar 24, 2023 · 4 comments

Comments

@kiran-4444
Copy link

kiran-4444 commented Mar 24, 2023

    @extend_schema(
        request={
            "multipart/form-data": {
                "type": "object",
                "properties": {
                    "project_id": {
                        "type": "string",
                        "description": "ID of the project.",
                        "default": "5",
                        "required": True,
                        'in': 'body',
                    },
                    "input": {
                        "type": "string",
                        "format": "binary",
                        "description": "Input file.",
                        "required": True,
                        'in': 'body',
                    },
                },
            },
        },
        examples=[
            OpenApiExample(
                "Example",
                summary="Example",
                description="Example",
                value={
                    "project_id": "5",
                },
                request_only=True,
            ),
        ]
    )
    def post(self, request, *args, **kwargs):

I have this, when I try to make the form type to application/json the example works but it's not working when I change it back to multipart/form-data.

@tfranzel
Copy link
Owner

Hi, it does not show up because the example is on the view level and the default value for media_type does not match

- media_type will default to 'application/json' unless implicitly specified

We are actually introducing OpenApiRequest right now in #966 where the media_type would be autofilled, since you would mount the example under multipart/form-data


                    'in': 'body',

I don't think this is legal OpenApi 3.0.3. Afaik in on exists on parameters

@kiran-4444
Copy link
Author

I didn't quite get it. Can you elaborate? Thanks!

@tfranzel
Copy link
Owner

tfranzel commented Mar 24, 2023

            OpenApiExample(
                "Example",
                summary="Example",
                description="Example",
                value={
                    "project_id": "5",
                },
                request_only=True,
                media_type="multipart/form-data"  # <<<<<<<
            ),

is the solution right now.

When we merge #966, you don't need to specify request_only and media_type when used with

@extend_schema(
    request={
        "multipart/form-data":  OpenApiRequest(
            {...your raw schema}, 
            examples=[OpenApiExample(...)]
        )
    }
)

@tfranzel
Copy link
Owner

closing this issue for now. feel free to comment if anything is missing or not working and we will follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants