From 60bef7a75f35f71f2c267fbf545ffea14120aa1e Mon Sep 17 00:00:00 2001 From: Debasish Bose Date: Mon, 2 Nov 2026 21:14:54 +1100 Subject: [PATCH] export: mint erwin-native ids ({UPPERCASE-GUID}+00000000) so real erwin imports erwin's import engine (EAT) hangs on a Modelith-minted object id: it was a lowercase SHA-1 brace GUID with no serial, e.g. {1754afad-...}. Real erwin (10.10) writes ids as an UPPERCASE brace GUID with a +8hex serial, e.g. {0007EEEF-A625-4777-B5CB-6BCF63932FF4}+00000000, and validates that shape on import. A scoped-export file of a real mart had 382/505 ids malformed (the model root, every new entity + its attributes, every relationship, the subject area, and 71 *_Ref elements) and the import hung at 'EAT is importing'; the 123 ids that loaded were the old erwin-origin objects keeping their preserved erwin_id. Fix _guid() to emit {UPPERCASE-GUID}+00000000. Every minted id and every <..._Ref> flows from the same id maps, so one change fixes ids and refs consistently; a preserved erwin_id is still emitted verbatim. Output stays XSD-valid and still round-trips through our own importer (it is case-insensitive on id joins). Tests assert the uppercase+serial shape and fail on any lowercase or suffix-less brace GUID. A Modelith-native model's export ids are a pure function of the model on disk, so re-exporting an existing model picks up the fix with no migration. --- .../emit-erd/src/mdl_emit_erd/erwin_export.py | 24 +++++++-- packages/emit-erd/tests/test_erwin_export.py | 50 +++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/packages/emit-erd/src/mdl_emit_erd/erwin_export.py b/packages/emit-erd/src/mdl_emit_erd/erwin_export.py index 28205cc..7ddf618 100644 --- a/packages/emit-erd/src/mdl_emit_erd/erwin_export.py +++ b/packages/emit-erd/src/mdl_emit_erd/erwin_export.py @@ -67,16 +67,30 @@ } +# erwin's native object id is an UPPERCASE brace-wrapped GUID followed by a "+8hex" serial, e.g. +# "{0007EEEF-A625-4777-B5CB-6BCF63932FF4}+00000000" (observed in a real erwin 10.10 export). A +# freshly minted object starts at serial 0 -> "+00000000". erwin's import engine (EAT) validates +# this exact shape and HANGS on a lowercase or suffix-less id, which is what our Modelith-native +# objects used to emit. Both parts matter: the GUID must be UPPERCASE and carry the serial. +# Preserved ids (an erwin-origin object's `erwin_id`) already have this shape and are emitted +# verbatim by `_oid`, so the normalisation here only ever touches ids we mint ourselves. +_ERWIN_SERIAL = "+00000000" + + def _guid(key: str) -> str: - """A stable erwin-shaped GUID {8-4-4-4-12} derived from a key, so output is reproducible. + """A stable erwin-native id {UPPERCASE-8-4-4-4-12}+00000000 derived from a key, so output is + reproducible. Used for synthetic keys (the model root, key-group members) and as the fallback for an - object with no preserved erwin GUID — any stable unique mapping works since the importer - only uses GUIDs as intra-document join keys.""" + object with no preserved erwin GUID (a Modelith-native object erwin treats as new). The + UPPERCASE GUID and the "+00000000" serial are BOTH REQUIRED: erwin's import engine hangs on a + lowercase or bare brace GUID. The importer only uses ids as intra-document join keys and is + case-insensitive on them, so any stable unique value round-trips through Modelith; the exact + erwin shape matters only for real erwin's own import.""" import hashlib - h = hashlib.sha1(key.encode("utf-8")).hexdigest() # noqa: S324 - not security, just a stable id - return f"{{{h[:8]}-{h[8:12]}-{h[12:16]}-{h[16:20]}-{h[20:32]}}}" + h = hashlib.sha1(key.encode("utf-8")).hexdigest().upper() # noqa: S324 - stable id, not security + return f"{{{h[:8]}-{h[8:12]}-{h[12:16]}-{h[16:20]}-{h[20:32]}}}{_ERWIN_SERIAL}" def _oid(obj) -> str: diff --git a/packages/emit-erd/tests/test_erwin_export.py b/packages/emit-erd/tests/test_erwin_export.py index 931ab74..e22b5cf 100644 --- a/packages/emit-erd/tests/test_erwin_export.py +++ b/packages/emit-erd/tests/test_erwin_export.py @@ -302,3 +302,53 @@ def test_unscoped_export_unchanged_by_the_scope_feature(): """Guard: emit_erwin(m) with no subject_area is identical to emit_erwin(m, subject_area=None).""" m = _source_model() assert emit_erwin(m) == emit_erwin(m, subject_area=None) + + +# --- erwin-native id shape ("{UPPERCASE-GUID}+00000000") --------------------------- +# erwin's import engine (EAT) HANGS on a lowercase or suffix-less brace GUID (observed against a +# real erwin 10.10 export, whose ids are "{UPPERCASE-GUID}+8hex"). Every id we MINT must be an +# UPPERCASE GUID carrying the "+00000000" new-object serial; a preserved erwin_id is emitted +# verbatim. These guard the exact regression that hung a real GHG mart import. + +import re # noqa: E402 + +# a minted erwin id: UPPERCASE {8-4-4-4-12} then "+00000000" +_MINTED_ID = re.compile(r'id="\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}\+00000000"') + + +def test_minted_model_root_id_is_uppercase_with_the_erwin_serial(): + """The model root id is always minted (no erwin source), so it must be an UPPERCASE GUID + ending with +00000000 — the exact shape erwin's import requires.""" + xml = emit_erwin(_source_model()) + assert _MINTED_ID.search(xml), "a minted id is not {UPPERCASE-GUID}+00000000" + + +def test_no_minted_id_is_lowercase_or_suffixless(): + """A model with NO erwin_id anywhere -> every emitted object id is minted. None may be a + LOWERCASE brace GUID, and none may be a brace GUID without a +NNNNNNNN serial — both shapes + hang erwin's EAT import.""" + from mdl_core.ids import new_ulid + from mdl_core.ir import Attribute, ConceptualEntity, LogicalEntity, Model, ProjectConfig + + m = Model(ProjectConfig(name="native")) + ce = ConceptualEntity(id=new_ulid(), name="Thing") + m.add(ce) + bk = Attribute(id=new_ulid(), name="thing_id", domain="string") + m.add(LogicalEntity(id=new_ulid(), name="thing", realises=ce.id, attributes=[bk])) + + xml = emit_erwin(m) + # any brace GUID containing a lowercase hex digit is wrong (erwin writes them uppercase) + lower = re.findall(r"\{[0-9A-Fa-f-]*[a-f][0-9A-Fa-f-]*\}", xml) + assert not lower, f"{len(lower)} lowercase brace GUID(s) emitted (erwin ids are uppercase)" + # any brace GUID (ids AND refs) not followed by a +NNNNNNNN serial is wrong + bare = re.findall(r"\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}(?!\+)", xml) + assert not bare, f"{len(bare)} brace GUID(s) emitted without a +NNNNNNNN serial" + + +def test_preserved_erwin_id_keeps_its_own_serial_verbatim(): + """A preserved erwin_id (which, for a real mart, already includes its serial) is emitted + UNCHANGED -- the serial fix only ever appends to ids we mint, never rewrites a preserved one.""" + m = _two_sa_model() + xml = emit_erwin(m) + le = next(iter(m.logical_entities.values())) + assert f'id="{le.erwin_id}"' in xml # verbatim, no extra serial appended