Hi,
I have a codebase which uses go-swagger to parse comments in Go files
and generate swagger documentation.
I recently upgraded to Go 1.19, and found that gofmt’s behavior formats
comments in a way which breaks go-swagger.
For example this code:
package somepackage
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
)
// SomeFunc do something
//
// swagger:operation POST /api/v1/somefunc someFunc
//
// Do something
//
// ---
// x-codeSamples:
// - lang: 'curl'
// source: |
// curl -u "${LOGIN}:${PASSWORD}" -d '{"key": "value"}' -X POST "https://{host}/api/v1/somefunc"
// curl -u "${LOGIN}:${PASSWORD}" -d '{"key2": "value2"}' -X POST "https://{host}/api/v1/somefunc"
// responses:
// '200':
// description: "Some func"
// examples:
// application/json:
// key: "value"
// '400':
// $ref: "#/responses/ErrorResponse"
// '503':
// $ref: "#/responses/ErrorResponse"
func SomeFunct(rw http.ResponseWriter, req *http.Request) {
/// do something
}
Gets reformatted as:
package somepackage
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
)
// SomeFunc do something
//
// swagger:operation POST /api/v1/somefunc someFunc
//
// # Do something
//
// ---
// x-codeSamples:
// - lang: 'curl'
// source: |
// curl -u "${LOGIN}:${PASSWORD}" -d '{"key": "value"}' -X POST "https://{host}/api/v1/somefunc"
// curl -u "${LOGIN}:${PASSWORD}" -d '{"key2": "value2"}' -X POST "https://{host}/api/v1/somefunc"
//
// responses:
//
// '200':
// description: "Some func"
// examples:
// application/json:
// key: "value"
// '400':
// $ref: "#/responses/ErrorResponse"
// '503':
// $ref: "#/responses/ErrorResponse"
func SomeFunct(rw http.ResponseWriter, req *http.Request) {
/// do something
}
Specifically the lines under source: |
get re-indented in such a way
that the YAML is invalid, and go-swagger fails to process this file.
This issue has been raised in the go-swagger community:
https://github.com/go-swagger/go-swagger/issues/2759
but I don’t think this can be worked around in go-swagger, since
go-swagger parses the .go files after they have been formatted by gofmt and written to disk.
Can anyone suggest a possible workaround, or a bugfix to gofmt?
Thanks.
—
Craig
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d6abf372-4cb1-4070-8a62-d4c6c6ba6bcen%40googlegroups.com.
Analysis in 2022 found that only 3% of doc comments in public Go modules were reformatted at all by the draft Go 1.19 gofmt. Limiting ourselves to those comments, about 87% of gofmt’s reformattings preserved the structure that a person would infer from reading the comment; about 6% were tripped up by these kinds of unindented lists, unindented multiline shell commands, and unindented brace-delimited code blocks.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d6abf372-4cb1-4070-8a62-d4c6c6ba6bcen%40googlegroups.com.