Fix: Ptr issues with POST/PUT bodies; Add nonempty query params only
This commit is contained in:
parent
5d074b181a
commit
5cddea77d1
|
@ -10,7 +10,7 @@ func (e Encoder) Api(server openapi.Server) string {
|
||||||
tBuf := bytes.Buffer{}
|
tBuf := bytes.Buffer{}
|
||||||
tBuf.WriteString(fmt.Sprintf("package %v\n\n", e.Package))
|
tBuf.WriteString(fmt.Sprintf("package %v\n\n", e.Package))
|
||||||
tBuf.WriteString(e.GeneratedHeader())
|
tBuf.WriteString(e.GeneratedHeader())
|
||||||
tBuf.WriteString("import (\n \"fmt\"\n \"reflect\"\n \"strings\"\n)\n\n")
|
tBuf.WriteString("import (\n \"fmt\"\n \"net/url\"\n \"reflect\"\n \"strings\"\n)\n\n")
|
||||||
tBuf.WriteString(Comment(server.Description, 0))
|
tBuf.WriteString(Comment(server.Description, 0))
|
||||||
tBuf.WriteString(fmt.Sprintf("const %v_OPENAPI_BASE string = \"%v\"\n\n", strings.ToUpper(e.Package), server.Url))
|
tBuf.WriteString(fmt.Sprintf("const %v_OPENAPI_BASE string = \"%v\"\n\n", strings.ToUpper(e.Package), server.Url))
|
||||||
// replace func:
|
// replace func:
|
||||||
|
@ -34,6 +34,22 @@ func joinIfArray(v interface{}) string {
|
||||||
}
|
}
|
||||||
return tBuf.String()
|
return tBuf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addQueryParameter(q *url.Values, k string, v interface{}) {
|
||||||
|
tHasValue := false
|
||||||
|
switch reflect.TypeOf(v).Kind() {
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
s := reflect.ValueOf(v)
|
||||||
|
tHasValue = s.Len() > 0
|
||||||
|
default:
|
||||||
|
if v != nil {
|
||||||
|
tHasValue = !reflect.DeepEqual(v, reflect.Zero(reflect.TypeOf(v)).Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tHasValue {
|
||||||
|
q.Add("pageNumber", joinIfArray(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
`)
|
`)
|
||||||
return tBuf.String()
|
return tBuf.String()
|
||||||
}
|
}
|
||||||
|
|
|
@ -185,17 +185,18 @@ func (e Encoder) Funcs(tag string, operations []*openapi.Operation) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// post/put params ("body")
|
// post/put params ("body")
|
||||||
tBuf.WriteString(" var tBuf *bytes.Buffer = nil\n")
|
tBufBody := "nil"
|
||||||
if op.RequestBody != nil {
|
if op.RequestBody != nil {
|
||||||
tBuf.WriteString(" var tData []byte = nil\n")
|
tBuf.WriteString(" var tData []byte = nil\n")
|
||||||
tBuf.WriteString(" tData, err = json.Marshal(data)\n")
|
tBuf.WriteString(" tData, err = json.Marshal(data)\n")
|
||||||
tBuf.WriteString(" if err != nil {\n")
|
tBuf.WriteString(" if err != nil {\n")
|
||||||
tBuf.WriteString(" return\n")
|
tBuf.WriteString(" return\n")
|
||||||
tBuf.WriteString(" }\n")
|
tBuf.WriteString(" }\n")
|
||||||
tBuf.WriteString(" tBuf = bytes.NewBuffer(tData)\n")
|
tBuf.WriteString(" tBuf := bytes.NewBuffer(tData)\n")
|
||||||
|
tBufBody = "tBuf"
|
||||||
}
|
}
|
||||||
|
|
||||||
tBuf.WriteString(fmt.Sprintf(" req, _ := http.NewRequest(\"%v\", tPath, tBuf)\n", op.Method()))
|
tBuf.WriteString(fmt.Sprintf(" req, _ := http.NewRequest(\"%v\", tPath, %v)\n", op.Method(), tBufBody))
|
||||||
// add header parameters
|
// add header parameters
|
||||||
for _, p := range tParams {
|
for _, p := range tParams {
|
||||||
if p.In == "header" {
|
if p.In == "header" {
|
||||||
|
@ -209,7 +210,7 @@ func (e Encoder) Funcs(tag string, operations []*openapi.Operation) string {
|
||||||
tQBuf.WriteString(" q := req.URL.Query()\n")
|
tQBuf.WriteString(" q := req.URL.Query()\n")
|
||||||
for _, p := range tParams {
|
for _, p := range tParams {
|
||||||
if p.In == "query" {
|
if p.In == "query" {
|
||||||
tQBuf.WriteString(fmt.Sprintf(" q.Add(\"%v\", joinIfArray(%v))\n", p.Name, NormalizeName(p.Name)))
|
tQBuf.WriteString(fmt.Sprintf(" addQueryParameter(&q, \"%v\", joinIfArray(%v))\n", p.Name, NormalizeName(p.Name)))
|
||||||
tQBufHasData = true
|
tQBufHasData = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -336,6 +336,10 @@ func (s Schema) GoType() string {
|
||||||
tRet = "bool"
|
tRet = "bool"
|
||||||
case "array":
|
case "array":
|
||||||
tRet = fmt.Sprintf("[]%v", s.Items.GoType())
|
tRet = fmt.Sprintf("[]%v", s.Items.GoType())
|
||||||
|
case "object":
|
||||||
|
// should not happen, but in order to not to break things
|
||||||
|
// return an empty struct
|
||||||
|
tRet = "struct{/*object*/}"
|
||||||
case "": // arrays may not be tagged as such. deduce from the items element:
|
case "": // arrays may not be tagged as such. deduce from the items element:
|
||||||
if s.Items != nil {
|
if s.Items != nil {
|
||||||
tRet = fmt.Sprintf("[]%v", s.Items.GoType())
|
tRet = fmt.Sprintf("[]%v", s.Items.GoType())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user