From 127a8b4e5f36d451f79883add73c54d401d1a221 Mon Sep 17 00:00:00 2001 From: Alec Sammon Date: Thu, 11 May 2023 11:38:05 +0100 Subject: [PATCH 1/2] Fix escaping of quote marks in descriptions --- diagram/diagram_util.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/diagram/diagram_util.go b/diagram/diagram_util.go index 848754d..a7f1ffc 100644 --- a/diagram/diagram_util.go +++ b/diagram/diagram_util.go @@ -2,9 +2,10 @@ package diagram import ( "fmt" - "github.com/sirupsen/logrus" "strings" + "github.com/sirupsen/logrus" + "github.com/KarnerTh/mermerd/config" "github.com/KarnerTh/mermerd/database" ) @@ -62,7 +63,7 @@ func getDescription(options []string, column database.ColumnResult) string { description = append(description, "<"+column.EnumValues+">") } case "columnComments": - description = append(description, column.Comment) + description = append(description, escapeComments(column.Comment)) default: logrus.Errorf("Could not parse option %q", option) } @@ -70,6 +71,10 @@ func getDescription(options []string, column database.ColumnResult) string { return strings.TrimSpace(strings.Join(description, " ")) } +func escapeComments(str string) string { + return strings.ReplaceAll(str, `"`, "#quot;") +} + func shouldSkipConstraint(config config.MermerdConfig, tables []ErdTableData, constraint database.ConstraintResult) bool { if config.ShowAllConstraints() { return false From 7b3078e60cf5f372c7b1cb2f05fd314412c80ab4 Mon Sep 17 00:00:00 2001 From: Alec Sammon Date: Thu, 11 May 2023 11:48:09 +0100 Subject: [PATCH 2/2] * add tests and comment --- diagram/diagram_util.go | 6 ++++-- diagram/diagram_util_test.go | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/diagram/diagram_util.go b/diagram/diagram_util.go index a7f1ffc..5f0b3c8 100644 --- a/diagram/diagram_util.go +++ b/diagram/diagram_util.go @@ -2,9 +2,8 @@ package diagram import ( "fmt" - "strings" - "github.com/sirupsen/logrus" + "strings" "github.com/KarnerTh/mermerd/config" "github.com/KarnerTh/mermerd/database" @@ -71,6 +70,9 @@ func getDescription(options []string, column database.ColumnResult) string { return strings.TrimSpace(strings.Join(description, " ")) } +// escapeComments escapes invalid characters in comments. +// mermaid does not support quote marks ("), as it uses this to mark the comment, as such these need to be replaced +// with `#quot;`. func escapeComments(str string) string { return strings.ReplaceAll(str, `"`, "#quot;") } diff --git a/diagram/diagram_util_test.go b/diagram/diagram_util_test.go index 87f0d35..0b5ecda 100644 --- a/diagram/diagram_util_test.go +++ b/diagram/diagram_util_test.go @@ -128,7 +128,9 @@ func TestTableNameInSlice(t *testing.T) { func TestGetColumnData(t *testing.T) { columnName := "testColumn" enumValues := "a,b" - comment := "comment" + comment := `{"comment":"detail"}` + expectedComment := "{#quot;comment#quot;:#quot;detail#quot;}" + column := database.ColumnResult{ Name: columnName, IsPrimary: true, @@ -148,7 +150,7 @@ func TestGetColumnData(t *testing.T) { // Assert configMock.AssertExpectations(t) assert.Equal(t, columnName, result.Name) - assert.Equal(t, "<"+enumValues+"> "+comment, result.Description) + assert.Equal(t, "<"+enumValues+"> "+expectedComment, result.Description) assert.Equal(t, primaryKey, result.AttributeKey) }) @@ -180,7 +182,7 @@ func TestGetColumnData(t *testing.T) { // Assert configMock.AssertExpectations(t) assert.Equal(t, columnName, result.Name) - assert.Equal(t, comment, result.Description) + assert.Equal(t, expectedComment, result.Description) assert.Equal(t, primaryKey, result.AttributeKey) }) @@ -212,7 +214,7 @@ func TestGetColumnData(t *testing.T) { // Assert configMock.AssertExpectations(t) assert.Equal(t, columnName, result.Name) - assert.Equal(t, "<"+enumValues+"> "+comment, result.Description) + assert.Equal(t, "<"+enumValues+"> "+expectedComment, result.Description) assert.Equal(t, none, result.AttributeKey) })