[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 30 deletions

View File

@ -33,8 +33,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", "]" )
)
@ -544,13 +544,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 {
@ -615,9 +615,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 {

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 {

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", "]" )
)
@ -484,13 +484,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 {
@ -555,9 +555,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 {

View File

@ -35,8 +35,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", "]" )
)
@ -452,13 +452,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 {
@ -523,9 +523,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 {

View File

@ -0,0 +1,62 @@
package main
import (
"testing"
sw "go-petstore"
)
type testCase struct {
String string
ShouldMatch bool
}
func TestJsonCheck(t *testing.T) {
testCases := []testCase{
{"application/json", true},
{"application/vnd.org.application+json", true},
{"application/hal+json", true},
{"text/json", true},
{"text/vnd.org.application+json", true},
{"text/hal+json", true},
{"application/bson", false},
{"application/+json", false},
{"text/bson", false},
{"text/+json", false},
{"zip/json", false},
}
for _, c := range testCases {
actual := sw.JsonCheck.MatchString(c.String)
if actual != c.ShouldMatch {
t.Errorf("Expected %s to result in %v but got %v", c.String, c.ShouldMatch, actual)
}
}
}
func TestXmlRegex(t *testing.T) {
testCases := []testCase{
{"application/xml", true},
{"application/vnd.org.application+xml", true},
{"application/hal+xml", true},
{"text/xml", true},
{"text/vnd.org.application+xml", true},
{"text/hal+xml", true},
{"application/bmx", false},
{"application/+xml", false},
{"text/bmx", false},
{"text/+xml", false},
{"zip/xml", false},
}
for _, c := range testCases {
actual := sw.XmlCheck.MatchString(c.String)
if actual != c.ShouldMatch {
t.Errorf("Expected %s to result in %v but got %v", c.String, c.ShouldMatch, actual)
}
}
}

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", "]" )
)
@ -502,13 +502,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 {
@ -573,9 +573,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 {