swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.78k stars 6.02k forks source link

go-client: cannot use a.Configuration.GetAPIKeyWithPrefix("apikey") (type string) as type []string in assignment #3849

Closed loretoparisi closed 7 years ago

loretoparisi commented 7 years ago

When generating the go-client-generated.zip from editor.swagger.io I get several error in the generated code like

swagger/album_api.go:139: cannot use a.Configuration.GetAPIKeyWithPrefix("apikey") (type string) as type []string in assignment

due to a bad assign to []string literal of a string. I'm pretty new to go, so I'm not sure if this is due to a recent go compiler (See later).

Description
queryParams["apikey"] =  a.Configuration.GetAPIKeyWithPrefix("apikey")
Swagger-codegen version

I'm using

$ go version
go version go1.7.1 darwin/amd64

and latest swagger codegen from editor.swagger.io.

Swagger declaration file content or url

The file is available online at https://playground.musixmatch.com/swagger.json.

Command line used for generation
Steps to reproduce
  1. import from url the swagger definition above in the Swagger Editor
  2. Generate the Go client
  3. Open README.md
    Related issues

    Suggest a Fix

The fix is to replace this assignment

queryParams["apikey"] =  a.Configuration.GetAPIKeyWithPrefix("apikey")

with

queryParams.Add("apikey",a.Configuration.GetAPIKeyWithPrefix("apikey"))

I have achieved this from this example of usage of url.Values{}.

wing328 commented 7 years ago

@loretoparisi thanks for reporting the issue.

cc @guohuang

loretoparisi commented 7 years ago

Welcome! I also suggest to add this example, that for go beginners like me, took half a day to complete it!

/*
 * Musixmatch Go Client
 * @uses Swagger generated Go client
 * @author Loreto Parisi (loreto at musixmatch dot com)
 * @see https://developer.musixmatch.com/documentation
 *  @copy 2016 Musixmatch Spa
*/
package main

import (
    "fmt"
    "bytes"
    "encoding/json"
    "./swagger"
    "io"
    "io/ioutil"
    "os"
)

func jsonPrettyPrint(in string) string {
    var out bytes.Buffer
    err := json.Indent(&out, []byte(in), "", "\t")
    if err != nil {
        return in
    }
    return out.String()
}
func main() {

    albumApi := swagger.NewAlbumApi()

    configuration := albumApi.Configuration
    configuration.SetDebug(true)
    configuration.APIKey["apikey"] = "YOUR_API_KEY"

    var albumId = "14250417"
    jsonResponse, response, _  := albumApi.AlbumGetGet(albumId,"json","")

    defer response.Body.Close()
   // stay away of ioutil.ReadAll memory leaks
   htmlData, err := ioutil.ReadAll(io.LimitReader(response.Body, 2048))
   // unmarshall struct to json
    b, err := json.Marshal(jsonResponse.Message)
    if err != nil {
        fmt.Println(err)
        return
    }
    var c = jsonPrettyPrint( string(b) )
    fmt.Println(c)

}
guohuang commented 7 years ago

@loretoparisi I tried using Go version 1.7.1 on windows, I couldn't reproduce the compilation issue that you have, the function GetAPIKeyWithPrefix is only returning string, not []string, so i don't know what the issue is.

wing328 commented 7 years ago

Closing this as there's no further update.