Search in sources :

Example 36 with NamedDataSchema

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);
    }
}
Also used : FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ComplexDataSchema(com.linkedin.data.schema.ComplexDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema)

Example 37 with NamedDataSchema

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;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ComplexDataSchema(com.linkedin.data.schema.ComplexDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec)

Example 38 with NamedDataSchema

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;
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) CompatibilityResult(com.linkedin.data.schema.compatibility.CompatibilityResult)

Example 39 with NamedDataSchema

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);
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema)

Example 40 with NamedDataSchema

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);
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataMap(com.linkedin.data.DataMap)

Aggregations

NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)46 DataSchema (com.linkedin.data.schema.DataSchema)25 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)16 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)12 MapDataSchema (com.linkedin.data.schema.MapDataSchema)11 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)11 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)11 DataMap (com.linkedin.data.DataMap)8 IOException (java.io.IOException)8 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)7 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)7 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)6 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 SchemaParser (com.linkedin.data.schema.SchemaParser)6 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)5 File (java.io.File)5 Test (org.testng.annotations.Test)5 CustomInfoSpec (com.linkedin.pegasus.generator.spec.CustomInfoSpec)4 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)4 FileInputStream (java.io.FileInputStream)4