Repository: miLibris/flask-rest-jsonapi

Issue:
Relationships containing hyphens in url
Having endpoint definition containing hyphens as the last uri token such as `/persons/<int:person_id>/relationships/computers-owned` lead to error `PersonSchema has no attribute computers-owned`.

Effectively, due to python language, Schema attribute name cannot contains hyphens in their name.

At least we have to replace '-' by '_'.

Discussion:

Patch:
diff --git a/flask_rest_jsonapi/resource.py b/flask_rest_jsonapi/resource.py
index 0504c33..6136f20 100644
--- a/flask_rest_jsonapi/resource.py
+++ b/flask_rest_jsonapi/resource.py
@@ -514,7 +514,7 @@ class ResourceRelationship(with_metaclass(ResourceMeta, Resource)):
 
     def _get_relationship_data(self):
         """Get useful data for relationship management"""
-        relationship_field = request.path.split('/')[-1]
+        relationship_field = request.path.split('/')[-1].replace('-', '_')
 
         if relationship_field not in get_relationships(self.schema):
             raise RelationNotFound("{} has no attribute {}".format(self.schema.__name__, relationship_field))


Test patch:
diff --git a/tests/test_sqlalchemy_data_layer.py b/tests/test_sqlalchemy_data_layer.py
index d5f62a3..7d6270d 100644
--- a/tests/test_sqlalchemy_data_layer.py
+++ b/tests/test_sqlalchemy_data_layer.py
@@ -38,6 +38,7 @@ def person_model(base):
         name = Column(String, nullable=False)
         birth_date = Column(DateTime)
         computers = relationship("Computer", backref="person")
+        computers_owned = relationship("Computer")
     yield Person
 
 
@@ -124,6 +125,7 @@ def person_schema():
                                  schema='ComputerSchema',
                                  type_='computer',
                                  many=True)
+        computers_owned = computers
     yield PersonSchema
 
 
@@ -301,6 +303,7 @@ def register_routes(client, app, api_blueprint, person_list, person_detail, pers
     api.route(person_list, 'person_list', '/persons')
     api.route(person_detail, 'person_detail', '/persons/<int:person_id>')
     api.route(person_computers, 'person_computers', '/persons/<int:person_id>/relationships/computers')
+    api.route(person_computers, 'person_computers_owned', '/persons/<int:person_id>/relationships/computers-owned')
     api.route(person_computers, 'person_computers_error', '/persons/<int:person_id>/relationships/computer')
     api.route(person_list_raise_jsonapiexception, 'person_list_jsonapiexception', '/persons_jsonapiexception')
     api.route(person_list_raise_exception, 'person_list_exception', '/persons_exception')
@@ -1549,3 +1552,8 @@ def test_api_resources(app, person_list):
     api = Api()
     api.route(person_list, 'person_list2', '/persons', '/person_list')
     api.init_app(app)
+
+
+def test_relationship_containing_hyphens(client, register_routes, person_computers, computer_schema, person):
+    response = client.get('/persons/{}/relationships/computers-owned'.format(person.person_id), content_type='application/vnd.api+json')
+    assert response.status_code == 200