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

Commit 45772ac

Browse files
Clément Denistangiel
Clément Denis
authored andcommitted
Add description on enum constants (#133)
* Fix enum constant name to be backward compatible with endpoints v1 * Add description on enum constant by using the @description annotation * Iterate on enum fields instead of constants to avoid reflection exception * Update Javadoc on @description annotation * Add description on $ref reference * Fix indent and update copyright
1 parent be24045 commit 45772ac

File tree

8 files changed

+78
-8
lines changed

8 files changed

+78
-8
lines changed

Diff for: endpoints-framework/src/main/java/com/google/api/server/spi/config/Description.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
import java.lang.annotation.Target;
2222

2323
/**
24-
* Annotation to specify the description of an API parameter.
24+
* Annotation to specify the description of an API parameter or enum constants.
25+
* The description will be ignored if the annotation is used on resource fields.
2526
*/
26-
@Target(ElementType.PARAMETER)
27+
@Target({ElementType.PARAMETER, ElementType.FIELD})
2728
@Retention(RetentionPolicy.RUNTIME)
2829
public @interface Description {
2930
/**

Diff for: endpoints-framework/src/main/java/com/google/api/server/spi/config/model/SchemaRepository.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.api.client.util.Maps;
44
import com.google.api.server.spi.TypeLoader;
5+
import com.google.api.server.spi.config.Description;
56
import com.google.api.server.spi.config.ResourcePropertySchema;
67
import com.google.api.server.spi.config.ResourceSchema;
78
import com.google.api.server.spi.config.annotationreader.ApiAnnotationIntrospector;
@@ -153,9 +154,12 @@ private Schema getOrCreateTypeForConfig(
153154
Schema.Builder builder = Schema.builder()
154155
.setName(Types.getSimpleName(type, config.getSerializationConfig()))
155156
.setType("string");
156-
for (Object enumConstant : type.getRawType().getEnumConstants()) {
157-
builder.addEnumValue(enumConstant.toString());
158-
builder.addEnumDescription("");
157+
for (java.lang.reflect.Field field : type.getRawType().getFields()) {
158+
if (field.isEnumConstant()) {
159+
builder.addEnumValue(field.getName());
160+
Description description = field.getAnnotation(Description.class);
161+
builder.addEnumDescription(description == null ? "" : description.value());
162+
}
159163
}
160164
schema = builder.build();
161165
typesForConfig.put(type, schema);

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ private JsonSchema convertToDiscoverySchema(Schema schema) {
238238

239239
private JsonSchema convertToDiscoverySchema(Field f) {
240240
if (f.schemaReference() != null) {
241-
return new JsonSchema().set$ref(f.schemaReference().get().name());
241+
return new JsonSchema()
242+
.setDescription(f.description())
243+
.set$ref(f.schemaReference().get().name());
242244
}
243245
JsonSchema fieldSchema = new JsonSchema()
244246
.setType(f.type().getDiscoveryType())

Diff for: endpoints-framework/src/test/java/com/google/api/server/spi/config/jsonwriter/ResourceSchemaProviderTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.api.server.spi.config.model.ApiConfig;
3232
import com.google.api.server.spi.testing.DefaultValueSerializer;
3333
import com.google.api.server.spi.testing.TestEndpoint;
34+
import com.google.api.server.spi.testing.TestEnum;
3435
import com.google.common.reflect.TypeToken;
3536

3637
import org.junit.Before;
@@ -77,6 +78,7 @@ public void testDescribedProperty() {
7778
ResourceSchema schema = getResourceSchema(DescribedPropertyBean.class);
7879
assertEquals("description of foo", schema.getProperties().get("foo").getDescription());
7980
assertEquals("description of bar", schema.getProperties().get("bar").getDescription());
81+
assertEquals("description of choice", schema.getProperties().get("choice").getDescription());
8082
}
8183

8284
@Test
@@ -197,6 +199,8 @@ private static class DescribedPropertyBean {
197199
public String getBar() {
198200
return null;
199201
}
202+
@ApiResourceProperty(description = "description of choice")
203+
public TestEnum choice;
200204
}
201205

202206
/**

Diff for: endpoints-framework/src/test/resources/com/google/api/server/spi/discovery/foo_with_description_endpoint.json

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"description": "Just Foo Things",
1515
"discoveryVersion": "v1",
1616
"icons": {
17-
"x16": "http://www.google.com/images/icons/product/search-16.gif",
18-
"x32": "http://www.google.com/images/icons/product/search-32.gif"
17+
"x16": "https://www.gstatic.com/images/branding/product/1x/googleg_16dp.png",
18+
"x32": "https://www.gstatic.com/images/branding/product/1x/googleg_32dp.png"
1919
},
2020
"id": "foo:v1",
2121
"kind": "discovery#restDescription",
@@ -228,6 +228,10 @@
228228
"FooDescription": {
229229
"id": "FooDescription",
230230
"properties": {
231+
"choice": {
232+
"$ref": "TestEnumDescription",
233+
"description": "description of choice"
234+
},
231235
"name": {
232236
"description":"description of name",
233237
"type": "string"
@@ -239,6 +243,18 @@
239243
}
240244
},
241245
"type": "object"
246+
},
247+
"TestEnumDescription": {
248+
"enum": [
249+
"VALUE1",
250+
"VALUE2"
251+
],
252+
"enumDescriptions": [
253+
"description of value1",
254+
"description of value2"
255+
],
256+
"id": "TestEnumDescription",
257+
"type":"string"
242258
}
243259
},
244260
"servicePath": "foo/v1/",

Diff for: endpoints-framework/src/test/resources/com/google/api/server/spi/swagger/foo_with_description_endpoint.swagger

+10
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,18 @@
234234
}
235235
}
236236
},
237+
"TestEnumDescription": {
238+
"enum": [
239+
"VALUE1",
240+
"VALUE2"
241+
]
242+
},
237243
"FooDescription": {
238244
"properties": {
245+
"choice": {
246+
"description": "description of choice",
247+
"$ref": "#/definitions/TestEnumDescription"
248+
},
239249
"name": {
240250
"description": "description of name",
241251
"type": "string"

Diff for: test-utils/src/main/java/com/google/api/server/spi/testing/FooDescription.java

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class FooDescription {
2626
private String name;
2727
private int value;
2828
private String hidden;
29+
@ApiResourceProperty(description = "description of choice")
30+
private TestEnumDescription choice;
2931

3032
public String getName() {
3133
return name;
@@ -43,4 +45,8 @@ private String getHidden() {
4345
private void setHidden(String hidden) {
4446
this.hidden = hidden;
4547
}
48+
49+
public TestEnumDescription getChoice() {
50+
return choice;
51+
}
4652
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2018 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.Description;
19+
20+
public enum TestEnumDescription {
21+
@Description("description of value1")
22+
VALUE1,
23+
@Description("description of value2")
24+
VALUE2;
25+
26+
public String notAConstant;
27+
}

0 commit comments

Comments
 (0)