Search in sources :

Example 1 with ResourceSchema

use of com.google.api.server.spi.config.ResourceSchema 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 ResourceSchema

use of com.google.api.server.spi.config.ResourceSchema in project endpoints-java by cloudendpoints.

the class ResourceSchemaProviderTest method testConfigCustomResourceSerializedPropertyReturnsSchema.

@Test
public void testConfigCustomResourceSerializedPropertyReturnsSchema() {
    config.getSerializationConfig().addSerializationConfig(SinglePropertyResourceSerializer.class);
    ResourceSchema schema = getResourceSchema(SinglePropertyBean.class);
    String name = SinglePropertyBean.class.getSimpleName();
    assertThat(schema.getProperties().keySet()).containsExactly(name);
    assertEquals(Long.class, schema.getProperties().get(name).getJavaType());
}
Also used : ResourceSchema(com.google.api.server.spi.config.ResourceSchema) Test(org.junit.Test)

Example 3 with ResourceSchema

use of com.google.api.server.spi.config.ResourceSchema in project endpoints-java by cloudendpoints.

the class ResourceSchemaProviderTest method testRenamedProperty.

@Test
public void testRenamedProperty() {
    ResourceSchema schema = getResourceSchema(RenamedPropertyBean.class);
    assertThat(schema.getProperties().keySet()).containsExactly("bar");
}
Also used : ResourceSchema(com.google.api.server.spi.config.ResourceSchema) Test(org.junit.Test)

Example 4 with ResourceSchema

use of com.google.api.server.spi.config.ResourceSchema in project endpoints-java by cloudendpoints.

the class ResourceSchemaProviderTest method testAnnotationCustomResourceSerializedPropertyReturnsSchema.

@Test
public void testAnnotationCustomResourceSerializedPropertyReturnsSchema() {
    ResourceSchema schema = getResourceSchema(CustomResourceSerializerBean.class);
    assertEquals("CustomizedName", schema.getName());
    assertThat(schema.getProperties().keySet()).containsExactly("baz", "qux");
    assertEquals(Integer.class, schema.getProperties().get("baz").getJavaType());
    assertEquals(Boolean.class, schema.getProperties().get("qux").getJavaType());
}
Also used : ResourceSchema(com.google.api.server.spi.config.ResourceSchema) Test(org.junit.Test)

Example 5 with ResourceSchema

use of com.google.api.server.spi.config.ResourceSchema in project endpoints-java by cloudendpoints.

the class ResourceSchemaProviderTest method testNoClassPropertyReturned.

@Test
public void testNoClassPropertyReturned() {
    ResourceSchema schema = getResourceSchema(SinglePropertyBean.class);
    assertThat(schema.getProperties().keySet()).containsExactly("foo");
}
Also used : ResourceSchema(com.google.api.server.spi.config.ResourceSchema) Test(org.junit.Test)

Aggregations

ResourceSchema (com.google.api.server.spi.config.ResourceSchema)21 Test (org.junit.Test)18 ResourcePropertySchema (com.google.api.server.spi.config.ResourcePropertySchema)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