Skip to content

Commit

Permalink
Render code blocks properly in OpenAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefftree committed Jul 4, 2024
1 parent 0aa61b4 commit 111929c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/generators/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const (
tagValueFalse = "false"
)

const codeBlockDelimiter = "```"

// Used for temporary validation of patch struct tags.
// TODO: Remove patch struct tag validation because they we are now consuming OpenAPI on server.
var tempPatchTags = [...]string{
Expand Down Expand Up @@ -922,13 +924,32 @@ func (g openAPITypeWriter) generateDescription(CommentLines []string) {
}
}

inCodeBlock := false
for _, line := range CommentLines {
// Ignore all lines after ---
if line == "---" {
break
}
line = strings.TrimRight(line, " ")
leading := strings.TrimLeft(line, " ")

if leading == codeBlockDelimiter {
if !inCodeBlock {
delPrevChar()
buffer.WriteString("\n" + leading + "\n")
inCodeBlock = true
} else {
buffer.WriteString(leading + "\n")
inCodeBlock = false
}
continue
} else if inCodeBlock {
// code blocks should be outputted as is with no left trimming
line = line + "\n"
buffer.WriteString(line)
continue
}

switch {
case len(line) == 0: // Keep paragraphs
delPrevChar()
Expand Down
66 changes: 66 additions & 0 deletions pkg/generators/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,72 @@ Extensions: spec.Extensions{
})
}

func TestDescription(t *testing.T) {
inputFile := `
package foo
// Blah is a test.
// +k8s:openapi-gen=true
type Blah struct {
// Should
// render
// one
// line.
// ` + "```" + `
// func foo() {
// indents
// }
// ` + "```" + `
// normal
// plain
// text.
String string
}`

packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
e := packagestest.Export(t, x, []packagestest.Module{{
Name: "example.com/base/foo",
Files: map[string]interface{}{
"foo.go": inputFile,
},
}})
defer e.Cleanup()

callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
if callErr != nil {
t.Fatal(callErr)
}
if funcErr != nil {
t.Fatal(funcErr)
}
assertEqual(t, callBuffer.String(),
`"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)

assertEqual(t, funcBuffer.String(),
`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
return common.OpenAPIDefinition{
Schema: spec.Schema{
SchemaProps: spec.SchemaProps{
Description: "Blah is a test.",
Type: []string{"object"},
Properties: map[string]spec.Schema{
"String": {
SchemaProps: spec.SchemaProps{
Description: "Should render one line.\n`+"```"+`\nfunc foo() {\n\tindents\n}\n`+"```"+`\nnormal plain text.",
Default: "",
Type: []string{"string"},
Format: "",
},
},
},
Required: []string{"String"},
},
},
}
}`)
})
}

func TestEmptyProperties(t *testing.T) {
inputFile := `
package foo
Expand Down

0 comments on commit 111929c

Please sign in to comment.