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

fix: Replace target URL for DOI items #365

Merged
merged 11 commits into from
Aug 3, 2023
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ jobs:
python-version: "3.10"
- uses: actions/checkout@v3
- name: Install mypy
run: pip install "mypy<1.0.0" && pip install -r requirements.txt
run: pip install mypy && pip install -r requirements.txt
- name: Run mypy
run: mypy --ignore-missing-imports --install-types --non-interactive .
run: mkdir .mypy_cache && mypy --ignore-missing-imports --install-types --non-interactive .
- name: Build the Docker image
run: docker compose -f docker-compose.test.yml build
- name: Run tests
Expand Down
14 changes: 12 additions & 2 deletions bibxml/xml2rfc_adapters.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"""
Defines adapters for xml2rfc paths. See :term:`xml2rfc adapter`.
"""

from typing import Optional, List, cast, Sequence
import typing
kesara marked this conversation as resolved.
Show resolved Hide resolved
from typing import Optional, List, cast, Sequence, Iterable
import urllib
import logging
import re
from urllib.parse import urlparse

from relaton.models import Link
from relaton.models.bibdata import BibliographicItem, DocID, VersionInfo

from bib_models.util import get_primary_docid
from common.util import as_list
from doi.crossref import get_bibitem as get_doi_bibitem
from datatracker.internet_drafts import get_internet_draft
from datatracker.internet_drafts import remove_version
Expand Down Expand Up @@ -626,6 +629,13 @@ def resolve(self) -> BibliographicItem:
if not result:
raise RefNotFoundError()
else:
# https://github.com/ietf-tools/bibxml-service/issues/332
link = as_list(result.bibitem.link or [])
for index, _ in enumerate(link):
parsed_link = urlparse(typing.cast(typing.List[Link],result.bibitem.link[index]).content)
stefanomunarini marked this conversation as resolved.
Show resolved Hide resolved
if parsed_link.netloc == "dx.doi.org":
typing.cast(typing.List[Link], result.bibitem.link[index]).content = \
parsed_link._replace(scheme="https")._replace(netloc="doi.org").geturl()
return result.bibitem

def format_anchor(self) -> Optional[str]:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ requests_oauthlib>=1.3.1,<1.4
requests_cache>=0.7.4,<1.0
flake8
whitenoise>=5.3.0,<6.0
pydantic>=1.9.1,<1.10
pydantic>=1.10
crossrefapi>=1.5,<2
simplejson
sentry-sdk
Expand Down
19 changes: 18 additions & 1 deletion xml2rfc_compat/tests/test_adapters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from urllib.parse import urlparse

from django.test import TestCase

from bib_models import BibliographicItem
from bibxml.settings import XML2RFC_PATH_PREFIX
from bibxml.xml2rfc_adapters import RfcAdapter, MiscAdapter, InternetDraftsAdapter, W3cAdapter, ThreeGPPAdapter, \
IeeeAdapter, IanaAdapter, RfcSubseriesAdapter, NistAdapter
IeeeAdapter, IanaAdapter, RfcSubseriesAdapter, NistAdapter, DoiAdapter
from common.util import as_list
from main.exceptions import RefNotFoundError


Expand Down Expand Up @@ -139,3 +142,17 @@ def test_nist_not_found(self):
adapter = NistAdapter(self.dirname, "bibxml-nist", self.nist_ref+"A")
with self.assertRaises(RefNotFoundError):
adapter.resolve()

def test_doi(self):
adapter = DoiAdapter(self.dirname, "bibxml7", self.doi_ref)
bibitem = adapter.resolve()
self._assert_is_instance_of_bibliographicitem(bibitem)
self._assert_refs_equal(bibitem, self.doi_ref)

def test_doi_should_replace_target_URL(self):
adapter = DoiAdapter(self.dirname, "bibxml7", self.doi_ref)
bibitem = adapter.resolve()
for link in as_list(bibitem.link or []):
if content := link.__getattribute__("content"):
url_parse = urlparse(content)
self.assertNotEqual("http://dx.doi.org", f"{url_parse.scheme}://{url_parse.netloc}")
kesara marked this conversation as resolved.
Show resolved Hide resolved
Loading