use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class SnapshotGenerator method recordType.
private void recordType(DataSchema schema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
if (schema instanceof NamedDataSchema) {
NamedDataSchema namedDataSchema = (NamedDataSchema) schema;
if (!foundTypes.containsKey(namedDataSchema.getFullName())) {
foundTypes.put(namedDataSchema.getFullName(), namedDataSchema);
if (// recurse into record, record any contained types.
schema instanceof RecordDataSchema) {
RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
for (NamedDataSchema includedSchema : recordDataSchema.getInclude()) {
recordType(includedSchema, foundTypes, typeOrder);
}
for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
recordType(field.getType(), foundTypes, typeOrder);
}
} else if (schema instanceof TyperefDataSchema) {
recordType(schema.getDereferencedDataSchema(), foundTypes, typeOrder);
}
typeOrder.add(namedDataSchema);
}
} else if (schema instanceof ArrayDataSchema) {
ArrayDataSchema arraySchema = (ArrayDataSchema) schema;
recordType(arraySchema.getItems(), foundTypes, typeOrder);
} else if (schema instanceof MapDataSchema) {
MapDataSchema mapSchema = (MapDataSchema) schema;
recordType(mapSchema.getValues(), foundTypes, typeOrder);
} else if (schema instanceof UnionDataSchema) {
UnionDataSchema unionSchema = (UnionDataSchema) schema;
for (DataSchema type : unionSchema.getTypes()) {
recordType(type, foundTypes, typeOrder);
}
}
}
use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class RequestBuilderSpecGenerator method generateQueryParamBindingMethods.
private void generateQueryParamBindingMethods(ParameterSchemaArray parameters, RequestBuilderSpec requestBuilderClass) {
for (ParameterSchema param : parameters) {
final String paramName = param.getName();
final DataSchema typeSchema = RestSpecCodec.textToSchema(param.getType(), _schemaResolver);
final ClassTemplateSpec typeClassSpec = schemaToTemplateSpec(typeSchema);
final boolean isOptional = RestLiToolsUtils.isParameterOptional(param);
final String setMethodName = RestLiToolsUtils.nameCamelCase(paramName + "Param");
final QueryParamBindingMethodSpec spec = new QueryParamBindingMethodSpec(setMethodName);
spec.setParamName(paramName);
spec.setNeedAddParamMethod(false);
spec.setOptional(isOptional);
spec.setArgType(typeClassSpec);
spec.setDoc(param.getDoc());
requestBuilderClass.addQueryParamMethod(spec);
if (typeSchema instanceof ArrayDataSchema) {
final DataSchema itemsSchema = ((ArrayDataSchema) typeSchema).getItems();
final ClassTemplateSpec itemSpecClass = schemaToTemplateSpec(itemsSchema);
final String addMethodName = RestLiToolsUtils.nameCamelCase("add" + RestLiToolsUtils.normalizeCaps(paramName) + "Param");
final QueryParamBindingMethodSpec addSpec = new QueryParamBindingMethodSpec(addMethodName);
addSpec.setParamName(paramName);
addSpec.setNeedAddParamMethod(true);
addSpec.setOptional(isOptional);
addSpec.setArgType(itemSpecClass);
addSpec.setDoc(param.getDoc());
requestBuilderClass.addQueryParamMethod(addSpec);
}
}
}
use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class RestLiDataValidator method buildArrayDataSchemaByProjection.
/**
* Build a new {@link ArrayDataSchema} schema that contains only the masked fields.
*/
private static ArrayDataSchema buildArrayDataSchemaByProjection(ArrayDataSchema originalSchema, DataMap maskMap) {
if (maskMap.containsKey(FilterConstants.WILDCARD)) {
DataSchema newItemsSchema = reuseOrBuildDataSchema(originalSchema.getItems(), maskMap.get(FilterConstants.WILDCARD));
ArrayDataSchema newSchema = new ArrayDataSchema(newItemsSchema);
if (originalSchema.getProperties() != null) {
newSchema.setProperties(originalSchema.getProperties());
}
return newSchema;
}
throw new IllegalArgumentException("Missing wildcard key in projection mask: " + maskMap.keySet());
}
use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class RestLiAnnotationReader method checkParameterHasTyperefSchema.
private static boolean checkParameterHasTyperefSchema(Parameter<?> parameter) {
boolean result = false;
DataSchema dataSchema = parameter.getDataSchema();
Class<?> dataType = parameter.getType();
if (dataType.isArray()) {
if (dataSchema instanceof ArrayDataSchema) {
dataSchema = ((ArrayDataSchema) dataSchema).getItems();
} else {
throw new ResourceConfigException("Array typed parameter " + parameter.getName() + " must have an array schema.");
}
}
if (dataSchema instanceof TyperefDataSchema) {
result = true;
}
return result;
}
use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class RestLiAnnotationReader method checkTyperefSchema.
private static String checkTyperefSchema(final Class<?> type, final DataSchema dataSchema) {
if (type.isArray() && dataSchema instanceof ArrayDataSchema) {
registerCoercerForPrimitiveTypeRefArray((ArrayDataSchema) dataSchema);
}
if (!(dataSchema instanceof TyperefDataSchema)) {
return null;
}
TyperefDataSchema typerefSchema = (TyperefDataSchema) dataSchema;
boolean ok;
DataSchema.Type schemaType = typerefSchema.getDereferencedType();
Class<?>[] validTypes = RestModelConstants.PRIMITIVE_DATA_SCHEMA_TYPE_ALLOWED_TYPES.get(schemaType);
if (validTypes != null) {
String javaClassNameFromSchema = CustomTypeUtil.getJavaCustomTypeClassNameFromSchema(typerefSchema);
if (javaClassNameFromSchema != null) {
registerCoercer(typerefSchema);
ok = type.getName().equals(javaClassNameFromSchema) || (type.isArray() && (type.getComponentType().getName()).equals(javaClassNameFromSchema));
} else {
ok = checkParameterType(type, validTypes);
}
} else {
try {
DataSchema inferredSchema = DataTemplateUtil.getSchema(type);
DataSchema derefSchema = typerefSchema.getDereferencedDataSchema();
if (inferredSchema.equals(derefSchema)) {
return null;
}
return "typeref " + typerefSchema + " is not compatible with (" + type + ") with schema " + derefSchema;
} catch (TemplateRuntimeException e) {
}
ok = false;
}
if (!ok) {
return "typeref " + typerefSchema + " is not compatible with (" + type + ")";
}
return null;
}
Aggregations