Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render code blocks properly in OpenAPI #482

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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