From aeb936b01219e718518651e6f919ba751ba82955 Mon Sep 17 00:00:00 2001 From: Tom PERRILLAT-COLLOMB Date: Thu, 24 Aug 2023 16:07:02 +0200 Subject: [PATCH] fix(er): bug if relationship is declared first --- packages/mermaid/src/diagrams/er/erDb.js | 3 +++ packages/mermaid/src/diagrams/er/erRenderer.js | 2 +- .../mermaid/src/diagrams/er/parser/erDiagram.jison | 5 ----- .../src/diagrams/er/parser/erDiagram.spec.js | 14 ++++++++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/mermaid/src/diagrams/er/erDb.js b/packages/mermaid/src/diagrams/er/erDb.js index e863fb5794e..2f412f7dd8b 100644 --- a/packages/mermaid/src/diagrams/er/erDb.js +++ b/packages/mermaid/src/diagrams/er/erDb.js @@ -36,6 +36,9 @@ const addEntity = function (name, alias = undefined) { if (entities[name] === undefined) { entities[name] = { attributes: [], alias: alias }; log.info('Added new entity :', name); + } else if (entities[name] && !entities[name].alias && alias) { + entities[name].alias = alias; + log.info(`Add alias '${alias}' to entity '${name}'`); } return entities[name]; diff --git a/packages/mermaid/src/diagrams/er/erRenderer.js b/packages/mermaid/src/diagrams/er/erRenderer.js index 3c372ae9b73..0c19d491b61 100644 --- a/packages/mermaid/src/diagrams/er/erRenderer.js +++ b/packages/mermaid/src/diagrams/er/erRenderer.js @@ -326,7 +326,7 @@ const drawEntities = function (svgNode, entities, graph) { .style('text-anchor', 'middle') .style('font-family', getConfig().fontFamily) .style('font-size', conf.fontSize + 'px') - .text(entities[entityName].alias || entityName); + .text(entities[entityName].alias ?? entityName); const { width: entityWidth, height: entityHeight } = drawAttributes( groupNode, diff --git a/packages/mermaid/src/diagrams/er/parser/erDiagram.jison b/packages/mermaid/src/diagrams/er/parser/erDiagram.jison index ea8abc21a9b..4abc6557cc5 100644 --- a/packages/mermaid/src/diagrams/er/parser/erDiagram.jison +++ b/packages/mermaid/src/diagrams/er/parser/erDiagram.jison @@ -104,23 +104,18 @@ statement yy.addEntity($1); yy.addEntity($3); yy.addRelationship($1, $5, $3, $2); - /*console.log($1 + $2 + $3 + ':' + $5);*/ } | entityName BLOCK_START attributes BLOCK_STOP { - /* console.log('detected block'); */ yy.addEntity($1); yy.addAttributes($1, $3); - /* console.log('handled block'); */ } | entityName BLOCK_START BLOCK_STOP { yy.addEntity($1); } | entityName { yy.addEntity($1); } | entityName SQS entityName SQE BLOCK_START attributes BLOCK_STOP { - /* console.log('detected block'); */ yy.addEntity($1, $3); yy.addAttributes($1, $6); - /* console.log('handled block'); */ } | entityName SQS entityName SQE BLOCK_START BLOCK_STOP { yy.addEntity($1, $3); } | entityName SQS entityName SQE { yy.addEntity($1, $3); } diff --git a/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js b/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js index a480e50e531..98954edb3a7 100644 --- a/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js +++ b/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js @@ -142,6 +142,20 @@ describe('when parsing ER diagram it...', function () { expect(entities.hasOwnProperty(entity)).toBe(true); expect(entities[entity].alias).toBe(alias); }); + + it('can have an alias even if the relationship is defined first', function () { + const firstEntity = 'foo'; + const secondEntity = 'bar'; + const alias = 'batman'; + erDiagram.parser.parse( + `erDiagram\n${firstEntity} ||--o| ${secondEntity} : rel\nclass ${firstEntity}["${alias}"]\n` + ); + const entities = erDb.getEntities(); + expect(entities.hasOwnProperty(firstEntity)).toBe(true); + expect(entities.hasOwnProperty(secondEntity)).toBe(true); + expect(entities[firstEntity].alias).toBe(alias); + expect(entities[secondEntity].alias).toBeUndefined(); + }); }); describe('attribute name', () => {