Search in sources :

Example 1 with Description

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

the class ApiConfigAnnotationReaderTest method testParameterDescription.

@Test
public void testParameterDescription() throws Exception {
    @Api
    final class TestParameterDescription {

        public void foo(@Description("desc") String param) {
        }
    }
    ApiConfig config = createConfig(TestParameterDescription.class);
    annotationReader.loadEndpointMethods(serviceContext, TestParameterDescription.class, config.getApiClassConfig().getMethods());
    ApiMethodConfig methodConfig = Iterables.getOnlyElement(config.getApiClassConfig().getMethods().values());
    ApiParameterConfig parameterConfig = Iterables.getOnlyElement(methodConfig.getParameterConfigs());
    assertEquals("desc", parameterConfig.getDescription());
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) Description(com.google.api.server.spi.config.Description) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) SimpleLevelOverridingApi(com.google.api.server.spi.testing.SimpleLevelOverridingApi) SimpleLevelOverridingInheritedApi(com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi) Api(com.google.api.server.spi.config.Api) Test(org.junit.Test)

Example 2 with Description

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

the class SchemaRepository method getOrCreateTypeForConfig.

private Schema getOrCreateTypeForConfig(TypeToken type, Map<TypeToken<?>, Schema> typesForConfig, ApiConfig config) {
    type = ApiAnnotationIntrospector.getSchemaType(type, config);
    Schema schema = typesForConfig.get(type);
    ApiKey key = config.getApiKey().withoutRoot();
    if (schema != null) {
        // the type construction is complete.
        if (schema != PLACEHOLDER_SCHEMA) {
            addSchemaToApi(key, schema);
        }
        return schema;
    }
    // We put a placeholder in because this is a recursive process that may result in circular
    // references. This should never be returned in the public interface.
    typesForConfig.put(type, PLACEHOLDER_SCHEMA);
    TypeToken<?> arrayItemType = Types.getArrayItemType(type);
    if (typeLoader.isSchemaType(type)) {
        throw new IllegalArgumentException("Can't add a primitive type as a resource");
    } else if (arrayItemType != null) {
        Field.Builder arrayItemSchema = Field.builder().setName(ARRAY_UNUSED_MSG);
        fillInFieldInformation(arrayItemSchema, arrayItemType, null, typesForConfig, config);
        schema = Schema.builder().setName(Types.getSimpleName(type, config.getSerializationConfig())).setType("object").addField("items", Field.builder().setName("items").setType(FieldType.ARRAY).setArrayItemSchema(arrayItemSchema.build()).build()).build();
        typesForConfig.put(type, schema);
        schemaByApiKeys.put(key, schema);
        return schema;
    } else if (Types.isObject(type)) {
        typesForConfig.put(type, ANY_SCHEMA);
        schemaByApiKeys.put(key, ANY_SCHEMA);
        return ANY_SCHEMA;
    } else if (Types.isMapType(type)) {
        typesForConfig.put(type, MAP_SCHEMA);
        schemaByApiKeys.put(key, MAP_SCHEMA);
        return MAP_SCHEMA;
    } else if (Types.isEnumType(type)) {
        Schema.Builder builder = Schema.builder().setName(Types.getSimpleName(type, config.getSerializationConfig())).setType("string");
        for (java.lang.reflect.Field field : type.getRawType().getFields()) {
            if (field.isEnumConstant()) {
                builder.addEnumValue(field.getName());
                Description description = field.getAnnotation(Description.class);
                builder.addEnumDescription(description == null ? "" : description.value());
            }
        }
        schema = builder.build();
        typesForConfig.put(type, schema);
        schemaByApiKeys.put(key, schema);
        return schema;
    } else {
        schema = createBeanSchema(type, typesForConfig, config);
        typesForConfig.put(type, schema);
        schemaByApiKeys.put(key, schema);
        return schema;
    }
}
Also used : Description(com.google.api.server.spi.config.Description) ResourceSchema(com.google.api.server.spi.config.ResourceSchema) ResourcePropertySchema(com.google.api.server.spi.config.ResourcePropertySchema)

Example 3 with Description

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

the class DiscoveryGenerator method convertMethodParameter.

private JsonSchema convertMethodParameter(ApiParameterConfig parameterConfig, boolean isPathParameter) {
    JsonSchema schema = new JsonSchema();
    TypeToken<?> type;
    if (parameterConfig.isRepeated()) {
        schema.setRepeated(true);
        type = parameterConfig.getRepeatedItemSerializedType();
    } else {
        type = parameterConfig.getSchemaBaseType();
    }
    if (parameterConfig.isEnum()) {
        List<String> enumValues = Lists.newArrayList();
        List<String> enumDescriptions = Lists.newArrayList();
        for (java.lang.reflect.Field field : type.getRawType().getFields()) {
            if (field.isEnumConstant()) {
                enumValues.add(field.getName());
                Description description = field.getAnnotation(Description.class);
                enumDescriptions.add(description == null ? "" : description.value());
            }
        }
        schema.setEnum(enumValues);
        schema.setEnumDescriptions(enumDescriptions);
        type = TypeToken.of(String.class);
    }
    schema.setType(typeLoader.getSchemaType(type));
    schema.setFormat(FieldType.fromType(type).getDiscoveryFormat());
    if (!parameterConfig.getNullable() && parameterConfig.getDefaultValue() == null) {
        schema.setRequired(true);
    }
    // TODO: Try to find a way to move default value interpretation/conversion into the
    // general configuration code.
    String defaultValue = parameterConfig.getDefaultValue();
    if (defaultValue != null) {
        Class<?> parameterClass = type.getRawType();
        try {
            objectMapper.convertValue(defaultValue, parameterClass);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("'%s' is not a valid default value for type '%s'", defaultValue, type));
        }
        schema.setDefault(defaultValue);
    }
    if (isPathParameter) {
        schema.setLocation("path");
    } else {
        schema.setLocation("query");
    }
    if (parameterConfig.getDescription() != null) {
        schema.setDescription(parameterConfig.getDescription());
    }
    return schema;
}
Also used : Description(com.google.api.server.spi.config.Description) RestDescription(com.google.api.services.discovery.model.RestDescription) JsonSchema(com.google.api.services.discovery.model.JsonSchema)

Aggregations

Description (com.google.api.server.spi.config.Description)3 Api (com.google.api.server.spi.config.Api)1 ResourcePropertySchema (com.google.api.server.spi.config.ResourcePropertySchema)1 ResourceSchema (com.google.api.server.spi.config.ResourceSchema)1 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)1 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)1 ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)1 SimpleLevelOverridingApi (com.google.api.server.spi.testing.SimpleLevelOverridingApi)1 SimpleLevelOverridingInheritedApi (com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi)1 JsonSchema (com.google.api.services.discovery.model.JsonSchema)1 RestDescription (com.google.api.services.discovery.model.RestDescription)1 Test (org.junit.Test)1