Skip to content

Commit

Permalink
Fix compliance of API document, add list of available feature types i…
Browse files Browse the repository at this point in the history
…n it
  • Loading branch information
aaime committed Jun 26, 2018
1 parent 8e45a9a commit 397bed0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/community/wfs3/pom.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<dependency> <dependency>
<groupId>io.swagger.core.v3</groupId> <groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId> <artifactId>swagger-core</artifactId>
<version>2.0.1</version> <version>2.0.2</version>
<exclusions> <exclusions>
<!-- Just need the module to read/write swagger definitions, keep deps to a minimum --> <!-- Just need the module to read/write swagger definitions, keep deps to a minimum -->
<exclusion> <exclusion>
Expand Down Expand Up @@ -118,6 +118,7 @@
<artifactId>hamcrest-library</artifactId> <artifactId>hamcrest-library</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>

</dependencies> </dependencies>


</project> </project>
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.swagger.v3.oas.models.media.Content; import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType; import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.StringSchema; import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.servers.Server; import io.swagger.v3.oas.models.servers.Server;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
Expand All @@ -21,6 +22,7 @@
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.geoserver.ManifestLoader; import org.geoserver.ManifestLoader;
import org.geoserver.catalog.Catalog;
import org.geoserver.config.ContactInfo; import org.geoserver.config.ContactInfo;
import org.geoserver.ows.URLMangler; import org.geoserver.ows.URLMangler;
import org.geoserver.ows.util.ResponseUtils; import org.geoserver.ows.util.ResponseUtils;
Expand Down Expand Up @@ -92,6 +94,16 @@ public OpenAPI build(BaseRequest request, WFSInfo wfs) {
"/collections/{collectionId}/items/{featureId}", "/collections/{collectionId}/items/{featureId}",
FeatureCollectionResponse.class); FeatureCollectionResponse.class);


// provide a list of valid values for collectionId
Parameter collectionId = api.getComponents().getParameters().get("collectionId");
Catalog catalog = wfs.getGeoServer().getCatalog();
List<String> validCollectionIds =
catalog.getFeatureTypes()
.stream()
.map(ft -> NCNameResourceCodec.encode(ft))
.collect(Collectors.toList());
collectionId.getSchema().setEnum(validCollectionIds);

return api; return api;
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
*/ */
package org.geoserver.wfs3.response; package org.geoserver.wfs3.response;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.Annotated; import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlAnnotationIntrospector; import com.fasterxml.jackson.dataformat.xml.JacksonXmlAnnotationIntrospector;
import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Arrays; import java.util.Arrays;
Expand Down Expand Up @@ -77,10 +76,7 @@ public void write(Object value, OutputStream output, Operation operation)
throws IOException, ServiceException { throws IOException, ServiceException {
ObjectMapper mapper; ObjectMapper mapper;
if (isYamlFormat(operation)) { if (isYamlFormat(operation)) {
YAMLFactory factory = new YAMLFactory(); mapper = Yaml.mapper();
mapper = new ObjectMapper(factory);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
} else if (isXMLFormat(operation)) { } else if (isXMLFormat(operation)) {
mapper = new XmlMapper(); mapper = new XmlMapper();
// using a custom annotation introspector to set the desired namespace // using a custom annotation introspector to set the desired namespace
Expand All @@ -98,8 +94,7 @@ public String findNamespace(Annotated ann) {
}); });


} else { } else {
mapper = new ObjectMapper(); mapper = Json.mapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
} }


mapper.writeValue(output, value); mapper.writeValue(output, value);
Expand Down
96 changes: 92 additions & 4 deletions src/community/wfs3/src/test/java/org/geoserver/wfs3/ApiTest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@
*/ */
package org.geoserver.wfs3; package org.geoserver.wfs3;


import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;


import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.servers.Server;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import net.sf.json.JSON; import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
Expand All @@ -17,14 +32,22 @@ public class ApiTest extends WFS3TestSupport {


@Test @Test
public void testApiJson() throws Exception { public void testApiJson() throws Exception {
JSON json = getAsJSON("wfs3/api"); String json = getAsString("wfs3/api");
print(json); System.out.println(json);

ObjectMapper mapper = Json.mapper();
OpenAPI api = mapper.readValue(json, OpenAPI.class);
validateApi(api);
} }


@Test @Test
public void testApiYaml() throws Exception { public void testApiYaml() throws Exception {
String yaml = getAsString("wfs3/api?f=application/x-yaml"); String yaml = getAsString("wfs3/api?f=application/x-yaml");
System.out.println(yaml); System.out.println(yaml);

ObjectMapper mapper = Yaml.mapper();
OpenAPI api = mapper.readValue(yaml, OpenAPI.class);
validateApi(api);
} }


@Test @Test
Expand All @@ -38,6 +61,71 @@ public void testYamlAsAcceptsHeader() throws Exception {
assertEquals("application/x-yaml", response.getContentType()); assertEquals("application/x-yaml", response.getContentType());
String yaml = string(new ByteArrayInputStream(response.getContentAsString().getBytes())); String yaml = string(new ByteArrayInputStream(response.getContentAsString().getBytes()));


System.out.println(yaml); ObjectMapper mapper = Yaml.mapper();
OpenAPI api = mapper.readValue(yaml, OpenAPI.class);
validateApi(api);
}

private void validateApi(OpenAPI api) {
// only one server
List<Server> servers = api.getServers();
assertThat(servers, hasSize(1));
assertThat(servers.get(0).getUrl(), equalTo("http://localhost:8080/geoserver/wfs3"));

// paths
Paths paths = api.getPaths();
assertThat(paths.size(), equalTo(6));

// ... landing page
PathItem landing = paths.get("/");
assertNotNull(landing);
assertThat(landing.getGet().getOperationId(), equalTo("getLandingPage"));

// ... conformance
PathItem conformance = paths.get("/conformance");
assertNotNull(conformance);
assertThat(conformance.getGet().getOperationId(), equalTo("getRequirementsClasses"));

// ... collections
PathItem collections = paths.get("/collections");
assertNotNull(collections);
assertThat(collections.getGet().getOperationId(), equalTo("describeCollections"));

// ... collection
PathItem collection = paths.get("/collections/{collectionId}");
assertNotNull(collection);
assertThat(collection.getGet().getOperationId(), equalTo("describeCollection"));

// ... features
PathItem items = paths.get("/collections/{collectionId}/items");
assertNotNull(items);
Operation itemsGet = items.getGet();
assertThat(itemsGet.getOperationId(), equalTo("getFeatures"));
List<Parameter> parameters = itemsGet.getParameters();
List<String> itemGetParamNames =
parameters.stream().map(p -> p.get$ref()).collect(Collectors.toList());
assertThat(
itemGetParamNames,
contains(
"#/components/parameters/collectionId",
"#/components/parameters/limit",
"#/components/parameters/bbox",
"#/components/parameters/time"));

// ... feature
PathItem item = paths.get("/collections/{collectionId}/items/{featureId}");
assertNotNull(item);
assertThat(item.getGet().getOperationId(), equalTo("getFeature"));

// check collectionId parameter
Parameter collectionId = api.getComponents().getParameters().get("collectionId");
List<String> collectionIdValues = collectionId.getSchema().getEnum();
List<String> expectedCollectionIds =
getCatalog()
.getFeatureTypes()
.stream()
.map(ft -> NCNameResourceCodec.encode(ft))
.collect(Collectors.toList());
assertThat(collectionIdValues, equalTo(expectedCollectionIds));
} }
} }

0 comments on commit 397bed0

Please sign in to comment.