Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit e879c7e

Browse files
committed
don't generate discovery when discoverable is false
1 parent 1b51f85 commit e879c7e

4 files changed

Lines changed: 104 additions & 5 deletions

File tree

endpoints-framework/src/main/java/com/google/api/server/spi/discovery/DiscoveryGenerator.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ public Result writeDiscovery(
117117
ImmutableSet.Builder<ApiKey> preferred = ImmutableSet.builder();
118118
for (ApiKey apiKey : configsByKey.keySet()) {
119119
ImmutableList<ApiConfig> apiConfigs = configsByKey.get(apiKey);
120-
builder.put(apiKey, writeApi(apiKey, apiConfigs, context, schemaRepository));
121-
// last config takes precedence (same as writeApi)
122-
if (Iterables.getLast(apiConfigs).getIsDefaultVersion()) {
123-
preferred.add(apiKey);
120+
if (context.generateAll || apiConfigs.get(0).getIsDiscoverable()) {
121+
builder.put(apiKey, writeApi(apiKey, apiConfigs, context, schemaRepository));
122+
// last config takes precedence (same as writeApi)
123+
if (Iterables.getLast(apiConfigs).getIsDefaultVersion()) {
124+
preferred.add(apiKey);
125+
}
124126
}
125127
}
126128
ImmutableMap<ApiKey, RestDescription> discoveryDocs = builder.build();
@@ -454,6 +456,7 @@ public static class DiscoveryContext {
454456
private String scheme = "https";
455457
private String hostname = "myapi.appspot.com";
456458
private String basePath = "/_ah/api";
459+
private boolean generateAll = true;
457460

458461
public String getApiRoot() {
459462
return scheme + "://" + hostname + basePath;
@@ -491,6 +494,14 @@ public DiscoveryContext setBasePath(String basePath) {
491494
this.basePath = Strings.stripTrailingSlash(basePath);
492495
return this;
493496
}
497+
498+
/**
499+
* Returns whether or not APIs with discoverable set to false should be generated.
500+
*/
501+
public DiscoveryContext setGenerateAll(boolean generateAll) {
502+
this.generateAll = generateAll;
503+
return this;
504+
}
494505
}
495506

496507
private static Map<String, JsonSchema> createStandardParameters() {

endpoints-framework/src/main/java/com/google/api/server/spi/discovery/LocalDiscoveryProvider.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public DirectoryList getDirectory(String root) {
5858
private synchronized void ensureDiscoveryResult() {
5959
if (discoveryDocs == null) {
6060
DiscoveryGenerator.Result result = generator.writeDiscovery(
61-
getAllApiConfigs(), new DiscoveryContext().setApiRoot(PLACEHOLDER_ROOT), repository);
61+
getAllApiConfigs(),
62+
new DiscoveryContext()
63+
.setApiRoot(PLACEHOLDER_ROOT)
64+
.setGenerateAll(false),
65+
repository);
6266
directoryList = result.directory();
6367
ImmutableMap.Builder<ApiKey, RestDescription> builder = ImmutableMap.builder();
6468
for (Map.Entry<ApiKey, RestDescription> entry : result.discoveryDocs().entrySet()) {

endpoints-framework/src/test/java/com/google/api/server/spi/discovery/DiscoveryGeneratorTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.api.server.spi.testing.FooEndpoint;
3737
import com.google.api.server.spi.testing.MultipleParameterEndpoint;
3838
import com.google.api.server.spi.testing.NamespaceEndpoint;
39+
import com.google.api.server.spi.testing.NonDiscoverableEndpoint;
3940
import com.google.api.server.spi.testing.PrimitiveEndpoint;
4041
import com.google.api.services.discovery.model.DirectoryList;
4142
import com.google.api.services.discovery.model.RestDescription;
@@ -182,6 +183,24 @@ public void testWriteDiscovery_directory() throws Exception {
182183
assertThat(result.directory()).isEqualTo(readExpectedAsDirectory("directory.json"));
183184
}
184185

186+
@Test
187+
public void testWriteDiscovery_nonDiscoverableEndpointButGenerateAll() throws Exception {
188+
getDiscovery(context, NonDiscoverableEndpoint.class);
189+
RestDescription doc = getDiscovery(new DiscoveryContext(), NonDiscoverableEndpoint.class);
190+
RestDescription expected = readExpectedAsDiscovery("foo_endpoint_default_context.json");
191+
compareDiscovery(expected, doc);
192+
}
193+
194+
@Test
195+
public void testWriteDiscovery_nonDiscoverableEndpoint() throws Exception {
196+
DiscoveryGenerator.Result result = generator.writeDiscovery(
197+
ImmutableList.of(
198+
configLoader.loadConfiguration(ServiceContext.create(), NonDiscoverableEndpoint.class)),
199+
new DiscoveryContext().setGenerateAll(false));
200+
assertThat(result.discoveryDocs()).isEmpty();
201+
assertThat(result.directory().getItems()).isEmpty();
202+
}
203+
185204
@Test
186205
public void testDirectoryIsCloneable() throws Exception {
187206
ApiConfig config = configLoader.loadConfiguration(ServiceContext.create(), FooEndpoint.class);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.api.server.spi.testing;
17+
18+
import com.google.api.server.spi.config.AnnotationBoolean;
19+
import com.google.api.server.spi.config.Api;
20+
import com.google.api.server.spi.config.ApiMethod;
21+
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
22+
import com.google.api.server.spi.config.Description;
23+
import com.google.api.server.spi.config.Named;
24+
import com.google.api.server.spi.response.CollectionResponse;
25+
26+
@Api(
27+
name = "foo",
28+
version = "v1",
29+
audiences = {"audience"},
30+
title = "The Foo API",
31+
description = "Just Foo Things",
32+
documentationLink = "https://example.com",
33+
canonicalName = "CanonicalName",
34+
discoverable = AnnotationBoolean.FALSE)
35+
public class NonDiscoverableEndpoint {
36+
@ApiMethod(name = "foo.create", description = "create desc", path = "foos/{id}",
37+
httpMethod = HttpMethod.PUT)
38+
public Foo createFoo(@Named("id") @Description("id desc") String id, Foo foo) {
39+
return null;
40+
}
41+
@ApiMethod(name = "foo.get", description = "get desc", path = "foos/{id}",
42+
httpMethod = HttpMethod.GET)
43+
public Foo getFoo(@Named("id") @Description("id desc") String id) {
44+
return null;
45+
}
46+
@ApiMethod(name = "foo.update", description = "update desc", path = "foos/{id}",
47+
httpMethod = HttpMethod.POST)
48+
public Foo updateFoo(@Named("id") @Description("id desc") String id, Foo foo) {
49+
return null;
50+
}
51+
@ApiMethod(name = "foo.delete", description = "delete desc", path = "foos/{id}",
52+
httpMethod = HttpMethod.DELETE)
53+
public Foo deleteFoo(@Named("id") @Description("id desc") String id) {
54+
return null;
55+
}
56+
@ApiMethod(name = "foo.list", description = "list desc", path = "foos",
57+
httpMethod = HttpMethod.GET)
58+
public CollectionResponse<Foo> listFoos(@Named("n") Integer n) {
59+
return null;
60+
}
61+
@ApiMethod(name = "toplevel", path = "foos", httpMethod = HttpMethod.POST)
62+
public CollectionResponse<Foo> toplevel() {
63+
return null;
64+
}
65+
}

0 commit comments

Comments
 (0)