[BUG][GO] Add support for all +json and +xml suffixed media types (#16816)

* Add support for all +json and +xml suffixed media types to generated Go client

* Export JsonCheck and XmlCheck and add external tests

* Remove client_test.mustache
This commit is contained in:
Josh Raker
2023-10-16 12:47:09 -04:00
committed by GitHub
parent 2f214ee6c6
commit d1fa38e286
6 changed files with 92 additions and 30 deletions

View File

@@ -36,8 +36,8 @@ import (
)
var (
jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`)
xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`)
JsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?json)`)
XmlCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?xml)`)
queryParamSplit = regexp.MustCompile(`(^|&)([^&]+)`)
queryDescape = strings.NewReplacer( "%5B", "[", "%5D", "]" )
)
@@ -473,13 +473,13 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err
err = os.Remove((*f).Name())
return
}
if xmlCheck.MatchString(contentType) {
if XmlCheck.MatchString(contentType) {
if err = xml.Unmarshal(b, v); err != nil {
return err
}
return nil
}
if jsonCheck.MatchString(contentType) {
if JsonCheck.MatchString(contentType) {
if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas
if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined
if err = unmarshalObj.UnmarshalJSON(b); err != nil {
@@ -544,9 +544,9 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
_, err = bodyBuf.WriteString(s)
} else if s, ok := body.(*string); ok {
_, err = bodyBuf.WriteString(*s)
} else if jsonCheck.MatchString(contentType) {
} else if JsonCheck.MatchString(contentType) {
err = json.NewEncoder(bodyBuf).Encode(body)
} else if xmlCheck.MatchString(contentType) {
} else if XmlCheck.MatchString(contentType) {
var bs []byte
bs, err = xml.Marshal(body)
if err == nil {