Skip to content

Commit eb90cda

Browse files
authored
fix: Make generating docs non destructive (#323)
#### Summary Before this change, after the first time a description was written to the string it would be deleted. Because all of the `schema` objects are passed by reference, this means that it would be permanently deleted if the initial object is passed in as a struct and not marshaled/unmarshaled. This PR removes that specific destructive aspect and instead overwrites the `Comments` attribute which we do not use. This change doesn't impact any existing code as all instances still use the `Generate` rather than the `GenerateFromSchema`
1 parent 6ac2fcd commit eb90cda

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Diff for: jsonschema/docs/docs.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func generateDoc(root jsonschema.Schema, headerLevel int) (string, error) {
1717
return toc + "\n\n" + buff.String(), err
1818
}
1919

20+
// GenerateFromSchema generates a markdown documentation from a jsonschema.Schema. During the writing process the `Comment` attribute can be overwritten
21+
// To avoid this use the `Generate` function which will not modify the original schema
2022
func GenerateFromSchema(schema jsonschema.Schema, headerLevel int) (string, error) {
2123
return generateDoc(schema, headerLevel)
2224
}
@@ -131,15 +133,15 @@ func writeProperty(property *jsonschema.Schema, required bool, buff *strings.Bui
131133
}
132134

133135
func writeDescription(sc *jsonschema.Schema, buff *strings.Builder) {
134-
if len(sc.Description) == 0 {
136+
if len(sc.Description) == 0 || sc.Comments == "skip_description" {
135137
return
136138
}
137139

138140
buff.WriteString("\n ")
139141
buff.WriteString(strings.ReplaceAll(sc.Description, "\n", "\n "))
140142
buff.WriteString("\n")
141143

142-
sc.Description = "" // already used
144+
sc.Comments = "skip_description"
143145
}
144146

145147
func writeValueAnnotations(sc *jsonschema.Schema, buff *strings.Builder) {

Diff for: jsonschema/docs/docs_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ func TestGCP(t *testing.T) {
5858

5959
func TestClickHouse(t *testing.T) {
6060
genSnapshot(t, "testdata/clickhouse.json")
61-
genSnapshot(t, "testdata/clickhouse.json")
61+
genSnapshotStruct(t, "testdata/clickhouse.json")
6262

6363
}
6464

6565
func TestFiletypes(t *testing.T) {
6666
genSnapshot(t, "testdata/filetypes.json")
67-
genSnapshot(t, "testdata/filetypes.json")
67+
genSnapshotStruct(t, "testdata/filetypes.json")
6868

6969
}
7070

7171
func TestFileDestination(t *testing.T) {
7272
genSnapshot(t, "testdata/file-destination.json")
73-
genSnapshot(t, "testdata/file-destination.json")
73+
genSnapshotStruct(t, "testdata/file-destination.json")
7474

7575
}

0 commit comments

Comments
 (0)