I have an endpoint that is coded like this:
class IrisMatching(viewsets.GenericViewSet):
parser_classes = (parsers.JSONParser,)
serializer_class = [] # serializers.IrisLookupSerializer
authentication_classes = []
permission_classes = []
# def get(self, request, *args, **kwargs):
# return Response(request.data)
def post(self, request, *args, **kwargs):
print(request.data)
print(request.data)
After some experimentation I was able to get it to show up in Swagger by making the following incantation in the urls.py file. I have two url.py files in this app I’ve inherited and I have a lot of questions about how that all works… But - that’s another question…
For now - this is how I got the endpoint to show up in the Swagger page:
url(r'^matching/ztest/', views.IrisMatching.as_view({'post': 'post'}))
I do get a Swagger endpoint now, but it says “No Parameters” (which I suppose makes sense from a certain perspective). It looks like this…
What do I need to do so that the Swagger page shows a field for the user to enter JSON on this endpoint? Preferrably populated with a correct JSON example of what I want the user to submit?
Thank you - I’m an old Java dev and just inherited this app written by folks who are no longer with the company.
I should add - the data I want to submit has nothing to do with any models or database rows. It is, instead, some strings and a list of Id’s to be consumed by a custom query to do a bit of housekeeping in Prod when there is a certain data issue that comes up regularly…
OK. I can partially answer my question.
I added a serializer to my class above and there is now space for submitting JSON that matches the Serializer’s return from “to_representation”
My concern now is that all the examples I have for Serializers depend on Models. Can I add a Model that does NOT exist in the database? Is there another / better way?
So- this solved it for me:
class FixStaffingChangeReportHireFlagsSerializer(JSONSerializer):
affected_fields = serializers.CharField(max_length=10)
boolean_to_set = serializers.CharField(max_length=5)
list_of_ids = serializers.ListField()
And resulted in this:
You can also try this, here request_body
specifies what data should be POST for the create()
and what kind of responses
will be returned via this URL endpoint. swagger_auto_schema
is a decorator for any method in your viewsets.
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
class ChangePasswordViewSet(viewsets.ViewSet):
@swagger_auto_schema(
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"email": openapi.Schema(
type=openapi.TYPE_STRING, description="Email Address"
),
"old_password": openapi.Schema(
type=openapi.TYPE_STRING, description="Previous Password"
),
"new_password": openapi.Schema(
type=openapi.TYPE_STRING, description="New Password"
),
},
),
responses={
status.HTTP_202_ACCEPTED: openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"status": openapi.Schema(type=openapi.TYPE_NUMBER),
"message": openapi.Schema(type=openapi.TYPE_STRING),
},
),
status.HTTP_401_UNAUTHORIZED: openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"status": openapi.Schema(type=openapi.TYPE_NUMBER),
"message": openapi.Schema(type=openapi.TYPE_STRING),
},
),
},
)
def create(self, request):
....
Nice! Thank you addwebsolution!