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

Fix readonly related fields generating incorrect schema #275

Conversation

diesieben07
Copy link
Contributor

This fixes the issue outlined in #274. I've added a test for this case as well.

@codecov
Copy link

codecov bot commented Jan 28, 2021

Codecov Report

Merging #275 (23ca3a8) into master (f23de89) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #275      +/-   ##
==========================================
+ Coverage   98.37%   98.39%   +0.01%     
==========================================
  Files          53       53              
  Lines        4438     4476      +38     
==========================================
+ Hits         4366     4404      +38     
  Misses         72       72              
Impacted Files Coverage Δ
drf_spectacular/openapi.py 96.29% <100.00%> (-0.01%) ⬇️
tests/test_regressions.py 100.00% <100.00%> (ø)
drf_spectacular/settings.py 100.00% <0.00%> (ø)
tests/test_extend_schema.py 100.00% <0.00%> (ø)
drf_spectacular/utils.py 98.76% <0.00%> (+<0.01%) ⬆️
drf_spectacular/plumbing.py 96.50% <0.00%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f23de89...23ca3a8. Read the comment docs.

Copy link
Owner

@tfranzel tfranzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diesieben07 awesome catch! funny how such a bug can go unnoticed for so long.

however the bugfix does break in some cases. this test shows it with indirect_referenced_model_ro. field.field_name does not always apply.

for regressions we prefer consise minimalistic tests and put it in test_regressions.py.
also, i prefer to not add full yamls dumps for single bugs. i prepared this for your convenience. please use it instead.

def test_incorrect_foreignkey_type_on_readonly_field(no_warnings):
    class ReferencingModel(models.Model):
        id = models.UUIDField(primary_key=True)
        referenced_model = models.ForeignKey(SimpleModel, on_delete=models.CASCADE)
        referenced_model_ro = models.ForeignKey(SimpleModel, on_delete=models.CASCADE)
        referenced_model_m2m = models.ManyToManyField(SimpleModel)
        referenced_model_m2m_ro = models.ManyToManyField(SimpleModel)

    class ReferencingModelSerializer(serializers.ModelSerializer):
        indirect_referenced_model_ro = serializers.PrimaryKeyRelatedField(
            source='referenced_model',
            read_only=True,
        )

        class Meta:
            fields = '__all__'
            read_only_fields = ['id', 'referenced_model_ro', 'referenced_model_m2m_ro']
            model = ReferencingModel

    class ReferencingModelViewset(viewsets.ModelViewSet):
        serializer_class = ReferencingModelSerializer
        queryset = ReferencingModel.objects.all()

    schema = generate_schema('/x/', ReferencingModelViewset)
    properties = schema['components']['schemas']['ReferencingModel']['properties']

    assert properties['referenced_model']['type'] == 'integer'
    assert properties['referenced_model_ro']['type'] == 'integer'
    assert properties['referenced_model_m2m']['items']['type'] == 'integer'
    assert properties['referenced_model_m2m_ro']['items']['type'] == 'integer'
    assert properties['indirect_referenced_model_ro']['type'] == 'integer'

@diesieben07
Copy link
Contributor Author

Thanks for the note about field_name, I had totally forgot about source. I've switched the code to your smaller regression test as well.

@diesieben07 diesieben07 requested a review from tfranzel January 31, 2021 15:57
Copy link
Owner

@tfranzel tfranzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excellent work! thank you so much. still puzzled how this went unnoticed for so long.

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

Successfully merging this pull request may close these issues.

2 participants