Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

package com.spotify.github.v3.search.requests;

import java.util.Optional;

import javax.annotation.Nullable;

import org.immutables.value.Value;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.spotify.github.GithubStyle;
import com.spotify.github.Parameters;
import java.util.Optional;
import javax.annotation.Nullable;
import org.immutables.value.Value;

/**
* Search parameters resource defines required and optional parameters. To be serialized as
Expand All @@ -47,4 +50,15 @@ public interface SearchParameters extends Parameters {

/** The sort order if sort parameter is provided. One of asc or desc. Default: desc */
Optional<String> order();

/**
* The number of results per page (max 100). Default: 30
*/
@SuppressWarnings("checkstyle:methodname")
Optional<Integer> per_page();
Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense to use the java method naming convention of camelCase names here and instead use the @JsonProperty("per_page") annotation to make it compatible with github's format. WDYT?

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 think it makes sense to use the java method naming convention of camelCase names here and instead use the @JsonProperty("per_page") annotation to make it compatible with github's format. WDYT?

I'd like to use camelCase as well, but it turned out that serialization will be broken in this case:

Here is the method from Parameters.java:

/**
   * Goes through all public methods defined in an interface that extends this interface and calls
   * them in the context of the class that called this method, then joins the method name with the
   * result it produced using an ampersand (&amp;) as a delimiter.
   *
   * <p>It works on interfaces with deep inheritance and filters out any methods defined on this
   * interface (with the assumption that they come from the same class loader).
   *
   * @return String of "key=value" joined on &amp;
   */
  default String serialize() {
    return Arrays.stream(this.getClass().getInterfaces())
       ...
        .filter(method -> !method.getDeclaringClass().equals(Parameters.class))
        .collect(
            toMap(
                Method::getName,
                method -> {
                  ...
        .sorted((entry1, entry2) -> entry1.getKey().compareTo(entry2.getKey()))
        .map(entry -> entry.getKey() + "=" + entry.getValue().get())
        .collect(joining("&"));
  }

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for clarifying. You're right, since we're not dealing with JSON here at all my suggestion won't work. This is fine as-is then 👍


/**
* Page number of the results to fetch. Default: 1
*/
Optional<Integer> page();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -34,8 +34,22 @@ public void testFullSerialize() {
.q("bogus-query")
.sort("bogus-sort")
.order("bogus-order")
.per_page(50)
.page(2)
.build();

assertThat(params.serialize(), is("order=bogus-order&page=2&per_page=50&q=bogus-query&sort=bogus-sort"));
}

@Test
public void testSerializeWithoutPageAndPerPageParameters() {
final SearchParameters params =
ImmutableSearchParameters.builder()
.q("bogus-query")
.sort("bogus-sort")
.order("bogus-order")
.build();

assertThat(params.serialize(), is("order=bogus-order&q=bogus-query&sort=bogus-sort"));
}
}