marshmallow-jsonapi icon indicating copy to clipboard operation
marshmallow-jsonapi copied to clipboard

related_url doesn't get added if no kwargs

Open fhriley opened this issue 4 years ago • 3 comments

If you have a relationship with a related url of /foo/bar, you specify the relationship in the schema like this:

bar = fields.Relationship(
    related_url="/foo/bar",
    type_="bar",
)

This will never add the related URL to the output due to the following check in the code:

https://github.com/marshmallow-code/marshmallow-jsonapi/blob/540cf1be0832fb3255cfcb649250f57fcf76ccad/marshmallow_jsonapi/fields.py#L159

Instead, this code should do the following:

if non_null_params:
    return self.related_url.format(**non_null_params)
else:
    return self.related_url

This makes more sense because we've already checked that the related_url was specified:

https://github.com/marshmallow-code/marshmallow-jsonapi/blob/540cf1be0832fb3255cfcb649250f57fcf76ccad/marshmallow_jsonapi/fields.py#L154

and it doesn't make sense to just ignore it.

fhriley avatar Dec 04 '19 21:12 fhriley

Although you seem to be right about this behaviour, I wonder what the point is of having a relationship that doesn't use any kwargs. Normally the related URL would be /posts/1/comments, or /authors/1 , both of which use an argument. What is your use-case for a hard-coded URL?

multimeric avatar Dec 04 '19 23:12 multimeric

It is a one to one relationship and there will never be more than one. For example, there is one 10 GigE interface and that interface has one SFP, which is in the API as /xgige/sfp. This is much cleaner than having /xgiges/1/sfps/1, which also doesn't make sense to clients of the api since there aren't multiple interfaces.

fhriley avatar Dec 04 '19 23:12 fhriley

Yeah okay I think that makes sense. Should be an easy fix.

multimeric avatar Dec 05 '19 00:12 multimeric