Skip to content

Commit

Permalink
Fix staticcheck ST1005 errors in generated Go client (OpenAPITools#13633
Browse files Browse the repository at this point in the history
)

* Fix staticcheck ST1005 errors in go client

* Samples updated

* sample test fix for openapiv3 petstore go client
  • Loading branch information
maelick authored Oct 18, 2022
1 parent 3f4e3af commit 41255c1
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func typeCheckParameter(obj interface{}, expected string, name string) error {

// Check the type is as expected.
if reflect.TypeOf(obj).String() != expected {
return fmt.Errorf("Expected %s to be of type %s but received %s.", name, expected, reflect.TypeOf(obj).String())
return fmt.Errorf("expected %s to be of type %s but received %s", name, expected, reflect.TypeOf(obj).String())
}
return nil
}
Expand Down Expand Up @@ -514,7 +514,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
}

if bodyBuf.Len() == 0 {
err = fmt.Errorf("Invalid body type %s\n", contentType)
err = fmt.Errorf("invalid body type %s\n", contentType)
return nil, err
}
return bodyBuf, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (c *Configuration) AddDefaultHeader(key string, value string) {
// URL formats template on a index using given variables
func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) {
if index < 0 || len(sc) <= index {
return "", fmt.Errorf("Index %v out of range %v", index, len(sc)-1)
return "", fmt.Errorf("index %v out of range %v", index, len(sc)-1)
}
server := sc[index]
url := server.URL
Expand All @@ -222,7 +222,7 @@ func (sc ServerConfigurations) URL(index int, variables map[string]string) (stri
}
}
if !found {
return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
return "", fmt.Errorf("the variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
}
url = strings.Replace(url, "{"+name+"}", value, -1)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
var jsonDict map[string]interface{}
err = json.Unmarshal(data, &jsonDict)
if err != nil {
return fmt.Errorf("Failed to unmarshal JSON into map for the discriminator lookup.")
return fmt.Errorf("failed to unmarshal JSON into map for the discriminator lookup")
}

{{/-first}}
Expand Down Expand Up @@ -59,7 +59,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
}

{{/anyOf}}
return fmt.Errorf("Data failed to match schemas in anyOf({{classname}})")
return fmt.Errorf("data failed to match schemas in anyOf({{classname}})")
}

// Marshal data from the first non-nil pointers in the struct to JSON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
var jsonDict map[string]interface{}
err = newStrictDecoder(data).Decode(&jsonDict)
if err != nil {
return fmt.Errorf("Failed to unmarshal JSON into map for the discriminator lookup.")
return fmt.Errorf("failed to unmarshal JSON into map for the discriminator lookup")
}

{{/-first}}
Expand All @@ -45,7 +45,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
return nil // data stored in dst.{{{modelName}}}, return on the first match
} else {
dst.{{{modelName}}} = nil
return fmt.Errorf("Failed to unmarshal {{classname}} as {{{modelName}}}: %s", err.Error())
return fmt.Errorf("failed to unmarshal {{classname}} as {{{modelName}}}: %s", err.Error())
}
}

Expand Down Expand Up @@ -75,11 +75,11 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
{{/oneOf}}

return fmt.Errorf("Data matches more than one schema in oneOf({{classname}})")
return fmt.Errorf("data matches more than one schema in oneOf({{classname}})")
} else if match == 1 {
return nil // exactly one match
} else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf({{classname}})")
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
}
{{/discriminator}}
{{/useOneOfDiscriminatorLookup}}
Expand All @@ -106,11 +106,11 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
{{/oneOf}}

return fmt.Errorf("Data matches more than one schema in oneOf({{classname}})")
return fmt.Errorf("data matches more than one schema in oneOf({{classname}})")
} else if match == 1 {
return nil // exactly one match
} else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf({{classname}})")
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
}
{{/useOneOfDiscriminatorLookup}}
}
Expand Down
42 changes: 21 additions & 21 deletions modules/openapi-generator/src/main/resources/go/signing.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,26 @@ func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error {
// are invalid.
func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Context, error) {
if h.KeyId == "" {
return nil, fmt.Errorf("Key ID must be specified")
return nil, fmt.Errorf("key ID must be specified")
}
if h.PrivateKeyPath == "" && h.privateKey == nil {
return nil, fmt.Errorf("Private key path must be specified")
return nil, fmt.Errorf("private key path must be specified")
}
if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok {
return nil, fmt.Errorf("Invalid signing scheme: '%v'", h.SigningScheme)
return nil, fmt.Errorf("invalid signing scheme: '%v'", h.SigningScheme)
}
m := make(map[string]bool)
for _, h := range h.SignedHeaders {
if strings.EqualFold(h, HttpHeaderAuthorization) {
return nil, fmt.Errorf("Signed headers cannot include the 'Authorization' header")
return nil, fmt.Errorf("signed headers cannot include the 'Authorization' header")
}
m[h] = true
}
if len(m) != len(h.SignedHeaders) {
return nil, fmt.Errorf("List of signed headers cannot have duplicate names")
return nil, fmt.Errorf("list of signed headers cannot have duplicate names")
}
if h.SignatureMaxValidity < 0 {
return nil, fmt.Errorf("Signature max validity must be a positive value")
return nil, fmt.Errorf("signature max validity must be a positive value")
}
if err := h.loadPrivateKey(); err != nil {
return nil, err
Expand All @@ -168,7 +168,7 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) {
default:
// Do not change '%T' to anything else such as '%v'!
// The value of the private key must not be returned.
return nil, fmt.Errorf("Unsupported key: %T", h.privateKey)
return nil, fmt.Errorf("unsupported key: %T", h.privateKey)
}
}

