Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspecting serializers doesn't honor read_only=True flag on nested serializers #239

Open
jw-lilly opened this issue Oct 25, 2018 · 3 comments
Labels
enhancement Enhancement proposal

Comments

@jw-lilly
Copy link

I'll probably end up submitting a fix for this myself, eventually, but I bet somebody else can fix this faster.

class ChildSerializer(serializers.Serializer):
    somefield = serializers.CharField()

class ParentSerializer(serializers.Serializer):
    stuff = ChildSerializer(read_only=True)
    otherfield = serializers.CharField(read_only=True)
    data = serializers.CharField()

In this example, I find that the swagger UI will display the stuff property (bug?) in the example create/post payload for an endpoint that uses this serializer, while it won't (as expected) show the otherfield property. I think this is a bug.

@AayushB
Copy link

AayushB commented Nov 8, 2018

I am seeing this same exact issue. Definitely a bug.

@axnsan12
Copy link
Owner

axnsan12 commented Nov 28, 2018

This is, unfortunately, quite hard to work around. The simple fix is to make all your serializers generate inline schemas (set ref_name = None in Meta), but then of course you lose the advantage of named models in your OpenAPI spec.

The main problem is that OpenAPI makes it hard to use a Schema by reference but override only some of its properties (you can do it with an allOf hack, but most tools have a hard time dealing with that type of construct). So the same model can be marked as readOnly either everywhere it's used or nowhere at all.

Another option would be to generate different models from the same serializer according to the required context, which goes into the same bucket as #70.

@axnsan12 axnsan12 added bug Bug report/fix enhancement Enhancement proposal and removed bug Bug report/fix labels Nov 28, 2018
@ivellios
Copy link
Contributor

If that helps, I have noticed it happens only for many=False (which is the default one in the example above). For many=True it works as expected.

Setting ref_name = None for the nested serializer's Meta class still is an acceptable workaround in most cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement proposal
Projects
None yet
Development

No branches or pull requests

4 participants