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

Create new mock request on each operation #250

Merged
merged 2 commits into from
Jan 20, 2021

Conversation

merll
Copy link
Contributor

@merll merll commented Jan 6, 2021

We have come across a few instances where views, filter backends etc. access properties of the request, for example the HTTP method. Since the mock request is only created on the first iteration, request path and method are identical for all following operations.

This PR changes the generator so that it creates a fresh mock request for each operation, avoiding further issues with introspection.

@tfranzel
Copy link
Owner

hi @merll as you saw this breaks the tests. pretty sure it also is not correct conceptually. if a versioned request comes in, the given version needs to be used for a correct schema. doing this potentially wipes out the given version.

your issue would most likely require a different resolution.

@codecov
Copy link

codecov bot commented Jan 20, 2021

Codecov Report

Merging #250 (93a7102) into master (882ade7) will decrease coverage by 0.06%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #250      +/-   ##
==========================================
- Coverage   98.32%   98.25%   -0.07%     
==========================================
  Files          53       53              
  Lines        4233     4366     +133     
==========================================
+ Hits         4162     4290     +128     
- Misses         71       76       +5     
Impacted Files Coverage Δ
drf_spectacular/generators.py 96.66% <85.71%> (+0.11%) ⬆️
drf_spectacular/types.py 94.73% <0.00%> (-5.27%) ⬇️
tests/conftest.py 92.68% <0.00%> (-2.44%) ⬇️
tests/contrib/test_django_filters.py 98.75% <0.00%> (-1.25%) ⬇️
drf_spectacular/hooks.py 100.00% <0.00%> (ø)
tests/test_polymorphic.py 100.00% <0.00%> (ø)
tests/test_regressions.py 100.00% <0.00%> (ø)
drf_spectacular/settings.py 100.00% <0.00%> (ø)
drf_spectacular/serializers.py 100.00% <0.00%> (ø)
... and 5 more

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 882ade7...93a7102. Read the comment docs.

@merll
Copy link
Contributor Author

merll commented Jan 20, 2021

I see. I pushed a change now that uses the incoming request if present, but still generates a new mock request on each iteration. That should fix the described side-effect without breaking existing behavior.

@tfranzel
Copy link
Owner

tfranzel commented Jan 20, 2021

excellent! i suppose you are using the CLI for generation, because the issue should be the same for a real request (via the schema API endpoint). will probably also have to look into the other case.

interesting that this has not come up before.

@tfranzel tfranzel merged commit cb3adcd into tfranzel:master Jan 20, 2021
@merll merll deleted the refresh-mock-request branch February 8, 2021 19:34
tfranzel added a commit that referenced this pull request May 2, 2021
we now always generate a mocked request, not just when schema is
genenerated over CLI. use incoming request as reference if available.
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.

None yet

2 participants