Skip to content
New issue

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

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

Already on GitHub? # to your account

Hiding Fields in Serializers #100

Closed
savitmk opened this issue Jun 19, 2020 · 6 comments
Closed

Hiding Fields in Serializers #100

savitmk opened this issue Jun 19, 2020 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@savitmk
Copy link

savitmk commented Jun 19, 2020

What would be the best method for hiding a subset of fields on a serializer?

@tfranzel
Copy link
Owner

there is no mechanism for that. It is likely doable with a custom Extension (https://drf-spectacular.readthedocs.io/en/latest/customization.html#declare-serializer-magic-with-openapiserializerextension) and if not with a postprocessing hook (step 5). not sure if its worth the effort though.

@savitmk
Copy link
Author

savitmk commented Jun 22, 2020

I tried creating a custom serializer field (e.g. MyField) and annotating it with @extend_schema_field(OpenApiTypes.NONE), which does not cause MyField to appear in the Swagger example request body, but MyField does appear in the request schema with an empty definition, MyField: {}.

Would it be incorrect for fields marked OpenApiTypes.NONE to not appear at all? Could OpenApiTypes include an IGNORE option which would mean the field is not written to the schema?

@tfranzel
Copy link
Owner

but MyField does appear in the request schema with an empty definition, MyField: {}.

i tried reproducing your statement with a test but was unable to fcee218#diff-05942919b2ed60a337f71617b2dbce8fR698

Would it be incorrect for fields marked OpenApiTypes.NONE to not appear at all
i agree and that was the original intention. if you can still produce this, please provide a code snippet for reproduction.

i also did a small improvement to OpenApiTypes.NONE. it should now be interchangable with None everywhere for convenience.

What would be the best method for hiding a subset of fields on a serializer?

i gave this a bit more thought and i think it is a reasonable feature. in fact it was a tiny change. extend_schema_serializer(exclude_fields=['MyField']) should do exactly what you want now.

@tfranzel tfranzel added the enhancement New feature or request label Jun 24, 2020
@tfranzel
Copy link
Owner

@savitmk could you test the changes and comment on the issue reproduction? thanks.

@savitmk
Copy link
Author

savitmk commented Jun 24, 2020

extend_schema_serializer(exclude_fields=['MyField'])

this is perfect, thank you!

comment on the issue reproduction

@extend_schema_field(OpenApiTypes.NONE)
class MyField(serializers.Field):
    pass

In my serializer:

class ExampleSerializer(serializers.Serializer):
    my_field = MyField()

On latest release v0.9.8, in the result swagger UI, in "Example Value" I do not see my_field. When I click "Schema" I see my_field: {}.

On latest master, I do not see my_field in the schema.

@tfranzel
Copy link
Owner

i tracked the bug. it was already fixed in 0.9.9 with 24fcef9. it was targeted at another problem but it also fixed this one. i think we can call this resolved.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants