Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allowformat through GET validation filtering#535

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

serenecloud
Copy link

@serenecloudserenecloud commentedDec 28, 2018
edited
Loading

Issue

The guides onhttps://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#configuration recommend including therest_framework_json_api.filters.QueryParameterValidationFilter in initial configuration which is good advice, but by default, the Django REST Framework UI provides a dropdown which allows you to pickformat=api orformat=vnd.api+json

screenshot at 2018-12-28 15-10-51

Without allowing theformat parameter through you get the following error:

HTTP 400 Bad RequestAllow: GET, POST, HEAD, OPTIONSContent-Type: application/vnd.api+jsonVary: Accept{    "errors": [        {            "detail": "invalid query parameter: format",            "source": {                "pointer": "/data"            },            "status": "400"        }    ]}

Description of the Change

This change addsformat toquery_regex

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • changelog entry added toCHANGELOG.md
  • author name inAUTHORS

@codecov
Copy link

codecovbot commentedDec 28, 2018
edited
Loading

Codecov Report

Merging#535 intomaster willnot change coverage.
The diff coverage is100%.

Impacted file tree graph

@@           Coverage Diff           @@##           master     #535   +/-   ##=======================================  Coverage   94.33%   94.33%           =======================================  Files          60       60             Lines        3725     3725           =======================================  Hits         3514     3514             Misses        211      211
Impacted FilesCoverage Δ
rest_framework_json_api/filters.py100% <100%> (ø)⬆️

Continue to review full report at Codecov.

Legend -Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing data
Powered byCodecov. Last updatecc64dad...7a0d0b6. Read thecomment docs.

@sliverc
Copy link
Member

Not sure how to understandhttps://jsonapi.org/format/#query-parameters whether it allows additional query parameters or not.

Another way would be to changeURL_FORMAT_OVERRIDE.

@n2ygk What do you think?

@sliverc
Copy link
Member

JSON API version 1.1 Release candidate gets a bit more specific on query parameters, seehttps://jsonapi.org/format/1.1/#query-parameters-custom

This means for a valid implementation of format query parameter,URL_FORMAT_OVERRIDE would need to be set tofilter[format]. This sounds a bit odd though but defining aprofile is properly a bit of a overkill.

auvipy reacted with heart emoji

@n2ygk
Copy link
Contributor

Not sure how to understandhttps://jsonapi.org/format/#query-parameters whether it allows additional query parameters or not.

Another way would be to changeURL_FORMAT_OVERRIDE.

@n2ygk What do you think?

It says you can have additional "non-standard" query parameters but with the "additional requirement that they MUST contain at least one non a-z character" in order to avoid naming collisions. It's their version of anX- header.

I personally don't like use of theformat= query parameter despite what the browseable API offers (which is a carryover from DRF which has no rules about query parameters). The functionality can be achieve usingAccepts: application/vnd.api+json header. Perhaps best to overrideformat to be something containing a non a-z character? It's not really a JSON API compliance thing since it's just used by the Browseable API: an HTML response is not covered by JSON API and it's not afilter.

@sliverc
Copy link
Member

What we can do is to allow query parameter which set byURL_FORMAT_OVERRIDE in QueryParameterValidationFilter and add a recommendation to setURL_FORMAT_OVERRIDE tocontentFormat.

n2ygk reacted with thumbs up emoji

@sliverc
Copy link
Member

@serenecloud Are you open to adjust your PR ascommented?

@serenecloud
Copy link
Author

@sliverc I'm not sure what the change involves at this point. Changing tocontentFormat would require a change on the Django end to have this working, yes?

@sliverc
Copy link
Member

@serenecloud
In the README.md there is a section how rest framework needs to be configured to work well with DJA. There the config optionURL_FORMAT_OVERRIDE would need to set tocontentFormat as a recommendation. The QueryParameterValidationFilter would need to be adjusted not to hard codeformat query parameter but use the URL_FORMAT_OVERRIDE value.

