Skip to content

Add URL to the vulnerability and package details view in the API serializers - #1423

Merged
TG1999 merged 7 commits into
aboutcode-org:mainfrom
TG1999:fix_issue_1419
Feb 23, 2024
Merged

Add URL to the vulnerability and package details view in the API serializers#1423
TG1999 merged 7 commits into
aboutcode-org:mainfrom
TG1999:fix_issue_1419

Conversation

@TG1999

@TG1999 TG1999 commented Feb 19, 2024

Copy link
Copy Markdown
Contributor

Reference: #1419

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
@TG1999 TG1999 changed the title Fix issue 1419 Fix issue #1419 Feb 19, 2024
@TG1999 TG1999 changed the title Fix issue #1419 Add URL to the vulnerability and package details view in the API serializers Feb 19, 2024

@tdruez tdruez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
@TG1999

TG1999 commented Feb 19, 2024

Copy link
Copy Markdown
Contributor Author

@tdruez we already have get_absolute_url method on both Package and Vulnerability models, but it returns /packages/pkg:foo/bar and not public.vulnerablecode.io/packages/pkg:foo/bar, I have added a new method get_details_url for the needed purpose. Please review, thanks!

@tdruez tdruez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your implementation has too much duplication.
When copy/pasting things around in Python, it's usually time for refactoring.

You can create a generic mixin including:

    def get_resource_url(self, instance):
        """
        Return the instance fully qualified URL including the schema and domain.

        Usage:
            resource_url = serializers.SerializerMethodField()
        """
        resource_url = obj.get_absolute_url()

        if request := self.context.get("request", None):
            return request.build_absolute_uri(location=resource_url)

        return resource_url

Then simply define a clean field resource_url = serializers.SerializerMethodField() on your serializers instead of overriding the to_representation

Also, the tests are failing.

TG1999 and others added 3 commits February 23, 2024 17:45
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
@tdruez

tdruez commented Feb 23, 2024

Copy link
Copy Markdown
Contributor

@TG1999 LGTM but the documentation check is failing. Ready to merge otherwise.

@TG1999
TG1999 merged commit 4e5ef60 into aboutcode-org:main Feb 23, 2024
@TG1999
TG1999 deleted the fix_issue_1419 branch February 23, 2024 18:05
TG1999 added a commit to TG1999/vulnerablecode that referenced this pull request Jul 19, 2024
… API serializers (aboutcode-org#1423)

* Add vulnerability_url in API

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

* Add package_url in API

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

* Fix tests

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

* Address review comments

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

* Address review comments

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

* Fix tests

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

---------

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants