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

Registering body decoder for wildard */* content-type #481

Open
EmilLaursen opened this issue Jan 27, 2022 · 2 comments
Open

Registering body decoder for wildard */* content-type #481

EmilLaursen opened this issue Jan 27, 2022 · 2 comments

Comments

@EmilLaursen
Copy link

This is similar to #473

except I want to accept any content-type:

/api/v1/srv/upload/kbl:
    post:
      tags:
        - srv
      summary: Upload xml file to process it.
      operationId: upload.file
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
                - file
              type: object
              properties:
                file:
                  type: string
                  format: binary
            encoding:
              file:
                contentType: "*/*"

If I use a request with content-type application/pdf in the file form field, I get the content type not supported "application/pdf" error. Registering a specific RegisterBodyDecoder for application/pdf will work, but I would like to register one on the wildcard */*

	openapi3filter.RegisterBodyDecoder("*/*", noopDecoder)

which accepts application/pdf and all other unknown media-types! Is this possible? I do not know which content-types I receive in advance, so I can not register a decoder for each one.

@EmilLaursen EmilLaursen changed the title Unsupported content type in multipart request validation Registering body decoder for wildard */* content-type Jan 27, 2022
@fenollp
Copy link
Collaborator

fenollp commented Feb 23, 2022

This won't work today: the header content-type matching logic for registered body decoders does not handle wildcards yet.

decoder, ok := bodyDecoders[mediaType]
if !ok {

You should be able to achieve what you want by adding some code there. Something akin to:

	// ...
	decoder, ok := bodyDecoders[mediaType]
	catchAllDecoder, canCatchAll := bodyDecoders[mediaType]
	if !ok {
		if canCatchAll {
			decoder = catchAllDecoder
		} else {
	// ...

@EmilLaursen
Copy link
Author

I can take a stab at this when I have some free time

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