marshmallow-code / marshmallow-code/flask-marshmallow

Allow additional URL parameters to HyperlinkRelated

Open
#144 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
887
Forks
65
Avg merge
7h 25m
Merged PRs (30d)
3

Description

This is basically a reiteration of #54.

I have an API where a report conceptually has multiple samples. Thus, the sample URL has two parameters, e.g. (my example uses Flask-Restful):
```python
restful.add_resource(Sample, '/reports//samples/')
```

And then the marshmallow schema uses `HyperlinkRelated`:
```python
class ReportSchema(ModelSchema):
samples = ma.List(ma.HyperlinkRelated('rest_api.sample', url_key='sample_id'))
```

This will then fail with:
```
werkzeug.routing.BuildError: Could not build url for endpoint 'rest_api.sample' with values ['sample_id']. Did you forget to specify values ['report_id']?
```

Notably, this also doesn't work with `URLFor()`, because that can only take attributes from the parent object, not the relationship objects:
```python
class ReportSchema(ModelSchema):
samples = ma.List(ma.URLFor("rest_api.sample", sample_id='', report_id=''))
```
```
AttributeError: 'sample_id' is not a valid attribute of
```

***

What we need is a dictionary that maps URL parameters to fields on the related object, e.g.
```python
class ReportSchema(ModelSchema):
samples = ma.List(ma.HyperlinkRelated('rest_api.sample', url_map={
'sample_id': 'sample_id',
'report_id': 'report_id'
}))
```
In this case, the `url_map` field means "fill in the missing segments of the URL by using fields from the relationship object".

Contributor guide

Open the contributing guide

Research direction

Start at the HyperlinkRelated and URLFor entry points, then trace how endpoint values are assembled for related objects. Done means the proposed url_map can supply multiple URL parameters from each relationship object, allowing nested report/sample URLs to build without the missing report_id error.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.