Skip to content

Commit

Permalink
[BUG][GO] Add support for all +json and +xml suffixed media types (Op…
Browse files Browse the repository at this point in the history
…enAPITools#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
  • Loading branch information
joshraker authored Oct 16, 2023
1 parent 2f214ee commit d1fa38e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 30 deletions.
12 changes: 6 additions & 6 deletions modules/openapi-generator/src/main/resources/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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", "]" )
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions samples/client/echo_api/go/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions samples/client/petstore/go/go-petstore/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions samples/openapi3/client/petstore/go/api_client_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
12 changes: 6 additions & 6 deletions samples/openapi3/client/petstore/go/go-petstore/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1fa38e

Please sign in to comment.