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