use of com.google.api.services.discovery.model.JsonSchema 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;
}
use of com.google.api.services.discovery.model.JsonSchema in project endpoints-java by cloudendpoints.
the class DiscoveryGenerator method convertMethodParameters.
private Map<String, JsonSchema> convertMethodParameters(ApiMethodConfig methodConfig) {
Map<String, JsonSchema> parameters = Maps.newLinkedHashMap();
Collection<String> pathParameters = methodConfig.getPathParameters();
for (ApiParameterConfig parameterConfig : methodConfig.getParameterConfigs()) {
if (parameterConfig.getClassification() == Classification.API_PARAMETER) {
parameters.put(parameterConfig.getName(), convertMethodParameter(parameterConfig, pathParameters.contains(parameterConfig.getName())));
}
}
return parameters;
}
Aggregations