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

Add description on enum constants #133

Merged
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 @@ -21,9 +21,10 @@
import java.lang.annotation.Target;

/**
* Annotation to specify the description of an API parameter.
* Annotation to specify the description of an API parameter or enum constants.
* The description will be ignored if the annotation is used on resource fields.
*/
@Target(ElementType.PARAMETER)
@Target({ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Description {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.api.client.util.Maps;
import com.google.api.server.spi.TypeLoader;
import com.google.api.server.spi.config.Description;
import com.google.api.server.spi.config.ResourcePropertySchema;
import com.google.api.server.spi.config.ResourceSchema;
import com.google.api.server.spi.config.annotationreader.ApiAnnotationIntrospector;
Expand Down Expand Up @@ -153,9 +154,12 @@ private Schema getOrCreateTypeForConfig(
Schema.Builder builder = Schema.builder()
.setName(Types.getSimpleName(type, config.getSerializationConfig()))
.setType("string");
for (Object enumConstant : type.getRawType().getEnumConstants()) {
builder.addEnumValue(enumConstant.toString());
builder.addEnumDescription("");
for (java.lang.reflect.Field field : type.getRawType().getFields()) {
if (field.isEnumConstant()) {
builder.addEnumValue(field.getName());
Description description = field.getAnnotation(Description.class);
builder.addEnumDescription(description == null ? "" : description.value());
}
}
schema = builder.build();
typesForConfig.put(type, schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ private JsonSchema convertToDiscoverySchema(Schema schema) {

private JsonSchema convertToDiscoverySchema(Field f) {
if (f.schemaReference() != null) {
return new JsonSchema().set$ref(f.schemaReference().get().name());
return new JsonSchema()
.setDescription(f.description())
.set$ref(f.schemaReference().get().name());
}
JsonSchema fieldSchema = new JsonSchema()
.setType(f.type().getDiscoveryType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.api.server.spi.config.model.ApiConfig;
import com.google.api.server.spi.testing.DefaultValueSerializer;
import com.google.api.server.spi.testing.TestEndpoint;
import com.google.api.server.spi.testing.TestEnum;
import com.google.common.reflect.TypeToken;

import org.junit.Before;
Expand Down Expand Up @@ -77,6 +78,7 @@ public void testDescribedProperty() {
ResourceSchema schema = getResourceSchema(DescribedPropertyBean.class);
assertEquals("description of foo", schema.getProperties().get("foo").getDescription());
assertEquals("description of bar", schema.getProperties().get("bar").getDescription());
assertEquals("description of choice", schema.getProperties().get("choice").getDescription());
}

@Test
Expand Down Expand Up @@ -197,6 +199,8 @@ private static class DescribedPropertyBean {
public String getBar() {
return null;
}
@ApiResourceProperty(description = "description of choice")
public TestEnum choice;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"description": "Just Foo Things",
"discoveryVersion": "v1",
"icons": {
"x16": "http://www.google.com/images/icons/product/search-16.gif",
"x32": "http://www.google.com/images/icons/product/search-32.gif"
"x16": "https://www.gstatic.com/images/branding/product/1x/googleg_16dp.png",
"x32": "https://www.gstatic.com/images/branding/product/1x/googleg_32dp.png"
},
"id": "foo:v1",
"kind": "discovery#restDescription",
Expand Down Expand Up @@ -228,6 +228,10 @@
"FooDescription": {
"id": "FooDescription",
"properties": {
"choice": {
"$ref": "TestEnumDescription",
"description": "description of choice"
},
"name": {
"description":"description of name",
"type": "string"
Expand All @@ -239,6 +243,18 @@
}
},
"type": "object"
},
"TestEnumDescription": {
"enum": [
"VALUE1",
"VALUE2"
],
"enumDescriptions": [
"description of value1",
"description of value2"
],
"id": "TestEnumDescription",
"type":"string"
}
},
"servicePath": "foo/v1/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,18 @@
}
}
},
"TestEnumDescription": {
"enum": [
"VALUE1",
"VALUE2"
]
},
"FooDescription": {
"properties": {
"choice": {
"description": "description of choice",
"$ref": "#/definitions/TestEnumDescription"
},
"name": {
"description": "description of name",
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class FooDescription {
private String name;
private int value;
private String hidden;
@ApiResourceProperty(description = "description of choice")
private TestEnumDescription choice;

public String getName() {
return name;
Expand All @@ -43,4 +45,8 @@ private String getHidden() {
private void setHidden(String hidden) {
this.hidden = hidden;
}

public TestEnumDescription getChoice() {
return choice;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2018 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.Description;

public enum TestEnumDescription {
@Description("description of value1")
VALUE1,
@Description("description of value2")
VALUE2;

public String notAConstant;
}