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.32k stars 259 forks source link

how to use file input upload with serializer in request of extend_schema #761

Closed hossainirad closed 2 years ago

hossainirad commented 2 years ago
class InputSerializer(serializers.ModelSerializer):
    class Meta:
        model = Concert
        fields = [
            'title',
            'description',
            'cover',
            'thumbnail',
            'featured_cover',
        ]

class BlaBlaBlaAPIView()...
    @extend_schema(
        request={InputSerializer},
        responses={201: OutputSerializer},
        operation_id="OutputSerializer",
        tags=["AdminCrud"],
    )
    def post(self, request):
    # some codes ...

InputSerializer have some ImageField that come from a model. how can I have an input file for them in docs record?

I have seen https://github.com/tfranzel/drf-spectacular/issues/476 but how can a use them together? thanks for your time!

tfranzel commented 2 years ago

apart from that you code example should then work as expected. the manual schema from #476 should not be needed then.

tfranzel commented 2 years ago

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

abhinavsingh commented 4 months ago

@tfranzel COMPONENT_SPLIT_REQUEST: True is the key here, but unfortunately, changing this setting mid-project results in change of entire schema and hence generated api clients.

Is it possible to override this only for a single viewset somehow?

tfranzel commented 4 months ago

sry, this is a global setting. usage of COMPONENT_SPLIT_REQUEST: True is highly recommended in the docs when someone wants generate clients.

I recommend switching because it will save you time down the line. it produces vastly superior clients.

abhinavsingh commented 4 months ago

@tfranzel Thank you for the reply. I was able to reach FAQ section where you mentioned it https://github.com/tfranzel/drf-spectacular/blob/2501bfd1f74fedacdec4051adc76df22bc847976/docs/faq.rst#filefield-imagefield-is-not-handled-properly-in-the-schema

Unfortunately, when we started the project, COMPONENT_SPLIT_REQUEST: False was the default (may-be even today) and we didn't cared about it, until yesterday when we needed a file upload endpoint.

We are checking at our end, if somehow we can incorporate w/o disrupting mobile/web client apps in a major way. Alternatively, we might end up with vanilla Django upload endpoint w/ out-of-bound authentication/permission checks.

tfranzel commented 4 months ago

sry to hear that. well there is a whole section in the docs about this topic: https://drf-spectacular.readthedocs.io/en/latest/client_generation.html

I migrated a project once before and the required changes are usually not too bad.