From 3cf0f07baf6a273346ae386b10784715d779865d Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 17 May 2021 23:12:52 +0200 Subject: [PATCH] *: fix API Schema for file uploads Signed-off-by: Jens Langhammer --- authentik/core/api/applications.py | 21 +++++++------- authentik/flows/api/flows.py | 32 ++++++++++----------- authentik/providers/saml/api.py | 4 ++- schema.yml | 45 +++++++++++++++++------------- 4 files changed, 55 insertions(+), 47 deletions(-) diff --git a/authentik/core/api/applications.py b/authentik/core/api/applications.py index 189407f3e..651331207 100644 --- a/authentik/core/api/applications.py +++ b/authentik/core/api/applications.py @@ -6,9 +6,14 @@ from django.db.models import QuerySet from django.http.response import HttpResponseBadRequest from django.shortcuts import get_object_or_404 from drf_spectacular.types import OpenApiTypes -from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema +from drf_spectacular.utils import ( + OpenApiParameter, + OpenApiResponse, + extend_schema, + inline_serializer, +) from rest_framework.decorators import action -from rest_framework.fields import SerializerMethodField +from rest_framework.fields import FileField, SerializerMethodField from rest_framework.parsers import MultiPartParser from rest_framework.request import Request from rest_framework.response import Response @@ -155,15 +160,11 @@ class ApplicationViewSet(ModelViewSet): @permission_required("authentik_core.change_application") @extend_schema( - request=OpenApiTypes.NONE, - parameters=[ - OpenApiParameter( - name="file", - location=OpenApiParameter.QUERY, # TODO: In Form - type=OpenApiTypes.BINARY, - required=True, + request={ + "multipart/form-data": inline_serializer( + "SetIcon", fields={"file": FileField()} ) - ], + }, responses={ 200: OpenApiResponse(description="Success"), 400: OpenApiResponse(description="Bad request"), diff --git a/authentik/flows/api/flows.py b/authentik/flows/api/flows.py index b7830c5e6..24249dd34 100644 --- a/authentik/flows/api/flows.py +++ b/authentik/flows/api/flows.py @@ -7,9 +7,15 @@ from django.http.response import HttpResponseBadRequest, JsonResponse from django.urls import reverse from django.utils.translation import gettext as _ from drf_spectacular.types import OpenApiTypes -from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema +from drf_spectacular.utils import ( + OpenApiParameter, + OpenApiResponse, + extend_schema, + inline_serializer, +) from guardian.shortcuts import get_objects_for_user from rest_framework.decorators import action +from rest_framework.fields import FileField from rest_framework.parsers import MultiPartParser from rest_framework.request import Request from rest_framework.response import Response @@ -140,15 +146,11 @@ class FlowViewSet(ModelViewSet): ], ) @extend_schema( - request=OpenApiTypes.NONE, - parameters=[ - OpenApiParameter( - name="file", - location=OpenApiParameter.QUERY, # TODO: Form - type=OpenApiTypes.BINARY, - required=True, + request={ + "multipart/form-data": inline_serializer( + "SetIcon", fields={"file": FileField()} ) - ], + }, responses={ 204: OpenApiResponse(description="Successfully imported flow"), 400: OpenApiResponse(description="Bad request"), @@ -267,15 +269,11 @@ class FlowViewSet(ModelViewSet): @permission_required("authentik_flows.change_flow") @extend_schema( - request=OpenApiTypes.NONE, - parameters=[ - OpenApiParameter( - name="file", - location=OpenApiParameter.QUERY, # TODO: Form - type=OpenApiTypes.BINARY, - required=True, + request={ + "multipart/form-data": inline_serializer( + "SetIcon", fields={"file": FileField()} ) - ], + }, responses={ 200: OpenApiResponse(description="Success"), 400: OpenApiResponse(description="Bad request"), diff --git a/authentik/providers/saml/api.py b/authentik/providers/saml/api.py index 804f4b919..1e337fede 100644 --- a/authentik/providers/saml/api.py +++ b/authentik/providers/saml/api.py @@ -119,7 +119,9 @@ class SAMLProviderViewSet(ModelViewSet): ], ) @extend_schema( - request=SAMLProviderImportSerializer(), + request={ + "multipart/form-data": SAMLProviderImportSerializer, + }, responses={ 204: OpenApiResponse(description="Successfully imported provider"), 400: OpenApiResponse(description="Bad request"), diff --git a/schema.yml b/schema.yml index 88147f1a9..f3c304bb1 100644 --- a/schema.yml +++ b/schema.yml @@ -1268,12 +1268,6 @@ paths: operationId: core_applications_set_icon_create description: Set application icon parameters: - - in: query - name: file - schema: - type: string - format: binary - required: true - in: path name: slug schema: @@ -1282,6 +1276,12 @@ paths: required: true tags: - core + requestBody: + content: + multipart/form-data: + schema: + $ref: '#/components/schemas/SetIconRequest' + required: true security: - authentik: [] - cookieAuth: [] @@ -3948,12 +3948,6 @@ paths: operationId: flows_instances_set_background_create description: Set Flow background parameters: - - in: query - name: file - schema: - type: string - format: binary - required: true - in: path name: slug schema: @@ -3962,6 +3956,12 @@ paths: required: true tags: - flows + requestBody: + content: + multipart/form-data: + schema: + $ref: '#/components/schemas/SetIconRequest' + required: true security: - authentik: [] - cookieAuth: [] @@ -4012,15 +4012,14 @@ paths: post: operationId: flows_instances_import_flow_create description: Import flow from .akflow file - parameters: - - in: query - name: file - schema: - type: string - format: binary - required: true tags: - flows + requestBody: + content: + multipart/form-data: + schema: + $ref: '#/components/schemas/SetIconRequest' + required: true security: - authentik: [] - cookieAuth: [] @@ -23126,6 +23125,14 @@ components: $ref: '#/components/schemas/User' required: - user + SetIconRequest: + type: object + properties: + file: + type: string + format: binary + required: + - file SeverityEnum: enum: - notice