github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/convert_down.go (about)

     1  package openrtb_ext
     2  
     3  func ConvertDownTo25(r *RequestWrapper) error {
     4  	// schain
     5  	if err := moveSupplyChainFrom26To25(r); err != nil {
     6  		return err
     7  	}
     8  
     9  	// gdpr
    10  	if err := moveGDPRFrom26To25(r); err != nil {
    11  		return err
    12  	}
    13  	if err := moveConsentFrom26To25(r); err != nil {
    14  		return err
    15  	}
    16  
    17  	// ccpa
    18  	if err := moveUSPrivacyFrom26To25(r); err != nil {
    19  		return err
    20  	}
    21  
    22  	// eid
    23  	if err := moveEIDFrom26To25(r); err != nil {
    24  		return err
    25  	}
    26  
    27  	// imp
    28  	for _, imp := range r.GetImp() {
    29  		if err := moveRewardedFrom26ToPrebidExt(imp); err != nil {
    30  			return err
    31  		}
    32  	}
    33  
    34  	// Remove fields introduced in OpenRTB 2.6+. The previous OpenRTB 2.5 spec did not specify that
    35  	// bidders must tolerate new or unexpected fields.
    36  	clear26Fields(r)
    37  	clear202211Fields(r)
    38  	clear202303Fields(r)
    39  	clear202309Fields(r)
    40  
    41  	return nil
    42  }
    43  
    44  // moveSupplyChainFrom26To25 modifies the request to move the OpenRTB 2.6 supply chain
    45  // object (req.source.schain) to the OpenRTB 2.5 location (req.source.ext.schain). If the
    46  // OpenRTB 2.5 location is already present it may be overwritten. The OpenRTB 2.5 location
    47  // is expected to be empty.
    48  func moveSupplyChainFrom26To25(r *RequestWrapper) error {
    49  	if r.Source == nil || r.Source.SChain == nil {
    50  		return nil
    51  	}
    52  
    53  	// read and clear 2.6 location
    54  	schain26 := r.Source.SChain
    55  	r.Source.SChain = nil
    56  
    57  	// move to 2.5 location
    58  	sourceExt, err := r.GetSourceExt()
    59  	if err != nil {
    60  		return err
    61  	}
    62  	sourceExt.SetSChain(schain26)
    63  
    64  	return nil
    65  }
    66  
    67  // moveGDPRFrom26To25 modifies the request to move the OpenRTB 2.6 GDPR signal
    68  // field (req.regs.gdpr) to the OpenRTB 2.5 location (req.regs.ext.gdpr). If the
    69  // OpenRTB 2.5 location is already present it may be overwritten. The OpenRTB 2.5
    70  // location is expected to be empty.
    71  func moveGDPRFrom26To25(r *RequestWrapper) error {
    72  	if r.Regs == nil || r.Regs.GDPR == nil {
    73  		return nil
    74  	}
    75  
    76  	// read and clear 2.6 location
    77  	gdpr26 := r.Regs.GDPR
    78  	r.Regs.GDPR = nil
    79  
    80  	// move to 2.5 location
    81  	regExt, err := r.GetRegExt()
    82  	if err != nil {
    83  		return err
    84  	}
    85  	regExt.SetGDPR(gdpr26)
    86  
    87  	return nil
    88  }
    89  
    90  // moveConsentFrom26To25 modifies the request to move the OpenRTB 2.6 GDPR consent
    91  // field (req.user.consent) to the OpenRTB 2.5 location (req.user.ext.consent). If
    92  // the OpenRTB 2.5 location is already present it may be overwritten. The OpenRTB 2.5
    93  // location is expected to be empty.
    94  func moveConsentFrom26To25(r *RequestWrapper) error {
    95  	if r.User == nil || len(r.User.Consent) == 0 {
    96  		return nil
    97  	}
    98  
    99  	// read and clear 2.6 location
   100  	consent26 := r.User.Consent
   101  	r.User.Consent = ""
   102  
   103  	// move to 2.5 location
   104  	userExt, err := r.GetUserExt()
   105  	if err != nil {
   106  		return err
   107  	}
   108  	userExt.SetConsent(&consent26)
   109  
   110  	return nil
   111  }
   112  
   113  // moveUSPrivacyFrom26To25 modifies the request to move the OpenRTB 2.6 US Privacy (CCPA)
   114  // consent string (req.regs.us_privacy) to the OpenRTB 2.5 location (req.regs.ext.us_privacy).
   115  // If the OpenRTB 2.5 location is already present it may be overwritten. The OpenRTB 2.5
   116  // location is expected to be empty.
   117  func moveUSPrivacyFrom26To25(r *RequestWrapper) error {
   118  	if r.Regs == nil || len(r.Regs.USPrivacy) == 0 {
   119  		return nil
   120  	}
   121  
   122  	// read and clear 2.6 location
   123  	usprivacy26 := r.Regs.USPrivacy
   124  	r.Regs.USPrivacy = ""
   125  
   126  	// move to 2.5 location
   127  	regExt, err := r.GetRegExt()
   128  	if err != nil {
   129  		return err
   130  	}
   131  	regExt.SetUSPrivacy(usprivacy26)
   132  
   133  	return nil
   134  }
   135  
   136  // moveEIDFrom26To25 modifies the request to move the OpenRTB 2.6 external identifiers
   137  // (req.user.eids) to the OpenRTB 2.5 location (req.user.ext.eids). If the OpenRTB 2.5
   138  // location is already present it may be overwritten. The OpenRTB 2.5 location is
   139  // expected to be empty.
   140  func moveEIDFrom26To25(r *RequestWrapper) error {
   141  	if r.User == nil || r.User.EIDs == nil {
   142  		return nil
   143  	}
   144  
   145  	// read and clear 2.6 location
   146  	eid26 := r.User.EIDs
   147  	r.User.EIDs = nil
   148  
   149  	// move to 2.5 location
   150  	userExt, err := r.GetUserExt()
   151  	if err != nil {
   152  		return err
   153  	}
   154  	userExt.SetEid(&eid26)
   155  
   156  	return nil
   157  }
   158  
   159  // moveRewardedFrom26ToPrebidExt modifies the impression to move the OpenRTB 2.6 rewarded
   160  // signal (imp[].rwdd) to the OpenRTB 2.x Prebid specific location (imp[].ext.prebid.is_rewarded_inventory).
   161  // If the Prebid specific location is already present, it may be overwritten. The Prebid specific
   162  // location is expected to be empty.
   163  func moveRewardedFrom26ToPrebidExt(i *ImpWrapper) error {
   164  	if i.Rwdd == 0 {
   165  		return nil
   166  	}
   167  
   168  	// read and clear 2.6 location
   169  	rwdd26 := i.Rwdd
   170  	i.Rwdd = 0
   171  
   172  	// move to Prebid specific location
   173  	impExt, err := i.GetImpExt()
   174  	if err != nil {
   175  		return err
   176  	}
   177  	impExtPrebid := impExt.GetOrCreatePrebid()
   178  	impExtPrebid.IsRewardedInventory = &rwdd26
   179  	impExt.SetPrebid(impExtPrebid)
   180  
   181  	return nil
   182  }
   183  
   184  // clear26Fields sets all fields introduced in OpenRTB 2.6 to default values, which
   185  // will cause them to be omitted during json marshal.
   186  func clear26Fields(r *RequestWrapper) {
   187  	r.WLangB = nil
   188  	r.CatTax = 0
   189  
   190  	if app := r.App; app != nil {
   191  		app.CatTax = 0
   192  		app.KwArray = nil
   193  
   194  		if content := r.App.Content; content != nil {
   195  			content.CatTax = 0
   196  			content.KwArray = nil
   197  			content.LangB = ""
   198  			content.Network = nil
   199  			content.Channel = nil
   200  
   201  			if producer := r.App.Content.Producer; producer != nil {
   202  				producer.CatTax = 0
   203  			}
   204  		}
   205  
   206  		if publisher := r.App.Publisher; publisher != nil {
   207  			publisher.CatTax = 0
   208  		}
   209  	}
   210  
   211  	if site := r.Site; site != nil {
   212  		site.CatTax = 0
   213  		site.KwArray = nil
   214  
   215  		if content := r.Site.Content; content != nil {
   216  			content.CatTax = 0
   217  			content.KwArray = nil
   218  			content.LangB = ""
   219  			content.Network = nil
   220  			content.Channel = nil
   221  
   222  			if producer := r.Site.Content.Producer; producer != nil {
   223  				producer.CatTax = 0
   224  			}
   225  		}
   226  
   227  		if publisher := r.Site.Publisher; publisher != nil {
   228  			publisher.CatTax = 0
   229  		}
   230  	}
   231  
   232  	if device := r.Device; device != nil {
   233  		device.SUA = nil
   234  		device.LangB = ""
   235  	}
   236  
   237  	if regs := r.Regs; regs != nil {
   238  		regs.GDPR = nil
   239  		regs.USPrivacy = ""
   240  	}
   241  
   242  	if source := r.Source; source != nil {
   243  		source.SChain = nil
   244  	}
   245  
   246  	if user := r.User; user != nil {
   247  		user.KwArray = nil
   248  		user.Consent = ""
   249  		user.EIDs = nil
   250  	}
   251  
   252  	for _, imp := range r.GetImp() {
   253  		imp.Rwdd = 0
   254  		imp.SSAI = 0
   255  
   256  		if audio := imp.Audio; audio != nil {
   257  			audio.PodDur = 0
   258  			audio.RqdDurs = nil
   259  			audio.PodID = ""
   260  			audio.PodSeq = 0
   261  			audio.SlotInPod = 0
   262  			audio.MinCPMPerSec = 0
   263  		}
   264  
   265  		if video := imp.Video; video != nil {
   266  			video.MaxSeq = 0
   267  			video.PodDur = 0
   268  			video.PodID = ""
   269  			video.PodSeq = 0
   270  			video.RqdDurs = nil
   271  			video.SlotInPod = 0
   272  			video.MinCPMPerSec = 0
   273  		}
   274  	}
   275  }
   276  
   277  // clear202211Fields sets all fields introduced in OpenRTB 2.6-202211 to default values
   278  // which will cause them to be omitted during json marshal.
   279  func clear202211Fields(r *RequestWrapper) {
   280  	r.DOOH = nil
   281  
   282  	if app := r.App; app != nil {
   283  		app.InventoryPartnerDomain = ""
   284  	}
   285  
   286  	if site := r.Site; site != nil {
   287  		site.InventoryPartnerDomain = ""
   288  	}
   289  
   290  	if regs := r.Regs; regs != nil {
   291  		regs.GPP = ""
   292  		regs.GPPSID = nil
   293  	}
   294  
   295  	for _, imp := range r.GetImp() {
   296  		imp.Qty = nil
   297  		imp.DT = 0
   298  	}
   299  }
   300  
   301  // clear202303Fields sets all fields introduced in OpenRTB 2.6-202303 to default values
   302  // which will cause them to be omitted during json marshal.
   303  func clear202303Fields(r *RequestWrapper) {
   304  	for _, imp := range r.GetImp() {
   305  		imp.Refresh = nil
   306  
   307  		if video := imp.Video; video != nil {
   308  			video.Plcmt = 0
   309  		}
   310  	}
   311  }
   312  
   313  // clear202309Fields sets all fields introduced in OpenRTB 2.6-202309 to default values
   314  // which will cause them to be omitted during json marshal.
   315  func clear202309Fields(r *RequestWrapper) {
   316  	r.ACat = nil
   317  
   318  	for _, imp := range r.GetImp() {
   319  		if audio := imp.Audio; audio != nil {
   320  			audio.DurFloors = nil
   321  		}
   322  
   323  		if video := imp.Video; video != nil {
   324  			video.DurFloors = nil
   325  		}
   326  
   327  		if pmp := imp.PMP; pmp != nil {
   328  			for i := range pmp.Deals {
   329  				pmp.Deals[i].Guar = 0
   330  				pmp.Deals[i].MinCPMPerSec = 0
   331  				pmp.Deals[i].DurFloors = nil
   332  			}
   333  		}
   334  	}
   335  }