8000 fix(gen-jsonld): handle multiple contexts by Silvanoc · Pull Request #2602 · linkml/linkml · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(gen-jsonld): handle multiple contexts #2602

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions linkml/generators/jsonldgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import os
from copy import deepcopy
from dataclasses import dataclass
from typing import Any, Optional
from dataclasses import dataclass, field
from typing import Any, Optional, Union

Check warning on line 6 in linkml/generators/jsonldgen.py

View check run for this annotation

Codecov / codecov/patch

linkml/generators/jsonldgen.py#L5-L6

Added lines #L5 - L6 were not covered by tests

import click
from jsonasobj2 import as_json, items, loads
Expand Down Expand Up @@ -54,7 +54,7 @@
original_schema: SchemaDefinition = None
"""See https://github.com/linkml/linkml/issues/871"""

context: str = None
context: Optional[Union[str, list[str]]] = field(default_factory=list)

Check warning on line 57 in linkml/generators/jsonldgen.py

View check run for this annotation

Codecov / codecov/patch

linkml/generators/jsonldgen.py#L57

Added line #L57 was not covered by tests
"""Path to a JSONLD context file"""

def __post_init__(self) -> None:
Expand Down Expand Up @@ -149,14 +149,14 @@
def visit_subset(self, ss: SubsetDefinition) -> None:
self._visit(ss)

def end_schema(self, context: str = None, **_) -> str:
def end_schema(self, context: Optional[Union[str, list[str]]] = None, **_) -> str:

Check warning on line 152 in linkml/generators/jsonldgen.py

View check run for this annotation

Codecov / codecov/patch

linkml/generators/jsonldgen.py#L152

Added line #L152 was not covered by tests
self._add_type(self.schema)
base_prefix = self.default_prefix()

# TODO: fix this, see https://github.com/linkml/linkml/issues/871
# JSON LD adjusts context reference using '@base'. If context is supplied and not a URI, generate an
# absolute URI for it
if context is None and self.format == "jsonld":
if not context and self.format == "jsonld":
# TODO: Once we get pyld running w/ relative contexts, we need to figure out how to generate and add
# the relative (?) context reference below
# model_context = self.schema.source_file.replace('.yaml', '.prefixes.context.jsonld')
Expand Down Expand Up @@ -189,11 +189,16 @@
return out


# Option "context" can be specified multiple times.
# Using a callback to process "context" to get a list instead of a tuple,
# because the context can get extended later on.
@shared_arguments(JSONLDGenerator)
@click.command(name="jsonld")
@click.option(
"--context",
multiple=True,
type=click.STRING,
callback=lambda ctx, param, value: list(value),
help=f"JSONLD context file (default: {METAMODEL_CONTEXT_URI} and <model>.prefixes.context.jsonld)",
)
@click.version_option(__version__, "-V", "--version")
Expand Down
39 changes: 39 additions & 0 deletions tests/test_scripts/__snapshots__/genjsonld/simple_uri_test.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,45 @@
"generation_date": "2000-01-01T00:00:00",
"@type": "SchemaDefinition",
"@context": [
"https://w3id.org/linkml/meta.context.jsonld",
{
"xsd": "http://www.w3.org/2001/XMLSchema#",
"bar": {
"@id": "http://samples.r.us/bar_",
"@prefix": true
},
"foo": "http://samples.r.us/foo#",
"linkml": "https://w3id.org/linkml/",
"simple_uri_test": "https://raw.githubusercontent.com/linkml/linkml/main/tests/test_scripts/input/simple_uri_test/",
"@vocab": "https://raw.githubusercontent.com/linkml/linkml/main/tests/test_scripts/input/simple_uri_test/",
"tag": {
"@type": "xsd:anyURI",
"@id": "linkml:tag"
},
"value": {
"@type": "linkml:Any",
"@id": "linkml:value"
},
"extensions": {
"@type": "linkml:Extension",
"@id": "linkml:extensions"
},
"state": {
"@id": "foo:state"
},
"AnyValue": {
"@id": "linkml:Any"
},
"Extensible": {
"@id": "linkml:Extensible"
},
"Extension": {
"@id": "linkml:Extension"
},
"Model": {
"@id": "foo:model"
}
},
"https://w3id.org/linkml/extensions.context.jsonld",
"simple_slots.context.jsonld",
"includes/simple_types.context.jsonld",
Expand Down
38 changes: 38 additions & 0 deletions tests/test_scripts/test_gen_jsonld.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import pytest
from click.testing import CliRunner
from rdflib import Graph, URIRef
Expand Down Expand Up @@ -62,6 +64,42 @@ def test_simple_uris(input_path, snapshot):
assert result.output == snapshot("genjsonld/simple_uri_test.jsonld")


@pytest.mark.parametrize(
"arguments,mock_context_paths, context",
[
(
["-f", "jsonld"],
["context.jsonld", "just_another.context.jsonld"],
[
"context.jsonld",
"just_another.context.jsonld",
"https://w3id.org/linkml/extensions.context.jsonld",
"simple_slots.context.jsonld",
"includes/simple_types.context.jsonld",
"https://w3id.org/linkml/types.context.jsonld",
{
"@base": "https://raw.githubusercontent.com/linkml/linkml/main/tests/test_scripts/input/simple_uri_test/"
},
],
),
],
)
def test_context(input_path, arguments, mock_context_paths, context):
"""Test generation of meta.yaml JSON"""

if mock_context_paths:
context_opt = "--context"
context_args = [x for pair in zip([context_opt] * len(mock_context_paths), mock_context_paths) for x in pair]
extended_arguments = arguments + context_args + [str(input_path("simple_uri_test.yaml"))]
else:
extended_arguments = arguments + [str(input_path("simple_uri_test.yaml"))]

runner = CliRunner()
result = runner.invoke(cli, extended_arguments)
assert result.exit_code == 0
assert json.loads(result.output)["@context"] == context


def check_size(
g: Graph,
g2: Graph,
Expand Down
0