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

Allow custom primary key #23

Open
mahenzon opened this issue Oct 7, 2020 · 3 comments
Open

Allow custom primary key #23

mahenzon opened this issue Oct 7, 2020 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers hacktoberfest

Comments

@mahenzon
Copy link
Collaborator

mahenzon commented Oct 7, 2020

There's an option to set custom url_field, but no way to do patch, because id is hardcoded

if "id" not in json_data["data"]:
raise BadRequest('Missing id in "data" node', source={"pointer": "/data/id"})
if str(json_data["data"]["id"]) != str(kwargs[getattr(self._data_layer, "url_field", "id")]):
raise BadRequest(
"Value of id does not match the resource identifier in url", source={"pointer": "/data/id"}
)

I know that another pk field is violation of JSON:API spec, but this feature would be neat

@mahenzon mahenzon added enhancement New feature or request good first issue Good for newcomers hacktoberfest labels Oct 7, 2020
@Znbiz
Copy link
Contributor

Znbiz commented Oct 7, 2020

This required is dictated the https://marshmallow-jsonapi.readthedocs.io/en/latest/_modules/marshmallow_jsonapi/schema.html

"""Schema class that formats data according to JSON API 1.0.
    Must define the ``type_`` `class Meta` option.

    Example: ::

        from marshmallow_jsonapi import Schema, fields

        def dasherize(text):
            return text.replace('_', '-')

        class PostSchema(Schema):
            id = fields.Str(dump_only=True)  # Required
            title = fields.Str()

            author = fields.HyperlinkRelated(
                '/authors/{author_id}',
                url_kwargs={'author_id': '<author.id>'},
            )

            comments = fields.HyperlinkRelated(
                '/posts/{post_id}/comments',
                url_kwargs={'post_id': '<id>'},
                # Include resource linkage
                many=True, include_resource_linkage=True,
                type_='comments'
            )

            class Meta:
                type_ = 'posts'  # Required
                inflect = dasherize

    """

@mahenzon
Copy link
Collaborator Author

another use-case:

    api.route(
        views.CompanyDetail,
        "company_detail",
        "/api/company/<int:id>/",
        "/api/company/<string:company_number>/",
        tag="CompanyInfo",
    )

Swagger generates two endpoints but both require id. We have to make it more flexible

@mahenzon
Copy link
Collaborator Author

I suggest such a patch:

        pk_field_name = getattr(self._data_layer, "url_field", "id")

        if pk_field_name not in json_data["data"]:
            raise BadRequest(f'Missing {pk_field_name} in "data" node', source={"pointer": f"/data/{pk_field_name}"})
        if str(json_data["data"][pk_field_name]) != str(kwargs[pk_field_name]):
            raise BadRequest(
                "Value of id does not match the resource identifier in url", source={"pointer": f"/data/{pk_field_name}"}
            )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers hacktoberfest
Projects
None yet
Development

No branches or pull requests

2 participants