Copy link
Contributor

@n2ygkn2ygk left a comment

Choose a reason for hiding this comment

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

I'm not happy with hardcoding allowing "format" query parameter since that violates the jsonapi spec in that a non-standard QP has a "standard looking" name. Furthermore, anything that is not 'Application/vnd.api+json' is not jsonapi and it should only be used when the non-jsonapi DRF browseable API is used during development -- it should not be used in production.

@sliverc Does the Browseable API code respect the value ofFORMAT_SUFFIX_KWARG? Then perhapsquery_regex could include the value of that kwarg. This still "breaks" jsonapi if the default is used.

Perhaps just document that if you intend to use the browseable API, you should subclass the QueryParameterValdationFilter and overridequery_regex?

@sliverc
Copy link
Member

@n2ygk I am a bit confused on your comment as you have already upvoted mycomment and I thought you agreed with it. Browsable API can be used in production and we even document it this way in the README.md so it needs to work.
FORMAT_SUFFIX_KWARG is only used for custom url patterns and what we are looking for isURL_FORMAT_OVERRIDE and README.md simply needs to be adjusted to configure this according to JSON API spec (like contentFormat).

We can simply ignore URL_FORMAT_OVERRIDE in QueryParameterValidationFilter if it is not changed from format to something JSON API valid but to be honest this rather sounds like "perfect is the enemy of good"... 😄 if someone wants to break json api spec we cannot stop them, but as long as our recommend configuration doesn't we are safe.

@n2ygk
Copy link
Contributor

n2ygk commentedFeb 18, 2019
edited
Loading

@n2ygk I am a bit confused on your comment as you have already upvoted mycomment and I thought you agreed with it. Browsable API can be used in production and we even document it this way in the README.md so it needs to work.
FORMAT_SUFFIX_KWARG is only used for custom url patterns and what we are looking for isURL_FORMAT_OVERRIDE and README.md simply needs to be adjusted to configure this according to JSON API spec (like contentFormat).

We can simply ignore URL_FORMAT_OVERRIDE in QueryParameterValidationFilter if it is not changed from format to something JSON API valid but to be honest this rather sounds like "perfect is the enemy of good"... 😄 if someone wants to break json api spec we cannot stop them, but as long as our recommend configuration doesn't we are safe.

I upvoted your comment but the PR hardcodes "format" at0da5fc2

I'm happy with them breaking the spec explicitly as they like, but this makes the default be to allowformat in the Query Parameters. (And yes, I should have commented on the specific code change, sorry.)

And I missed that about using the browsable API in production. I guess to be "technical" that's not a JSONAPI interaction at that point.

@n2ygk
Copy link
Contributor

I'm not sure what I'm doing differently, but the browseable API seems to work for me with no special setup. Not sure why that is. Is theformat QP processed and popped before theQueryParameterValidationFilter backend? See mysettings. Will investigate further and report back.

@sliverc
Copy link
Member

@n2ygk
Ahh now I think I understand where the confusion comes from.. 😄 I think you have answered@serenecloud question before I did but in the GitHub history my comment comes above yours (at least on my view) but the timestamp of your answer is actually before mine...
So I have thought you have answered to mycomment so I was confused... Hope this makes sense to you.

So to clarify are you ok with this PR being adjusted as I have outlined incomment? It is just rephrasing what is basically written in the previouscomment but includes how to do it.

@sliverc
Copy link
Member

sliverc commentedFeb 18, 2019
edited
Loading

@n2ygk
Our answers overlapped again 😉 Sometimes a chat application would be the better way of communication 😄

I think you need to configure another renderer likerest_framework.renderers.JSONRenderer to activate the option as seen in the screenshot of initial PR comment.

@n2ygk
Copy link
Contributor

@n2ygk
Our answers overlapped again 😉 Sometimes a chat application would be the better way of communication 😄

