Skip to content

Commit ce1ed08

Browse files
committed
fix: Doc generate - Escape pipe symbols in output enums in md
This then guarantees the generated markdown cells align properly. Signed-off-by: Mihai Galos <[email protected]>
1 parent 8603d0c commit ce1ed08

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

pkg/tools/gen/escape.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package gen
2+
3+
import "strings"
4+
5+
func DeepEscapePipesInPackage(pkg *KclPackage) *KclPackage {
6+
if pkg == nil {
7+
return nil
8+
}
9+
escaped := &KclPackage{
10+
Name: pkg.Name,
11+
Version: pkg.Version,
12+
Description: pkg.Description,
13+
SchemaList: nil,
14+
SubPackageList: nil,
15+
}
16+
17+
if pkg.SchemaList != nil {
18+
escaped.SchemaList = make([]*KclOpenAPIType, len(pkg.SchemaList))
19+
for i, schema := range pkg.SchemaList {
20+
escaped.SchemaList[i] = deepEscapePipesInSchema(schema)
21+
}
22+
}
23+
if pkg.SubPackageList != nil {
24+
escaped.SubPackageList = make([]*KclPackage, len(pkg.SubPackageList))
25+
for i, sub := range pkg.SubPackageList {
26+
escaped.SubPackageList[i] = DeepEscapePipesInPackage(sub)
27+
}
28+
}
29+
return escaped
30+
}
31+
32+
func deepEscapePipesInSchema(schema *KclOpenAPIType) *KclOpenAPIType {
33+
if schema == nil {
34+
return nil
35+
}
36+
escaped := *schema
37+
if schema.Enum != nil {
38+
escaped.Enum = make([]string, len(schema.Enum))
39+
for i, v := range schema.Enum {
40+
escaped.Enum[i] = strings.ReplaceAll(v, "|", "\\|")
41+
}
42+
}
43+
return &escaped
44+
}

pkg/tools/gen/gendoc.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,17 @@ func (g *GenContext) renderPackage(spec *SwaggerV2Spec, parentDir string) error
282282
switch strings.ToLower(string(g.Format)) {
283283
case string(Markdown):
284284
docFileName := fmt.Sprintf("%s.%s", pkgName, g.Format)
285+
286+
// Escape pipe symbols in all string fields of the package before templating
287+
escapedPkg := DeepEscapePipesInPackage(pkg)
288+
285289
var buf bytes.Buffer
286290
err := g.Template.ExecuteTemplate(&buf, "packageDoc", struct {
287291
EscapeHtml bool
288292
Data *KclPackage
289293
}{
290294
EscapeHtml: g.EscapeHtml,
291-
Data: pkg,
295+
Data: escapedPkg,
292296
})
293297
if err != nil {
294298
return fmt.Errorf("failed to render package %s with template, err: %s", pkg.Name, err)

pkg/tools/gen/gendoc_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"bufio"
55
"errors"
66
"fmt"
7-
assert2 "github.com/stretchr/testify/assert"
87
"os"
98
"path/filepath"
109
"strings"
1110
"testing"
11+
12+
assert2 "github.com/stretchr/testify/assert"
1213
)
1314

1415
func TestIndexContent(t *testing.T) {
@@ -249,3 +250,17 @@ func readLines(path string) ([]string, error) {
249250
}
250251
return lines, scanner.Err()
251252
}
253+
254+
func TestDeepEscapePipesInSchema_EnumPipeEscaping(t *testing.T) {
255+
schema := &KclOpenAPIType{
256+
Enum: []string{"A|B", "C", "D|E|F"},
257+
}
258+
259+
escaped := deepEscapePipesInSchema(schema)
260+
261+
assert2.Equal(t, []string{"A\\|B", "C", "D\\|E\\|F"}, escaped.Enum)
262+
assert2.Equal(t, schema.Default, escaped.Default)
263+
assert2.Equal(t, schema.Description, escaped.Description)
264+
assert2.Equal(t, schema.ExternalDocs, escaped.ExternalDocs)
265+
assert2.Equal(t, schema.Ref, escaped.Ref)
266+
}

0 commit comments

Comments
 (0)