Skip to content
Merged
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
43 changes: 25 additions & 18 deletions internal/test/schemas/schemas.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion internal/test/schemas/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ components:
type: object
AnyType1: {}
AnyType2:
description: This should be an interface{}
description: |
AnyType2 represents any type.

This should be an interface{}
CustomStringType:
type: string
format: custom
Expand Down
8 changes: 4 additions & 4 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
}
return Schema{
GoType: refType,
Description: schema.Description,
Description: StringToGoComment(schema.Description),
}, nil
}

outSchema := Schema{
Description: schema.Description,
Description: StringToGoComment(schema.Description),
}

// We can't support this in any meaningful way
Expand Down Expand Up @@ -576,7 +576,7 @@ func paramToGoType(param *openapi3.Parameter, path []string) (Schema, error) {
if len(param.Content) > 1 {
return Schema{
GoType: "string",
Description: param.Description,
Description: StringToGoComment(param.Description),
}, nil
}

Expand All @@ -586,7 +586,7 @@ func paramToGoType(param *openapi3.Parameter, path []string) (Schema, error) {
// If we don't have json, it's a string
return Schema{
GoType: "string",
Description: param.Description,
Description: StringToGoComment(param.Description),
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/templates.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/codegen/templates/typedef.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{range .Types}}
// {{ with .Schema.Description }}{{ . }}{{ else }}{{.TypeName}} defines model for {{.JsonName}}.{{ end }}
{{ with .Schema.Description }}{{ . }}{{ else }}// {{.TypeName}} defines model for {{.JsonName}}.{{ end }}
type {{.TypeName}} {{if and (opts.AliasTypes) (.CanAlias)}}={{end}} {{.Schema.TypeDecl}}
{{end}}
4 changes: 4 additions & 0 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ func PathToTypeName(path []string) string {
// StringToGoComment renders a possible multi-line string as a valid Go-Comment.
// Each line is prefixed as a comment.
func StringToGoComment(in string) string {
if len(in) == 0 || len(strings.TrimSpace(in)) == 0 { // ignore empty comment
return ""
}

// Normalize newlines from Windows/Mac to Linux
in = strings.Replace(in, "\r\n", "\n", -1)
in = strings.Replace(in, "\r", "\n", -1)
Expand Down
8 changes: 4 additions & 4 deletions pkg/codegen/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ func TestStringToGoComment(t *testing.T) {
}{
{
input: "",
expected: "// ",
message: "blank string should be preserved with comment",
expected: "",
message: "blank string should be ignored due to human unreadable",
},
{
input: " ",
expected: "// ",
message: "whitespace should be preserved with comment",
expected: "",
message: "whitespace should be ignored due to human unreadable",
},
{
input: "Single Line",
Expand Down