use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class TemplateSpecGenerator method checkForClassNameConflict.
/**
* Checks if a class name conflict occurs, if it occurs throws {@link IllegalArgumentException}.
*
* @param className provides the template spec class name.
* @param schema provides the {@link DataSchema} that would be bound if there is no conflict.
*
* @throws IllegalArgumentException
*/
private void checkForClassNameConflict(String className, DataSchema schema) throws IllegalArgumentException {
final DataSchema schemaFromClassName = _classNameToSchemaMap.get(className);
boolean conflict = false;
if (schemaFromClassName != null && schemaFromClassName != schema) {
final DataSchema.Type schemaType = schema.getType();
if (schemaFromClassName.getType() != schemaType) {
conflict = true;
} else if (schema instanceof NamedDataSchema) {
conflict = true;
} else if (!schemaFromClassName.equals(schema)) {
assert schemaType == DataSchema.Type.ARRAY || schemaType == DataSchema.Type.MAP;
//
// see schemaForArrayItemsOrMapValues
//
// When the schema bound to the specified class name is different
// from the specified schema, then emit a log message when this occurs.
//
_log.info("Class name: " + className + ", bound to schema:" + schemaFromClassName + ", instead of schema: " + schema);
}
}
if (conflict) {
throw new IllegalArgumentException("Class name conflict detected, class name: " + className + ", class already bound to schema: " + schemaFromClassName + ", attempting to rebind to schema: " + schema);
}
}
use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class TemplateSpecGenerator method classInfoForUnnamed.
/*
* Determine name and class for unnamed types.
*/
private ClassInfo classInfoForUnnamed(ClassTemplateSpec enclosingClass, String name, DataSchema schema) {
assert !(schema instanceof NamedDataSchema);
assert !(schema instanceof PrimitiveDataSchema);
final ClassInfo classInfo = classNameForUnnamedTraverse(enclosingClass, name, schema);
final String className = classInfo.bindingName();
final DataSchema schemaFromClassName = _classNameToSchemaMap.get(className);
if (schemaFromClassName == null) {
final ClassTemplateSpec classTemplateSpec = ClassTemplateSpec.createFromDataSchema(schema);
if (enclosingClass != null && classInfo.namespace.equals(enclosingClass.getFullName())) {
// enclosingClass flag indicates whether a class is nested or not.
classTemplateSpec.setEnclosingClass(enclosingClass);
classTemplateSpec.setClassName(classInfo.name);
classTemplateSpec.setModifiers(ModifierSpec.PUBLIC, ModifierSpec.STATIC, ModifierSpec.FINAL);
} else {
classTemplateSpec.setNamespace(classInfo.namespace);
classTemplateSpec.setClassName(classInfo.name);
classTemplateSpec.setPackage(classInfo.packageName);
classTemplateSpec.setModifiers(ModifierSpec.PUBLIC);
}
classInfo.definedClass = classTemplateSpec;
} else {
checkForClassNameConflict(className, schema);
classInfo.existingClass = _schemaToClassMap.get(schemaFromClassName);
}
return classInfo;
}
use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class ResourceCompatibilityChecker method checkType.
/**
* @return whether the optionality check passes
*/
private boolean checkType(Object pathTail, String prevType, String currType, boolean allowNull) {
if (prevType == null && currType == null && allowNull) {
return true;
}
if (prevType == null || currType == null) {
_infoMap.addRestSpecInfo(pathTail, CompatibilityInfo.Type.TYPE_MISSING, _infoPath);
return false;
}
try {
final DataSchema prevSchema = RestSpecCodec.textToSchema(prevType, _prevSchemaResolver);
final DataSchema currSchema = RestSpecCodec.textToSchema(currType, _currSchemaResolver);
CompatibilityResult compatibilityResult = CompatibilityChecker.checkCompatibility(prevSchema, currSchema, defaultOptions);
if (!compatibilityResult.getMessages().isEmpty()) {
if (prevType.equals(currType) && prevSchema instanceof NamedDataSchema) {
if (!_namedSchemasChecked.contains(prevType)) {
addNamedCompatibilityMessages(compatibilityResult.getMessages());
_namedSchemasChecked.add(prevType);
}
return compatibilityResult.isError();
}
addCompatibilityMessages(pathTail, compatibilityResult.getMessages());
return compatibilityResult.isError();
}
return true;
} catch (IllegalArgumentException e) {
_infoMap.addRestSpecInfo(pathTail, CompatibilityInfo.Type.TYPE_UNKNOWN, _infoPath, e.getMessage());
return false;
}
}
use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class RestLiHTMLDocumentationRenderer method addRelated.
private void addRelated(Object parent, Map<String, Object> pageModel) {
final Node<?> node = _relationships.getRelationships(parent);
Map<String, ResourceSchema> relatedResources;
Map<String, NamedDataSchema> relatedSchemas;
synchronized (this) {
relatedResources = _relatedResourceCache.get(parent);
if (relatedResources == null) {
relatedResources = new HashMap<String, ResourceSchema>();
final Iterator<Node<ResourceSchema>> resourcesItr = node.getAdjacency(ResourceSchema.class).iterator();
while (resourcesItr.hasNext()) {
final ResourceSchema currResource = (ResourceSchema) resourcesItr.next().getObject();
relatedResources.put(currResource.getName(), currResource);
}
_relatedResourceCache.put(parent, relatedResources);
}
relatedSchemas = _relatedSchemaCache.get(parent);
if (relatedSchemas == null) {
relatedSchemas = new HashMap<String, NamedDataSchema>();
final Iterator<Node<NamedDataSchema>> schemaItr = node.getAdjacency(NamedDataSchema.class).iterator();
while (schemaItr.hasNext()) {
final NamedDataSchema currResource = (NamedDataSchema) schemaItr.next().getObject();
relatedSchemas.put(currResource.getFullName(), currResource);
}
_relatedSchemaCache.put(parent, relatedSchemas);
}
}
pageModel.put("relatedResources", relatedResources);
pageModel.put("relatedSchemas", relatedSchemas);
}
use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class RestLiJSONDocumentationRenderer method addRelatedModels.
private void addRelatedModels(ResourceSchema resourceSchema, DataMap models) throws IOException {
Map<String, DataMap> relatedSchemas;
synchronized (this) {
relatedSchemas = _relatedSchemaCache.get(resourceSchema);
if (relatedSchemas == null) {
relatedSchemas = new HashMap<String, DataMap>();
final Node<?> node = _relationships.getRelationships(resourceSchema);
final Iterator<Node<NamedDataSchema>> schemaItr = node.getAdjacency(NamedDataSchema.class).iterator();
while (schemaItr.hasNext()) {
final NamedDataSchema currResource = (NamedDataSchema) schemaItr.next().getObject();
relatedSchemas.put(currResource.getFullName(), _codec.stringToMap(currResource.toString()));
}
_relatedSchemaCache.put(resourceSchema, relatedSchemas);
}
}
models.putAll(relatedSchemas);
}
Aggregations