Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Nodeify Discovery objects #1322

Merged
merged 12 commits into from
Jun 5, 2017
62 changes: 46 additions & 16 deletions src/main/java/com/google/api/codegen/discovery/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
*/
package com.google.api.codegen.discovery;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

/**
* A representation of a Discovery Document.
Expand All @@ -29,7 +31,7 @@
* example, this class combines all methods in the Discovery Document into one list for convenience.
*/
@AutoValue
public abstract class Document {
public abstract class Document implements Node {

// TODO(saicheems): Assert that all references link to a valid schema?

Expand Down Expand Up @@ -66,23 +68,35 @@ public static Document from(DiscoveryNode root) {
String servicePath = root.getString("servicePath");
String title = root.getString("title");
String version = root.getString("version");
String id = root.getString("id");

This comment was marked as spam.

This comment was marked as spam.

boolean versionModule = root.getBoolean("version_module");

return new AutoValue_Document(
"", // authInstructionsUrl (only intended to be overridden).
authType,
canonicalName,
description,
"", // discoveryDocUrl (only intended to be overridden).
methods,
name,
revision,
rootUrl,
schemas,
servicePath,
title,
version,
versionModule);
Document thisDocument =
new AutoValue_Document(
id,
"", // authInstructionsUrl (only intended to be overridden).
authType,
canonicalName,
description,
"", // discoveryDocUrl (only intended to be overridden).
methods,
name,
revision,
rootUrl,
schemas,
servicePath,
title,
version,
versionModule);

for (Schema schema : schemas.values()) {
schema.setParent(thisDocument);
}
for (Method method : methods) {
method.setParent(thisDocument);
}

return thisDocument;
}

/**
Expand Down Expand Up @@ -132,6 +146,22 @@ private static Map<String, Schema> parseSchemas(DiscoveryNode root) {
return schemas;
}

/** @return the auth instructions URL. */
@JsonIgnore @Nullable private Node parent;

This comment was marked as spam.

This comment was marked as spam.


public Node parent() {
return parent;
}

// Package private for use by other Node objects.
void setParent(Node parent) {

This comment was marked as spam.

This comment was marked as spam.

this.parent = parent;
}

/** @return whether or not to version the module. */
@JsonProperty("id")
public abstract String id();

This comment was marked as spam.

This comment was marked as spam.


/** @return the auth instructions URL. */
@JsonProperty("authInstructionsUrl")
public abstract String authInstructionsUrl();
Expand Down
61 changes: 48 additions & 13 deletions src/main/java/com/google/api/codegen/discovery/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* <p>Note that this class is not necessarily a 1-1 mapping of the official specification.
*/
@AutoValue
public abstract class Method implements Comparable<Method> {
public abstract class Method implements Comparable<Method>, Node {

/**
* Returns a method constructed from root.
Expand All @@ -38,6 +38,17 @@ public abstract class Method implements Comparable<Method> {
* @return a method.
*/
public static Method from(DiscoveryNode root, String path) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

return Method.from(root, path, null);
}

/**
* Returns a method constructed from root.
*
* @param root the root node to parse.
* @param path the full path to this node (ex: "resources.foo.methods.bar").
* @return a method.
*/
public static Method from(DiscoveryNode root, String path, Node parent) {
String description = root.getString("description");
String httpMethod = root.getString("httpMethod");
String id = root.getString("id");
Expand Down Expand Up @@ -73,25 +84,49 @@ public static Method from(DiscoveryNode root, String path) {
boolean supportsMediaDownload = root.getBoolean("supportsMediaDownload");
boolean supportsMediaUpload = root.getBoolean("supportsMediaUpload");

return new AutoValue_Method(
description,
httpMethod,
id,
parameterOrder,
parameters,
path,
request,
response,
scopes,
supportsMediaDownload,
supportsMediaUpload);
Method thisMethod =
new AutoValue_Method(
description,
httpMethod,
id,
parameterOrder,
parameters,
path,
request,
response,
scopes,
supportsMediaDownload,
supportsMediaUpload);

thisMethod.parent = parent;
if (request != null) {
request.setParent(thisMethod);
}
if (response != null) {
response.setParent(thisMethod);
}
for (Schema schema : parameters.values()) {
schema.setParent(thisMethod);
}
return thisMethod;
}

@Override
public int compareTo(Method other) {
return id().compareTo(other.id());
}

/** @return the parent Node. */
private Node parent;

void setParent(Node parent) {
this.parent = parent;
}

public Node parent() {
return parent;
}

/** @return the description. */
public abstract String description();

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/google/api/codegen/discovery/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright 2017 Google Inc
*
* 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.codegen.discovery;

import javax.annotation.Nullable;

/** Represents a node in a tree of nodes. */
public interface Node {
/** @return the immediate parent of this node. */
@Nullable
Node parent();

/** @return the ID of this node. */
String id();

This comment was marked as spam.

This comment was marked as spam.

}
71 changes: 54 additions & 17 deletions src/main/java/com/google/api/codegen/discovery/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package com.google.api.codegen.discovery;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.auto.value.AutoValue;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -25,7 +26,7 @@
* <p>Note that this class is not necessarily a 1-1 mapping of the official specification.
*/
@AutoValue
public abstract class Schema {
public abstract class Schema implements Node {

/**
* Returns true if this schema contains a property with the given name.
Expand All @@ -45,6 +46,17 @@ public boolean hasProperty(String name) {
* @return a schema.
*/
public static Schema from(DiscoveryNode root, String path) {
return Schema.from(root, path, null);
}

/**
* Returns a schema constructed from root, or an empty schema if root has no children.
*
* @param root the root node to parse.
* @param path the full path to this node (ex: "methods.foo.parameters.bar").
* @return a schema.
*/
public static Schema from(DiscoveryNode root, String path, Node parent) {
if (root.isEmpty()) {
return empty();
}
Expand Down Expand Up @@ -77,22 +89,35 @@ public static Schema from(DiscoveryNode root, String path) {
boolean required = root.getBoolean("required");
Type type = Type.getEnum(root.getString("type"));

return new AutoValue_Schema(
additionalProperties,
defaultValue,
description,
format,
id,
isEnum,
items,
location,
path,
pattern,
properties,
reference,
repeated,
required,
type);
Schema thisSchema =
new AutoValue_Schema(
additionalProperties,
defaultValue,
description,
format,
id,
isEnum,
items,
location,
path,

This comment was marked as spam.

This comment was marked as spam.

pattern,
properties,
reference,
repeated,
required,
type);
thisSchema.parent = parent;
if (items != null) {
items.setParent(thisSchema);
}
for (Schema schema : properties.values()) {
schema.setParent(thisSchema);
}
if (additionalProperties != null) {
additionalProperties.setParent(thisSchema);
}

return thisSchema;
}

public static Schema empty() {
Expand All @@ -114,6 +139,18 @@ public static Schema empty() {
Type.EMPTY);
}

@JsonIgnore @Nullable private Node parent;

/** @return the {@link Node} that contains this Schema. */
@Nullable
public Node parent() {
return parent;
}

void setParent(Node parent) {
this.parent = parent;
}

/** @return the schema of the additionalProperties, or null if none. */
@Nullable
public abstract Schema additionalProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ public void testDocumentFromJson() throws IOException {
Truth.assertThat(methods.get(0).id()).isEqualTo("myapi.bar.baz.get");
Truth.assertThat(methods.get(0).parameterOrder()).isEqualTo(Collections.singletonList("p1"));
Truth.assertThat(methods.get(0).path()).isEqualTo("resources.bar.methods.baz");
Truth.assertThat(methods.get(0).parent() instanceof Document);
Truth.assertThat(methods.get(0).parent()).isEqualTo(document);

Map<String, Schema> parameters = methods.get(0).parameters();
Truth.assertThat(parameters.get("p1").type()).isEqualTo(Schema.Type.BOOLEAN);
Truth.assertThat(parameters.get("p1").required()).isTrue();
Truth.assertThat(parameters.get("p1").location()).isEqualTo("query");
Truth.assertThat(parameters.get("p1").path())
.isEqualTo("resources.bar.methods.baz.parameters.p1");
Truth.assertThat(parameters.get("p1").parent() instanceof Method);
Truth.assertThat(parameters.get("p1").parent()).isEqualTo(methods.get(0));

Truth.assertThat(methods.get(1).description()).isEqualTo("Insert a foo.");
Truth.assertThat(methods.get(1).id()).isEqualTo("myapi.foo.insert");
Expand All @@ -79,6 +83,8 @@ public void testDocumentFromJson() throws IOException {
Truth.assertThat(schemas.get("GetBazRequest").path()).isEqualTo("schemas.GetBazRequest");
Truth.assertThat(schemas.get("GetBazRequest").properties().get("foo").path())
.isEqualTo("schemas.GetBazRequest.properties.foo");
Truth.assertThat(schemas.get("GetBazRequest").parent() instanceof Document);
Truth.assertThat(schemas.get("GetBazRequest").parent()).isEqualTo(document);

Truth.assertThat(schemas.get("Baz").type()).isEqualTo(Schema.Type.STRING);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "discovery#document",
"auth": {
"oauth2": {
"scopes": {
Expand Down