From d1fa38e2865513e0a29ce33bfe3d9aaf338603f1 Mon Sep 17 00:00:00 2001 From: Josh Raker <48493233+joshraker@users.noreply.github.com> Date: Mon, 16 Oct 2023 12:47:09 -0400 Subject: [PATCH] [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 --- .../src/main/resources/go/client.mustache | 12 ++-- samples/client/echo_api/go/client.go | 12 ++-- .../client/petstore/go/go-petstore/client.go | 12 ++-- .../x-auth-id-alias/go-experimental/client.go | 12 ++-- .../client/petstore/go/api_client_test.go | 62 +++++++++++++++++++ .../client/petstore/go/go-petstore/client.go | 12 ++-- 6 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 samples/openapi3/client/petstore/go/api_client_test.go diff --git a/modules/openapi-generator/src/main/resources/go/client.mustache b/modules/openapi-generator/src/main/resources/go/client.mustache index 3f6dcb7bf59..f26227b3f95 100644 --- a/modules/openapi-generator/src/main/resources/go/client.mustache +++ b/modules/openapi-generator/src/main/resources/go/client.mustache @@ -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 { diff --git a/samples/client/echo_api/go/client.go b/samples/client/echo_api/go/client.go index 2c88423e340..a24ca6124e5 100644 --- a/samples/client/echo_api/go/client.go +++ b/samples/client/echo_api/go/client.go @@ -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 { diff --git a/samples/client/petstore/go/go-petstore/client.go b/samples/client/petstore/go/go-petstore/client.go index 620543ae4ff..63e925e8cdd 100644 --- a/samples/client/petstore/go/go-petstore/client.go +++ b/samples/client/petstore/go/go-petstore/client.go @@ -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 { diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go b/samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go index d23863248f9..d986d66eefb 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go +++ b/samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go @@ -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 { diff --git a/samples/openapi3/client/petstore/go/api_client_test.go b/samples/openapi3/client/petstore/go/api_client_test.go new file mode 100644 index 00000000000..976eb4de501 --- /dev/null +++ b/samples/openapi3/client/petstore/go/api_client_test.go @@ -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) + } + } +} diff --git a/samples/openapi3/client/petstore/go/go-petstore/client.go b/samples/openapi3/client/petstore/go/go-petstore/client.go index f807178e442..c6f6fa66b70 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/client.go +++ b/samples/openapi3/client/petstore/go/go-petstore/client.go @@ -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 {