Skip to content
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

Merge site object for SmartadserverBidder #1225

Merged
merged 4 commits into from
Apr 22, 2021

Conversation

muuki88
Copy link
Contributor

@muuki88 muuki88 commented Apr 12, 2021

The Smartadserver adapter overrides the entire site object, which removes all openrtb extensions like domain, page or any other information that has been set.

Comment on lines 65 to 73
final Publisher publisher = siteOpt.flatMap(site -> Optional.ofNullable(site.getPublisher()))
.map(p -> p.toBuilder())
.orElse(Publisher.builder())
.id(networkId)
.build();
final Site site = siteOpt
.map(s -> s.toBuilder())
.orElse(Site.builder())
.publisher(publisher).build();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm coming from Scala with a focus on functional styles. Is this kind of programming style okay for this project?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we prefer to use methods like modify[DesiredObject], like this

private Site modifySite(Site site, Integer networkId) {
        final Site.SiteBuilder siteBuilder = site != null ? site.toBuilder() : Site.builder();
        final Publisher sitePublisher = site != null ? site.getPublisher() : null;

        return siteBuilder.publisher(modifyPublisher(sitePublisher, networkId)).build();
    }

    private Publisher modifyPublisher(Publisher publisher, Integer networkId) {
        final Publisher.PublisherBuilder publisherBuilder = publisher != null
                ? publisher.toBuilder()
                : Publisher.builder();

        return publisherBuilder.id(String.valueOf(networkId)).build();
    }

Then your bidRequest can be created like

final BidRequest updatedRequest = request.toBuilder()
                        .imp(Collections.singletonList(imp))
                        .site(modifySite(request.getSite(), extImpSmartadserver.getNetworkId()))
                        .build();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored according to your suggestions 😃

Out of curiosity. Was not using Optional an explicit decisions and if why so? Performance, too much refactoring? I was thinking about opening an issue to discuss this as I'm stumbling over NPEs quite a lot 😂 (not used to null anymore)

Comment on lines 101 to 107
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1);
final Site site = result.getValue().get(0).getPayload().getSite();
assertThat(site).isEqualTo(Site.builder()
.domain("www.foo.com")
.publisher(Publisher.builder().domain("foo.com").id("4").build())
.build());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually do like this

final Site expectedSite = Site.builder()
                .domain("www.foo.com")
                .publisher(Publisher.builder().domain("foo.com").id("4").build())
                .build();
        assertThat(result.getErrors()).isEmpty();
        assertThat(result.getValue())
                .extracting(HttpRequest::getPayload)
                .extracting(BidRequest::getSite)
                .containsExactly(expectedSite);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks nicer 😄

Comment on lines 65 to 73
final Publisher publisher = siteOpt.flatMap(site -> Optional.ofNullable(site.getPublisher()))
.map(p -> p.toBuilder())
.orElse(Publisher.builder())
.id(networkId)
.build();
final Site site = siteOpt
.map(s -> s.toBuilder())
.orElse(Site.builder())
.publisher(publisher).build();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we prefer to use methods like modify[DesiredObject], like this

private Site modifySite(Site site, Integer networkId) {
        final Site.SiteBuilder siteBuilder = site != null ? site.toBuilder() : Site.builder();
        final Publisher sitePublisher = site != null ? site.getPublisher() : null;

        return siteBuilder.publisher(modifyPublisher(sitePublisher, networkId)).build();
    }

    private Publisher modifyPublisher(Publisher publisher, Integer networkId) {
        final Publisher.PublisherBuilder publisherBuilder = publisher != null
                ? publisher.toBuilder()
                : Publisher.builder();

        return publisherBuilder.id(String.valueOf(networkId)).build();
    }

Then your bidRequest can be created like

final BidRequest updatedRequest = request.toBuilder()
                        .imp(Collections.singletonList(imp))
                        .site(modifySite(request.getSite(), extImpSmartadserver.getNetworkId()))
                        .build();

@@ -81,6 +83,30 @@ public void makeHttpRequestsShouldCreateCorrectURL() {
.isEqualTo("https://test.endpoint.com/path/api/bid?testParam=testVal&callerId=5");
}

@Test
public void makeHttpRequestsShouldMergeExistingPublisherData() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this test also checks that we update Site object, so it will be great to rename it like
makeHttpRequestsShouldUpdateSiteObjectIfPresent or another one

@muuki88
Copy link
Contributor Author

muuki88 commented Apr 14, 2021

Thanks a lot for the detailed code review ❤️ I implemented all the requested changes.

@muuki88 muuki88 requested a review from SerhiiNahornyi April 14, 2021 07:19
@@ -107,6 +103,21 @@ private String getUri() {
.toString();
}

private Site modifySite(Site site, Integer networkId) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more small change, pls, make methods modifySite and modifyPublisher to be static

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muuki88 agree?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do! I must have missed that comment.

@rpanchyk rpanchyk merged commit 589b9a1 into prebid:master Apr 22, 2021
nickluck9 pushed a commit that referenced this pull request Aug 9, 2021
* Merge site object for SmartadserverBidder

* Fix integration test

* Apply requested code review changes

* Make modifySite and modifyPublisher static
nickluck9 pushed a commit that referenced this pull request Aug 10, 2021
* Merge site object for SmartadserverBidder

* Fix integration test

* Apply requested code review changes

* Make modifySite and modifyPublisher static
nickluck9 pushed a commit that referenced this pull request Aug 10, 2021
* Merge site object for SmartadserverBidder

* Fix integration test

* Apply requested code review changes

* Make modifySite and modifyPublisher static
@muuki88 muuki88 deleted the smartadserver-merge-site-object branch March 9, 2024 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants