Skip to content

Commit d1fa38e

Browse files
authored
[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
1 parent 2f214ee commit d1fa38e

File tree

6 files changed

+92
-30
lines changed

6 files changed

+92
-30
lines changed

modules/openapi-generator/src/main/resources/go/client.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333
)
3434

3535
var (
36-
jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`)
37-
xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`)
36+
JsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?json)`)
37+
XmlCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?xml)`)
3838
queryParamSplit = regexp.MustCompile(`(^|&)([^&]+)`)
3939
queryDescape = strings.NewReplacer( "%5B", "[", "%5D", "]" )
4040
)
@@ -544,13 +544,13 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err
544544
err = os.Remove((*f).Name())
545545
return
546546
}
547-
if xmlCheck.MatchString(contentType) {
547+
if XmlCheck.MatchString(contentType) {
548548
if err = xml.Unmarshal(b, v); err != nil {
549549
return err
550550
}
551551
return nil
552552
}
553-
if jsonCheck.MatchString(contentType) {
553+
if JsonCheck.MatchString(contentType) {
554554
if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas
555555
if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined
556556
if err = unmarshalObj.UnmarshalJSON(b); err != nil {
@@ -615,9 +615,9 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
615615
_, err = bodyBuf.WriteString(s)
616616
} else if s, ok := body.(*string); ok {
617617
_, err = bodyBuf.WriteString(*s)
618-
} else if jsonCheck.MatchString(contentType) {
618+
} else if JsonCheck.MatchString(contentType) {
619619
err = json.NewEncoder(bodyBuf).Encode(body)
620-
} else if xmlCheck.MatchString(contentType) {
620+
} else if XmlCheck.MatchString(contentType) {
621621
var bs []byte
622622
bs, err = xml.Marshal(body)
623623
if err == nil {

samples/client/echo_api/go/client.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/petstore/go/go-petstore/client.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
sw "go-petstore"
7+
)
8+
9+
type testCase struct {
10+
String string
11+
ShouldMatch bool
12+
}
13+
14+
func TestJsonCheck(t *testing.T) {
15+
testCases := []testCase{
16+
{"application/json", true},
17+
{"application/vnd.org.application+json", true},
18+
{"application/hal+json", true},
19+
{"text/json", true},
20+
{"text/vnd.org.application+json", true},
21+
{"text/hal+json", true},
22+
23+
{"application/bson", false},
24+
{"application/+json", false},
25+
{"text/bson", false},
26+
{"text/+json", false},
27+
28+
{"zip/json", false},
29+
}
30+
31+
for _, c := range testCases {
32+
actual := sw.JsonCheck.MatchString(c.String)
33+
if actual != c.ShouldMatch {
34+
t.Errorf("Expected %s to result in %v but got %v", c.String, c.ShouldMatch, actual)
35+
}
36+
}
37+
}
38+
39+
func TestXmlRegex(t *testing.T) {
40+
testCases := []testCase{
41+
{"application/xml", true},
42+
{"application/vnd.org.application+xml", true},
43+
{"application/hal+xml", true},
44+
{"text/xml", true},
45+
{"text/vnd.org.application+xml", true},
46+
{"text/hal+xml", true},
47+
48+
{"application/bmx", false},
49+
{"application/+xml", false},
50+
{"text/bmx", false},
51+
{"text/+xml", false},
52+
53+
{"zip/xml", false},
54+
}
55+
56+
for _, c := range testCases {
57+
actual := sw.XmlCheck.MatchString(c.String)
58+
if actual != c.ShouldMatch {
59+
t.Errorf("Expected %s to result in %v but got %v", c.String, c.ShouldMatch, actual)
60+
}
61+
}
62+
}

samples/openapi3/client/petstore/go/go-petstore/client.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)