Skip to content

Commit d2c4455

Browse files
author
Clément Denis
committed
Support the required flag on resource properties (Discovery and Swagger)
1 parent 13d6a2e commit d2c4455

File tree

20 files changed

+473
-18
lines changed

20 files changed

+473
-18
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ To install test versions to Maven for easier dependency management, simply run:
2828

2929
These are the most notable additions to
3030
[the original project by Google](https://github.com/cloudendpoints/endpoints-java), currently
31-
inactive:
32-
- Allow adding [arbitrary data](https://github.com/AODocs/endpoints-java/pull/20) to generic errors
33-
- [Improve errors](https://github.com/AODocs/endpoints-java/pull/30) on malformed JSON
34-
- Generated Swagger spec is [compatible](https://github.com/AODocs/endpoints-java/pull/34) with
35-
[Cloud Endpoints Portal ](https://cloud.google.com/endpoints/docs/frameworks/dev-portal-overview)
36-
([and](https://github.com/AODocs/endpoints-java/pull/38)
37-
[other](https://github.com/AODocs/endpoints-java/pull/36)
38-
[improvements](https://github.com/AODocs/endpoints-java/pull/37))
31+
inactive:
32+
- Runtime
33+
- Allow [adding arbitrary data](https://github.com/AODocs/endpoints-java/pull/20) to generic errors
34+
- [Improve returned errors](https://github.com/AODocs/endpoints-java/pull/30) on malformed JSON
35+
- Discovery and Swagger
36+
- [Add description on resources and resource usage as request body](https://github.com/AODocs/endpoints-java/commit/bbb1eff2bb9e7d28fc2ec17599257d0ef610531d)
37+
- [Support declaring resource properties as required](https://github.com/AODocs/endpoints-java/pull/41)
38+
- Swagger
39+
- Generated spec is [fully compatible](https://github.com/AODocs/endpoints-java/pull/34) with
40+
[Cloud Endpoints Portal](https://cloud.google.com/endpoints/docs/frameworks/dev-portal-overview) (and is 100% valid Swagger spec)
41+
- Support [multi-API service](https://github.com/AODocs/endpoints-java/pull/40/commits/1f18d2f64f1538e63a7836a5cd52ff639fc624fd) in Endpoints Management
42+
- [New options](https://github.com/AODocs/endpoints-java/pull/37) to combine common parameters in same path, extract parameter refs at spec level, add error model description, customize spec title and description
43+
- [Add description support](https://github.com/AODocs/endpoints-java/pull/40/commits/bbb1eff2bb9e7d28fc2ec17599257d0ef610531d) for resource and resource usage
3944

4045
Check
4146
[closed PRs](https://github.com/AODocs/endpoints-java/pulls?q=is%3Apr+sort%3Aupdated-desc+is%3Aclosed)

endpoints-framework/src/main/java/com/google/api/server/spi/IoUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.PushbackInputStream;
3131
import java.io.RandomAccessFile;
3232
import java.net.URL;
33+
import java.nio.charset.StandardCharsets;
3334
import java.util.ArrayList;
3435
import java.util.List;
3536
import java.util.zip.GZIPInputStream;
@@ -55,7 +56,7 @@ private IoUtil() {}
5556
public static String readResourceFile(Class<?> c, String fileName) throws IOException {
5657
URL url = c.getResource(fileName);
5758
StringBuilder sb = new StringBuilder();
58-
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
59+
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
5960
for (String line = in.readLine(); line != null; line = in.readLine()) {
6061
sb.append(line);
6162
}

endpoints-framework/src/main/java/com/google/api/server/spi/config/ApiResourceProperty.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@
3434
String name() default "";
3535

3636
/**
37-
* The description that the property represented by the annotated getter, setter, or field should appear
38-
* as in the API.
37+
* The description that the property represented by the annotated getter, setter, or field should
38+
* appear as in the API.
3939
*/
4040
String description() default "";
4141

42+
/**
43+
* Whether or not the property represented by the annotated getter, setter or field is "required":
44+
* - For requests, indicates the property is required for the resource to be accepted
45+
* - For responses, indicates the property will be returned by the server (before applying
46+
* partial response filtering)
47+
* In both cases, this is only a "hint": this is not enforced in any way.
48+
*/
49+
AnnotationBoolean required() default AnnotationBoolean.UNSPECIFIED;
50+
4251
/**
4352
* Whether or not the property represented by the annotated getter, setter or field should be
4453
* ignored for the API.

endpoints-framework/src/main/java/com/google/api/server/spi/config/ResourcePropertySchema.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
public class ResourcePropertySchema {
3131
private final TypeToken<?> type;
3232
private String description;
33+
private Boolean required;
3334

3435
private ResourcePropertySchema(TypeToken<?> type) {
3536
this.type = type;
@@ -56,7 +57,16 @@ public String getDescription() {
5657
public void setDescription(String description) {
5758
this.description = description;
5859
}
59-
60+
61+
public Boolean getRequired() {
62+
return required;
63+
}
64+
65+
public ResourcePropertySchema setRequired(Boolean required) {
66+
this.required = required;
67+
return this;
68+
}
69+
6070
/**
6171
* Returns a default resource property schema for a given type.
6272
*

endpoints-framework/src/main/java/com/google/api/server/spi/config/annotationreader/ApiAnnotationIntrospector.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.List;
5050
import java.util.Map;
5151

52+
import javax.annotation.Nonnull;
5253
import javax.annotation.Nullable;
5354

5455
/**
@@ -68,6 +69,23 @@ public boolean hasIgnoreMarker(AnnotatedMember member) {
6869
return apiProperty != null && apiProperty.ignored() == AnnotationBoolean.TRUE;
6970
}
7071

72+
@Override
73+
public Boolean hasRequiredMarker(AnnotatedMember member) {
74+
ApiResourceProperty apiProperty = member.getAnnotation(ApiResourceProperty.class);
75+
Nonnull nonnull = member.getAnnotation(Nonnull.class);
76+
Nullable nullable = member.getAnnotation(Nullable.class);
77+
if (apiProperty != null && apiProperty.required() != AnnotationBoolean.UNSPECIFIED) {
78+
return Boolean.parseBoolean(apiProperty.required().name());
79+
}
80+
if (nonnull != null) {
81+
return true;
82+
}
83+
if (nullable != null) {
84+
return false;
85+
}
86+
return null;
87+
}
88+
7189
@Override
7290
public PropertyName findNameForSerialization(Annotated a) {
7391
ApiResourceProperty apiName = a.getAnnotation(ApiResourceProperty.class);

endpoints-framework/src/main/java/com/google/api/server/spi/config/jsonwriter/JacksonResourceSchemaProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public ResourceSchema getResourceSchema(TypeToken<?> type, ApiConfig config) {
7272
if (propertyType != null) {
7373
ResourcePropertySchema propertySchema = ResourcePropertySchema.of(propertyType);
7474
propertySchema.setDescription(definition.getMetadata().getDescription());
75+
propertySchema.setRequired(definition.getMetadata().getRequired());
7576
schemaBuilder.addProperty(name, propertySchema);
7677
} else {
7778
logger.atWarning().log("No type found for property '%s' on class '%s'.", name, type);

endpoints-framework/src/main/java/com/google/api/server/spi/config/model/Schema.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ public static abstract class Field {
8282
/** The type classification of the field. */
8383
public abstract FieldType type();
8484

85+
/** The description of the field. */
8586
@Nullable public abstract String description();
8687

88+
/** The required status of the field. */
89+
@Nullable public abstract Boolean required();
90+
8791
/**
8892
* If {@link #type()} is {@link FieldType#OBJECT}, a reference to the schema type that the field
8993
* refers to.
@@ -106,6 +110,7 @@ public abstract static class Builder {
106110
public abstract Builder setName(String name);
107111
public abstract Builder setType(FieldType type);
108112
public abstract Builder setDescription(String description);
113+
public abstract Builder setRequired(Boolean required);
109114
public abstract Builder setSchemaReference(SchemaReference ref);
110115
public abstract Builder setArrayItemSchema(Field schema);
111116
public abstract Field build();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,11 @@ private Schema createBeanSchema(
272272
ResourcePropertySchema propertySchema = entry.getValue();
273273
TypeToken<?> propertyType = propertySchema.getType();
274274
if (propertyType != null) {
275+
String description = propertySchema.getDescription();
275276
Field.Builder fieldBuilder = Field.builder()
276277
.setName(propertyName)
277-
.setDescription(propertySchema.getDescription());
278+
.setDescription(Strings.isNullOrEmpty(description) ? null : description)
279+
.setRequired(propertySchema.getRequired());
278280
fillInFieldInformation(fieldBuilder, propertyType, typesForConfig, config);
279281
builder.addField(propertyName, fieldBuilder.build());
280282
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ private JsonSchema convertToDiscoverySchema(Field f) {
279279
.setType(f.type().getDiscoveryType())
280280
.setDescription(f.description())
281281
.setFormat(f.type().getDiscoveryFormat());
282+
if (f.required() != null) {
283+
fieldSchema.setRequired(f.required());
284+
}
282285
if (f.type() == FieldType.ARRAY) {
283286
fieldSchema.setItems(convertToDiscoverySchema(f.arrayItemSchema()));
284287
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Models:
2+
- minimum,maximum,pattern,readonly,annotations

0 commit comments

Comments
 (0)