Search in sources :

Example 1 with ResourcePropertySchema

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();
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ResourceSchema(com.google.api.server.spi.config.ResourceSchema) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ResourcePropertySchema(com.google.api.server.spi.config.ResourcePropertySchema) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ResourcePropertySchema

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): "&lt;name&gt;": {"type": "&lt;type&gt;"}, 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);
        }
    }
}
Also used : ResourceSchema(com.google.api.server.spi.config.ResourceSchema) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ResourcePropertySchema(com.google.api.server.spi.config.ResourcePropertySchema)

Example 3 with ResourcePropertySchema

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();
}
Also used : Field(com.google.api.server.spi.config.model.Schema.Field) ResourceSchema(com.google.api.server.spi.config.ResourceSchema) ResourceSchema(com.google.api.server.spi.config.ResourceSchema) ResourcePropertySchema(com.google.api.server.spi.config.ResourcePropertySchema) ResourcePropertySchema(com.google.api.server.spi.config.ResourcePropertySchema)

Aggregations

ResourcePropertySchema (com.google.api.server.spi.config.ResourcePropertySchema)3 ResourceSchema (com.google.api.server.spi.config.ResourceSchema)3 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Field (com.google.api.server.spi.config.model.Schema.Field)1