Repository: swaggest/rest
Pull request #162 (2023-06-06 16:02:20)

fix: allow 'application/json;charset=UTF-8'
This is a regression - charset was allowed in versions prior to v0.2.50

vearutop: @brianwilkinson, out of curiosity, what is your setup that emits such content type (what kind/language/framework of client)?

Related to https://github.com/swaggest/rest/pull/158.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/swaggest/rest/pull/162?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest) Report
> Merging [#162](https://app.codecov.io/gh/swaggest/rest/pull/162?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest) (351a51c) into [master](https://app.codecov.io/gh/swaggest/rest/commit/91e0f4fce00cabffd19825fa491d61f4f4261407?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest) (91e0f4f) will **increase** coverage by `0.06%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master     #162      +/-   ##
==========================================
+ Coverage   77.98%   78.04%   +0.06%     
==========================================
  Files          28       28              
  Lines        1608     1608              
==========================================
+ Hits         1254     1255       +1     
+ Misses        233      232       -1     
  Partials      121      121              
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `78.04% <100.00%> (+0.06%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://app.codecov.io/gh/swaggest/rest/pull/162?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest) | Coverage Δ | |
|---|---|---|
| [request/jsonbody.go](https://app.codecov.io/gh/swaggest/rest/pull/162?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest#diff-cmVxdWVzdC9qc29uYm9keS5nbw==) | `100.00% <100.00%> (+2.70%)` | :arrow_up: |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest)

brianwilkinson: > 

Hi @vearutop. We have a suite of integration tests written in groovy/java that have all started to fail because of this change. I have not yet discovered if it is also a problem for our go client applications.
vearutop: Thank you for details. 👍 

Patch:
diff --git a/request/jsonbody.go b/request/jsonbody.go
index ec40072..a7ddafa 100644
--- a/request/jsonbody.go
+++ b/request/jsonbody.go
@@ -72,7 +72,7 @@ func checkJSONBodyContentType(contentType string, tolerateFormData bool) (ret bo
 		return false, nil
 	}
 
-	if strings.ToLower(contentType) != "application/json" {
+	if !strings.Contains(strings.ToLower(contentType), "application/json") {
 		if tolerateFormData && (contentType == "application/x-www-form-urlencoded" || contentType == "multipart/form-data") {
 			return true, nil
 		}


Test patch:
diff --git a/request/jsonbody_test.go b/request/jsonbody_test.go
index c8f7aab..abc4d72 100644
--- a/request/jsonbody_test.go
+++ b/request/jsonbody_test.go
@@ -117,3 +117,16 @@ func Test_decodeJSONBody_tolerateFormData(t *testing.T) {
 	assert.Empty(t, i.CustomerID)
 	assert.Empty(t, i.Type)
 }
+
+func Test_decodeJSONBody_charset(t *testing.T) {
+	req, err := http.NewRequest(http.MethodPost, "any", bytes.NewBufferString(`{"amount": 123}`))
+	require.NoError(t, err)
+	req.Header.Set("Content-Type", "application/json;charset=utf-8")
+
+	type Input struct {
+		Amount int `json:"amount" formData:"amount"`
+	}
+	i := Input{}
+
+	assert.NoError(t, decodeJSONBody(readJSON, false)(req, &i, nil))
+}