Expand All @@ -181,7 +181,7 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) {
var file *os.File
file, err = os.Open(h.PrivateKeyPath)
if err != nil {
return fmt.Errorf("Cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err)
return fmt.Errorf("cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err)
}
defer func() {
err = file.Close()
Expand All @@ -199,7 +199,7 @@ func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error {
pemBlock, _ := pem.Decode(priv)
if pemBlock == nil {
// No PEM data has been found.
return fmt.Errorf("File '%s' does not contain PEM data", h.PrivateKeyPath)
return fmt.Errorf("file '%s' does not contain PEM data", h.PrivateKeyPath)
}
var privKey []byte
var err error
Expand All @@ -225,7 +225,7 @@ func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error {
return err
}
default:
return fmt.Errorf("Key '%s' is not supported", pemBlock.Type)
return fmt.Errorf("key '%s' is not supported", pemBlock.Type)
}
return nil
}
Expand All @@ -248,7 +248,7 @@ func SignRequest(
auth HttpSignatureAuth) error {
if auth.privateKey == nil {
return fmt.Errorf("Private key is not set")
return fmt.Errorf("private key is not set")
}
now := time.Now()
date := now.UTC().Format(http.TimeFormat)
Expand All @@ -262,7 +262,7 @@ func SignRequest(
var expiresUnix float64
if auth.SignatureMaxValidity < 0 {
return fmt.Errorf("Signature validity must be a positive value")
return fmt.Errorf("signature validity must be a positive value")
}
if auth.SignatureMaxValidity > 0 {
e := now.Add(auth.SignatureMaxValidity)
Expand All @@ -278,10 +278,10 @@ func SignRequest(
h = crypto.SHA256
prefix = "SHA-256="
default:
return fmt.Errorf("Unsupported signature scheme: %v", auth.SigningScheme)
return fmt.Errorf("unsupported signature scheme: %v", auth.SigningScheme)
}
if !h.Available() {
return fmt.Errorf("Hash '%v' is not available", h)
return fmt.Errorf("hash '%v' is not available", h)
}
// Build the "(request-target)" signature header.
Expand All @@ -308,7 +308,7 @@ func SignRequest(
m[h] = true
}
if len(m) != len(signedHeaders) {
return fmt.Errorf("List of signed headers must not have any duplicates")
return fmt.Errorf("list of signed headers must not have any duplicates")
}
hasCreatedParameter := false
hasExpiresParameter := false
Expand All @@ -317,15 +317,15 @@ func SignRequest(
var value string
switch header {
case strings.ToLower(HttpHeaderAuthorization):
return fmt.Errorf("Cannot include the 'Authorization' header as a signed header.")
return fmt.Errorf("cannot include the 'Authorization' header as a signed header")
case HttpSignatureParameterRequestTarget:
value = requestTarget
case HttpSignatureParameterCreated:
value = fmt.Sprintf("%d", created)
hasCreatedParameter = true
case HttpSignatureParameterExpires:
if auth.SignatureMaxValidity.Nanoseconds() == 0 {
return fmt.Errorf("Cannot set '(expires)' signature parameter. SignatureMaxValidity is not configured.")
return fmt.Errorf("cannot set '(expires)' signature parameter. SignatureMaxValidity is not configured")
}
value = fmt.Sprintf("%.3f", expiresUnix)
hasExpiresParameter = true
Expand Down Expand Up @@ -361,7 +361,7 @@ func SignRequest(
if v, ok = r.Header[canonicalHeader]; !ok {
// If a header specified in the headers parameter cannot be matched with
// a provided header in the message, the implementation MUST produce an error.
return fmt.Errorf("Header '%s' does not exist in the request", canonicalHeader)
return fmt.Errorf("header '%s' does not exist in the request", canonicalHeader)
}
// If there are multiple instances of the same header field, all
// header field values associated with the header field MUST be
Expand All @@ -376,7 +376,7 @@ func SignRequest(
fmt.Fprintf(&sb, "%s: %s", header, value)
}
if expiresUnix != 0 && !hasExpiresParameter {
return fmt.Errorf("SignatureMaxValidity is specified, but '(expired)' parameter is not present")
return fmt.Errorf("signatureMaxValidity is specified, but '(expired)' parameter is not present")
}
msg := []byte(sb.String())
msgHash := h.New()
Expand All @@ -394,14 +394,14 @@ func SignRequest(
case "", HttpSigningAlgorithmRsaPSS:
signature, err = rsa.SignPSS(rand.Reader, key, h, d, nil)
default:
return fmt.Errorf("Unsupported signing algorithm: '%s'", auth.SigningAlgorithm)
return fmt.Errorf("unsupported signing algorithm: '%s'", auth.SigningAlgorithm)
}
case *ecdsa.PrivateKey:
signature, err = key.Sign(rand.Reader, d, h)
case ed25519.PrivateKey: // requires go 1.13
signature, err = key.Sign(rand.Reader, msg, crypto.Hash(0))
default:
return fmt.Errorf("Unsupported private key")
return fmt.Errorf("unsupported private key")
}
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/go/go-petstore/client.go

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

4 changes: 2 additions & 2 deletions samples/client/petstore/go/go-petstore/configuration.go

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

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

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

4 changes: 2 additions & 2 deletions samples/openapi3/client/petstore/go/go-petstore/client.go

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

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

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

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

Loading

0 comments on commit 41255c1

Please sign in to comment.