use of com.google.api.server.spi.config.ResourcePropertySchema in project endpoints-java by cloudendpoints.
the class JacksonResourceSchemaProvider method getResourceSchema.
@Override
public ResourceSchema getResourceSchema(TypeToken<?> type, ApiConfig config) {
ResourceSchema schema = super.getResourceSchema(type, config);
if (schema != null) {
return schema;
}
ObjectMapper objectMapper = ObjectMapperUtil.createStandardObjectMapper(config.getSerializationConfig());
JavaType javaType = objectMapper.getTypeFactory().constructType(type.getRawType());
BeanDescription beanDescription = objectMapper.getSerializationConfig().introspect(javaType);
ResourceSchema.Builder schemaBuilder = ResourceSchema.builderForType(type.getRawType());
Set<String> genericDataFieldNames = getGenericDataFieldNames(type);
for (BeanPropertyDefinition definition : beanDescription.findProperties()) {
TypeToken<?> propertyType = getPropertyType(type, toMethod(definition.getGetter()), toMethod(definition.getSetter()), definition.getField(), config);
String name = definition.getName();
if (genericDataFieldNames == null || genericDataFieldNames.contains(name)) {
if (hasUnresolvedType(propertyType)) {
logger.warning("skipping field '" + name + "' of type '" + propertyType + "' because it is unresolved.");
continue;
}
if (propertyType != null) {
ResourcePropertySchema propertySchema = ResourcePropertySchema.of(propertyType);
propertySchema.setDescription(definition.getMetadata().getDescription());
schemaBuilder.addProperty(name, propertySchema);
} else {
logger.warning("No type found for property '" + name + "' on class '" + type + "'.");
}
} else {
logger.fine("skipping field '" + name + "' because it's not a Java client model field.");
}
}
return schemaBuilder.build();
}
use of com.google.api.server.spi.config.ResourcePropertySchema in project endpoints-java by cloudendpoints.
the class JsonConfigWriter method addBeanProperties.
/**
* Iterates over the given JavaBean class and adds the following to the given config object
* (the value of "properties" of a schema object): "<name>": {"type": "<type>"}, where
* "name" is the name of the JavaBean property and "type" the type of its value.
*/
private void addBeanProperties(ObjectNode schemasNode, ObjectNode node, TypeToken<?> beanType, ApiConfig apiConfig, List<ApiParameterConfig> parameterConfigs) throws ApiConfigException {
// CollectionResponse<T> is treated as a bean but it is a parameterized type, too.
ResourceSchema schema = resourceSchemaProvider.getResourceSchema(beanType, apiConfig);
for (Entry<String, ResourcePropertySchema> entry : schema.getProperties().entrySet()) {
String propertyName = entry.getKey();
ObjectNode propertyNode = objectMapper.createObjectNode();
TypeToken<?> propertyType = entry.getValue().getType();
if (propertyType != null) {
addTypeToNode(schemasNode, propertyType, beanType, propertyNode, apiConfig, parameterConfigs);
node.set(propertyName, propertyNode);
}
}
}
use of com.google.api.server.spi.config.ResourcePropertySchema in project endpoints-java by cloudendpoints.
the class SchemaRepository method createBeanSchema.
private Schema createBeanSchema(TypeToken<?> type, Map<TypeToken<?>, Schema> typesForConfig, ApiConfig config) {
Schema.Builder builder = Schema.builder().setName(Types.getSimpleName(type, config.getSerializationConfig())).setType("object");
ResourceSchema schema = resourceSchemaProvider.getResourceSchema(type, config);
for (Entry<String, ResourcePropertySchema> entry : schema.getProperties().entrySet()) {
String propertyName = entry.getKey();
ResourcePropertySchema propertySchema = entry.getValue();
TypeToken<?> propertyType = propertySchema.getType();
if (propertyType != null) {
Field.Builder fieldBuilder = Field.builder().setName(propertyName);
fillInFieldInformation(fieldBuilder, propertyType, propertySchema.getDescription(), typesForConfig, config);
builder.addField(propertyName, fieldBuilder.build());
}
}
return builder.build();
}
Aggregations