I think you need to configure another renderer likerest_framework.renders.JSONRenderer to activate the option as seen in the screenshot of initial PR comment.

Let me quote your reply to sync up;-)

I see what happened in my#535 (comment). If I just GET /resource in the browseable API, no format QP is sent in yet it seems to work right, I guess because of a default somewhere. If I explicitly add?format=api via the GET drop-down in the UI then I can reproduce@serenecloud's original issue.

Per your#535 (comment) I still think that "silently" checking whether it's for hardcodedformat or theURL_FORMAT_OVERRIDE value (which defaults toformat) still means that the default action will be to allow a non-JSONAPI compliant query parameter to pass strictQueryParameterValidationFilter. Maybe be more explicit with a DJA setting orQueryParameterValidatioFilter attribute to say it's being allowed or vice-versa? Then the documented default can allow it explicitly and when I deploy a backend service that is strictly jsonapi, I can make sure I am not allowing that QP to sneak in.

@sliverc
Copy link
Member

@n2ygk
What we could do is to log a warning whenURL_FORMAT_OVERRIDE is set toformat and a uri contains aformat query parameter, noting user to configure URL_FORMAT_OVERRIDE to contentFormat. How does this sound?

@n2ygk
Copy link
Contributor

n2ygk commentedFeb 19, 2019 via email

The client app won’t see the error. Jsonapi makes the point of beingclient-friendly by reporting useful errors. (Something needing improvementsin DJA). Let’s say I have an attribute called format and I meant‘filter[format]=api’ but instead I gave ‘format=api’....Better to make this explicitly configured.
On Tue, Feb 19, 2019 at 2:48 AM Oliver Sauder ***@***.***> wrote:@n2ygk <https://github.com/n2ygk> What we could do is to log a warning when URL_FORMAT_OVERRIDE <https://www.django-rest-framework.org/api-guide/settings/#url_format_override> is set to format and a uri contains a format query parameter, noting user to configure URL_FORMAT_OVERRIDE to contentFormat. How does this sound? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#535 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AEJ5d6_Vt0CSm-eZwM15F3p8anrE1-JDks5vO6wwgaJpZM4ZjXp1> .

@sliverc
Copy link
Member

@n2ygk
I see your point but I think we should really avoid adding additional configuration options for what is available in DRF.
Instead let's raise an error after all whenURL_FORMAT_OVERRIDE isformat and query parameter isformat.

@n2ygk
Copy link
Contributor

n2ygk commentedFeb 24, 2019
edited
Loading

@n2ygk
I see your point but I think we should really avoid adding additional configuration options for what is available in DRF.
Instead let's raise an error after all whenURL_FORMAT_OVERRIDE isformat and query parameter isformat.

@sliverc
I don't quite understand what you mean here. The default value of URL_FORMAT_OVERRIDE isformat. The Browseable API requiresformat=api. So how does one explicitly allow the DRF browseable APIor explicitly enforce only valid jsonapi query parameters?

@sliverc
Copy link
Member

@n2ygk
We document to changeURL_FORMAT_OVERRIDE tocontentFormat in our README.
In theQueryParameterValidationFilter it's checked whether a query parameter is passed on which is equal configured value inURL_FORMAT_OVERRIDE. If this is true and the value ofURL_FORMAT_OVERRIDE is set to something else thanformat then we let the user pass. IfURL_FORMAT_OVERRIDE is still set toformat a 400 error is raised (as would be the case today already without this change).

n2ygk reacted with thumbs up emoji

@n2ygk
Copy link
Contributor

@sliverc Where'd we end up with this? I've lost track.

@sliverc
Copy link
Member

@n2ygk This PR should be adjusted as outlined in#535 (comment)

@sliverc
Copy link
Member

Closing in favor of#812

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@n2ygkn2ygkn2ygk requested changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@serenecloud@sliverc@n2ygk

[8]ページ先頭

©2009-2025 Movatter.jp