self links are constructed using the dumped data, not the original input
See original GitHub issueI have the following schema:
from marshmallow_jsonapi.flask import Schema
class SampleDataSchema(Schema):
class Meta:
type_ = 'sample_data'
self_view = 'rest_api.sampledata'
self_view_many = 'rest_api.sampledata'
self_view_kwargs = {
'sample_id': '<sample_id>'
}
id = fields.Integer(attribute='sample_data_id')
value = fields.String()
# These methods have been removed since they're irrelevant to my issue
key = ma.Method('type_key')
section = ma.Method('type_section')
A model:
class SampleData(db.Model, CRUDMixin):
__tablename__ = "sample_data"
sample_data_id = Column(Integer, primary_key=True)
sample_id = Column(Integer, ForeignKey('sample.sample_id', ondelete='CASCADE'), index=True, nullable=False)
value = Column(Unicode)
sample = relationship('Sample', back_populates='data')
And a view function:
def get(self, sample_id):
samples = db.session.query(
models.SampleData
).options(
joinedload(models.SampleData.data_type)
).filter(
models.SampleData.sample_id == sample_id
).all()
return schemas.SampleDataSchema(many=True).dump(samples)
However, when I hit the view endpoint, I get this error:
File "/home/michael/Programming/MegaQC/venv/lib/python3.6/site-packages/marshmallow/schema.py", line 545, in dump
POST_DUMP, result, many=many, original_data=obj
File "/home/michael/Programming/MegaQC/venv/lib/python3.6/site-packages/marshmallow/schema.py", line 993, in _invoke_dump_processors
tag, pass_many=True, data=data, many=many, original_data=original_data
File "/home/michael/Programming/MegaQC/venv/lib/python3.6/site-packages/marshmallow/schema.py", line 1122, in _invoke_processors
data = processor(data, many=many, **kwargs)
File "/home/michael/Programming/MegaQC/venv/lib/python3.6/site-packages/marshmallow_jsonapi/schema.py", line 135, in format_json_api_response
ret = self.format_items(data, many)
File "/home/michael/Programming/MegaQC/venv/lib/python3.6/site-packages/marshmallow_jsonapi/schema.py", line 392, in format_items
return [self.format_item(item) for item in data]
File "/home/michael/Programming/MegaQC/venv/lib/python3.6/site-packages/marshmallow_jsonapi/schema.py", line 392, in <listcomp>
return [self.format_item(item) for item in data]
File "/home/michael/Programming/MegaQC/venv/lib/python3.6/site-packages/marshmallow_jsonapi/schema.py", line 381, in format_item
links = self.get_resource_links(item)
File "/home/michael/Programming/MegaQC/venv/lib/python3.6/site-packages/marshmallow_jsonapi/schema.py", line 413, in get_resource_links
kwargs = resolve_params(item, self.opts.self_url_kwargs or {})
File "/home/michael/Programming/MegaQC/venv/lib/python3.6/site-packages/marshmallow_jsonapi/utils.py", line 58, in resolve_params
"attribute of {obj!r}".format(attr_name=attr_name, obj=obj)
AttributeError: 'sample_id' is not a valid attribute of {'value': '20.fastq', 'id': 501, 'key': 'fastqc__Filename', 'section': 'fastqc'}
So, from the last line it’s obvious that marshmallow-jsonapi is trying to pull the field sample_id
from the dumped data, since it has an id
field, which was not on the original model. However, this makes very little sense to me. We often want to generate a link using the model, but not to include the foreign key in the final data. Shouldn’t we generate the self links using the original record, like we do for relationship links?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Why do I get "Pickle - EOFError: Ran out of input" reading an ...
I would check that the file is not empty first: ... A typical construct for reading from an Unpickler object would be like...
Read more >pickle — Python object serialization — Python 3.11.1 ...
Data stream format¶ · Protocol version 0 is the original “human-readable” protocol and is backwards compatible with earlier versions of Python. · Protocol...
Read more >ElasticDump - GitHub
If the --direction is dump , which is the default, --input MUST be a URL for the base location of an ElasticSearch server...
Read more >TensorFlow Lite inference
You must load the .tflite model into memory, which contains the model's execution graph. ... Raw input data for the model generally does...
Read more >Working with JSON in Swift - Swift Blog - Apple Developer
Swift's built-in language features make it easy to safely extract and work with JSON data decoded with Foundation APIs — without the need ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s fine, I totally understand. I’m just putting them here so that they’re better documented so I or someone else can solve them down the track. And the workarounds might help people in the meantime. Please don’t feel like I’m demanding that they be fixed or anything.
Incidentally, I think the best way to solve this would be to make
format_json_api_response
use@post_dump(pass_original=True)
, which is a flag I didn’t know about when I first made this issue.