Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.
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 @@ -116,10 +116,12 @@ public Result writeDiscovery(
ImmutableSet.Builder<ApiKey> preferred = ImmutableSet.builder();
for (ApiKey apiKey : configsByKey.keySet()) {
ImmutableList<ApiConfig> 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<ApiKey, RestDescription> discoveryDocs = builder.build();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, JsonSchema> createStandardParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiKey, RestDescription> builder = ImmutableMap.builder();
for (Map.Entry<ApiKey, RestDescription> entry : result.discoveryDocs().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Foo> listFoos(@Named("n") Integer n) {
return null;
}
@ApiMethod(name = "toplevel", path = "foos", httpMethod = HttpMethod.POST)
public CollectionResponse<Foo> toplevel() {
return null;
}
}