Skip to content

UOE-12855: Fix [Google SDK]: Getting 400 bad request when consented_p… #1129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 64 additions & 23 deletions modules/pubmatic/openwrap/sdk/googlesdk/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
const androidAppId = "com.google.ads.mediation.pubmatic.PubMaticMediationAdapter"
const iOSAppId = "GADMediationAdapterPubMatic"

const (
consentedProvidersSettingsListKey = "consented_providers_settings"
consentedProvidersKey = "consented_providers"
)

var jsoniterator = jsoniter.ConfigCompatibleWithStandardLibrary

type wrapperData struct {
Expand Down Expand Up @@ -238,39 +243,75 @@ func modifyRequestWithGoogleFeature(request *openrtb2.BidRequest, features featu
}

func modifyRequestWithStaticData(request *openrtb2.BidRequest) {
if len(request.Imp) == 0 {
if request == nil {
return
}

// Always set secure to 1
request.Imp[0].Secure = ptrutil.ToPtr(int8(1))
if len(request.Imp) > 0 {
// Always set secure to 1
request.Imp[0].Secure = ptrutil.ToPtr(int8(1))

//Set gpid
if len(request.Imp[0].TagID) > 0 {
request.Imp[0].Ext, _ = jsonparser.Set(request.Imp[0].Ext, []byte(strconv.Quote(request.Imp[0].TagID)), "gpid")
}
//Set gpid
if len(request.Imp[0].TagID) > 0 {
request.Imp[0].Ext, _ = jsonparser.Set(request.Imp[0].Ext, []byte(strconv.Quote(request.Imp[0].TagID)), "gpid")
}

// Remove metric
request.Imp[0].Metric = nil
// Remove metric
request.Imp[0].Metric = nil

// Remove banner if impression is rewarded and banner and video both are present
if request.Imp[0].Rwdd == 1 && request.Imp[0].Banner != nil && request.Imp[0].Video != nil {
request.Imp[0].Banner = nil
}
// Remove banner if impression is rewarded and banner and video both are present
if request.Imp[0].Rwdd == 1 && request.Imp[0].Banner != nil && request.Imp[0].Video != nil {
request.Imp[0].Banner = nil
}

// Remove unsupported fields from banner
if request.Imp[0].Banner != nil {
request.Imp[0].Banner.WMin = 0
request.Imp[0].Banner.HMin = 0
request.Imp[0].Banner.WMax = 0
request.Imp[0].Banner.HMax = 0
// Remove unsupported fields from banner
if request.Imp[0].Banner != nil {
request.Imp[0].Banner.WMin = 0
request.Imp[0].Banner.HMin = 0
request.Imp[0].Banner.WMax = 0
request.Imp[0].Banner.HMax = 0
}

// Remove native from request
request.Imp[0].Native = nil

// Remove video from request
request.Imp[0].Video = nil
}

// Remove native from request
request.Imp[0].Native = nil
// change data type of user.ext.consented_providers_settings.consented_providers from []string to []int
if request.User != nil && request.User.Ext != nil {
consentedProvidedBytes, dataType, _, err := jsonparser.Get(request.User.Ext, consentedProvidersSettingsListKey, consentedProvidersKey)
if err != nil || dataType != jsonparser.Array {
return
}

// Remove video from request
request.Imp[0].Video = nil
var consentedProviders []int
_, err = jsonparser.ArrayEach(consentedProvidedBytes, func(provider []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil || dataType != jsonparser.String {
return
}

providerInt, err := strconv.Atoi(string(provider))
if err == nil {
consentedProviders = append(consentedProviders, providerInt)
}
})
if err != nil {
// Delete consented_providers_settings.consented_providers in case of errors to avoid bad request
request.User.Ext = jsonparser.Delete(request.User.Ext, consentedProvidersSettingsListKey, consentedProvidersKey)
return
}

providersBytes, err := jsoniterator.Marshal(consentedProviders)
if err != nil {
// Delete consented_providers_settings.consented_providers in case of errors to avoid bad request
request.User.Ext = jsonparser.Delete(request.User.Ext, consentedProvidersSettingsListKey, consentedProvidersKey)
return
}

request.User.Ext, _ = jsonparser.Set(request.User.Ext, providersBytes, consentedProvidersSettingsListKey, consentedProvidersKey)
}
}

func modifyRequestWithSignalData(request *openrtb2.BidRequest, signalData *openrtb2.BidRequest) {
Expand Down
116 changes: 116 additions & 0 deletions modules/pubmatic/openwrap/sdk/googlesdk/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,122 @@ func TestModifyRequestWithStaticData(t *testing.T) {
},
},
},
{
name: "Convert consented_providers from string array to int array",
request: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Secure: ptrutil.ToPtr(int8(1)),
},
},
User: &openrtb2.User{
Ext: []byte(`{"consented_providers_settings":{"consented_providers":["1","2","3"]}}`),
},
},
expectedResult: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Secure: ptrutil.ToPtr(int8(1)),
},
},
User: &openrtb2.User{
Ext: []byte(`{"consented_providers_settings":{"consented_providers":[1,2,3]}}`),
},
},
},
{
name: "Handle mixed valid and invalid string values in consented_providers",
request: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Secure: ptrutil.ToPtr(int8(1)),
},
},
User: &openrtb2.User{
Ext: []byte(`{"consented_providers_settings":{"consented_providers":["1","invalid","3"]},"other_field":"value"}`),
},
},
expectedResult: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Secure: ptrutil.ToPtr(int8(1)),
},
},
User: &openrtb2.User{
Ext: []byte(`{"consented_providers_settings":{"consented_providers":[1,3]},"other_field":"value"}`),
},
},
},
{
name: "No change when consented_providers_settings is missing",
request: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Secure: ptrutil.ToPtr(int8(1)),
},
},
User: &openrtb2.User{
Ext: []byte(`{"other_field":"value"}`),
},
},
expectedResult: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Secure: ptrutil.ToPtr(int8(1)),
},
},
User: &openrtb2.User{
Ext: []byte(`{"other_field":"value"}`),
},
},
},
{
name: "No change when consented_providers is not an array",
request: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Secure: ptrutil.ToPtr(int8(1)),
},
},
User: &openrtb2.User{
Ext: []byte(`{"consented_providers_settings":{"consented_providers":"not_an_array"}}`),
},
},
expectedResult: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Secure: ptrutil.ToPtr(int8(1)),
},
},
User: &openrtb2.User{
Ext: []byte(`{"consented_providers_settings":{"consented_providers":"not_an_array"}}`),
},
},
},
{
name: "Remove native and video from request",
request: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Native: &openrtb2.Native{
Request: `{"native":{"layout":1}}`,
},
Video: &openrtb2.Video{
MIMEs: []string{"video/mp4"},
},
},
},
},
expectedResult: &openrtb2.BidRequest{
Imp: []openrtb2.Imp{
{
Native: nil,
Video: nil,
Secure: ptrutil.ToPtr(int8(1)),
},
},
},
},
}

for _, tt := range tests {
Expand Down
Loading