Skip to content

Commit

Permalink
Fix escaping of quote marks in descriptions (#33)
Browse files Browse the repository at this point in the history
* Fix escaping of quote marks in descriptions

* * add tests and comment
  • Loading branch information
alecsammon authored May 12, 2023
1 parent 64b8032 commit 89a2932
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 8 additions & 1 deletion diagram/diagram_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,21 @@ 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)
}
}
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;")
}

func shouldSkipConstraint(config config.MermerdConfig, tables []ErdTableData, constraint database.ConstraintResult) bool {
if config.ShowAllConstraints() {
return false
Expand Down
10 changes: 6 additions & 4 deletions diagram/diagram_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
})

Expand Down Expand Up @@ -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)
})

Expand Down Expand Up @@ -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)
})

Expand Down

0 comments on commit 89a2932

Please sign in to comment.