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());
}
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;
}
}
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;
}
Aggregations