This repository was archived by the owner on Jun 30, 2023. It is now read-only.
File tree 8 files changed +78
-8
lines changed
main/java/com/google/api/server/spi
java/com/google/api/server/spi/config/jsonwriter
resources/com/google/api/server/spi
test-utils/src/main/java/com/google/api/server/spi/testing
8 files changed +78
-8
lines changed Original file line number Diff line number Diff line change 21
21
import java .lang .annotation .Target ;
22
22
23
23
/**
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.
25
26
*/
26
- @ Target (ElementType .PARAMETER )
27
+ @ Target ({ ElementType .PARAMETER , ElementType . FIELD } )
27
28
@ Retention (RetentionPolicy .RUNTIME )
28
29
public @interface Description {
29
30
/**
Original file line number Diff line number Diff line change 2
2
3
3
import com .google .api .client .util .Maps ;
4
4
import com .google .api .server .spi .TypeLoader ;
5
+ import com .google .api .server .spi .config .Description ;
5
6
import com .google .api .server .spi .config .ResourcePropertySchema ;
6
7
import com .google .api .server .spi .config .ResourceSchema ;
7
8
import com .google .api .server .spi .config .annotationreader .ApiAnnotationIntrospector ;
@@ -153,9 +154,12 @@ private Schema getOrCreateTypeForConfig(
153
154
Schema .Builder builder = Schema .builder ()
154
155
.setName (Types .getSimpleName (type , config .getSerializationConfig ()))
155
156
.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
+ }
159
163
}
160
164
schema = builder .build ();
161
165
typesForConfig .put (type , schema );
Original file line number Diff line number Diff line change @@ -238,7 +238,9 @@ private JsonSchema convertToDiscoverySchema(Schema schema) {
238
238
239
239
private JsonSchema convertToDiscoverySchema (Field f ) {
240
240
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 ());
242
244
}
243
245
JsonSchema fieldSchema = new JsonSchema ()
244
246
.setType (f .type ().getDiscoveryType ())
Original file line number Diff line number Diff line change 31
31
import com .google .api .server .spi .config .model .ApiConfig ;
32
32
import com .google .api .server .spi .testing .DefaultValueSerializer ;
33
33
import com .google .api .server .spi .testing .TestEndpoint ;
34
+ import com .google .api .server .spi .testing .TestEnum ;
34
35
import com .google .common .reflect .TypeToken ;
35
36
36
37
import org .junit .Before ;
@@ -77,6 +78,7 @@ public void testDescribedProperty() {
77
78
ResourceSchema schema = getResourceSchema (DescribedPropertyBean .class );
78
79
assertEquals ("description of foo" , schema .getProperties ().get ("foo" ).getDescription ());
79
80
assertEquals ("description of bar" , schema .getProperties ().get ("bar" ).getDescription ());
81
+ assertEquals ("description of choice" , schema .getProperties ().get ("choice" ).getDescription ());
80
82
}
81
83
82
84
@ Test
@@ -197,6 +199,8 @@ private static class DescribedPropertyBean {
197
199
public String getBar () {
198
200
return null ;
199
201
}
202
+ @ ApiResourceProperty (description = "description of choice" )
203
+ public TestEnum choice ;
200
204
}
201
205
202
206
/**
Original file line number Diff line number Diff line change 14
14
"description" : " Just Foo Things" ,
15
15
"discoveryVersion" : " v1" ,
16
16
"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 "
19
19
},
20
20
"id" : " foo:v1" ,
21
21
"kind" : " discovery#restDescription" ,
228
228
"FooDescription" : {
229
229
"id" : " FooDescription" ,
230
230
"properties" : {
231
+ "choice" : {
232
+ "$ref" : " TestEnumDescription" ,
233
+ "description" : " description of choice"
234
+ },
231
235
"name" : {
232
236
"description" :" description of name" ,
233
237
"type" : " string"
239
243
}
240
244
},
241
245
"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"
242
258
}
243
259
},
244
260
"servicePath" : " foo/v1/" ,
Original file line number Diff line number Diff line change 234
234
}
235
235
}
236
236
},
237
+ "TestEnumDescription": {
238
+ "enum": [
239
+ "VALUE1",
240
+ "VALUE2"
241
+ ]
242
+ },
237
243
"FooDescription": {
238
244
"properties": {
245
+ "choice": {
246
+ "description": "description of choice",
247
+ "$ref": "#/definitions/TestEnumDescription"
248
+ },
239
249
"name": {
240
250
"description": "description of name",
241
251
"type": "string"
Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ public class FooDescription {
26
26
private String name ;
27
27
private int value ;
28
28
private String hidden ;
29
+ @ ApiResourceProperty (description = "description of choice" )
30
+ private TestEnumDescription choice ;
29
31
30
32
public String getName () {
31
33
return name ;
@@ -43,4 +45,8 @@ private String getHidden() {
43
45
private void setHidden (String hidden ) {
44
46
this .hidden = hidden ;
45
47
}
48
+
49
+ public TestEnumDescription getChoice () {
50
+ return choice ;
51
+ }
46
52
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments