From 5c6c20f09d79332cb22235220380dde3e1346448 Mon Sep 17 00:00:00 2001 From: Daniel Tang Date: Thu, 19 Apr 2018 14:31:00 -0700 Subject: [PATCH] don't generate discovery when discoverable is false --- .../spi/discovery/DiscoveryGenerator.java | 19 ++++-- .../spi/discovery/LocalDiscoveryProvider.java | 6 +- .../spi/discovery/DiscoveryGeneratorTest.java | 19 ++++++ .../spi/testing/NonDiscoverableEndpoint.java | 65 +++++++++++++++++++ 4 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 test-utils/src/main/java/com/google/api/server/spi/testing/NonDiscoverableEndpoint.java diff --git a/endpoints-framework/src/main/java/com/google/api/server/spi/discovery/DiscoveryGenerator.java b/endpoints-framework/src/main/java/com/google/api/server/spi/discovery/DiscoveryGenerator.java index 87a43cef..48bd734f 100644 --- a/endpoints-framework/src/main/java/com/google/api/server/spi/discovery/DiscoveryGenerator.java +++ b/endpoints-framework/src/main/java/com/google/api/server/spi/discovery/DiscoveryGenerator.java @@ -116,10 +116,12 @@ public Result writeDiscovery( ImmutableSet.Builder preferred = ImmutableSet.builder(); for (ApiKey apiKey : configsByKey.keySet()) { ImmutableList apiConfigs = configsByKey.get(apiKey); - builder.put(apiKey, writeApi(apiKey, apiConfigs, context, schemaRepository)); - // last config takes precedence (same as writeApi) - if (Iterables.getLast(apiConfigs).getIsDefaultVersion()) { - preferred.add(apiKey); + if (context.generateAll || apiConfigs.get(0).getIsDiscoverable()) { + builder.put(apiKey, writeApi(apiKey, apiConfigs, context, schemaRepository)); + // last config takes precedence (same as writeApi) + if (Iterables.getLast(apiConfigs).getIsDefaultVersion()) { + preferred.add(apiKey); + } } } ImmutableMap discoveryDocs = builder.build(); @@ -450,6 +452,7 @@ public static class DiscoveryContext { private String scheme = "https"; private String hostname = "myapi.appspot.com"; private String basePath = "/_ah/api"; + private boolean generateAll = true; public String getApiRoot() { return scheme + "://" + hostname + basePath; @@ -487,6 +490,14 @@ public DiscoveryContext setBasePath(String basePath) { this.basePath = Strings.stripTrailingSlash(basePath); return this; } + + /** + * Returns whether or not APIs with discoverable set to false should be generated. + */ + public DiscoveryContext setGenerateAll(boolean generateAll) { + this.generateAll = generateAll; + return this; + } } private static Map createStandardParameters() { diff --git a/endpoints-framework/src/main/java/com/google/api/server/spi/discovery/LocalDiscoveryProvider.java b/endpoints-framework/src/main/java/com/google/api/server/spi/discovery/LocalDiscoveryProvider.java index 339b062d..5f949932 100644 --- a/endpoints-framework/src/main/java/com/google/api/server/spi/discovery/LocalDiscoveryProvider.java +++ b/endpoints-framework/src/main/java/com/google/api/server/spi/discovery/LocalDiscoveryProvider.java @@ -58,7 +58,11 @@ public DirectoryList getDirectory(String root) { private synchronized void ensureDiscoveryResult() { if (discoveryDocs == null) { DiscoveryGenerator.Result result = generator.writeDiscovery( - getAllApiConfigs(), new DiscoveryContext().setApiRoot(PLACEHOLDER_ROOT), repository); + getAllApiConfigs(), + new DiscoveryContext() + .setApiRoot(PLACEHOLDER_ROOT) + .setGenerateAll(false), + repository); directoryList = result.directory(); ImmutableMap.Builder builder = ImmutableMap.builder(); for (Map.Entry entry : result.discoveryDocs().entrySet()) { diff --git a/endpoints-framework/src/test/java/com/google/api/server/spi/discovery/DiscoveryGeneratorTest.java b/endpoints-framework/src/test/java/com/google/api/server/spi/discovery/DiscoveryGeneratorTest.java index 9e0ae795..ba0bafd4 100644 --- a/endpoints-framework/src/test/java/com/google/api/server/spi/discovery/DiscoveryGeneratorTest.java +++ b/endpoints-framework/src/test/java/com/google/api/server/spi/discovery/DiscoveryGeneratorTest.java @@ -36,6 +36,7 @@ import com.google.api.server.spi.testing.FooEndpoint; import com.google.api.server.spi.testing.MultipleParameterEndpoint; import com.google.api.server.spi.testing.NamespaceEndpoint; +import com.google.api.server.spi.testing.NonDiscoverableEndpoint; import com.google.api.server.spi.testing.PrimitiveEndpoint; import com.google.api.services.discovery.model.DirectoryList; import com.google.api.services.discovery.model.RestDescription; @@ -182,6 +183,24 @@ public void testWriteDiscovery_directory() throws Exception { assertThat(result.directory()).isEqualTo(readExpectedAsDirectory("directory.json")); } + @Test + public void testWriteDiscovery_nonDiscoverableEndpointButGenerateAll() throws Exception { + getDiscovery(context, NonDiscoverableEndpoint.class); + RestDescription doc = getDiscovery(new DiscoveryContext(), NonDiscoverableEndpoint.class); + RestDescription expected = readExpectedAsDiscovery("foo_endpoint_default_context.json"); + compareDiscovery(expected, doc); + } + + @Test + public void testWriteDiscovery_nonDiscoverableEndpoint() throws Exception { + DiscoveryGenerator.Result result = generator.writeDiscovery( + ImmutableList.of( + configLoader.loadConfiguration(ServiceContext.create(), NonDiscoverableEndpoint.class)), + new DiscoveryContext().setGenerateAll(false)); + assertThat(result.discoveryDocs()).isEmpty(); + assertThat(result.directory().getItems()).isEmpty(); + } + @Test public void testDirectoryIsCloneable() throws Exception { ApiConfig config = configLoader.loadConfiguration(ServiceContext.create(), FooEndpoint.class); diff --git a/test-utils/src/main/java/com/google/api/server/spi/testing/NonDiscoverableEndpoint.java b/test-utils/src/main/java/com/google/api/server/spi/testing/NonDiscoverableEndpoint.java new file mode 100644 index 00000000..13531a99 --- /dev/null +++ b/test-utils/src/main/java/com/google/api/server/spi/testing/NonDiscoverableEndpoint.java @@ -0,0 +1,65 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.api.server.spi.testing; + +import com.google.api.server.spi.config.AnnotationBoolean; +import com.google.api.server.spi.config.Api; +import com.google.api.server.spi.config.ApiMethod; +import com.google.api.server.spi.config.ApiMethod.HttpMethod; +import com.google.api.server.spi.config.Description; +import com.google.api.server.spi.config.Named; +import com.google.api.server.spi.response.CollectionResponse; + +@Api( + name = "foo", + version = "v1", + audiences = {"audience"}, + title = "The Foo API", + description = "Just Foo Things", + documentationLink = "https://example.com", + canonicalName = "CanonicalName", + discoverable = AnnotationBoolean.FALSE) +public class NonDiscoverableEndpoint { + @ApiMethod(name = "foo.create", description = "create desc", path = "foos/{id}", + httpMethod = HttpMethod.PUT) + public Foo createFoo(@Named("id") @Description("id desc") String id, Foo foo) { + return null; + } + @ApiMethod(name = "foo.get", description = "get desc", path = "foos/{id}", + httpMethod = HttpMethod.GET) + public Foo getFoo(@Named("id") @Description("id desc") String id) { + return null; + } + @ApiMethod(name = "foo.update", description = "update desc", path = "foos/{id}", + httpMethod = HttpMethod.POST) + public Foo updateFoo(@Named("id") @Description("id desc") String id, Foo foo) { + return null; + } + @ApiMethod(name = "foo.delete", description = "delete desc", path = "foos/{id}", + httpMethod = HttpMethod.DELETE) + public Foo deleteFoo(@Named("id") @Description("id desc") String id) { + return null; + } + @ApiMethod(name = "foo.list", description = "list desc", path = "foos", + httpMethod = HttpMethod.GET) + public CollectionResponse listFoos(@Named("n") Integer n) { + return null; + } + @ApiMethod(name = "toplevel", path = "foos", httpMethod = HttpMethod.POST) + public CollectionResponse toplevel() { + return null; + } +} \ No newline at end of file