Skip to content

Commit

Permalink
Fixed Swagger generation bug where an entity has nothing to sort by (#…
Browse files Browse the repository at this point in the history
…975)

* Fixed Swagger generation bug where an entity has nothing to sort by

* Changed logic so ID is now a sortable parameter

* Removed unused imports:

* Removed unused imports:

* Reverting owasp check back to level 6

* Fixing checkstyles
  • Loading branch information
aklish authored Sep 19, 2019
1 parent b343831 commit e2de2a8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ public Path getCollectionPath() {

path.get(new JsonApiOperation()
.description(getDescription)
.tag(getTag())
.parameter(getSortParameter())
.parameter(getSparseFieldsParameter())
.parameter(getIncludeParameter())
.parameter(getSortParameter())
.tag(getTag())
.response(200, okPluralResponse));

for (Parameter param : getFilterParameters()) {
Expand Down Expand Up @@ -475,6 +475,9 @@ private Parameter getSortParameter() {
.flatMap(Collection::stream)
.collect(Collectors.toList());

filterAttributes.add("id");
filterAttributes.add("-id");

return new QueryParameter()
.name("sort")
.type("array")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package com.yahoo.elide.contrib.swagger;

import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.contrib.swagger.model.Resource;
import com.yahoo.elide.contrib.swagger.models.Author;
import com.yahoo.elide.contrib.swagger.models.Book;
Expand Down Expand Up @@ -44,6 +45,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import javax.persistence.Entity;
import javax.persistence.Id;

public class SwaggerBuilderTest {
EntityDictionary dictionary;
Expand Down Expand Up @@ -396,7 +399,7 @@ public void testSortParam() throws Exception {

Assert.assertEquals(sortParam.getIn(), "query");

List<String> sortValues = Arrays.asList("title", "-title");
List<String> sortValues = Arrays.asList("id", "-id", "title", "-title");
Assert.assertTrue(((StringProperty) sortParam.getItems()).getEnum().containsAll(sortValues));
Assert.assertEquals(sortParam.getCollectionFormat(), "csv");
}
Expand All @@ -420,8 +423,8 @@ public void testIncludeParam() throws Exception {

Assert.assertEquals(includeParam.getIn(), "query");

List<String> sortValues = Arrays.asList("authors", "publisher");
Assert.assertTrue(((StringProperty) includeParam.getItems()).getEnum().containsAll(sortValues));
List<String> includeValues = Arrays.asList("authors", "publisher");
Assert.assertTrue(((StringProperty) includeParam.getItems()).getEnum().containsAll(includeValues));
Assert.assertEquals(includeParam.getCollectionFormat(), "csv");
}

Expand All @@ -444,8 +447,8 @@ public void testSparseFieldsParam() throws Exception {

Assert.assertEquals(fieldParam.getIn(), "query");

List<String> sortValues = Arrays.asList("title", "authors", "publisher");
Assert.assertTrue(((StringProperty) fieldParam.getItems()).getEnum().containsAll(sortValues));
List<String> filterValues = Arrays.asList("title", "authors", "publisher");
Assert.assertTrue(((StringProperty) fieldParam.getItems()).getEnum().containsAll(filterValues));
Assert.assertEquals(fieldParam.getCollectionFormat(), "csv");
}

Expand Down Expand Up @@ -558,6 +561,36 @@ public void testGlobalErrorResponses() throws Exception {
}
}

@Test
public void testSortParameter() {
@Entity
@Include(rootLevel = true)
class NothingToSort {
@Id
long name;
}
EntityDictionary entityDictionary = new EntityDictionary(Maps.newHashMap());

entityDictionary.bindEntity(NothingToSort.class);
Info info = new Info().title("Test Service").version("1.0");

SwaggerBuilder builder = new SwaggerBuilder(entityDictionary, info);
Swagger testSwagger = builder.build();

List<Parameter> params = testSwagger.getPaths().get("/nothingToSort").getGet().getParameters();

QueryParameter sortParam = (QueryParameter) params.stream()
.filter((param) -> param.getName().equals("sort"))
.findFirst()
.get();

Assert.assertEquals(sortParam.getIn(), "query");

List<String> sortValues = Arrays.asList("id", "-id");
Assert.assertEquals(((StringProperty) sortParam.getItems()).getEnum(), sortValues);
}


/**
* Verifies that the given property is of type 'Data' containing a reference to the given model.
* @param property The property to check
Expand Down

0 comments on commit e2de2a8

Please sign in to comment.