Skip to content

feat(generators): add --normalize-prefixes for conventional prefix names in generated artifacts - #4

Open
jdsika wants to merge 1 commit into
mainfrom
fix/normalize-well-known-prefixes
Open

jdsika wants to merge 1 commit into
mainfrom
fix/normalize-well-known-prefixes

Conversation

@jdsika

@jdsika jdsika commented Mar 26, 2026 •

Copy link
Copy Markdown

Mirror of linkml#3446, same head. Review there; this PR tracks the fork stack.

2026-11-16 (fork): rebased onto upstream main e5ba58b4c; still a single commit, signed, CI green. Upstream now only deprecates --xsd-anyuri-as-iri (linkml#4156, linkml#4157), so the obsolete is_xsd_anyuri_range helper and its three call sites are dropped; --normalize-prefixes is unaffected. Conflicts in owl.rst, jsonldcontextgen.py, owlgen.py and shaclgen.py were resolved additively.

2026-11-19 (fork): the plan no longer lets a prefix that an import declares and nothing uses hold a name, so sdo becomes schema next to linkml:types; self-contained documents keep every prefix their own CURIEs use and the order of their declarations; gen-sparql no longer drops queries with the option. Still a single commit, signed.

The description below is the one on linkml#3446.

Merge order of this series: linkml#3716 → linkml#4168 → linkml#3993 → linkml#4144 → linkml#4171 → linkml#3989 → linkml#3990 → linkml#4028 → linkml#4169 → linkml#4170 → linkml#3446 → linkml#4246. linkml#4156, linkml#4157 and linkml#4158 merged upstream on 2026-11-12. A stack is rebased when its base lands.

Read first: generators/common/prefixes.py (the plan); the eight generators only wire it in (170 lines); the rest is tests, snapshots and docs.

Adds an opt-in --normalize-prefixes option that writes the conventional name of a well-known namespace, the same in each of the seven generators that offer it. Only names change; every IRI stays the same. Without the option the output of all seven is byte-identical to main.

In plain words: a prefix is a nickname for a long web address, and many schemas pick their own nickname for a well-known address. The option writes the nickname everyone else uses. A nickname is not taken when the generated files already use that word for something else: for a different address, or as the name of a class, slot or enum value. A nickname that an imported file merely declares, and that no generated file needs, does not count as used.

That last point is what makes the option work for schema.org. Nearly every schema imports linkml:types, which declares schema for the old http://schema.org/. A schema that wants today's https://schema.org/ cannot rely on the nickname schema itself (the generators read it three different ways, see below), so it declares sdo. With the option the generated files say schema, and the JSON-LD context understands both schema and sdo.

Reproduce: without and with the option

sdo.yaml:

id: https://example.org/s
name: s
prefixes:
  ex: https://example.org/
  sdo: https://schema.org/
  linkml: https://w3id.org/linkml/
default_prefix: ex
imports:
  - linkml:types        # declares schema: http://schema.org/
classes:
  Person:
    class_uri: sdo:Person
    attributes:
      name:
        slot_uri: sdo:name
Command Without the option (LinkML today) With --normalize-prefixes
gen-owl --no-use-native-uris --no-metadata sdo.yaml @prefix sdo: <https://schema.org/> .
sdo:Person a owl:Class ;
@prefix schema: <https://schema.org/> .
schema:Person a owl:Class ;
gen-jsonld-context --no-metadata sdo.yaml "sdo": "https://schema.org/",
"@id": "sdo:Person"
"schema": "https://schema.org/",
"sdo": "https://schema.org/",
"@id": "schema:Person"

The IRI is https://schema.org/Person in every cell. The context keeps sdo, so data written with it still expands. With the option each generator run reports the name it took over, once:

WARNING:linkml.generators.common.prefixes:Prefix 'schema' is written for <https://schema.org/>. An import declares it for <http://schema.org/>, which nothing uses; LinkML still reads it so in data

Why the schema does not simply declare schema itself. The same file with sdo replaced by schema throughout (direct.yaml), without the option:

Command Result
gen-jsonld-context direct.yaml, gen-shex direct.yaml, gen-sssom direct.yaml ValueError: Prefix: schema mismatch between s and types
gen-owl --no-use-native-uris direct.yaml, gen-shacl direct.yaml <http://schema.org/Person> a owl:Class ; and sh:targetClass <http://schema.org/Person>: the import's namespace, not the one the schema declares
gen-sparql -d out direct.yaml PREFIX schema: <http://schema.org/> with schema:Person: the import's namespace again
linkml generate yarrrml direct.yaml schema: https://schema.org/: the schema's namespace

Next to linkml:types the seven generators read a schema's own schema: three different ways. The only CURIE spelling that all of them read as https://schema.org/ is an alias (full IRIs in class_uri / slot_uri are the alternative), and this option is what gives the alias its conventional name in the output.

The ordinary case is the same mechanism: a schema that declares dce: http://purl.org/dc/elements/1.1/ and w3prov: http://www.w3.org/ns/prov# gets dc:title and prov:Agent; a prefix without a conventional name (ex) keeps its name.

The rule and its basis

linkml.generators.common.prefixes.preferred_prefixes(sv) computes one plan per schema, a curies Converter with the preferred name per namespace and the other names as synonyms. Every generator applies the same plan, so artifacts generated in separate runs agree.

A conventional name is given to a namespace unless the schema's documents already write it for something else:

  • the schema declares it itself for another namespace (its JSON-LD context defines every prefix the schema declares);
  • only an import declares it and it is in use: an element of the schema or its imports is identified by an IRI of that namespace (class_uri, slot_uri, a type's uri, enum_uri, a permissible value's meaning, or the IRI derived from the element's name), is mapped to one (class, slot, enum, permissible value), is written with the prefix in a CURIE, names it in id_prefixes, or a schema lists it in emit_prefixes;
  • no schema declares it and a CURIE, id_prefixes or emit_prefixes names it all the same (it is then read with a default CURIE map or as an unknown namespace);
  • two namespaces that the documents write want the same conventional name; or
  • it is the name of a class, slot or permissible value, which a JSON-LD context uses as a term (a slot time blocks owltime → time).

A prefix that only an import declares and nothing uses is needed by no document generated for the schema, so it holds no name. linkml:types uses schema only in the mappings of its types, which none of the seven generators writes (a test checks each of them, with and without the option).

Why an unused declaration in an import does not count. In each output format a prefix declaration reaches no further than the document that carries it, so a declaration in an imported schema binds nothing in a generated document that does not repeat it:

Format What it says
Turtle "The '@Prefix' or 'PREFIX' directive associates a prefix label with an IRI" (Turtle 1.1 §2.4)
SPARQL "The PREFIX keyword associates a prefix label with an IRI" (SPARQL 1.1 §4.1.1.1)
JSON-LD a context defines terms for the data that names it (JSON-LD 1.1 §3.1)
CURIE "There MUST be a prefix binding for the prefix … in scope" where the CURIE is written (CURIE Syntax 1.0 §3)
RDF "Namespace IRIs and namespace prefixes are not a formal part of the RDF data model" (RDF 1.1 Concepts §1.4)

LinkML's own schema language is the exception: a schema is read with the prefixes of its imports. That is the first point under What to weigh.

schema.org. http://schema.org/ and https://schema.org/ are two namespaces: "Two IRIs are equal if and only if they are equivalent under Simple String Comparison" (RDF 1.1 Concepts §3.2). schema.org calls https its "preferred form in examples" and says "both 'https://schema.org' and 'http://schema.org' are fine" (FAQ). The two sources of conventional names each give schema to one of them: prefixmaps to http, rdflib to https. If a schema writes http://schema.org/ too (slot_uri: schema:legacyName), schema is in use for that namespace and sdo keeps its name.

What to weigh

  • LinkML's own data tools read a released name differently. Loaders and dumpers expand the CURIEs of data with the prefixes of the schema and its imports. With sdo + linkml:types, data that writes schema:Thing is http://schema.org/Thing to linkml-convert and https://schema.org/Thing to a JSON-LD processor using the normalized context (without the option the context leaves schema undefined). Data written with the declared name, sdo, is read alike by both.
  • The warning fires for the headline case, once per generator run, because of the point above; it is a WARNING and not INFO so that it is seen at the default log level. It is per plan: gen-yarrrml and gen-sssom do not declare prefixes that only an import declares, so they can print it without renaming anything.
  • The names depend on the installed prefixmaps and rdflib. A release that adds a namespace to linked_data (prefixmaps main adds wgs for http://www.w3.org/2003/01/geo/wgs84_pos#, ICD10CM, NANDO) changes what the option writes for schemas that alias it. Pin both where generated artifacts are committed.
  • gen-sparql is touched without the option too: its template now takes element IRIs through one function, and its working copy of the SchemaView gets its caches reset. Without the option its output is byte-identical to main. With the option the reset matters: the copy compares equal to the original, so once the plan had filled the original's caches, materialization rewrote the input schema and two repository schemas lost queries (linkml_issue_388.yaml 6 → 4, not_required.yaml 17 → 16).
  • New importable surface: linkml.generators.common.prefixes and Generator.prefix_schemaview().

Per generator

  • gen-owl, gen-shacl, gen-shex (ShExC and RDF): bind the planned names in rdflib; a declared schema: http://schema.org/ also wins over rdflib's default schema → https, so no schema1. gen-shex's RDF output binds no schema prefix by default and the planned ones with the option.
  • gen-sparql, gen-yarrrml, gen-sssom: rename the prefixes the document declares, in their original order, and the CURIEs using them (SSSOM: the CURIE columns only). A prefix that the document's own CURIEs use keeps what it denotes, declared or not (skos:exactMatch in SSSOM). gen-sparql renders its queries once to learn which prefixes they use, and keeps the PREFIX lines of the CURIEs it writes as they stand (named graphs, the template's rdf: and linkml:).
  • gen-jsonld-context: adds the planned names, rewrites compact IRIs in values (not keys, which data uses) and the frame @type. A compact IRI whose prefix a context no longer defines "is interpreted as IRI instead" (JSON-LD 1.1 §4.1.5), which is why every declared name stays. --no-prefixes renames nothing, since its prefixes then come from another context.
  • Not offered: gen-prefix-map (a prefix map gives each namespace one name, the schema's; curies rejects synonyms there), gen-rdf and gen-jsonld (CURIEs are literal data), gen-python, gen-pydantic, gen-namespaces (prefix names are part of the generated code). To change the names users write, rename the prefixes in the schema.

The conventional names come from two curated sources, no hand-written table: the linked_data context of prefixmaps, as asked in review, and the names rdflib binds by default, which the RDF generators serialize with. The two name no namespace differently. prefixmaps' merged context is not used: it names a namespace after the first registry in its merge order that lists it, which for W3C vocabularies is often the upper-cased Bioregistry name (ADMS for http://www.w3.org/ns/adms#, TIME for http://www.w3.org/2006/time#, ODRL). Released prefixmaps (≥ 0.2.2) works.

Testing

test_normalize_prefixes.py, 1026 tests over 16 schemas with hard cases:

  • every document means the same with and without the option, and writes the planned names;
  • no document and no context writes a namespace the plan leaves out as unused;
  • what counts as use is checked against the plan and against what the generators write;
  • a schema with nothing to rename is byte-identical with the option;
  • data expands the same under the default and the normalized context, alone and combined with an import's context.

Fifteen mutations of the plan and of the generators each fail tests. The module passes with prefixmaps 0.2.2, 0.2.6 and main (ada53ec). tests/linkml and tests/linkml_runtime pass. Without the option, the output of the seven generators is byte-identical to main on every repository schema both can generate.

What the tests cover in detail
  • Schemas: both schema.org variants, an import's name unused, used by the import, and used by the importer, dc/dce, a case-only alias, a slot or enum value named like a prefix, an alias as default_prefix, in a type uri or enum meaning, prefixes declared only in an import, a declared schema: http://schema.org/.
  • Documents: rdflib isomorphism (ttl, xml, n3, json-ld, nt; --diff-stable, mapped URIs, metaclasses, metadata profile, annotations, type objects), ShExJ, SPARQL parse trees with expanded names, YARRRML and SSSOM with expanded CURIEs.
  • Use: 21 ways an import can refer to its own prefix (URIs, mappings, id_prefixes, emit_prefixes, name-derived IRI, a full IRI, a type mapping, a description, …), each checked against the plan and 20 of them against what gen-owl, gen-shacl, gen-shex and gen-jsonld-context write.
  • Two imports declaring one name for two namespaces, in both import orders and both roles; a SchemaView whose namespace cache was filled before the imports loaded; a prefix written in a CURIE that nothing declares.
  • Context: every term expands the same (--use-curies, --flatprefixes, --fix-multivalue-containers, --no-mergeimports); a name only the normalized context defines is the conventional name of a namespace the default context defines; --no-model + --no-prefixes contexts, gen-prefix-map + --no-prefixes, an importer with --exclude-imports next to its import's context (also where the import uses the name), frames, YAML text input with local imports.
  • gen-sparql: a CURIE with a colon in its local part, a name declared for two namespaces, an undeclared prefix, named graphs; the same queries with and without the option, and an unchanged input view.
  • A schema declaring schema: https://schema.org/ next to linkml:types: refused by gen-jsonld-context and gen-shex, read as http by gen-owl and gen-shacl.
  • The warning: emitted when an import's name is given away, naming only the other namespaces, silent otherwise.
  • CLI: offered on the seven generators, --no-normalize-prefixes equals the default, rejected elsewhere.
  • test_csv_limit now restores csv.field_size_limit: left at 20, it broke prefixmaps' CSV loading in later tests on the same xdist worker.

Limitations

  • A context generated for an import defines every prefix the import declares, used or not. Data that names that context and then the normalized context of the importing schema gets the latter's meaning for a released name: "Duplicate context terms are overridden using a most-recently-defined-wins mechanism" (JSON-LD 1.1 §4.1). A name the import uses is not released, so its meaning does not change.
  • Uses of an import's prefix outside the list above (see_also, annotations) do not hold the name. Only the RDF generators write them, as full IRIs, so the graph is the same with and without the option.
  • JSON-LD compaction picks the shortest name (JSON-LD 1.1 API, IRI Compaction), so data compacted with the context may still use the declared one.
  • Data that uses a newly added name as a bare enum value or JSON key would read it as the prefix. Valid data cannot (those names are blocked).
  • gen-yarrrml and gen-sssom don't declare imported prefixes, with or without the option; their CURIEs with such prefixes stay undeclared and unrenamed.

Checklist

  • My code follows the contributor guidelines
  • I have added tests that prove my fix/feature works
  • Existing tests pass locally with my changes

@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch 3 times, most recently from 42e007b to 71db97f Compare March 26, 2026 17:16
@jdsika jdsika changed the title fix(generators): normalize well-known prefixes in output fix(generators): add --normalize-prefixes flag for well-known prefix names Mar 26, 2026
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch 8 times, most recently from 60f62c4 to 1b36dc0 Compare April 2, 2026 09:38
@jdsika

jdsika commented Apr 2, 2026

Copy link
Copy Markdown
Author

🔍 Adversarial Review — PR #4

Summary

Solid feature with thorough test coverage, but contains a confirmed race condition between Phase 1 and Phase 2 of normalize_graph_prefixes() that can silently swap namespace URIs (HTTPS → HTTP) for schema.org. The cross-PR conflict with PR #1 (well_known_prefix_map() defined differently in both) also needs a merge strategy.

🐛 Bugs & Issues

1. Phase 2 overwrites Phase 1 normalization for HTTP/HTTPS variants (Confirmed)

When a schema declares sdo: https://schema.org/ and the metamodel runtime injects schema: http://schema.org/, the following sequence occurs:

  • Phase 1 correctly rebinds sdo → schema: https://schema.org/ (user's HTTPS)
  • After Phase 1, the graph still contains schema1: http://schema.org/ (orphaned runtime HTTP binding)
  • Phase 2 sees schema1: http://schema.org/, looks up wk["http://schema.org/"] = "schema", checks collision ("schema" not in schema_prefixes — user declared sdo, not schema), and rebinds schema: http://schema.org/ with override=True, replace=True
  • This silently replaces the Phase 1 HTTPS binding with the HTTP variant

Reproduced locally with rdflib 7.x:

from rdflib import Graph, Namespace, URIRef
g = Graph()
g.bind('schema', Namespace('http://schema.org/'))      # metamodel
g.namespace_manager.bind('sdo', URIRef('https://schema.org/'))  # user

# Phase 1
g.bind('schema', Namespace('https://schema.org/'), override=True, replace=True)
# → schema: https://schema.org/ ✅, schema1: http://schema.org/ (orphan)

# Phase 2
g.bind('schema', Namespace('http://schema.org/'), override=True, replace=True)
# → schema: http://schema.org/ ❌  (HTTPS binding silently lost!)

Fix: Phase 2 should check whether std_pfx is already bound in the graph (not just schema_prefixes) before rebinding:

# In Phase 2, before rebinding:
current_ns_for_std = dict(graph.namespaces()).get(std_pfx)
if current_ns_for_std and str(current_ns_for_std) != ns_str:
    continue  # std_pfx already claimed by Phase 1 for a different URI

2. test_sdo_normalised_to_schema may be passing for the wrong reason

The test asserts pfx["schema"] == "https://schema.org/", but if Phase 2 overwrites this with HTTP, the test should fail. If it passes, it may be because the metamodel's emit_prefixes don't include schema for the test schema, meaning the HTTP binding is never injected — hiding the bug in production schemas that DO trigger both bindings. The test should explicitly set up both HTTP and HTTPS bindings to exercise Phase 2 properly.

⚠️ Concerns

3. Cross-PR conflict with PR #1 — well_known_prefix_map() defined differently

PR #1 PR #4
Approach Dynamic: {str(ns): str(pfx) for pfx, ns in RdfGraph().namespaces()} Static: MappingProxyType({...}) with ~30 entries
rdflib dependency Changes with rdflib version Frozen, version-independent
HTTP/HTTPS variants Only what rdflib provides Explicitly includes both

PR #4's static approach is explicitly designed to solve a problem with PR #1's dynamic approach (rdflib defaults changed between 6.x and 7.x). These will conflict on merge. Recommendation: merge PR #4 first, or rebase PR #1 to use PR #4's static map.

4. Cross-PR conflict — flag forwarding in jsonldgen.py

PR #4 forwards both normalize_prefixes AND deterministic to the embedded ContextGenerator:

for flag in ("normalize_prefixes", "deterministic"):
    if hasattr(self, flag):
        context_kwargs.setdefault(flag, getattr(self, flag))

PR #1 adds deterministic handling in a different location (post-serialization in end_schema). PR #4's forwarding of deterministic creates a dependency on PR #1's field existing. The hasattr guard prevents a crash, but semantically PR #4 shouldn't forward a flag it doesn't define. Either:

5. PR title says fix: but commit says feat: — which is it?

The commit message uses feat(generators): while the PR title uses fix(generators):. This is a new opt-in flag, which is a feature — feat: is correct per conventional commits.

6. Static map maintenance burden

The _WELL_KNOWN_PREFIX_MAP needs manual updates when rdflib adds new curated defaults. The test_matches_rdflib_defaults test catches additions but not removals. Consider adding a reverse check: entries in the static map that are NOT in rdflib's defaults could indicate stale entries.

🧪 Test Coverage Assessment

Strong coverage (18 tests across 7 classes):

  • ✅ OWL, SHACL, JSON-LD context normalization (sdo→schema, dce→dc)
  • ✅ HTTP/HTTPS schema.org variants
  • ✅ Collision detection with warnings (caplog)
  • ✅ Cross-generator consistency
  • ✅ Static map integrity and copy safety
  • ✅ JSONLDGenerator flag forwarding
  • ✅ Flag-off backward compatibility

Gaps:

  • ❌ No test for normalize_prefixes + exclude_imports interaction
  • ❌ No test for empty schema (zero prefixes) — is _prefix_remap.clear() safe?
  • ❌ No test that explicitly sets up both HTTP and HTTPS runtime bindings to exercise Phase 2 overwrite (the scenario from Bug feat(generators): add --deterministic flag with hybrid RDFC-1.0 + rdflib serialization #1)
  • ❌ No test for add_prefix being called before __post_init__ completes (defensive, but _prefix_remap access would crash)
  • ❌ CURIE remapping test (test_normalize_prefixes_curie_remapping) only checks that Person @id starts with schema: — doesn't verify full_name slot's @id uses schema:name instead of sdo:name

📋 Fix Plan

  1. Fix Phase 2 overwrite bug — add a graph-binding check before rebinding in Phase 2 to prevent overwriting Phase 1's normalization
  2. Add regression test — explicitly set up a graph with both schema: http://schema.org/ and sdo: https://schema.org/, run normalize, and assert the HTTPS binding survives
  3. Remove "deterministic" from forwarding loop — or gate it behind hasattr only (already done) but add a comment explaining the cross-PR dependency
  4. Fix PR title — change fix: to feat: for consistency with the commit
  5. Add empty-schema test — verify no crash when schema has zero prefixes
  6. Add exclude_imports interaction test — verify normalization works when imports are excluded
  7. Strengthen CURIE remapping test — assert that slot URIs (not just class URIs) are remapped in the context

✅ What's Good

  • Excellent documentation: the 3-case handling in visit_schema is well-commented with case numbers
  • Defensive _prefix_remap initialization: correctly placed before super().__post_init__() with clear comment explaining why
  • Collision detection: warning on skip is the right behavior — no silent data loss
  • MappingProxyType for the static map: prevents accidental mutation
  • well_known_prefix_map() returns a copy: callers can't corrupt the canonical map
  • Phase 2 concept is sound: cleaning up runtime-injected orphan bindings is the right idea, it just needs the overwrite guard
  • Cross-generator consistency test: ensures OWL, SHACL, and JSON-LD agree — excellent
  • Backward compatibility: flag defaults to off, existing behavior preserved

@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch from d9fb738 to dd0ce0c Compare April 2, 2026 15:24
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch 3 times, most recently from 8d098e9 to ef47b47 Compare April 24, 2026 20:26
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch 11 times, most recently from 8dc3ece to 260a97e Compare May 11, 2026 15:49
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch from 260a97e to e56ca65 Compare May 11, 2026 16:15
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch from e56ca65 to 5f17e19 Compare June 9, 2026 15:21
@rmessaou
rmessaou force-pushed the fix/normalize-well-known-prefixes branch 2 times, most recently from ce55ccb to 909eb7d Compare July 8, 2026 08:49
@jdsika jdsika self-assigned this Jul 11, 2026
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch from a4b329c to 32821d0 Compare September 11, 2026 12:33
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch from 32821d0 to 0342283 Compare October 2, 2026 11:48
@jdsika jdsika changed the title fix(generators): add --normalize-prefixes flag for well-known prefix names feat(generators): add --normalize-prefixes flag for well-known prefix names Oct 13, 2026
jdsika added a commit that referenced this pull request Oct 14, 2026
Review update from #4: test_csv_limit left the process-wide CSV field
size limit at 20, so later tests on the same xdist worker that read
prefixmaps' CSV data failed intermittently.

Signed-off-by: jdsika <[email protected]>
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch from 0342283 to 316b430 Compare October 14, 2026 07:34
jdsika added a commit that referenced this pull request Oct 14, 2026
Review update from #4: test_csv_limit left the process-wide CSV field
size limit at 20, so later tests on the same xdist worker that read
prefixmaps' CSV data failed intermittently.

Signed-off-by: jdsika <[email protected]>
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch 2 times, most recently from 9ede52d to 29098c8 Compare October 23, 2026 11:48
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch from 29098c8 to d6e7eae Compare October 30, 2026 13:50
@jdsika jdsika changed the title feat(generators): add --normalize-prefixes flag for well-known prefix names feat(generators): add --normalize-prefixes for conventional prefix names in every generator Oct 30, 2026
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch 2 times, most recently from 0f2e3d2 to 53485b5 Compare November 18, 2026 16:01
…mes in generated artifacts

Add an opt-in --normalize-prefixes option that writes the conventional
name of a well-known namespace, e.g. dc for
http://purl.org/dc/elements/1.1/ where a schema declares dce, and writes
the same name in each of the generators that offer it: gen-owl,
gen-shacl, gen-shex, gen-sparql, gen-yarrrml, gen-sssom and
gen-jsonld-context. Only names change; every IRI stays the same.

The names come from two curated sources, the prefixmaps linked_data
context and the names rdflib binds by default, which the RDF generators
serialize with; the two name no namespace differently. prefixmaps'
merged context is not used: it names a namespace after the first
registry in its merge order that lists it, which for W3C vocabularies is
often the upper-cased Bioregistry name, such as ADMS for
http://www.w3.org/ns/adms#.

linkml.generators.common.prefixes computes one plan per schema as a
curies Converter of preferred names with the other names as synonyms. In
each output format a prefix declaration reaches no further than the
document that carries it (Turtle 1.1 2.4, SPARQL 1.1 4.1.1.1, JSON-LD
1.1 3.1, CURIE Syntax 1.0 3), so a declaration in an imported schema
binds nothing in a generated document that does not repeat it. A
conventional name is therefore unavailable for a namespace where the
schema's documents write it for another one: the schema declares it
itself, or only an import declares it and it is in use, meaning that an
element is identified by or mapped to an IRI of that namespace, is
written with the prefix in a CURIE, names it in id_prefixes, or a schema
lists it in emit_prefixes; or nothing declares it and a CURIE,
id_prefixes or emit_prefixes names it all the same. A prefix that only
an import declares and nothing uses holds no name.

linkml:types declares schema for http://schema.org/ and uses it only in
the mappings of its types, which none of these generators writes. A
schema that declares schema for https://schema.org/ next to it is
refused by gen-jsonld-context, gen-shex and gen-sssom, and gen-owl and
gen-shacl then write http://schema.org/ IRIs; so a schema declares
another name, such as sdo, and the option writes schema for
https://schema.org/, which schema.org calls its preferred form in
examples. LinkML's loaders and dumpers keep expanding schema in data as
the import declares it, which the plan reports as a warning. A prefix
keeps its name, with a warning, where two namespaces want the same
conventional name or the name is that of a class, slot or permissible
value, which a JSON-LD context uses as a term.

How a generator applies the plan depends on what its prefixes are. Self-
contained documents (Turtle, RDF/XML, ShExC, SPARQL, YARRRML, SSSOM) use
only the preferred names, as a prefix there only abbreviates IRIs (RDF
1.1 Concepts 1.4); they rename only the prefixes they declare, keep the
order of their declarations, and a prefix that their own CURIEs use
keeps what it denotes, declared or not. A JSON-LD context is shared with
the data written against it, and a compact IRI whose prefix it no longer
defines stops expanding (JSON-LD 1.1 4.1.5), so it keeps every declared
name and adds the preferred one; it writes its own compact IRIs, but not
its keys, with the preferred names, and renames nothing with --no-
prefixes. SchemaLoader-based generators read the plan from a SchemaView
of the schema as given, resolving local imports of YAML text against
base_dir.

gen-sparql renders its queries once to learn which prefixes they use and
keeps the declarations of those it writes as they stand, the named
graphs and the template's own. It resets the caches of its working copy
of the SchemaView: the copy compares equal to the original, so once the
plan had filled the original's caches the copy was served those
elements, and materialization rewrote the input and dropped queries.

gen-prefix-map gives each namespace one name, the schema's, gen-rdf and
gen-jsonld write CURIEs as literal data, and gen-python, gen-pydantic
and gen-namespaces make prefix names part of the generated code, so they
do not offer the option.

test_csv_limit set csv.field_size_limit(20) without restoring it, so any
later test on the same worker that read prefixmaps' CSV data failed; it
now restores the limit.

Signed-off-by: jdsika <[email protected]>
@jdsika
jdsika force-pushed the fix/normalize-well-known-prefixes branch from 53485b5 to 6458056 Compare November 19, 2026 09:47
@jdsika jdsika changed the title feat(generators): add --normalize-prefixes for conventional prefix names in every generator feat(generators): add --normalize-prefixes for conventional prefix names in generated artifacts Nov 19, 2026
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.

1 participant