Show maximum, minimum and default in Swagger for D

2019-08-10 03:31发布

问题:

I am developing a v2 for my API, and I would like to document it properly. Since I use Django Rest Framework (3.1.1), and it is recommended in their docs to use Swagger, I am using it.

In the documentation, I see that one of its features is to show "Field default values, minimum, maximum, read-only and required attributes". However, in my example, I only see "Description", "Parameter Type" and "Data Type", as in the image:

I would like to see more columns, such as minimum, maximum, and default, with the values taken from the models (not having to rewrite them in the serializers. This is my serializer, in case it helps:

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id', 'email', 'first_name', 'last_name', 'password',)
        write_only_fields = ('password',)
        read_only_fields = ('id',)

    def create(self, validated_data):
    [...]

Nobody seems to have the same problem so maybe (I hope not) I am missing something trivial. Hope you can help. Thanks!

回答1:

class MySourceView(generics.RetrieveUpdateDestroyAPIView):
    """
    MySource documentation
    """
    serializer_class = MySourceSerializer
    queryset = MySource.objects.all()

    def get(self, request, *args, **kwargs):
        """
        GET Documentation
        ---
            parameters_strategy: replace
            parameters:
                - name: pk
                  description: "Liverpool FC is the best."
                  required: true
                  type: string
                  paramType: path
                  defaultValue: 5
        """
        return self.retrieve(request, *args, **kwargs)

You can do it as YAML docstring in specify method. More details in Swagger RESTful API Documentation Specification 4.3.3 Data Type Fields