Drf-yasg: Field with callable default value generates an invalid schema

Created on 21 Dec 2017  Â·  4Comments  Â·  Source: axnsan12/drf-yasg

Hi @axnsan12, cool project! I tried it out in https://github.com/yunity/karrot-backend and stumbled upon this error:

TypeError: Object of type 'CurrentUserDefault' is not JSON serializable
(full stacktrace)

One of our serializers uses DRFs CurrentUserDefault like this:

class FeedbackSerializer(serializers.ModelSerializer):
    class Meta:
        model = FeedbackModel
        fields = ['given_by']
        extra_kwargs = {'given_by': {'default': serializers.CurrentUserDefault()}}

When enabling validation, flex throws this error:

flex.exceptions.ValidationError: 'definitions':
    - 'Feedback':
        - 'properties':
            - 'properties':
                - 'given_by':
                    - 'default':
                        - "The value of `default` must be of one of the declared types for the schema. `CurrentUserDefault()` is not one of `['string']`"
    - 'User':
        - 'default':
            - "The value of `default` must be of one of the declared types for the schema. `CurrentUserDefault()` is not one of `['object']`"

Is it feasible to add support for CurrentUserDefault into drf-yasg?

bug

All 4 comments

Oh, just noticed #24, so I'll close this issue again.

If it does throw an error then this issue does indeed need to be handled. As said in #24, it cannot be handled by making it work, but one would at least expect it to not crash and just not populate the default value.

I'll look into it.

Fixed and pushed 1.0.6.

Can you check if that works for your case?

It works great ✨ Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

agethecoolguy picture agethecoolguy  Â·  4Comments

geekashu picture geekashu  Â·  5Comments

Amoki picture Amoki  Â·  4Comments

phihag picture phihag  Â·  5Comments

iamhssingh picture iamhssingh  Â·  3Comments