From 3da442b0cf9bd5caa6a693231d4b5774ab3848cf Mon Sep 17 00:00:00 2001 From: Miel Vander Sande Date: Tue, 12 May 2026 09:06:40 +0200 Subject: [PATCH 1/3] Add testcase for compacting with @vocab --- tests/test_jsonld.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_jsonld.py b/tests/test_jsonld.py index 5331438d..3e86f3c9 100644 --- a/tests/test_jsonld.py +++ b/tests/test_jsonld.py @@ -1,3 +1,6 @@ +import json + +import pyld import pytest import pyld.jsonld as jsonld @@ -969,3 +972,25 @@ def test_no_initial_context_and_with_skip_expand_does_not_drop_property_when_arr compacted = jsonld.compact(input, {"@vocab": "http://example.org#"}, {"skipExpansion": True}) expected = {"@context": {"@vocab": "http://example.org#"}, "name": "Bob"} assert compacted == expected + + # Issue 83 + def test_with_vocab(self): + ctx = {'@vocab': 'http://ex.org/#', 'path': {'@type': '@id'}} + input = { + 'http://ex.org/#maxCount': 1, + 'http://ex.org/#path': 'http://ex.org/#shortname' + } + expected = { + "@context": { + "@vocab": "http://ex.org/#", + "path": { + "@type": "@id" + } + }, + "maxCount": 1, + "path": "http://ex.org/#shortname" + } + + compacted = jsonld.compact(input, ctx) + + assert compacted == expected From 4d3664422fc1148d799a00c8c9a44f32bbfbed02 Mon Sep 17 00:00:00 2001 From: Miel Vander Sande Date: Tue, 12 May 2026 09:52:49 +0200 Subject: [PATCH 2/3] Add test case for compaction with @vocab --- tests/test_jsonld.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/tests/test_jsonld.py b/tests/test_jsonld.py index 3e86f3c9..0720b9f4 100644 --- a/tests/test_jsonld.py +++ b/tests/test_jsonld.py @@ -974,21 +974,34 @@ def test_no_initial_context_and_with_skip_expand_does_not_drop_property_when_arr assert compacted == expected # Issue 83 - def test_with_vocab(self): + def test_with_vocab_no_id(self): + """ + Compacting with @vocab should not compact a plain string value + """ ctx = {'@vocab': 'http://ex.org/#', 'path': {'@type': '@id'}} input = { - 'http://ex.org/#maxCount': 1, - 'http://ex.org/#path': 'http://ex.org/#shortname' + 'http://ex.org/#path': 'http://ex.org/#shortname', } expected = { - "@context": { - "@vocab": "http://ex.org/#", - "path": { - "@type": "@id" - } - }, - "maxCount": 1, - "path": "http://ex.org/#shortname" + "@context": {"@vocab": "http://ex.org/#", "path": {"@type": "@id"}}, + "http://ex.org/#path": "http://ex.org/#shortname", + } + + compacted = jsonld.compact(input, ctx) + + assert compacted == expected + + def test_with_vocab_with_id(self): + """ + Compacting with @vocab should compact an @id value + """ + ctx = {'@vocab': 'http://ex.org/#', 'path': {'@type': '@id'}} + input = { + 'http://ex.org/#path': {'@id': 'http://ex.org/#shortname'}, + } + expected = { + "@context": {"@vocab": "http://ex.org/#", "path": {"@type": "@id"}}, + "path": "http://ex.org/#shortname", } compacted = jsonld.compact(input, ctx) From 1dfd42a14a3a1287e66c4d1b9e45905fb08df476 Mon Sep 17 00:00:00 2001 From: Miel Vander Sande Date: Tue, 12 May 2026 13:20:55 +0200 Subject: [PATCH 3/3] Satisfy linter --- tests/test_jsonld.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_jsonld.py b/tests/test_jsonld.py index 0720b9f4..0e0906fd 100644 --- a/tests/test_jsonld.py +++ b/tests/test_jsonld.py @@ -1,6 +1,3 @@ -import json - -import pyld import pytest import pyld.jsonld as jsonld