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

AbsoluteURLFor: Field parameters are passed to URL #67

Open
floqqi opened this issue Aug 24, 2017 · 1 comment
Open

AbsoluteURLFor: Field parameters are passed to URL #67

floqqi opened this issue Aug 24, 2017 · 1 comment

Comments

@floqqi
Copy link

floqqi commented Aug 24, 2017

I use AbsoluteURLFor for generating an URL to an image:

from flask_marshmallow import Marshmallow
from flask_marshmallow.fields import AbsoluteURLFor
from marshmallow import fields

ma = Marshmallow()

class TestSchema(ma.Schema):
    my_logo = AbsoluteURLFor(
        'logo_api',  # /api/logos/<id>
        id='<logo_id>',
        dump_to='myLogo'
    )

The resulting response is:

{
  "myLogo": "http://localhost:5000/api/logos/1?dump_to=myLogo"
}

The expected result is:

{
  "myLogo": "http://localhost:5000/api/logos/1"
}

If I skip the dump_to-parameter the result is:

{
  "my_logo": "http://localhost:5000/api/logos/1"
}

This is because the field parameter dump_to is passed as a parameter to flask.url_for. But this is (of course) not my intention. Can this be fixed or do I have to use another field like Function for example?

@mattig
Copy link

mattig commented Oct 29, 2018

I ran into this same limitation or side effect as I wanted to add description to the Field class metadata. One work-around looked something like this:

    player_url = AbsoluteURLFor('players.entry', player_id='<player_id>')
    player_url.metadata['description'] = "Fully qualified URL of the player resource"

I wrote a wrapper for this particular case:

def Url(endpoint, **kwargs):
    """
    Returns AbsoluteURLFor field. If 'kwargs' contains 'doc' it will not be passed to
    AbsoluteURLFor but instead added to the metadata of the parent class as 'description'.
    """
    doc = kwargs.pop('doc')
    url = AbsoluteURLFor(endpoint, **kwargs)
    if doc:
        url.metadata['description'] = doc
    return url

One potential fix is to tag which parameters should not be forwarded to url_for by prefixing them with an underscore.

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

No branches or pull requests

2 participants